KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > odmg > UserTestCases


1 package org.apache.ojb.odmg;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.ojb.broker.Identity;
6 import org.apache.ojb.junit.ODMGTestCase;
7 import org.apache.ojb.odmg.shared.Person;
8 import org.apache.ojb.odmg.shared.PersonImpl;
9 import org.apache.ojb.odmg.shared.Site;
10 import org.odmg.Implementation;
11 import org.odmg.OQLQuery;
12 import org.odmg.Transaction;
13 import org.odmg.TransactionNotInProgressException;
14
15
16 /**
17  * Collection of test cases sent by OJB users
18  *
19  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
20  * @version $Id: UserTestCases.java,v 1.13.2.4 2005/06/04 14:48:05 arminw Exp $
21  */

22 public class UserTestCases extends ODMGTestCase
23 {
24     public static void main(String JavaDoc[] args)
25     {
26         String JavaDoc[] arr = {UserTestCases.class.getName()};
27         junit.textui.TestRunner.main(arr);
28     }
29
30     /**
31      * Send by Antonio
32      * Note: The name attribute was declared as
33      * unique in the DB.
34      */

35     public void testDuplicateInsertion() throws Exception JavaDoc
36     {
37         String JavaDoc name = "testDuplicateInsertion_" + System.currentTimeMillis();
38         String JavaDoc nameNew = "testDuplicateInsertion_New_" + System.currentTimeMillis();
39
40         //System.out.println("TEST: Database open");
41

42         // insert an object with UNIQUE field NAME="A site"
43
//System.out.println("TEST: Insert first object");
44
newSite(odmg, name, 2, 1);
45
46         // insert another object with UNIQUE field NAME="A site"
47
// This should not create a new object (UNIQUE fields conflict) but
48
// should resume gracefuly
49
//System.out.println("TEST: Insert second object, should fail");
50
try
51         {
52             newSite(odmg, name, 3, 2);
53             assertTrue("We should get a SqlException 'Violation of unique index'", false);
54         }
55         catch(Exception JavaDoc e)
56         {
57             // we wait for this exception
58
assertTrue(true);
59         }
60
61         // insert an object with new UNIQUE field NAME
62
// should always work
63
//System.out.println("TEST: Insert third object");
64
try
65         {
66             newSite(odmg, nameNew, 1, 2);
67             assertTrue(true);
68         }
69         catch(Exception JavaDoc e)
70         {
71             e.printStackTrace();
72             assertTrue("This exception should not happend: " + e.getMessage(), false);
73             throw e;
74         }
75         //System.out.println("TEST: Database closed");
76
}
77
78     private void newSite(Implementation odmg, String JavaDoc name, int year, int semester) throws Exception JavaDoc
79     {
80         Transaction tx = null;
81         tx = odmg.newTransaction();
82         Site site = null;
83         tx.begin();
84
85         site = new Site();
86         site.setName(name);
87         site.setYear(new Integer JavaDoc(year));
88         site.setSemester(new Integer JavaDoc(semester));
89
90         tx.lock(site, Transaction.WRITE);
91         tx.commit();
92     }
93
94     public void testSimpleQueryDelete() throws Exception JavaDoc
95     {
96         String JavaDoc name = "testSimpleQueryDelete - " + System.currentTimeMillis();
97
98         Site site = new Site();
99         site.setName(name);
100         Transaction tx = odmg.newTransaction();
101         tx.begin();
102         tx.lock(site, Transaction.WRITE);
103         tx.commit();
104
105         OQLQuery query = odmg.newOQLQuery();
106         query.create("select sites from " + Site.class.getName() + " where name=$1");
107         query.bind(name);
108         tx.begin();
109         List JavaDoc result = (List JavaDoc) query.execute();
110         if(result.size() == 0)
111         {
112             fail("Stored object not found");
113         }
114         tx.commit();
115
116         tx.begin();
117         database.deletePersistent(site);
118         tx.commit();
119
120         query = odmg.newOQLQuery();
121         query.create("select sites from " + Site.class.getName() + " where name=$1");
122         query.bind(name);
123         tx.begin();
124         List JavaDoc result2 = (List JavaDoc) query.execute();
125         if(result2.size() > 0)
126         {
127             fail("We should not found deleted objects");
128         }
129         tx.commit();
130     }
131
132     /**
133      * User test case posted by Charles:
134      * <p/>
135      * Up to now, we've been just using the broker layer. I now have a usecase
136      * where we will need to use the ODMG layer. We do not want to use implicit
137      * locking; I want my developers to explicit lock each object to an ODMG
138      * transaction (implicit locking generates loads of queries for all the proxy
139      * collections - should be fixed since OJB1.0.3).
140      * <p/>
141      * It seems that something 'funny' happens if implicit locking is turned off -
142      * objects are not marked as being "dirty" when changed - even when they are
143      * explicitly lock to the transaction.
144      * <p/>
145      * As I am a complete novice in the ways of the ODMG, I don't really know where
146      * to look to sort this issue out so I have added a new test method to
147      * org.apache.ojb.odmg.UserTestCases (it should be attached to this email).
148      * Essentially, it creates an object and persists it; retrieves and updates it;
149      * then flushes the cache, and retrieves it again to ensure the update worked.
150      * If ImplicitLocking is TRUE, the test passes. If ImplicitLocking is FALSE,
151      * the test fails.
152      * <p/>
153      * I think this is incorrect, and would dearly like this to be resolved.
154      * <p/>
155      * thma's comment: IMO this works as designed. objects must be locked to
156      * an ODMG tx before any modifications are taking place.
157      * I simply moved the lock two lines up and the test passed.
158      */

159     public void testImplicitLocking() throws Exception JavaDoc
160     {
161         String JavaDoc name = "testImplicitLocking - " + System.currentTimeMillis();
162         String JavaDoc queryString = "select sites from " + Site.class.getName() + " where name = $1";
163
164         /* Create an object */
165         Site site = new Site();
166         site.setName(name);
167
168         TransactionExt tx = (TransactionExt) odmg.newTransaction();
169         // disable implicit locking for this tx instance
170
// this setting was used for the life-time of the tx instance
171
tx.setImplicitLocking(false);
172         tx.begin();
173         database.makePersistent(site);
174         tx.commit();
175
176         /* Retrieve from the object created, and set the year*/
177         OQLQuery query = odmg.newOQLQuery();
178         query.create(queryString);
179         query.bind(name);
180
181         tx.begin();
182         List JavaDoc result = (List JavaDoc) query.execute();
183         assertEquals(1, result.size());
184         site = (Site) result.get(0);
185         assertNotNull(site);
186         assertNull(site.getYear());
187         tx.lock(site, Transaction.WRITE);
188         site.setYear(new Integer JavaDoc(2003));
189         tx.commit();
190
191         /* Flush the cache, and retrieve the object again */
192         query = odmg.newOQLQuery();
193         query.create(queryString);
194         query.bind(name);
195         tx.begin();
196         tx.getBroker().clearCache();
197         result = (List JavaDoc) query.execute();
198         assertEquals(1, result.size());
199         site = (Site) result.get(0);
200         assertNotNull(site);
201         assertNotNull("year should not be null", site.getYear());
202         tx.commit();
203     }
204
205     /**
206      * store an object and then retrieve it by id.
207      */

208     public void testStoreRetrieveSameTxn() throws Exception JavaDoc
209     {
210         String JavaDoc name = "testStoreRetrieveSameTxn_" + System.currentTimeMillis();
211         Person mum = new PersonImpl();
212         mum.setFirstname(name);
213
214         TransactionExt txn = (TransactionExt) odmg.newTransaction();
215         txn.begin();
216         txn.lock(mum, Transaction.WRITE);
217         // System.out.println("locked for write: " + mum);
218
txn.commit();
219
220         txn.begin();
221         txn.getBroker().clearCache();
222         Identity mumId = txn.getBroker().serviceIdentity().buildIdentity(mum);
223         Person mum2 = (Person) txn.getBroker().getObjectByIdentity(mumId);
224         // System.out.println("retrieved: " + mum2);
225
txn.commit();
226         assertNotNull(mum2);
227         assertEquals(name, mum2.getFirstname());
228     }
229
230     public void testRetrieveNonExistent()
231     {
232         try
233         {
234             TransactionExt tx = (TransactionExt) odmg.newTransaction();
235             tx.begin();
236             // construct an id that does not exist in the database
237
Identity id = tx.getBroker().serviceIdentity().buildIdentity(PersonImpl.class, new Integer JavaDoc(-1));
238             tx.getBroker().getObjectByIdentity(id);
239             tx.abort();
240         }
241         catch(Exception JavaDoc exc)
242         {
243             exc.printStackTrace();
244             fail("caught unexpected exception: " + exc.toString());
245         }
246     }
247
248     /**
249      * Not recommended to use such a construct!!!
250      */

251     public void testRetrieveOutsideTxn()
252     {
253         try
254         {
255             // construct an id that does not exist in the database
256
Identity id = new Identity(Person.class, Person.class, new Integer JavaDoc[]{new Integer JavaDoc(-1)});
257             TransactionImpl txn = (TransactionImpl) odmg.newTransaction();
258             try
259             {
260                 txn.getObjectByIdentity(id);
261                 fail("expected TransactionNotInProgressException not thrown");
262             }
263             catch(TransactionNotInProgressException exc)
264             {
265                 // expected.
266
}
267         }
268         catch(Exception JavaDoc exc)
269         {
270             exc.printStackTrace();
271             fail("caught unexpected exception: " + exc.toString());
272         }
273     }
274 }
275
Popular Tags