Changeset 5215 for openmrs/branches/data_synchronization_bidirectional/test/api/org/openmrs/test/synchronization/engine/SyncBaseTest.java
- Timestamp:
- 08/09/08 16:18:42 (5 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
openmrs/branches/data_synchronization_bidirectional/test/api/org/openmrs/test/synchronization/engine/SyncBaseTest.java
r5133 r5215 1 1 package org.openmrs.test.synchronization.engine; 2 2 3 import static org.junit.Assert.assertEquals; 4 import static org.junit.Assert.assertFalse; 5 import static org.junit.Assert.fail; 6 7 import org.springframework.test.annotation.Rollback; 8 import org.springframework.test.annotation.NotTransactional; 9 import org.springframework.transaction.annotation.Transactional; 3 10 4 11 import java.text.DateFormat; … … 8 15 import org.apache.commons.logging.Log; 9 16 import org.apache.commons.logging.LogFactory; 10 import org.hibernate.Session;11 17 import org.openmrs.test.BaseContextSensitiveTest; 12 18 import org.openmrs.api.context.Context; … … 20 26 21 27 /** 22 * to setup common routines and initialization for all sync tests. 28 * Sets up common routines and initialization for all sync tests. Note for all sync tests: 29 * MUST MARK AS NotTransctional so that Tx that is created in runOnChild() menthod is 30 * committed upon exit of that method. 31 * 32 * Note: org.springframework.transaction.annotation.Propagation.REQUIRES_NEW doesn't help 33 * since on most EDBMS systems it doesn't do what spec says 23 34 * 24 35 */ … … 31 42 public abstract String getInitialDataset(); 32 43 33 public String getParentSetupDataset() {34 return "org/openmrs/test/synchronization/engine/include/SyncRemoteChildServer.xml";35 }36 37 44 protected void setupSyncTestChild() throws Exception { 38 45 initializeInMemoryDatabase(); … … 41 48 } 42 49 43 protected void setupSyncTestParent() throws Exception { 50 @Transactional 51 @Rollback(false) 52 protected void runOnChild(SyncTestHelper testMethods) throws Exception { 53 log.info("\n************************************* Running On Child *************************************"); 54 testMethods.runOnChild(); 55 } 56 57 @Transactional 58 protected void runOnParent(SyncTestHelper testMethods) throws Exception { 59 //now run parent 60 log.info("\n************************************* Running on Parent *************************************"); 61 testMethods.runOnParent(); 62 } 63 64 /** 65 * Sets up initial data set before set of instructions simulating child changes is executed. 66 * 67 * @see #runOnChild(SyncTestHelper) 68 * @see #runSyncTest(SyncTestHelper) 69 * 70 * @throws Exception 71 */ 72 @Transactional 73 @Rollback(false) 74 protected void beforeRunOnChild() throws Exception { 75 Context.openSession(); 44 76 deleteAllData(); 45 initializeInMemoryDatabase(); 46 executeDataSet(getInitialDataset()); 47 executeDataSet(getParentSetupDataset()); 77 executeDataSet("org/openmrs/test/synchronization/engine/include/SyncCreateTest.xml"); 78 authenticate(); 48 79 } 80 81 @Transactional 82 @Rollback(false) 83 protected void applySyncChanges() throws Exception { 49 84 50 public void runSyncTest(SyncTestHelper testMethods) throws Exception { 51 deleteAllData(); 52 Context.openSession(); 53 executeDataSet("org/openmrs/test/synchronization/engine/include/SyncCreateTest.xml"); 54 authenticate(); 55 56 log.info("\n************************************* Running On Child *************************************"); 57 testMethods.runOnChild(); 58 59 this.transactionManager.commit(this.transactionStatus); 60 Context.closeSession(); 61 Context.openSession(); 62 85 //get sync records created by child 63 86 List<SyncRecord> syncRecords = Context.getSynchronizationService().getSyncRecords(); 64 if (syncRecords == null || syncRecords.size() == 0) { 87 if (syncRecords == null || syncRecords.size() == 0) { 65 88 assertFalse("No changes found (i.e. sync records size is 0)", true); 66 89 } 67 90 68 log.info("\n************************************* Deleting Data *************************************"); 91 //now reload db from scratch 92 log.info("\n************************************* Reload Database *************************************"); 69 93 deleteAllData(); 70 71 94 executeDataSet("org/openmrs/test/synchronization/engine/include/SyncCreateTest.xml"); 72 95 executeDataSet("org/openmrs/test/synchronization/engine/include/SyncRemoteChildServer.xml"); … … 92 115 } 93 116 94 Context.clearSession(); 95 log.info("\n************************************* Running on Parent *************************************"); 117 return; 118 } 119 120 /** 121 * Executes the sync test workflow: 122 * <br/>1. prepopulate DB 123 * <br/>2. Execute set of instructions simulating sync child 124 * <br/>3. Fetch sync records, re-initialize DB for parent and then apply the sync records 125 * <br/>4. Execute set of instructions simulating sync parent; typically just asserts to ensure child changes 126 * came accross. 127 * 128 *<br/>Note: The non-transactional vs. transactional behavior of helper methods: each step must be in its own Tx since sync flushes 129 * its sync record at Tx boundry. Consequently it is required for the overall test to run as non-transactional 130 * and each individual step to be transactional; as stated in class comments, true nested transactions are RDMS fantasy, 131 * it mostly doesn't exist. 132 * 133 * @param testMethods helper object holding methods for child and parent execution 134 * @throws Exception 135 */ 136 @NotTransactional 137 public void runSyncTest(SyncTestHelper testMethods) throws Exception { 138 139 this.beforeRunOnChild(); 96 140 97 testMethods.runOnParent(); 98 Context.closeSession(); 99 } 141 this.runOnChild(testMethods); 142 143 this.applySyncChanges(); 144 145 this.runOnParent(testMethods); 146 } 100 147 } 101 148