01: package org.apache.ojb.broker.sqlcount;
02:
03: import org.apache.ojb.broker.Identity;
04: import org.apache.ojb.broker.PersistenceBroker;
05: import org.apache.ojb.broker.PersistenceBrokerFactory;
06: import org.apache.ojb.broker.Person;
07:
08: import java.util.Collection;
09:
10: /**
11: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
12: * @version $Id: SimpleCountTest.java,v 1.2 2003/10/27 15:49:15 oliverm Exp $
13: */
14: public class SimpleCountTest extends
15: org.apache.ojb.broker.sqlcount.AbstractCountTest {
16: private Identity aId;
17:
18: protected PersistenceBroker myPB;
19:
20: protected void setUp() throws Exception {
21: super .setUp();
22: resetStmtCount();
23: myPB = PersistenceBrokerFactory.defaultPersistenceBroker();
24: myPB.beginTransaction();
25: Person a = new Person();
26: a.setFirstname("A");
27: myPB.store(a);
28: aId = new Identity(a, myPB);
29: myPB.commitTransaction();
30: logStmtCount("Wrote test data");
31: }
32:
33: /**
34: * very simple test: retrieve a person from the database
35: */
36: public void testRetrievePerson() {
37: resetStmtCount();
38: myPB.clearCache();
39: logger.info("begin txn");
40: myPB.beginTransaction();
41: logger.info("retrieving person");
42: Person a = (Person) myPB.getObjectByIdentity(aId);
43: // SELECT ... FROM PERSON WHERE ID = ..
44: // SELECT ... FROM PERSON_PROJECT WHERE PERSON_ID = ..
45: // SELECT ... FROM PROJECT, PERSON_PROJECT WHERE ...
46: logger.info("comitting txn");
47: // COMMIT
48: myPB.commitTransaction();
49: assertStmtCount("retrieve Person by Identity", 4);
50: }
51:
52: public void testRetrievePersonTwice() {
53: resetStmtCount();
54: myPB.clearCache();
55: logger.info("begin txn");
56: myPB.beginTransaction();
57: Person a = (Person) myPB.getObjectByIdentity(aId); // see above
58: assertStmtCount("retrieve Person by Identity", 3);
59: resetStmtCount();
60: Person b = (Person) myPB.getObjectByIdentity(aId); // should use cache
61: assertSame(a, b);
62: assertStmtCount("retrieve Person 2nd time", 0);
63: myPB.commitTransaction();
64: }
65:
66: public void testRetrieveEmptyProjects() {
67: resetStmtCount();
68: myPB.clearCache();
69: logger.info("begin txn");
70: myPB.beginTransaction();
71: Person a = (Person) myPB.getObjectByIdentity(aId); // see above
72: assertStmtCount("retrieve Person by Identity", 3);
73: resetStmtCount();
74: logger.info("accessing projects");
75: Collection c = a.getProjects();
76: assertEquals(0, c.size());
77: assertStmtCount("accessing non-proxy collection", 0);
78: Collection d = a.getRoles();
79: assertStmtCount("accessing proxy-collection", 0);
80: assertEquals(0, d.size());
81: myPB.commitTransaction();
82: }
83: }
|