KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sqlcount > SimpleCountTest


1 package org.apache.ojb.broker.sqlcount;
2
3 import org.apache.ojb.broker.Identity;
4 import org.apache.ojb.broker.PersistenceBroker;
5 import org.apache.ojb.broker.PersistenceBrokerFactory;
6 import org.apache.ojb.broker.Person;
7
8 import java.util.Collection JavaDoc;
9
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
15         extends org.apache.ojb.broker.sqlcount.AbstractCountTest
16 {
17   private Identity aId;
18
19   protected PersistenceBroker myPB;
20
21   protected void setUp() throws Exception JavaDoc
22   {
23     super.setUp();
24     resetStmtCount();
25     myPB = PersistenceBrokerFactory.defaultPersistenceBroker();
26     myPB.beginTransaction();
27     Person a = new Person();
28     a.setFirstname("A");
29     myPB.store(a);
30     aId = new Identity(a, myPB);
31     myPB.commitTransaction();
32     logStmtCount("Wrote test data");
33   }
34
35   /**
36    * very simple test: retrieve a person from the database
37    */

38   public void testRetrievePerson()
39   {
40     resetStmtCount();
41     myPB.clearCache();
42     logger.info("begin txn");
43     myPB.beginTransaction();
44     logger.info("retrieving person");
45     Person a = (Person)myPB.getObjectByIdentity(aId);
46     // SELECT ... FROM PERSON WHERE ID = ..
47
// SELECT ... FROM PERSON_PROJECT WHERE PERSON_ID = ..
48
// SELECT ... FROM PROJECT, PERSON_PROJECT WHERE ...
49
logger.info("comitting txn");
50     // COMMIT
51
myPB.commitTransaction();
52     assertStmtCount("retrieve Person by Identity", 4);
53   }
54
55   public void testRetrievePersonTwice()
56   {
57     resetStmtCount();
58     myPB.clearCache();
59     logger.info("begin txn");
60     myPB.beginTransaction();
61     Person a = (Person)myPB.getObjectByIdentity(aId); // see above
62
assertStmtCount("retrieve Person by Identity", 3);
63     resetStmtCount();
64     Person b = (Person)myPB.getObjectByIdentity(aId); // should use cache
65
assertSame(a, b);
66     assertStmtCount("retrieve Person 2nd time", 0);
67     myPB.commitTransaction();
68   }
69
70   public void testRetrieveEmptyProjects()
71   {
72     resetStmtCount();
73     myPB.clearCache();
74     logger.info("begin txn");
75     myPB.beginTransaction();
76     Person a = (Person)myPB.getObjectByIdentity(aId); // see above
77
assertStmtCount("retrieve Person by Identity", 3);
78     resetStmtCount();
79     logger.info("accessing projects");
80     Collection JavaDoc c = a.getProjects();
81     assertEquals(0, c.size());
82     assertStmtCount("accessing non-proxy collection", 0);
83     Collection JavaDoc d = a.getRoles();
84     assertStmtCount("accessing proxy-collection", 0);
85     assertEquals(0, d.size());
86     myPB.commitTransaction();
87   }
88 }
89
Popular Tags