KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > webapp > model > JobInfoLookupTest


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

4 package org.oddjob.webapp.model;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.util.List JavaDoc;
9
10 import junit.framework.TestCase;
11
12 import org.apache.log4j.Level;
13 import org.apache.log4j.Logger;
14 import org.oddjob.Iconic;
15 import org.oddjob.Oddjob;
16 import org.oddjob.images.IconEvent;
17 import org.oddjob.images.IconHelper;
18 import org.oddjob.images.IconListener;
19 import org.oddjob.images.IconTip;
20 import org.oddjob.jobs.structural.FolderJob;
21 import org.oddjob.logging.LogHelper;
22 import org.oddjob.logging.LogEvent;
23 import org.oddjob.state.JobState;
24 import org.oddjob.util.ThreadManager;
25
26 public class JobInfoLookupTest extends TestCase {
27     private static final Logger logger = Logger.getLogger(JobInfoLookupTest.class);
28     
29     public IconRegistry ir = new IconRegistry();
30     
31     public void test1() {
32         Object JavaDoc root = new Object JavaDoc();
33         JobInfoLookup lookup = new JobInfoLookup(ir);
34         lookup.setRoot(root);
35         assertEquals("0", lookup.getRootRefId());
36     }
37     
38     public void testIcon() {
39         Iconic root = new Iconic() {
40             public void addIconListener(IconListener listener) {
41                 IconEvent e = new IconEvent(this, "foo");
42                 listener.iconEvent(e);
43             }
44             public IconTip iconForId(String JavaDoc id) {
45                 return IconHelper.completeIcon;
46             }
47             public void removeIconListener(IconListener listener) {
48             }
49             
50         };
51         
52         JobInfoLookup lookup = new JobInfoLookup(ir);
53         lookup.setRoot(root);
54         NodeInfo ni = lookup.nodeInfoFor(lookup.getRootRefId());
55         assertEquals("foo", ni.getIconId());
56         assertNotNull(ir.retrieve("foo"));
57     }
58
59     public void testNoChildren() {
60         String JavaDoc xml = "<oddjob/>";
61         
62         Oddjob oj = new Oddjob();
63         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
64         oj.run();
65         
66         JobInfoLookup lookup = new JobInfoLookup(ir);
67         lookup.setRoot(oj);
68         NodeInfo ni = lookup.nodeInfoFor(lookup.getRootRefId());
69         
70         assertFalse(ni.getHasChildren());
71         assertEquals(0, ni.getChildRefIds().length);
72     }
73
74     /**
75      * Test that children are added and removed correctly in the
76      * lookup.
77      *
78      */

79     public void testChildren() {
80         String JavaDoc xml = "<oddjob><echo text='Hello World'/></oddjob>";
81         
82         Oddjob oj = new Oddjob();
83         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
84         oj.run();
85         
86         JobInfoLookup lookup = new JobInfoLookup(ir);
87         lookup.setRoot(oj);
88         NodeInfo ni = lookup.nodeInfoFor(lookup.getRootRefId());
89         
90         assertTrue(ni.getHasChildren());
91         assertEquals(1, ni.getChildRefIds().length);
92
93         String JavaDoc childRef = ni.getChildRefIds()[0];
94         
95         oj.hardReset();
96         
97         ni = lookup.nodeInfoFor(lookup.getRootRefId());
98         assertEquals(0, ni.getChildRefIds().length);
99
100         try {
101             lookup.nodeInfoFor(childRef);
102             fail("childRef should not exist.");
103         } catch (IllegalStateException JavaDoc e) {
104             // past
105
}
106     }
107     
108     /**
109      * Test several children - trying to track down a bug where
110      * the first child is repeated.
111      *
112      */

113     public void testManyChildren() {
114         FolderJob f1 = new FolderJob();
115         f1.setName("F1");
116         FolderJob f2 = new FolderJob();
117         f2.setName("F2");
118         FolderJob f3 = new FolderJob();
119         f3.setName("F3");
120         
121         f1.addComponent(f2);
122         f2.addComponent(f3);
123         
124         JobInfoLookup lookup = new JobInfoLookup(ir);
125         lookup.setRoot(f1);
126         
127         String JavaDoc r1 = lookup.getRootRefId();
128         String JavaDoc r2 = lookup.nodeInfoFor(r1).getChildRefIds()[0];
129         String JavaDoc r3 = lookup.nodeInfoFor(r2).getChildRefIds()[0];
130         
131         NodeInfo ni = lookup.nodeInfoFor(r3);
132         assertEquals("F3", ni.getNodeName());
133     }
134     
135     public static class Loggable {
136         private Logger logger = Logger.getLogger("foo");
137         public String JavaDoc getLogger() {
138             return "foo";
139         }
140         void logSomething() {
141             logger.setLevel(Level.DEBUG);
142             logger.debug("Test");
143         }
144     }
145     
146     public void testLogging() {
147         Loggable l = new Loggable();
148         assertEquals("foo", LogHelper.getLogger(l));
149         
150         JobInfoLookup lookup = new JobInfoLookup(ir);
151         lookup.setLogFormat("%m");
152         lookup.setRoot(l);
153         l.logSomething();
154         
155         List JavaDoc events = lookup.logEventsFor(lookup.getRootRefId());
156         assertEquals(1, events.size());
157         LogEvent event = (LogEvent) events.get(0);
158         assertEquals("Test", event.getMessage());
159     }
160     
161     public void testCommands() throws Exception JavaDoc {
162         ThreadManager tm = new ThreadManager();
163         JobInfoLookup test = new JobInfoLookup(ir);
164         test.setThreadManager(tm);
165         
166         Oddjob oj = new Oddjob();
167         oj.setFile(new File JavaDoc("test/conf/oddjob-test.xml"));
168
169         test.setRoot(oj);
170         WebJobActions actions = test.actionsFor(test.getRootRefId());
171
172         assertTrue(actions.commands().contains("Run"));
173         assertTrue(actions.isEnabled("Run"));
174         
175         actions.action("Run");
176         while (tm.activeDescriptions().length > 0) {
177             logger.debug("Waiting for ThreadManager.");
178             Thread.yield();
179         }
180         
181         assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState());
182         
183         assertTrue(actions.commands().contains("Hard Reset"));
184         assertTrue(actions.isEnabled("Hard Reset"));
185         
186         actions.action("Hard Reset");
187         while (tm.activeDescriptions().length > 0) {
188             logger.debug("Waiting for ThreadManager.");
189             Thread.yield();
190         }
191         
192         assertEquals(JobState.READY, oj.lastJobStateEvent().getJobState());
193
194         actions.action("Run");
195         while (tm.activeDescriptions().length > 0) {
196             logger.debug("Waiting for ThreadManager.");
197             Thread.yield();
198         }
199         
200         assertEquals(JobState.COMPLETE, oj.lastJobStateEvent().getJobState());
201     }
202 }
203
Popular Tags