1 4 package org.oddjob.persist; 5 6 import java.io.File ; 7 import java.io.Serializable ; 8 9 import junit.framework.TestCase; 10 11 import org.apache.commons.beanutils.DynaBean; 12 import org.apache.commons.beanutils.PropertyUtils; 13 import org.oddjob.Helper; 14 import org.oddjob.Oddjob; 15 import org.oddjob.Resetable; 16 import org.oddjob.Stateful; 17 import org.oddjob.state.JobState; 18 import org.oddjob.structural.ChildHelper; 19 20 24 public class OddjobPersistTest extends TestCase { 25 26 private static File config = new File ("test/conf/oddjob-persist-test1.xml"); 27 private static File persistIn = new File ("test/persist"); 28 29 32 public void test1Save() { 33 File ser = new File (persistIn, "sequence.ser"); 35 ser.delete(); 36 37 Oddjob oj = new Oddjob(); 38 oj.setName("Oddjob"); 39 oj.setConfig(config); 40 41 oj.run(); 42 assertEquals("State should be complete", JobState.COMPLETE, Helper.getJobState(oj)); 43 44 Object seqJob = oj.lookup("oj2/sequence"); 45 46 assertTrue(seqJob instanceof Serializable ); 47 assertEquals(JobState.COMPLETE, 48 Helper.getJobState((Stateful) seqJob)); 49 50 assertTrue("Persisted file exists", ser.exists()); 51 } 52 53 56 public void test2Load() throws Exception { 57 58 Oddjob oj = new Oddjob(); 59 oj.setName("OJ again"); 60 oj.setConfig(config); 61 62 oj.setLoadOnly(true); 63 oj.run(); 64 65 Oddjob oj2 = (Oddjob) oj.lookup("oj2"); 66 oj2.setLoadOnly(true); 67 oj2.run(); 68 69 Object seqJob = oj.lookup("oj2/sequence"); 70 71 assertNotNull(seqJob); 72 assertEquals("Persisted state should be complete", JobState.COMPLETE, 73 Helper.getJobState(seqJob)); 74 75 assertEquals("Just loaded sequence wrong.", new Integer (1), 76 ((DynaBean) seqJob).get("current")); 77 78 Object [] children = ChildHelper.getChildren(oj); 79 assertEquals(1, children.length); 80 81 ((Resetable) seqJob).hardReset(); 83 84 assertEquals(JobState.READY, Helper.getJobState(seqJob)); 85 assertEquals(JobState.READY, oj.lastJobStateEvent().getJobState()); 86 87 ((Runnable ) seqJob).run(); 88 89 assertEquals("Second execute sequence wrong.", new Integer (2), 90 PropertyUtils.getProperty(seqJob, "current")); 91 assertEquals("State should be complete", JobState.COMPLETE, 92 Helper.getJobState(seqJob)); 93 } 94 } 95 | Popular Tags |