001: /*
002: * User: Michael Rettig
003: * Date: Aug 17, 2002
004: * Time: 3:05:39 PM
005: */
006: package net.sourceforge.jaxor.example.tests;
007:
008: import junit.textui.TestRunner;
009: import net.sourceforge.jaxor.EntityNotFoundException;
010: import net.sourceforge.jaxor.JaxorSession;
011: import net.sourceforge.jaxor.MetaRow;
012: import net.sourceforge.jaxor.api.EntityInterface;
013: import net.sourceforge.jaxor.api.JaxorContext;
014: import net.sourceforge.jaxor.api.JaxorTransaction;
015: import net.sourceforge.jaxor.api.UnitOfWork;
016: import net.sourceforge.jaxor.api.ValueChangeListener;
017: import net.sourceforge.jaxor.api.FieldAdapter;
018: import net.sourceforge.jaxor.example.domain.*;
019: import net.sourceforge.jaxor.impl.UnitOfWorkImpl;
020: import net.sourceforge.jaxor.util.SystemException;
021: import net.sourceforge.jaxor.util.JaxorTimestamp;
022:
023: public class UnitOfWorkTest extends TableTestCase {
024:
025: protected MetaRow getRow() {
026: return new AddressMetaRow();
027: }
028:
029: public void testInsertAndUpdate() {
030: AddressEntity ent = ObjectFactory.createAddress();
031: commit();
032: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
033: ent.setState("Fake state");
034: ent.setZipCode("fake zip");
035: commit();
036: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
037: assertEquals(ent.getState(), "Fake state");
038: }
039:
040: public void testUpdateRegistrationWithMutableTimestampFields() {
041: AddressEntity ent = ObjectFactory.createAddress();
042: commit();
043: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
044: java.sql.Timestamp insertDate = ent.getInsertDate();
045: UnitOfWork unitOfWork = getJaxor().getUnitOfWork();
046: assertEquals(0, unitOfWork.size());
047: MockChangeListener listener = new MockChangeListener();
048: FieldAdapter fieldAdapter = ((JaxorTimestamp) insertDate)
049: .getFieldAdapter();
050: fieldAdapter.addChangeListener(listener);
051: insertDate.setTime(0);
052: assertTrue(ent.getJaxorContext().getUnitOfWork() == unitOfWork);
053: FieldAdapter adapt = ent.getFields().getField(
054: new AddressMetaRow().getInsertDate().getColumn());
055: assertTrue(adapt == fieldAdapter);
056: assertEquals(1, listener.count);
057: assertTrue(insertDate.getClass() == JaxorTimestamp.class);
058: assertEquals(1, unitOfWork.size());
059: commit();
060: AddressEntity newAddress = AddressFinder.selectByPrimaryKey(ent
061: .getAddressId());
062: assertFalse(ent == newAddress);
063: assertEquals(0, newAddress.getInsertDate().getTime());
064: }
065:
066: private static class MockChangeListener implements
067: ValueChangeListener {
068: int count = 0;
069:
070: public boolean registerChange(Object newValue, Object oldValue,
071: FieldAdapter mapper) {
072: count++;
073: return true;
074: }
075: }
076:
077: public void testUpdateRegistrationWithMutableFieldAfterFlush() {
078: AddressEntity ent = ObjectFactory.createAddress();
079: getJaxor().flush();
080: assertEquals(0, getJaxor().getUnitOfWork().size());
081: ent.getInsertDate().setTime(0);
082: assertEquals(1, getJaxor().getUnitOfWork().size());
083: ent.getInsertDate().setTime(1);
084: assertEquals(1, getJaxor().getUnitOfWork().size());
085: }
086:
087: public void testNewAndUpdateInSameSession() {
088: assertEquals(0, AddressFinder.selectAll().size());
089: AddressEntity ent = ObjectFactory.createAddress();
090: Long id = ent.getAddressId();
091: assertEquals(1, getJaxor().getUnitOfWork().size());
092: commit();
093: ent = AddressFinder.selectByPrimaryKey(id);
094: ent.setState("Fake state");
095: ent.setZipCode("fake zip");
096: commit();
097: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
098: assertEquals(ent.getState(), "Fake state");
099: assertEquals(ent.getZipCode(), "fake zip");
100: }
101:
102: public void testMultiUpdateInSameSession() {
103: AddressEntity ent = ObjectFactory.createAddress();
104: commit();
105: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
106: ent.setState("Fake state");
107: ent.setZipCode("fake zip");
108: JaxorSession.commit();
109: assertEquals(ent.getState(), "Fake state");
110: assertEquals(ent.getZipCode(), "fake zip");
111: ent.setState("2");
112: ent.setZipCode("3");
113: commit();
114: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
115: assertEquals(ent.getState(), "2");
116: assertEquals(ent.getZipCode(), "3");
117: }
118:
119: public void testDeletion() {
120: //Mckoi does not support this level of transaction isolation
121: if (!isMckoiTest()) {
122: AddressEntity ent = ObjectFactory.createAddress();
123: commit();
124: ent.delete();
125: try {
126: AddressFinder.selectByPrimaryKey(ent.getAddressId());
127: fail("Should not be found");
128: } catch (EntityNotFoundException expected) {
129: }
130: }
131: }
132:
133: public void testDeletedEntityIsRemovedFromContextOnFlush() {
134: AddressEntity ent = ObjectFactory.createAddress();
135: commit();
136: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
137: ent.registerDelete();
138: getJaxor().flush();
139: try {
140: AddressFinder.selectByPrimaryKey(ent.getAddressId());
141: fail("Deleted object should not be found after flush");
142: } catch (EntityNotFoundException expected) {
143: }
144: }
145:
146: public void testMultiInsertInSameSession() {
147: AddressEntity ent = ObjectFactory.createAddress();
148: JaxorSession.commit();
149: assertNotNull(AddressFinder.selectByPrimaryKey(ent
150: .getAddressId()));
151: JaxorSession.commit();
152: assertNotNull(AddressFinder.selectByPrimaryKey(ent
153: .getAddressId()));
154: commit();
155: assertNotNull(AddressFinder.selectByPrimaryKey(ent
156: .getAddressId()));
157: }
158:
159: public void testMultUpdateInSameSession() {
160: AddressEntity ent = ObjectFactory.createAddress();
161: JaxorSession.commit();
162: AddressEntity address = AddressFinder.selectByPrimaryKey(ent
163: .getAddressId());
164: assertNotNull(address);
165: address.setCity("new mock city");
166: JaxorSession.commit();
167: address = AddressFinder.selectByPrimaryKey(ent.getAddressId());
168: assertNotNull(address);
169: assertEquals("new mock city", address.getCity());
170: address.setStreet("new fake street");
171: commit();
172: address = AddressFinder.selectByPrimaryKey(ent.getAddressId());
173: assertEquals("new fake street", address.getStreet());
174: assertNotNull(address);
175: }
176:
177: public void testInsertFailureThenSucceedOnRetry() {
178: final SystemException exc = new SystemException("failed");
179: AddressBase ent = new AddressImpl() {
180: boolean fail = true;
181:
182: public void validate() {
183: if (fail) {
184: fail = false;
185: throw exc;
186: }
187: }
188: };
189: ent.setAddressId(new Long(123));
190: ent.setMetaRow(new AddressMetaRow());
191: getJaxor().registerNew(ent);
192: ObjectFactory.setDefaultFields(ent);
193: try {
194: JaxorSession.commit();
195: fail("Should fail on flush");
196: } catch (Exception expected) {
197: assertTrue(expected == exc);
198: }
199: getJaxor().commit();
200: assertNotNull(AddressFinder.selectByPrimaryKey(new Long(123)));
201: }
202:
203: public void testDelete() {
204: AddressEntity ent = ObjectFactory.createAddress();
205: commit();
206: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
207: ent.registerDelete();
208: commit();
209: try {
210: AddressFinder.selectByPrimaryKey(ent.getAddressId());
211: fail("Address is deleted, so it should not be found");
212: } catch (EntityNotFoundException expected) {
213: }
214: assertEquals(0, AddressFinder.selectAll().size());
215: }
216:
217: public void testRegisterDelete() {
218: AddressEntity ent = ObjectFactory.createAddress();
219: commit();
220: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
221: ent.registerDelete();
222: commit();
223: try {
224: AddressFinder.selectByPrimaryKey(ent.getAddressId());
225: fail("Address is deleted, so it should not be found");
226: } catch (EntityNotFoundException expected) {
227: }
228: assertEquals(0, AddressFinder.selectAll().size());
229: }
230:
231: public void testRegisterDeleteWithCommit() {
232: AddressEntity ent = ObjectFactory.createAddress();
233: commit();
234: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
235: ent.registerDelete();
236: commit();
237: try {
238: AddressFinder.selectByPrimaryKey(ent.getAddressId());
239: fail("Address is deleted, so it should not be found");
240: } catch (EntityNotFoundException expected) {
241: }
242: assertEquals(0, AddressFinder.selectAll().size());
243: }
244:
245: public void testRegisteringForDeleteAndInsertInOneSession() {
246: AddressEntity ent = ObjectFactory.createAddress();
247: ent.registerDelete();
248: commit();
249: try {
250: AddressFinder.selectByPrimaryKey(ent.getAddressId());
251: fail("Address is deleted, so it should not be found");
252: } catch (EntityNotFoundException expected) {
253: }
254: }
255:
256: public void testDoNotRegisterUpdateWithSettingToSameValue() {
257: AddressEntity ent = ObjectFactory.createAddress();
258: commit();
259: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
260: ent.setState(ent.getState());
261: UnitOfWorkImpl impl = (UnitOfWorkImpl) JaxorSession
262: .getJaxorContext().getUnitOfWork();
263: assertEquals(0, impl.size());
264: }
265:
266: public void testMultipleUpdateOfObject() {
267: AddressEntity ent = ObjectFactory.createAddress();
268: commit();
269: MyUnitOfWorkImpl work = new MyUnitOfWorkImpl();
270: getJaxor().setUnitOfWork(work);
271: ent = AddressFinder.selectByPrimaryKey(ent.getAddressId());
272: ent.setState(ent.getState() + "01");
273: ent.setStreet(ent.getStreet() + "01");
274: assertEquals(1, work.updateCount);
275: assertEquals(1, work.size());
276: }
277:
278: public void testUpdateAfterInsert() {
279: JaxorContext context = getJaxor();
280: MyUnitOfWorkImpl work = new MyUnitOfWorkImpl();
281: context.setUnitOfWork(work);
282: AddressFinderBase finder = new AddressFinderBase(context);
283: AddressEntity address = finder.newInstance(new Long(124));
284: ObjectFactory.setDefaultFields(address);
285: assertEquals(0, work.updateCount);
286: context.flush();
287: address.setStreet(address.getStreet() + "01");
288: assertEquals(1, work.updateCount);
289: JaxorTransaction trans = context.getTransaction();
290: context.setTransaction(null);
291: assertNotNull("This should hit the cache", finder
292: .selectByAddressId(new Long(124)));
293: context.setTransaction(trans);
294:
295: }
296:
297: private static class MyUnitOfWorkImpl extends UnitOfWorkImpl {
298: public int updateCount = 0;
299:
300: public void registerUpdate(EntityInterface update) {
301: updateCount++;
302: super .registerUpdate(update);
303: }
304: }
305:
306: public static void main(String[] args) {
307: TestRunner.run(UnitOfWorkTest.class);
308: }
309:
310: }
|