001: package org.apache.ojb.otm;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import org.apache.ojb.broker.*;
019: import org.apache.ojb.odmg.shared.TestClassA;
020: import org.apache.ojb.odmg.shared.TestClassB;
021: import org.apache.ojb.otm.copy.MetadataObjectCopyStrategy;
022: import org.apache.ojb.otm.copy.ObjectCopyStrategy;
023: import org.apache.ojb.otm.copy.ReflectiveObjectCopyStrategy;
024: import org.apache.ojb.otm.copy.SerializeObjectCopyStrategy;
025: import org.apache.ojb.otm.core.Transaction;
026: import org.apache.ojb.otm.lock.LockingException;
027: import org.apache.ojb.junit.OJBTestCase;
028:
029: /**
030: * Created by IntelliJ IDEA.
031: * User: matthew.baird
032: * Date: Jul 7, 2003
033: * Time: 2:10:49 PM
034: */
035: public class CopyTest extends OJBTestCase {
036: private static Class CLASS = CopyTest.class;
037: private PersistenceBroker m_pb;
038: private static final int ITERATIONS = 10000;
039: private ObjectCopyStrategy m_mdcs = new MetadataObjectCopyStrategy();
040: private ObjectCopyStrategy m_scs = new SerializeObjectCopyStrategy();
041: private ObjectCopyStrategy m_rcs = new ReflectiveObjectCopyStrategy();
042: private TestKit _kit;
043: private OTMConnection _conn;
044: private Zoo m_zoo;
045: private TestClassA m_tca;
046: private BidirectionalAssociationObjectA m_baoa;
047:
048: public CopyTest(String name) {
049: super (name);
050: }
051:
052: public void setUp() throws Exception {
053: super .setUp();
054: ojbChangeReferenceSetting(TestClassA.class, "b", true, true,
055: true, false);
056: ojbChangeReferenceSetting(TestClassB.class, "a", true, true,
057: true, false);
058: m_pb = PersistenceBrokerFactory.defaultPersistenceBroker();
059: _kit = TestKit.getTestInstance();
060: _conn = _kit.acquireConnection(PersistenceBrokerFactory
061: .getDefaultKey());
062: }
063:
064: public void tearDown() throws Exception {
065: m_pb.close();
066: _conn.close();
067: _conn = null;
068: super .tearDown();
069: }
070:
071: public static void main(String[] args) {
072: String[] arr = { CLASS.getName() };
073: junit.textui.TestRunner.main(arr);
074: }
075:
076: public void testMetadataCopy() throws LockingException {
077: TestClassA tca = generateTestData();
078: TestClassB tcb = tca.getB();
079: internalTest(m_mdcs, tca, tcb);
080: }
081:
082: public void testSerializedCopy() throws LockingException {
083: TestClassA tca = generateTestData();
084: TestClassB tcb = tca.getB();
085: internalTest(m_scs, tca, tcb);
086: }
087:
088: public void testReflectiveCopy() throws LockingException {
089: TestClassA tca = generateTestData();
090: TestClassB tcb = tca.getB();
091: internalTest(m_rcs, tca, tcb);
092: }
093:
094: private void internalTest(ObjectCopyStrategy strategy,
095: TestClassA a, TestClassB b) {
096: TestClassA copy = (TestClassA) strategy.copy(a, m_pb);
097: assertTrue(a != copy);
098: assertTrue(copy.getOid().equals("someoid"));
099: assertTrue(copy.getValue1().equals("abc"));
100: assertTrue(copy.getValue2().equals("123"));
101: assertTrue(copy.getValue3() == 5);
102: assertTrue(copy.getB() != b);
103: assertTrue(copy.getB().getOid().equals("boid"));
104: assertTrue(copy.getB().getValue1().equals("hi there"));
105: }
106:
107: public void testMetadataCopy2() throws LockingException {
108: Zoo zoo = generateZoo();
109: internalTest2(m_mdcs, zoo);
110: }
111:
112: public void testSerializeCopy2() throws LockingException {
113: Zoo zoo = generateZoo();
114: internalTest2(m_scs, zoo);
115: }
116:
117: public void testReflectiveCopy2() throws LockingException {
118: Zoo zoo = generateZoo();
119: internalTest2(m_rcs, zoo);
120: }
121:
122: /**
123: * tests for recursion handling
124: */
125: public void testMetadataCopy3() throws LockingException {
126: BidirectionalAssociationObjectA a = generateBidirectional();
127: internalTest3(m_mdcs, a);
128: }
129:
130: public void testSerializeCopy3() throws LockingException {
131: BidirectionalAssociationObjectA a = generateBidirectional();
132: internalTest3(m_scs, a);
133: }
134:
135: public void testReflectiveCopy3() throws LockingException {
136: BidirectionalAssociationObjectA a = generateBidirectional();
137: internalTest3(m_rcs, a);
138: }
139:
140: private void internalTest3(ObjectCopyStrategy strategy,
141: BidirectionalAssociationObjectA a) {
142: BidirectionalAssociationObjectA copy = (BidirectionalAssociationObjectA) strategy
143: .copy(a, m_pb);
144: assertTrue(a != copy);
145: assertTrue(copy.getPk().equals("abc123"));
146: assertTrue(copy.getRelatedB().getPk().equals("xyz987"));
147: }
148:
149: private void internalTest2(ObjectCopyStrategy strategy, Zoo zoo) {
150: Zoo copy = (Zoo) strategy.copy(zoo, m_pb);
151: assertTrue(zoo != copy);
152: assertTrue(zoo.getAnimals().size() == copy.getAnimals().size());
153: }
154:
155: private BidirectionalAssociationObjectA generateBidirectional()
156: throws LockingException {
157: if (m_baoa != null) {
158: return m_baoa;
159: } else {
160: Transaction tx = _kit.getTransaction(_conn);
161: tx.begin();
162: BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
163: a.setPk("abc123");
164: Identity oid = _conn.getIdentity(a);
165: a = (BidirectionalAssociationObjectA) _conn
166: .getObjectByIdentity(oid);
167: if (a == null) {
168: a = new BidirectionalAssociationObjectA();
169: a.setPk("abc123");
170: _conn.makePersistent(a);
171: BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
172: b.setPk("xyz987");
173: _conn.makePersistent(b);
174: a.setRelatedB(b);
175: b.setRelatedA(a);
176: }
177: tx.commit();
178: m_baoa = a;
179: return m_baoa;
180: }
181: }
182:
183: private Zoo generateZoo() throws LockingException {
184: if (m_zoo != null) {
185: return m_zoo;
186: } else {
187: Transaction tx = _kit.getTransaction(_conn);
188: tx.begin();
189: Zoo zoo = new Zoo();
190: zoo.setZooId(1234);
191: Identity oid = _conn.getIdentity(zoo);
192: zoo = (Zoo) _conn.getObjectByIdentity(oid);
193: if (zoo == null) {
194: zoo = new Zoo();
195: zoo.setZooId(1234);
196: _conn.makePersistent(zoo);
197: Mammal mammal = new Mammal();
198: mammal.setName("molly");
199: mammal.setNumLegs(4);
200: mammal.setAge(55);
201: zoo.addAnimal(mammal);
202: _conn.makePersistent(mammal);
203: Reptile reptile = new Reptile();
204: reptile.setColor("green");
205: reptile.setName("hubert");
206: reptile.setAge(51);
207: zoo.addAnimal(reptile);
208: _conn.makePersistent(reptile);
209: }
210: tx.commit();
211: m_zoo = zoo;
212: return m_zoo;
213: }
214: }
215:
216: public void testPerformance() throws LockingException {
217: long start = System.currentTimeMillis();
218: for (int i = 0; i < ITERATIONS; i++) {
219: TestClassA tca = generateTestData();
220: TestClassB tcb = tca.getB();
221: TestClassA copy = (TestClassA) m_scs.copy(tca, m_pb);
222: }
223: long stop = System.currentTimeMillis();
224: System.out
225: .println("testSerializedCopy took: " + (stop - start));
226: start = System.currentTimeMillis();
227: for (int i = 0; i < ITERATIONS; i++) {
228: TestClassA tca = generateTestData();
229: TestClassB tcb = tca.getB();
230: TestClassA copy = (TestClassA) m_mdcs.copy(tca, m_pb);
231: }
232: stop = System.currentTimeMillis();
233: System.out.println("testMetadataCopy took: " + (stop - start));
234: start = System.currentTimeMillis();
235: for (int i = 0; i < ITERATIONS; i++) {
236: TestClassA tca = generateTestData();
237: TestClassB tcb = tca.getB();
238: TestClassA copy = (TestClassA) m_rcs.copy(tca, m_pb);
239: }
240: stop = System.currentTimeMillis();
241: System.out
242: .println("testReflectiveCopy took: " + (stop - start));
243: }
244:
245: private TestClassA generateTestData() throws LockingException {
246: if (m_tca != null) {
247: return m_tca;
248: } else {
249: Transaction tx = _kit.getTransaction(_conn);
250: tx.begin();
251: TestClassA tca = new TestClassA();
252: tca.setOid("someoid");
253: Identity oid = _conn.getIdentity(tca);
254: tca = (TestClassA) _conn.getObjectByIdentity(oid);
255: if (tca == null) {
256: tca = new TestClassA();
257: tca.setOid("someoid");
258: tca.setValue1("abc");
259: tca.setValue2("123");
260: tca.setValue3(5);
261: _conn.makePersistent(tca);
262: TestClassB tcb = new TestClassB();
263: tcb.setOid("boid");
264: tcb.setValue1("hi there");
265: _conn.makePersistent(tcb);
266: tca.setB(tcb);
267: }
268: tx.commit();
269: m_tca = tca;
270: return m_tca;
271: }
272: }
273: }
|