KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > TransactionDemarcationTest


1 package org.apache.ojb.broker;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.apache.ojb.broker.query.Criteria;
6 import org.apache.ojb.broker.query.Query;
7 import org.apache.ojb.broker.query.QueryByCriteria;
8 import org.apache.ojb.junit.PBTestCase;
9
10 /**
11  *
12  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
13  */

14 public class TransactionDemarcationTest extends PBTestCase
15 {
16     private Article[] articleArr;
17     private Person[] personArr;
18     private static final int COUNT = 20;
19
20     public TransactionDemarcationTest(String JavaDoc s)
21     {
22         super(s);
23     }
24
25     public void setUp() throws Exception JavaDoc
26     {
27         super.setUp();
28         articleArr = new Article[COUNT];
29         for (int i = 0; i < COUNT; i++)
30         {
31             Article a = createArticle(i);
32             articleArr[i] = a;
33         }
34
35         personArr = new Person[COUNT];
36         for (int i = 0; i < COUNT; i++)
37         {
38             Person a = createPerson(i);
39             personArr[i] = a;
40         }
41     }
42
43     public void testInsertDelete() throws Exception JavaDoc
44     {
45         doInsert();
46         doDelete();
47     }
48
49     public void doInsert() throws Exception JavaDoc
50     {
51         int beforeStore = getArticleCount();
52         broker.beginTransaction();
53         for (int i=0; i<articleArr.length; i++)
54         {
55             broker.store(articleArr[i]);
56         }
57         broker.commitTransaction();
58         int afterStore = getArticleCount();
59         assertEquals("Wrong number of articles stored", beforeStore+articleArr.length, afterStore);
60     }
61
62
63
64     public void doDelete() throws Exception JavaDoc
65     {
66         int beforeDelete = getArticleCount();
67         broker.beginTransaction();
68         for (int i=0; i<COUNT; i++)
69         {
70             broker.delete(articleArr[i]);
71         }
72         broker.commitTransaction();
73         int afterDelete = getArticleCount();
74         assertEquals("Wrong number of articles deleted", beforeDelete-articleArr.length, afterDelete);
75     }
76
77     public void testInsertDifferentObjects() throws Exception JavaDoc
78     {
79         int beforeStore = getArticleCount();
80         int beforeStorePersons = getPersonCount();
81         broker.beginTransaction();
82         for (int i=0; i<articleArr.length; i++)
83         {
84             broker.store(articleArr[i]);
85             broker.store(personArr[i]);
86         }
87         broker.commitTransaction();
88         int afterStore = getArticleCount();
89         int afterStorePersons = getPersonCount();
90         assertEquals("Wrong number of articles stored", beforeStore+articleArr.length, afterStore);
91         assertEquals("Wrong number of articles stored", beforeStorePersons+personArr.length, afterStorePersons);
92
93         int beforeDelete = getArticleCount();
94         int beforeDeletePerson = getPersonCount();
95         broker.beginTransaction();
96         for (int i=0; i<COUNT; i++)
97         {
98             broker.delete(personArr[i]);
99             broker.delete(articleArr[i]);
100         }
101         broker.commitTransaction();
102         int afterDelete = getArticleCount();
103         int afterDeletePerson = getPersonCount();
104         assertEquals("Wrong number of articles deleted", beforeDelete-articleArr.length, afterDelete);
105         assertEquals("Wrong number of articles deleted", beforeDeletePerson - personArr.length, afterDeletePerson);
106
107     }
108
109     public void testInsertDifferentObjectsWithinTransaction() throws Exception JavaDoc
110     {
111         int beforeStore = getArticleCount();
112         int beforeStorePersons = getPersonCount();
113
114         broker.beginTransaction();
115         broker.store(createPerson(99));
116         broker.store(createArticle(99));
117         broker.commitTransaction();
118
119         int afterStore = getArticleCount();
120         int afterStorePersons = getPersonCount();
121         assertEquals("Wrong number of articles stored", beforeStore+1, afterStore);
122         assertEquals("Wrong number of articles stored", beforeStorePersons+1, afterStorePersons);
123     }
124
125     public void testIterator() throws Exception JavaDoc
126     {
127         Criteria c = new Criteria();
128         Query q = new QueryByCriteria(Article.class, c);
129         Iterator JavaDoc it = broker.getIteratorByQuery(q);
130         it.hasNext();
131     }
132
133     public int getArticleCount()
134     {
135         Criteria c = new Criteria();
136         Query q = new QueryByCriteria(Article.class, c);
137         int count = 0;
138         count = broker.getCount(q);
139         return count;
140     }
141
142     public int getPersonCount()
143     {
144         Criteria c = new Criteria();
145         Query q = new QueryByCriteria(Person.class, c);
146         int count = 0;
147         count = broker.getCount(q);
148         return count;
149     }
150
151     private Article createArticle(int counter)
152     {
153         Article a = new Article();
154         a.setArticleName("New Performance Article " + counter);
155         a.setMinimumStock(100);
156         a.setOrderedUnits(17);
157         a.setPrice(0.45);
158         a.setStock(234);
159         a.setSupplierId(4);
160         a.setUnit("bottle");
161         return a;
162     }
163
164     private Person createPerson(int counter)
165     {
166         Person p = new Person();
167         p.setFirstname("firstname "+counter);
168         p.setLastname("lastname "+counter);
169         return p;
170     }
171
172     public static void main(String JavaDoc[] args)
173     {
174         String JavaDoc[] arr = {TransactionDemarcationTest.class.getName()};
175         junit.textui.TestRunner.main(arr);
176     }
177 }
178
Popular Tags