001: /*
002: * Copyright 2003 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: NptTest.java,v 1.1 2003/11/17 01:14:44 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test.npt;
012:
013: import com.triactive.jdo.test.*;
014: import javax.jdo.JDOHelper;
015: import javax.jdo.PersistenceManager;
016: import javax.jdo.Transaction;
017: import org.apache.log4j.Category;
018:
019: /**
020: * Tests the basic behavior of non-persistent transactional fields.
021: * This is by no means an exhaustive test of what should happen to such fields
022: * in all possible state transitions - we'll rely on the TCK for that.
023: *
024: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
025: * @version $Revision: 1.1 $
026: */
027:
028: public class NptTest extends StorageTestCase {
029: private static final Category LOG = Category
030: .getInstance(NptTest.class);
031:
032: private boolean schemaInitialized = false;
033: private PersistenceManager pm;
034: private Transaction tx;
035: private NptWidget pobjs[] = new NptWidget[TEST_OBJECT_COUNT];
036:
037: /**
038: * Used by the JUnit framework to construct tests. Normally, programmers
039: * would never explicitly use this constructor.
040: *
041: * @param name Name of the <tt>TestCase</tt>.
042: */
043:
044: public NptTest(String name) {
045: super (name);
046: }
047:
048: protected void setUp() throws Exception {
049: super .setUp();
050:
051: if (!schemaInitialized) {
052: addClassesToSchema(new Class[] { NptWidget.class, });
053: schemaInitialized = true;
054: }
055:
056: pm = pmf.getPersistenceManager();
057: tx = pm.currentTransaction();
058:
059: tx.setNontransactionalRead(true);
060: }
061:
062: protected void tearDown() throws Exception {
063: if (tx.isActive())
064: tx.rollback();
065:
066: pm.close();
067:
068: super .tearDown();
069: }
070:
071: public void testBasicStorage() throws Exception {
072: runStorageTestFor(NptWidget.class);
073: }
074:
075: public void testCommitWithRetainFalse() {
076: runCommitTest(false);
077: }
078:
079: public void testCommitWithRetainTrue() {
080: runCommitTest(true);
081: }
082:
083: public void runCommitTest(boolean retainValues) {
084: LOG.info("NPT test: commit with RetainValues=" + retainValues);
085:
086: tx.setRetainValues(retainValues);
087:
088: /*
089: * Insert some new objects and assert that the TX fields keep their
090: * values after commit.
091: */
092: tx.begin();
093: insert();
094: tx.commit();
095:
096: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
097: NptWidget expect = (NptWidget) objs[i];
098: NptWidget actual = pobjs[i];
099:
100: assertTrue("Object is not persistent: " + ids[i], JDOHelper
101: .isPersistent(actual));
102: assertEquals("Transactional field txInteger not retained",
103: expect.getTxInteger(), actual.getTxInteger());
104: assertEquals("Transactional field txString not retained",
105: expect.getTxString(), actual.getTxString());
106: }
107:
108: /*
109: * Update the TX fields in the objects and assert that they keep their
110: * new values after commit.
111: */
112: tx.begin();
113:
114: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
115: NptWidget expect = (NptWidget) objs[i];
116: NptWidget actual = pobjs[i];
117:
118: expect.fillTxRandom();
119: actual.setTxInteger(expect.getTxInteger());
120: actual.setTxString(expect.getTxString());
121:
122: assertTrue("Object is not dirty: " + ids[i], JDOHelper
123: .isDirty(actual));
124: }
125:
126: tx.commit();
127:
128: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
129: NptWidget expect = (NptWidget) objs[i];
130: NptWidget actual = pobjs[i];
131:
132: assertTrue("Object is not persistent: " + ids[i], JDOHelper
133: .isPersistent(actual));
134: assertEquals("Transactional field txInteger not retained",
135: expect.getTxInteger(), actual.getTxInteger());
136: assertEquals("Transactional field txString not retained",
137: expect.getTxString(), actual.getTxString());
138: }
139:
140: /* Cleanup. */
141: tx.begin();
142: pm.deletePersistentAll(pobjs);
143: tx.commit();
144: }
145:
146: public void testRollbackWithRestoreFalse() {
147: LOG.info("NPT test: rollback with RestoreValues=false");
148:
149: tx.setRestoreValues(false);
150:
151: /*
152: * Insert some new objects and assert that the TX fields keep their
153: * values after rollback.
154: */
155: tx.begin();
156: insert();
157: tx.rollback();
158:
159: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
160: NptWidget expect = (NptWidget) objs[i];
161: NptWidget actual = pobjs[i];
162:
163: assertTrue("Object is persistent: " + actual, !JDOHelper
164: .isPersistent(actual));
165: assertEquals(
166: "Transactional field txInteger affected by rollback",
167: expect.getTxInteger(), actual.getTxInteger());
168: assertEquals(
169: "Transactional field txString affected by rollback",
170: expect.getTxString(), actual.getTxString());
171: }
172: }
173:
174: public void testRollbackWithRestoreTrue() {
175: LOG.info("NPT test: rollback with RestoreValues=true");
176:
177: tx.setRestoreValues(true);
178:
179: /*
180: * Insert some new objects and assert that the TX fields get restored
181: * to their original values after rollback.
182: */
183: tx.begin();
184:
185: insert();
186:
187: for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
188: pobjs[i].fillRandom();
189:
190: tx.rollback();
191:
192: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
193: NptWidget expect = (NptWidget) objs[i];
194: NptWidget actual = pobjs[i];
195:
196: assertTrue("Object is persistent: " + actual, !JDOHelper
197: .isPersistent(actual));
198: assertEquals("Transactional field txInteger not restored",
199: expect.getTxInteger(), actual.getTxInteger());
200: assertEquals("Transactional field txString not restored",
201: expect.getTxString(), actual.getTxString());
202: }
203:
204: /*
205: * Update some existing objects and assert that the TX fields get
206: * restored to their original values after rollback.
207: */
208: tx.begin();
209: insert();
210: tx.commit();
211:
212: tx.begin();
213:
214: for (int i = 0; i < TEST_OBJECT_COUNT; ++i)
215: pobjs[i].fillRandom();
216:
217: tx.rollback();
218:
219: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
220: NptWidget expect = (NptWidget) objs[i];
221: NptWidget actual = pobjs[i];
222:
223: assertTrue("Object is not persistent: " + ids[i], JDOHelper
224: .isPersistent(actual));
225: assertEquals("Transactional field txInteger not restored",
226: expect.getTxInteger(), actual.getTxInteger());
227: assertEquals("Transactional field txString not restored",
228: expect.getTxString(), actual.getTxString());
229: }
230:
231: /* Cleanup. */
232: tx.begin();
233: pm.deletePersistentAll(pobjs);
234: tx.commit();
235: }
236:
237: private void insert() {
238: LOG.info("Inserting " + TEST_OBJECT_COUNT + " "
239: + NptWidget.class.getName() + " objects");
240:
241: for (int i = 0; i < TEST_OBJECT_COUNT; ++i) {
242: objs[i] = new NptWidget();
243: objs[i].fillRandom();
244:
245: pobjs[i] = (NptWidget) objs[i].clone();
246: assertFieldsEqual(objs[i], pobjs[i]);
247:
248: pm.makePersistent(pobjs[i]);
249: ids[i] = JDOHelper.getObjectId(pobjs[i]);
250: }
251: }
252: }
|