KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jobs > job > DependsJobTest


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.jobs.job;
5
6 import junit.framework.TestCase;
7
8 import org.oddjob.Helper;
9 import org.oddjob.Stateful;
10 import org.oddjob.framework.SimpleJob;
11 import org.oddjob.state.JobState;
12 import org.oddjob.state.JobStateHandler;
13 import org.oddjob.state.JobStateListener;
14
15 /**
16  *
17  */

18 public class DependsJobTest extends TestCase {
19
20     class TestJob extends SimpleJob {
21         boolean ran;
22         public int execute() {
23             ran = true;
24             return 0;
25         }
26     }
27     
28     /**
29      * Test that a ready job is run.
30      *
31      */

32     public void testReady() {
33         TestJob testJob = new TestJob();
34         DependsJob j = new DependsJob();
35         j.setJob(testJob);
36         j.run();
37         
38         assertTrue(testJob.ran);
39         assertEquals("Complete", JobState.COMPLETE, Helper.getJobState(j));
40     }
41
42     /**
43      * Test that a complete job isn't run.
44      *
45      */

46     public void testAlreadyComplete() {
47         TestJob testJob = new TestJob();
48         testJob.run();
49         testJob.ran = false;
50         assertEquals(JobState.COMPLETE, testJob.lastJobStateEvent().getJobState());
51         
52         DependsJob j = new DependsJob();
53         j.setJob(testJob);
54         j.run();
55         
56         assertFalse(testJob.ran);
57         assertEquals("Complete", JobState.COMPLETE, Helper.getJobState(j));
58     }
59
60     /**
61      * Test that an executing job is waited for.
62      *
63      */

64     public void testExecuting() throws InterruptedException JavaDoc {
65         class Executing implements Stateful {
66             JobStateHandler h = new JobStateHandler(this);
67             public void addJobStateListener(JobStateListener listener) {
68                 h.addJobStateListener(listener);
69             }
70             public void removeJobStateListener(JobStateListener listener) {
71                 h.removeJobStateListener(listener);
72             }
73         }
74         Executing testJob = new Executing();
75         testJob.h.requestJobStateExecuting();
76         
77         DependsJob j = new DependsJob();
78         j.setJob(testJob);
79         Thread JavaDoc t = new Thread JavaDoc(j);
80         t.start();
81         
82         while (JobState.EXECUTING != Helper.getJobState(j)) {
83             Thread.yield();
84         }
85         
86         testJob.h.setJobStateNotComplete();
87         
88         t.join();
89         assertEquals(JobState.NOT_COMPLETE, Helper.getJobState(j));
90     }
91 }
92
Popular Tags