KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > persist > OddjobPersistTest


1 /*
2  * Copyright © 2004, Rob Gordon.
3  */

4 package org.oddjob.persist;
5
6 import java.io.File JavaDoc;
7 import java.io.Serializable JavaDoc;
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 /**
21  *
22  * @author Rob Gordon.
23  */

24 public class OddjobPersistTest extends TestCase {
25
26     private static File JavaDoc config = new File JavaDoc("test/conf/oddjob-persist-test1.xml");
27     private static File JavaDoc persistIn = new File JavaDoc("test/persist");
28     
29     /*
30      * Test saving a job.
31      */

32     public void test1Save() {
33         // delete existing file.
34
File JavaDoc ser = new File JavaDoc(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 JavaDoc seqJob = oj.lookup("oj2/sequence");
45         
46         assertTrue(seqJob instanceof Serializable JavaDoc);
47         assertEquals(JobState.COMPLETE,
48                 Helper.getJobState((Stateful) seqJob));
49                 
50         assertTrue("Persisted file exists", ser.exists());
51     }
52     
53     /**
54      * Test loading the job we've just saved.
55      */

56     public void test2Load() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc(1),
76                 ((DynaBean) seqJob).get("current"));
77         
78         Object JavaDoc[] children = ChildHelper.getChildren(oj);
79         assertEquals(1, children.length);
80         
81         // otherwise it wouldn't run.
82
((Resetable) seqJob).hardReset();
83         
84         assertEquals(JobState.READY, Helper.getJobState(seqJob));
85         assertEquals(JobState.READY, oj.lastJobStateEvent().getJobState());
86
87          ((Runnable JavaDoc) seqJob).run();
88         
89         assertEquals("Second execute sequence wrong.", new Integer JavaDoc(2),
90                 PropertyUtils.getProperty(seqJob, "current"));
91         assertEquals("State should be complete", JobState.COMPLETE,
92                 Helper.getJobState(seqJob));
93     }
94 }
95
Popular Tags