KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > test > npt > NptTest


1 /*
2  * Copyright 2003 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: NptTest.java,v 1.1 2003/11/17 01:14:44 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.test.npt;
12
13 import com.triactive.jdo.test.*;
14 import javax.jdo.JDOHelper;
15 import javax.jdo.PersistenceManager;
16 import javax.jdo.Transaction;
17 import org.apache.log4j.Category;
18
19
20 /**
21  * Tests the basic behavior of non-persistent transactional fields.
22  * This is by no means an exhaustive test of what should happen to such fields
23  * in all possible state transitions - we'll rely on the TCK for that.
24  *
25  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
26  * @version $Revision: 1.1 $
27  */

28
29 public class NptTest extends StorageTestCase
30 {
31     private static final Category LOG = Category.getInstance(NptTest.class);
32
33     private boolean schemaInitialized = false;
34     private PersistenceManager pm;
35     private Transaction tx;
36     private NptWidget pobjs[] = new NptWidget[TEST_OBJECT_COUNT];
37
38
39     /**
40      * Used by the JUnit framework to construct tests. Normally, programmers
41      * would never explicitly use this constructor.
42      *
43      * @param name Name of the <tt>TestCase</tt>.
44      */

45
46     public NptTest(String JavaDoc name)
47     {
48         super(name);
49     }
50
51
52     protected void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55
56         if (!schemaInitialized)
57         {
58             addClassesToSchema(new Class JavaDoc[] { NptWidget.class, });
59             schemaInitialized = true;
60         }
61
62         pm = pmf.getPersistenceManager();
63         tx = pm.currentTransaction();
64
65         tx.setNontransactionalRead(true);
66     }
67
68
69     protected void tearDown() throws Exception JavaDoc
70     {
71         if (tx.isActive())
72             tx.rollback();
73
74         pm.close();
75
76         super.tearDown();
77     }
78
79
80     public void testBasicStorage() throws Exception JavaDoc
81     {
82         runStorageTestFor(NptWidget.class);
83     }
84
85
86     public void testCommitWithRetainFalse()
87     {
88         runCommitTest(false);
89     }
90
91
92     public void testCommitWithRetainTrue()
93     {
94         runCommitTest(true);
95     }
96
97
98     public void runCommitTest(boolean retainValues)
99     {
100         LOG.info("NPT test: commit with RetainValues=" + retainValues);
101
102         tx.setRetainValues(retainValues);
103
104         /*
105          * Insert some new objects and assert that the TX fields keep their
106          * values after commit.
107          */

108         tx.begin();
109         insert();
110         tx.commit();
111
112         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
113         {
114             NptWidget expect = (NptWidget)objs[i];
115             NptWidget actual = pobjs[i];
116
117             assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual));
118             assertEquals("Transactional field txInteger not retained", expect.getTxInteger(), actual.getTxInteger());
119             assertEquals("Transactional field txString not retained", expect.getTxString(), actual.getTxString());
120         }
121
122         /*
123          * Update the TX fields in the objects and assert that they keep their
124          * new values after commit.
125          */

126         tx.begin();
127
128         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
129         {
130             NptWidget expect = (NptWidget)objs[i];
131             NptWidget actual = pobjs[i];
132
133             expect.fillTxRandom();
134             actual.setTxInteger(expect.getTxInteger());
135             actual.setTxString(expect.getTxString());
136
137             assertTrue("Object is not dirty: " + ids[i], JDOHelper.isDirty(actual));
138         }
139
140         tx.commit();
141
142         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
143         {
144             NptWidget expect = (NptWidget)objs[i];
145             NptWidget actual = pobjs[i];
146
147             assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual));
148             assertEquals("Transactional field txInteger not retained", expect.getTxInteger(), actual.getTxInteger());
149             assertEquals("Transactional field txString not retained", expect.getTxString(), actual.getTxString());
150         }
151
152         /* Cleanup. */
153         tx.begin();
154         pm.deletePersistentAll(pobjs);
155         tx.commit();
156     }
157
158
159     public void testRollbackWithRestoreFalse()
160     {
161         LOG.info("NPT test: rollback with RestoreValues=false");
162
163         tx.setRestoreValues(false);
164
165         /*
166          * Insert some new objects and assert that the TX fields keep their
167          * values after rollback.
168          */

169         tx.begin();
170         insert();
171         tx.rollback();
172
173         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
174         {
175             NptWidget expect = (NptWidget)objs[i];
176             NptWidget actual = pobjs[i];
177
178             assertTrue("Object is persistent: " + actual, !JDOHelper.isPersistent(actual));
179             assertEquals("Transactional field txInteger affected by rollback", expect.getTxInteger(), actual.getTxInteger());
180             assertEquals("Transactional field txString affected by rollback", expect.getTxString(), actual.getTxString());
181         }
182     }
183
184
185     public void testRollbackWithRestoreTrue()
186     {
187         LOG.info("NPT test: rollback with RestoreValues=true");
188
189         tx.setRestoreValues(true);
190
191         /*
192          * Insert some new objects and assert that the TX fields get restored
193          * to their original values after rollback.
194          */

195         tx.begin();
196
197         insert();
198
199         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
200             pobjs[i].fillRandom();
201
202         tx.rollback();
203
204         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
205         {
206             NptWidget expect = (NptWidget)objs[i];
207             NptWidget actual = pobjs[i];
208
209             assertTrue("Object is persistent: " + actual, !JDOHelper.isPersistent(actual));
210             assertEquals("Transactional field txInteger not restored", expect.getTxInteger(), actual.getTxInteger());
211             assertEquals("Transactional field txString not restored", expect.getTxString(), actual.getTxString());
212         }
213
214         /*
215          * Update some existing objects and assert that the TX fields get
216          * restored to their original values after rollback.
217          */

218         tx.begin();
219         insert();
220         tx.commit();
221
222         tx.begin();
223
224         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
225             pobjs[i].fillRandom();
226
227         tx.rollback();
228
229         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
230         {
231             NptWidget expect = (NptWidget)objs[i];
232             NptWidget actual = pobjs[i];
233
234             assertTrue("Object is not persistent: " + ids[i], JDOHelper.isPersistent(actual));
235             assertEquals("Transactional field txInteger not restored", expect.getTxInteger(), actual.getTxInteger());
236             assertEquals("Transactional field txString not restored", expect.getTxString(), actual.getTxString());
237         }
238
239         /* Cleanup. */
240         tx.begin();
241         pm.deletePersistentAll(pobjs);
242         tx.commit();
243     }
244
245
246     private void insert()
247     {
248         LOG.info("Inserting " + TEST_OBJECT_COUNT + " " + NptWidget.class.getName() + " objects");
249
250         for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
251         {
252             objs[i] = new NptWidget();
253             objs[i].fillRandom();
254
255             pobjs[i] = (NptWidget)objs[i].clone();
256             assertFieldsEqual(objs[i], pobjs[i]);
257
258             pm.makePersistent(pobjs[i]);
259             ids[i] = JDOHelper.getObjectId(pobjs[i]);
260         }
261     }
262 }
263
Popular Tags