KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Author: Matthew Baird
3  * mattbaird@yahoo.com
4  */

5 package org.apache.ojb.broker;
6
7 import org.apache.ojb.broker.query.Criteria;
8 import org.apache.ojb.broker.query.Query;
9 import org.apache.ojb.broker.query.QueryFactory;
10 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
11 import org.apache.ojb.junit.PBTestCase;
12
13 import java.sql.Timestamp JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 /**
17  * This TestClass tests OJB ability to handle Contract Version Effectiveness patterns.
18  */

19 public class ContractVersionEffectivenessTest extends PBTestCase
20 {
21     private int COUNT = 10;
22     public static void main(String JavaDoc[] args)
23     {
24         String JavaDoc[] arr = {ContractVersionEffectivenessTest.class.getName()};
25         junit.textui.TestRunner.main(arr);
26     }
27
28     /**
29      * Insert the method's description here.
30      * Creation date: (24.12.2000 00:33:40)
31      */

32     public ContractVersionEffectivenessTest(String JavaDoc name)
33
34     {
35         super(name);
36     }
37
38     /**
39      * Insert the method's description here.
40      * Creation date: (06.12.2000 21:58:53)
41      */

42     public void setUp() throws Exception JavaDoc
43     {
44         super.setUp();
45     }
46
47     /**
48      * Insert the method's description here.
49      * Creation date: (06.12.2000 21:59:14)
50      */

51     public void tearDown() throws Exception JavaDoc
52     {
53         super.tearDown();
54     }
55
56     private void createTestData()
57     {
58         broker.beginTransaction();
59         for (int i = 0; i < COUNT; i++)
60         {
61             Contract contract = new Contract();
62             contract.setPk("C"+System.currentTimeMillis());
63             contract.setContractValue1("contractvalue1");
64             contract.setContractValue2(1);
65             contract.setContractValue3("contractvalue3");
66             contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
67             broker.store(contract);
68
69             Version version = new Version();
70             version.setPk("V"+System.currentTimeMillis());
71             version.setVersionValue1("versionvalue1");
72             version.setVersionValue2(1);
73             version.setVersionValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
74             version.setContract(contract);
75             broker.store(version);
76
77             Effectiveness eff = new Effectiveness();
78             eff.setPk("E"+System.currentTimeMillis());
79             eff.setEffValue1("effvalue1");
80             eff.setEffValue2(1);
81             eff.setEffValue3(new Timestamp JavaDoc(System.currentTimeMillis()));
82             eff.setVersion(version);
83             broker.store(eff);
84         }
85         broker.commitTransaction();
86     }
87
88     public void testCreateContractVersionEffectiveness()
89     {
90         createTestData();
91     }
92
93     public void testAutoRetrieveFalse()
94     {
95         ojbChangeReferenceSetting(Contract.class, "relatedToContract", false,
96                 ObjectReferenceDescriptor.CASCADE_OBJECT, ObjectReferenceDescriptor.CASCADE_OBJECT, false);
97         String JavaDoc name = "testAutoRetrieveFalse_" + System.currentTimeMillis();
98         Contract contract = new Contract();
99         contract.setPk("C"+System.currentTimeMillis());
100         contract.setContractValue1(name + "_Contract");
101         contract.setContractValue2(1);
102         contract.setContractValue3("contractvalue3");
103         contract.setContractValue4(new Timestamp JavaDoc(System.currentTimeMillis()));
104
105         RelatedToContract rc = new RelatedToContract();
106         rc.setPk("R_" + System.currentTimeMillis());
107         rc.setRelatedValue1(name + "_RelatedToContract");
108
109         contract.setRelatedToContract(rc);
110         broker.beginTransaction();
111         // auto-update is true
112
broker.store(contract);
113         broker.commitTransaction();
114
115         broker.clearCache();
116         Identity oid = broker.serviceIdentity().buildIdentity(contract);
117         Contract newC = (Contract) broker.getObjectByIdentity(oid);
118         assertNotNull(newC);
119         // auto-retrieve is false
120
assertNull(newC.getRelatedToContract());
121         broker.retrieveAllReferences(newC);
122         // now the field should be populated
123
assertNotNull(newC.getRelatedToContract());
124     }
125
126     public void testUpdateContractVersionEffectiveness()
127     {
128         createTestData();
129
130         Criteria crit = new Criteria();
131         Query q;
132         Iterator JavaDoc iter;
133         /**
134          * update effectiveness first
135          */

136         q = QueryFactory.newQuery(Effectiveness.class, crit);
137         iter = broker.getIteratorByQuery(q);
138         Effectiveness eff = null;
139         broker.beginTransaction();
140         while (iter.hasNext())
141         {
142             eff = (Effectiveness)iter.next();
143             eff.setEffValue1("effValueUpdated");
144             broker.store(eff);
145         }
146         broker.commitTransaction();
147         /**
148          * then version
149          */

150         Version version = null;
151         q = QueryFactory.newQuery(Version.class, crit);
152         iter = broker.getIteratorByQuery(q);
153         broker.beginTransaction();
154         while (iter.hasNext())
155         {
156             version = (Version) iter.next();
157             version.setVersionValue1("verValueUpdated");
158             broker.store(version);
159         }
160         broker.commitTransaction();
161         /**
162          * the contract
163          */

164         Contract contract = null;
165         q = QueryFactory.newQuery(Contract.class, crit);
166         iter = broker.getIteratorByQuery(q);
167         broker.beginTransaction();
168         while (iter.hasNext())
169         {
170             contract = (Contract) iter.next();
171             contract.setContractValue1("contractValueUpdated");
172             broker.store(contract);
173         }
174         broker.commitTransaction();
175     }
176     public void testDeleteContractVersionEffectiveness()
177     {
178         createTestData();
179
180         Criteria crit = new Criteria();
181         Query q;
182         Iterator JavaDoc iter;
183         /**
184          * delete effectiveness first
185          */

186         q = QueryFactory.newQuery(Effectiveness.class, crit);
187         iter = broker.getIteratorByQuery(q);
188         broker.beginTransaction();
189         while (iter.hasNext())
190         {
191             broker.delete(iter.next());
192         }
193         broker.commitTransaction();
194         /**
195          * then version
196          */

197         q = QueryFactory.newQuery(Version.class, crit);
198         iter = broker.getIteratorByQuery(q);
199         broker.beginTransaction();
200         while (iter.hasNext())
201         {
202             broker.delete(iter.next());
203         }
204         broker.commitTransaction();
205         /**
206          * the contract
207          */

208         q = QueryFactory.newQuery(Contract.class, crit);
209         iter = broker.getIteratorByQuery(q);
210         broker.beginTransaction();
211         while (iter.hasNext())
212         {
213             broker.delete(iter.next());
214         }
215         broker.commitTransaction();
216     }
217
218 }
219
Popular Tags