KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > action > StopActionTest


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

4 package org.oddjob.monitor.action;
5
6 import junit.framework.TestCase;
7
8 import org.apache.log4j.Logger;
9 import org.oddjob.Stateful;
10 import org.oddjob.Stoppable;
11 import org.oddjob.monitor.model.ExplorerContext;
12 import org.oddjob.monitor.model.ExplorerModel;
13 import org.oddjob.monitor.model.JobAction;
14 import org.oddjob.state.JobState;
15 import org.oddjob.state.JobStateEvent;
16 import org.oddjob.state.JobStateListener;
17 import org.oddjob.util.ThreadManager;
18
19 public class StopActionTest extends TestCase {
20     private static final Logger logger = Logger.getLogger(StopActionTest.class);
21
22     /**
23      * Test that performing the action works.
24      *
25      */

26     public void testPerform() throws Exception JavaDoc {
27         class MyS implements Stoppable {
28             boolean stopped = false;
29             public void stop() {
30                 stopped = true;
31             }
32         }
33         MyS sample = new MyS();
34                 
35         ExplorerModel em = new ExplorerModel();
36         em.setRoot(sample);
37         ThreadManager tm = new ThreadManager();
38         em.setThreadManager(tm);
39         ExplorerContext ec = new ExplorerContext(em);
40         
41         JobAction test = new StopAction();
42         test.optionSelect(sample, ec);
43         
44         test.action();
45         
46         while (tm.activeDescriptions().length > 0) {
47             logger.debug("Waiting for ThreadManager.");
48             Thread.yield();
49         }
50         assertTrue(sample.stopped);
51     }
52
53     /**
54      * Test action is enabled when a stateful is selected running.
55      *
56      */

57     public void testEnabled() {
58         class MySS implements Stateful, Stoppable {
59             boolean removed;
60             public void stop() {}
61             public void addJobStateListener(JobStateListener listener) {
62                 listener.jobStateChange(
63                         new JobStateEvent(this, JobState.EXECUTING));
64             }
65             public void removeJobStateListener(JobStateListener listener) {
66                 removed = true;
67             }
68         }
69         MySS sample = new MySS();
70         
71         ExplorerModel em = new ExplorerModel();
72         em.setRoot(sample);
73         ExplorerContext ec = new ExplorerContext(em);
74         
75         JobAction test = new StopAction();
76
77         test.optionSelect(sample, ec);
78         assertTrue(test.enabled());
79         
80         test.optionSelect(null, ec);
81         assertTrue(sample.removed);
82     }
83     
84     /**
85      * Test action is disable when a stateful is selected in complete state.
86      *
87      */

88     public void testDisabled() {
89         class MySS implements Stateful, Stoppable {
90             boolean removed;
91             public void stop() {}
92             public void addJobStateListener(JobStateListener listener) {
93                 listener.jobStateChange(
94                         new JobStateEvent(this, JobState.COMPLETE));
95             }
96             public void removeJobStateListener(JobStateListener listener) {
97                 removed = true;
98             }
99         }
100         MySS sample = new MySS();
101         
102         ExplorerModel em = new ExplorerModel();
103         em.setRoot(sample);
104         ExplorerContext ec = new ExplorerContext(em);
105         
106         JobAction test = new StopAction();
107
108         test.optionSelect(sample, ec);
109         assertFalse(test.enabled());
110         
111         test.optionSelect(null, ec);
112         assertTrue(sample.removed);
113
114     }
115     
116     /**
117      * Test action is disabled for an object.
118      *
119      */

120     public void testWithObject() {
121         JobAction test = new StopAction();
122         test.optionSelect(new Object JavaDoc(), null);
123         assertFalse(test.enabled());
124     }
125
126 }
127
Popular Tags