KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > OddjobTest


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

4 package org.oddjob;
5
6 import java.io.ByteArrayInputStream JavaDoc;
7 import java.io.File JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.apache.commons.beanutils.DynaBean;
12 import org.oddjob.arooa.ArooaConstants;
13 import org.oddjob.arooa.ArooaContext;
14 import org.oddjob.arooa.reflect.DefaultRegistryLookup;
15 import org.oddjob.arooa.reflect.IntrospectionHelper;
16 import org.oddjob.arooa.reflect.RegistryLookup;
17 import org.oddjob.arooa.registry.ComponentRegistry;
18 import org.oddjob.io.DeleteJob;
19 import org.oddjob.persist.SerializePersister;
20 import org.oddjob.state.JobState;
21 import org.oddjob.structural.StructuralEvent;
22 import org.oddjob.structural.StructuralListener;
23 import org.oddjob.values.VariablesJob;
24 import org.oddjob.values.types.PropertyType;
25 import org.xml.sax.helpers.LocatorImpl JavaDoc;
26
27 /**
28  *
29  * @author Rob Gordon.
30  */

31 public class OddjobTest extends TestCase {
32
33     /**
34      * Test resetting Oddjob
35      *
36      */

37     public void testReset() {
38         class MyL implements StructuralListener {
39             int count;
40             public void childAdded(StructuralEvent event) {
41                 count++;
42             }
43             public void childRemoved(StructuralEvent event) {
44                 count--;
45             }
46         }
47         Oddjob oj = new Oddjob();
48         oj.setFile(new File JavaDoc("test/conf/oddjob-test.xml"));
49         
50         MyL l = new MyL();
51         oj.addStructuralListener(l);
52         
53         assertEquals(0, l.count);
54         
55         oj.run();
56         
57         assertEquals(1, l.count);
58         
59         oj.hardReset();
60         
61         assertEquals(0, l.count);
62         
63         oj.run();
64         
65         assertEquals(1, l.count);
66     }
67     
68     /**
69      * Test loading an Oddjob without a child.
70      *
71      */

72     public void testLoadNoChild() {
73         String JavaDoc config = "<oddjob id='this'/>";
74         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
75         
76         Oddjob oj = new Oddjob();
77         oj.setInput(is);
78         oj.load();
79         Object JavaDoc test = oj.lookup("this");
80         assertTrue("Oddjob configured", oj == test);
81     }
82
83     public void testLoadNestedOddjob() {
84         String JavaDoc config = "<oddjob><oddjob id='nested'/></oddjob>";
85         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
86         
87         Oddjob oj = new Oddjob();
88         oj.setInput(is);
89         oj.load();
90         Object JavaDoc test = oj.lookup("nested");
91         assertNotNull("Nested oddjob", test);
92     }
93
94     public void testLoadOddjobClassloader() {
95         String JavaDoc config = "<oddjob><oddjob id='nested'>"
96             + "<classpath><files dir='build' pattern='*.jar'/></classpath>"
97             + "</oddjob></oddjob>";
98         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
99         
100         Oddjob oj = new Oddjob();
101         oj.setInput(is);
102         oj.setLoadOnly(true);
103         oj.run();
104         
105         String JavaDoc nestedConf = "<oddjob/>";
106         ByteArrayInputStream JavaDoc nestedIs = new ByteArrayInputStream JavaDoc(nestedConf.getBytes());
107         Oddjob test = (Oddjob) oj.lookup("nested");
108         test.setInput(nestedIs);
109         test.run();
110         
111         assertNotNull("Nested classloader",
112                 test.getClassLoader());
113     }
114     
115     public void testLookup() {
116         ComponentRegistry cr = new ComponentRegistry();
117         RegistryLookup lookup = new DefaultRegistryLookup(cr);
118         String JavaDoc config = "<oddjob id='this'><variables id='fruits'>" +
119                 "<value name='fruit' value='apple'/></variables></oddjob>";
120         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
121         ArooaContext ac = new ArooaContext();
122         ac.set(ArooaConstants.COMPONENT_REGISTRY, cr);
123         
124         Oddjob oj = new Oddjob();
125         cr.register("nested", oj);
126         oj.setContext(ac);
127         oj.setInput(is);
128         oj.run();
129         String JavaDoc fruit = (String JavaDoc) IntrospectionHelper.valueFor(
130                 lookup.getProperty("nested/fruits.fruit"));
131         assertEquals("apple", fruit);
132     }
133
134     public void testArgs() {
135         String JavaDoc config = "<oddjob id='this'><variables id='fruits'>" +
136                 "<value name='fruit' value='${this.args[0]}'/></variables></oddjob>";
137         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
138         Oddjob oj = new Oddjob();
139         oj.setInput(is);
140         oj.setArgs(new String JavaDoc[] { "apple" });
141         oj.run();
142         String JavaDoc fruit = (String JavaDoc) IntrospectionHelper.valueFor(
143                 ((DynaBean) oj.lookup("fruits")).get("fruit"));
144         assertEquals("apple", fruit);
145     }
146     
147     public void testSetArgs() {
148         ComponentRegistry cr = new ComponentRegistry();
149         RegistryLookup lookup = new DefaultRegistryLookup(cr);
150
151         String JavaDoc config = "<oddjob id='this'/>";
152         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
153         Oddjob oj = new Oddjob();
154         cr.register("nested", oj);
155         oj.setInput(is);
156         oj.run();
157         lookup.setProperty("nested.args[0]", "apple");
158         assertEquals("apple", oj.getArgs()[0]);
159     }
160     
161     public void testSetLookup() {
162         ComponentRegistry cr = new ComponentRegistry();
163         RegistryLookup lookup = new DefaultRegistryLookup(cr);
164         ArooaContext ac = new ArooaContext();
165         ac.set(ArooaConstants.COMPONENT_REGISTRY, cr);
166
167         String JavaDoc config = "<oddjob id='this'>" +
168                     "<variables id='vars'/>" +
169                 "</oddjob>";
170         ByteArrayInputStream JavaDoc is = new ByteArrayInputStream JavaDoc(config.getBytes());
171         Oddjob oj = new Oddjob();
172         cr.register("nested", oj);
173         oj.setInput(is);
174         oj.setContext(ac);
175         oj.run();
176         lookup.setProperty("nested/vars.fruit", "apple");
177         Object JavaDoc result = ((VariablesJob) oj.lookup("vars")).get("fruit");
178         assertEquals(PropertyType.class, result.getClass());
179         assertEquals("apple", IntrospectionHelper.valueFor(result, Object JavaDoc.class));
180     }
181     
182     /**
183      * Test Oddjob dir property.
184      *
185      */

186     public void testDir() {
187         Oddjob oj = new Oddjob();
188         oj.setFile(new File JavaDoc("xyz.xml"));
189         assertEquals(new File JavaDoc("whatever").getAbsoluteFile().getParentFile(), oj.getDir());
190     }
191     
192     /**
193      * Test Component Registry management. Test Oddjob
194      * adds and clears up it's children OK.
195      *
196      */

197     public void testRegistryManagement() {
198         ComponentRegistry cr = new ComponentRegistry();
199         ArooaContext ac = new ArooaContext();
200         ac.set(ArooaConstants.COMPONENT_REGISTRY, cr);
201         // set location so Oddjob thinks it really is a nested oddjob.
202
ac.setLocator(new LocatorImpl JavaDoc());
203         
204         
205         Oddjob oj = new Oddjob();
206         oj.setContext(ac);
207         
208         String JavaDoc xml = "<oddjob><echo id='echo' text='Hello'/></oddjob>";
209         
210         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
211         
212         assertEquals(0, cr.childCount());
213
214         oj.run();
215         
216         assertEquals(1, cr.childCount());
217         assertNotNull(oj.lookup("echo"));
218         
219         oj.hardReset();
220         
221         assertEquals(0, cr.childCount());
222         
223         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
224         oj.run();
225         
226         assertEquals(1, cr.childCount());
227         assertNotNull(oj.lookup("echo"));
228     }
229     
230     public void testPersist() {
231         DeleteJob dj = new DeleteJob();
232         dj.setFiles(new File JavaDoc[] { new File JavaDoc(new File JavaDoc("test/persist"), "e.ser") } );
233         dj.run();
234         
235         String JavaDoc xml = "<oddjob><echo id='e' text='Hello'/></oddjob>";
236             
237         Oddjob oj = new Oddjob();
238         
239         SerializePersister persister = new SerializePersister();
240         persister.setDir(new File JavaDoc("test/persist"));
241         
242         oj.setPersister(persister);
243         oj.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
244         
245         oj.run();
246         persister.close();
247         
248         Oddjob next = new Oddjob();
249         next.setPersister(persister);
250         next.setInput(new ByteArrayInputStream JavaDoc(xml.getBytes()));
251         next.setLoadOnly(true);
252         
253         next.run();
254         
255         assertEquals(JobState.COMPLETE, Helper.getJobState(
256                 next.lookup("e")));
257         
258     }
259 }
260
261
Popular Tags