Downloads Documentation Community Contribute Demo






Show Sidebar
Login | Register
Show
Ignore:
Timestamp:
08/09/08 16:18:42 (5 months ago)
Author:
mcunderlik
Message:

data_synchronization_bidirectional branch: fixed unit tests to work with Junit4 changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • openmrs/branches/data_synchronization_bidirectional/test/api/org/openmrs/test/synchronization/engine/SyncBaseTest.java

    r5133 r5215  
    11package org.openmrs.test.synchronization.engine; 
    22 
     3import static org.junit.Assert.assertEquals; 
     4import static org.junit.Assert.assertFalse; 
     5import static org.junit.Assert.fail; 
     6 
     7import org.springframework.test.annotation.Rollback; 
     8import org.springframework.test.annotation.NotTransactional; 
     9import org.springframework.transaction.annotation.Transactional; 
    310 
    411import java.text.DateFormat; 
     
    815import org.apache.commons.logging.Log; 
    916import org.apache.commons.logging.LogFactory; 
    10 import org.hibernate.Session; 
    1117import org.openmrs.test.BaseContextSensitiveTest; 
    1218import org.openmrs.api.context.Context; 
     
    2026 
    2127/** 
    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 
    2334 * 
    2435 */ 
     
    3142        public abstract String getInitialDataset(); 
    3243         
    33         public String getParentSetupDataset() { 
    34                 return "org/openmrs/test/synchronization/engine/include/SyncRemoteChildServer.xml"; 
    35         } 
    36  
    3744        protected void setupSyncTestChild() throws Exception { 
    3845                initializeInMemoryDatabase(); 
     
    4148        } 
    4249         
    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(); 
    4476                deleteAllData(); 
    45                 initializeInMemoryDatabase(); 
    46                 executeDataSet(getInitialDataset()); 
    47                 executeDataSet(getParentSetupDataset()); 
     77                executeDataSet("org/openmrs/test/synchronization/engine/include/SyncCreateTest.xml"); 
     78                authenticate();          
    4879        } 
     80         
     81        @Transactional 
     82        @Rollback(false) 
     83        protected void applySyncChanges() throws Exception { 
    4984                 
    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 
    6386                List<SyncRecord> syncRecords = Context.getSynchronizationService().getSyncRecords(); 
    64                 if (syncRecords == null || syncRecords.size() == 0) {  
     87                if (syncRecords == null || syncRecords.size() == 0) { 
    6588                        assertFalse("No changes found (i.e. sync records size is 0)", true); 
    6689                } 
    6790                 
    68                 log.info("\n************************************* Deleting Data *************************************"); 
     91                //now reload db from scratch 
     92                log.info("\n************************************* Reload Database *************************************"); 
    6993                deleteAllData(); 
    70                  
    7194                executeDataSet("org/openmrs/test/synchronization/engine/include/SyncCreateTest.xml"); 
    7295                executeDataSet("org/openmrs/test/synchronization/engine/include/SyncRemoteChildServer.xml"); 
     
    92115                } 
    93116                 
    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(); 
    96140                 
    97                 testMethods.runOnParent(); 
    98                 Context.closeSession(); 
    99         } 
     141                this.runOnChild(testMethods); 
     142                 
     143                this.applySyncChanges(); 
     144                 
     145                this.runOnParent(testMethods); 
     146        }        
    100147} 
    101148