KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.persist;
5
6 import java.io.File JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.apache.commons.beanutils.DynaBean;
11 import org.oddjob.Helper;
12 import org.oddjob.Resetable;
13 import org.oddjob.framework.RunnableWrapper;
14 import org.oddjob.jobs.EchoJob;
15 import org.oddjob.state.JobState;
16
17 /**
18  *
19  * @author Rob Gordon.
20  */

21 public class SerializePersisterTest extends TestCase {
22
23     /**
24      * Simple test of persisting something
25      *
26      */

27     public void testPersist() {
28         Object JavaDoc job = RunnableWrapper.wrapperFor(new EchoJob());
29         ((DynaBean) job).set("name", "Test");
30         ((DynaBean) job).set("text", "Hello World");
31         ((Runnable JavaDoc) job).run();
32
33         SerializePersister persister = new SerializePersister();
34         persister.setDir(new File JavaDoc("test/persist"));
35         
36         try {
37             persister.persist("test-persist", job);
38         }
39         catch (OddjobPersistException e) {
40             e.printStackTrace();
41             fail ("test persist should work." + e.getMessage());
42         }
43         
44         File JavaDoc check = new File JavaDoc("test/persist/test-persist.ser");
45         assertTrue(check.exists());
46     }
47     
48     /**
49      * Simple test of loading something back.
50      *
51      */

52     public void testLoad() {
53         SerializePersister persister = new SerializePersister();
54         persister.setDir(new File JavaDoc("test/persist"));
55             
56         Runnable JavaDoc job = (Runnable JavaDoc) persister.restore("test-persist");
57         
58         assertEquals(JobState.COMPLETE, Helper.getJobState(job));
59
60         // check we can run it again.
61

62         ((Resetable) job).hardReset();
63         
64         assertEquals(JobState.READY, Helper.getJobState(job));
65         
66         job.run();
67         
68         assertEquals(JobState.COMPLETE, Helper.getJobState(job));
69     }
70     
71     public void testNullDirectory() {
72         SerializePersister persister = new SerializePersister();
73         File JavaDoc file = persister.fileFor("whatever");
74         
75         assertNotNull(file);
76     }
77 }
78
Popular Tags