001: /**
002: * Author: Matthew Baird
003: * mattbaird@yahoo.com
004: */package org.apache.ojb.broker;
005:
006: import org.apache.ojb.broker.query.Criteria;
007: import org.apache.ojb.broker.query.Query;
008: import org.apache.ojb.broker.query.QueryFactory;
009: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
010: import org.apache.ojb.junit.PBTestCase;
011:
012: import java.sql.Timestamp;
013: import java.util.Iterator;
014:
015: /**
016: * This TestClass tests OJB ability to handle Contract Version Effectiveness patterns.
017: */
018: public class ContractVersionEffectivenessTest extends PBTestCase {
019: private int COUNT = 10;
020:
021: public static void main(String[] args) {
022: String[] arr = { ContractVersionEffectivenessTest.class
023: .getName() };
024: junit.textui.TestRunner.main(arr);
025: }
026:
027: /**
028: * Insert the method's description here.
029: * Creation date: (24.12.2000 00:33:40)
030: */
031: public ContractVersionEffectivenessTest(String name)
032:
033: {
034: super (name);
035: }
036:
037: /**
038: * Insert the method's description here.
039: * Creation date: (06.12.2000 21:58:53)
040: */
041: public void setUp() throws Exception {
042: super .setUp();
043: }
044:
045: /**
046: * Insert the method's description here.
047: * Creation date: (06.12.2000 21:59:14)
048: */
049: public void tearDown() throws Exception {
050: super .tearDown();
051: }
052:
053: private void createTestData() {
054: broker.beginTransaction();
055: for (int i = 0; i < COUNT; i++) {
056: Contract contract = new Contract();
057: contract.setPk("C" + System.currentTimeMillis());
058: contract.setContractValue1("contractvalue1");
059: contract.setContractValue2(1);
060: contract.setContractValue3("contractvalue3");
061: contract.setContractValue4(new Timestamp(System
062: .currentTimeMillis()));
063: broker.store(contract);
064:
065: Version version = new Version();
066: version.setPk("V" + System.currentTimeMillis());
067: version.setVersionValue1("versionvalue1");
068: version.setVersionValue2(1);
069: version.setVersionValue3(new Timestamp(System
070: .currentTimeMillis()));
071: version.setContract(contract);
072: broker.store(version);
073:
074: Effectiveness eff = new Effectiveness();
075: eff.setPk("E" + System.currentTimeMillis());
076: eff.setEffValue1("effvalue1");
077: eff.setEffValue2(1);
078: eff.setEffValue3(new Timestamp(System.currentTimeMillis()));
079: eff.setVersion(version);
080: broker.store(eff);
081: }
082: broker.commitTransaction();
083: }
084:
085: public void testCreateContractVersionEffectiveness() {
086: createTestData();
087: }
088:
089: public void testAutoRetrieveFalse() {
090: ojbChangeReferenceSetting(Contract.class, "relatedToContract",
091: false, ObjectReferenceDescriptor.CASCADE_OBJECT,
092: ObjectReferenceDescriptor.CASCADE_OBJECT, false);
093: String name = "testAutoRetrieveFalse_"
094: + System.currentTimeMillis();
095: Contract contract = new Contract();
096: contract.setPk("C" + System.currentTimeMillis());
097: contract.setContractValue1(name + "_Contract");
098: contract.setContractValue2(1);
099: contract.setContractValue3("contractvalue3");
100: contract.setContractValue4(new Timestamp(System
101: .currentTimeMillis()));
102:
103: RelatedToContract rc = new RelatedToContract();
104: rc.setPk("R_" + System.currentTimeMillis());
105: rc.setRelatedValue1(name + "_RelatedToContract");
106:
107: contract.setRelatedToContract(rc);
108: broker.beginTransaction();
109: // auto-update is true
110: broker.store(contract);
111: broker.commitTransaction();
112:
113: broker.clearCache();
114: Identity oid = broker.serviceIdentity().buildIdentity(contract);
115: Contract newC = (Contract) broker.getObjectByIdentity(oid);
116: assertNotNull(newC);
117: // auto-retrieve is false
118: assertNull(newC.getRelatedToContract());
119: broker.retrieveAllReferences(newC);
120: // now the field should be populated
121: assertNotNull(newC.getRelatedToContract());
122: }
123:
124: public void testUpdateContractVersionEffectiveness() {
125: createTestData();
126:
127: Criteria crit = new Criteria();
128: Query q;
129: Iterator iter;
130: /**
131: * update effectiveness first
132: */
133: q = QueryFactory.newQuery(Effectiveness.class, crit);
134: iter = broker.getIteratorByQuery(q);
135: Effectiveness eff = null;
136: broker.beginTransaction();
137: while (iter.hasNext()) {
138: eff = (Effectiveness) iter.next();
139: eff.setEffValue1("effValueUpdated");
140: broker.store(eff);
141: }
142: broker.commitTransaction();
143: /**
144: * then version
145: */
146: Version version = null;
147: q = QueryFactory.newQuery(Version.class, crit);
148: iter = broker.getIteratorByQuery(q);
149: broker.beginTransaction();
150: while (iter.hasNext()) {
151: version = (Version) iter.next();
152: version.setVersionValue1("verValueUpdated");
153: broker.store(version);
154: }
155: broker.commitTransaction();
156: /**
157: * the contract
158: */
159: Contract contract = null;
160: q = QueryFactory.newQuery(Contract.class, crit);
161: iter = broker.getIteratorByQuery(q);
162: broker.beginTransaction();
163: while (iter.hasNext()) {
164: contract = (Contract) iter.next();
165: contract.setContractValue1("contractValueUpdated");
166: broker.store(contract);
167: }
168: broker.commitTransaction();
169: }
170:
171: public void testDeleteContractVersionEffectiveness() {
172: createTestData();
173:
174: Criteria crit = new Criteria();
175: Query q;
176: Iterator iter;
177: /**
178: * delete effectiveness first
179: */
180: q = QueryFactory.newQuery(Effectiveness.class, crit);
181: iter = broker.getIteratorByQuery(q);
182: broker.beginTransaction();
183: while (iter.hasNext()) {
184: broker.delete(iter.next());
185: }
186: broker.commitTransaction();
187: /**
188: * then version
189: */
190: q = QueryFactory.newQuery(Version.class, crit);
191: iter = broker.getIteratorByQuery(q);
192: broker.beginTransaction();
193: while (iter.hasNext()) {
194: broker.delete(iter.next());
195: }
196: broker.commitTransaction();
197: /**
198: * the contract
199: */
200: q = QueryFactory.newQuery(Contract.class, crit);
201: iter = broker.getIteratorByQuery(q);
202: broker.beginTransaction();
203: while (iter.hasNext()) {
204: broker.delete(iter.next());
205: }
206: broker.commitTransaction();
207: }
208:
209: }
|