KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > util > xml > confix > test > JDOMProcessorCompositeTest


1 package org.sapia.util.xml.confix.test;
2
3 // Import of Sun's JDK classes
4
// ---------------------------
5
import java.io.ByteArrayInputStream JavaDoc;
6 import java.util.Iterator JavaDoc;
7
8 // Import of Junit classes
9
// ---------------------------
10
import junit.framework.TestCase;
11 import junit.textui.TestRunner;
12
13 // Import of Sapia's utility classes
14
// ---------------------------------
15
import org.sapia.util.xml.confix.ReflectionFactory;
16 import org.sapia.util.xml.confix.JDOMProcessor;
17 import org.jdom.Attribute;
18 import org.jdom.Element;
19
20
21 /**
22  *
23  *
24  * @author Jean-Cedric Desrochers
25  *
26  * <dl>
27  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2003 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
28  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
29  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
30  * </dl>
31  */

32 public class JDOMProcessorCompositeTest extends TestCase {
33
34   private static final String JavaDoc XML_SIMPLE_CONFIG =
35     "<Config name=\"config1\">" +
36     " <NamedValue name=\"param1\" value=\"value1\"/>" +
37     "</Config>";
38
39   private static final String JavaDoc XML_CONFIG_WITH_ELEMENT =
40     "<Config>" +
41     " <Name>config2</Name>" +
42     " <NamedValue name=\"param1\" value=\"value1\"/>" +
43     "</Config>";
44
45   private static final String JavaDoc XML_COMPOSITE_CONFIG_WITH_ELEMENT =
46     "<Config>" +
47     " <Name>config2</Name>" +
48     " <NamedValue name=\"param1\" value=\"value1\"/>" +
49     " <NamedValue>" +
50     " <Name>param2</Name>" +
51     " <Value>value2</Value>" +
52     " </NamedValue>" +
53     " <NamedValue name=\"param3\">" +
54     " <Value>value3</Value>" +
55     " </NamedValue>" +
56     " <NamedValue name=\"attrib-param4\" value=\"attrib-value4\">" +
57     " <Name>elem-param4</Name>" +
58     " <Value>elem-value4</Value>" +
59     " </NamedValue>" +
60     "</Config>";
61
62   private static final String JavaDoc XML_WRAPPED_NAMED_VALUE =
63     "<Config name=\"config3\">" +
64     " <WrappedNamedValue name=\"param1\" value=\"value1\"/>" +
65     " <WrappedNamedValue>" +
66     " <Name>param2</Name>" +
67     " <Value>value2</Value>" +
68     " </WrappedNamedValue>" +
69     "</Config>";
70
71   private static final String JavaDoc XML_WRAPPED_CONFIG =
72     "<WrappedConfig name=\"config4\">" +
73     " <NamedValue name=\"param1\" value=\"value1\"/>" +
74     " <WrappedNamedValue>" +
75     " <Name>param2</Name>" +
76     " <Value>value2</Value>" +
77     " </WrappedNamedValue>" +
78     "</WrappedConfig>";
79
80   private static final String JavaDoc XML_TEXTUAL_CONFIG =
81     "<TextualConfig name=\"config5\">" +
82     " <NamedValue name=\"param1\" value=\"value1\"/>" +
83     " <WrappedNamedValue>" +
84     " <Name>param2</Name>" +
85     " <Value>value2</Value>" +
86     " </WrappedNamedValue>" +
87     " THIS IS A FINAL CONFIG CONTENT" +
88     "</TextualConfig>";
89
90   private static final String JavaDoc XML_TEXTUAL_WRAPPED_CONFIG =
91     "<WrappedTextualConfig name=\"config6\">" +
92     " <NamedValue name=\"param1\" value=\"value1\"/>" +
93     " <WrappedTextualNamedValue>" +
94     " TEXT:TextualNamedValue" +
95     " <Name>param2</Name>" +
96     " <Value>value2</Value>" +
97     " </WrappedTextualNamedValue>" +
98     " THIS IS A FINAL CONFIG CONTENT" +
99     "</WrappedTextualConfig>";
100
101
102   private static final String JavaDoc XML_JDOM_HANDLER =
103     "<WrappedTextualConfig name=\"config7\">" +
104     " <NamedValue name=\"param1\" value=\"value1\"/>" +
105     " <CustomConfig type=\"ant\">" +
106     " <build default=\"build\"><target name=\"build\"><javac/></target></build>" +
107     " </CustomConfig>" +
108     " THIS IS A FINAL CONFIG CONTENT" +
109     "</WrappedTextualConfig>";
110     
111     private static final String JavaDoc XML_CONSUMER =
112         "<WrappedTextualConfig name=\"config7\">" +
113         " <NamedValue name=\"param1\" value=\"value1\"/>" +
114         " <XMLConsumerConfig type=\"ant\">" +
115         " <build default=\"build\"><target name=\"build\"><javac/></target></build>" +
116         " </XMLConsumerConfig>" +
117         " THIS IS A FINAL CONFIG CONTENT" +
118         "</WrappedTextualConfig>";
119     
120   private static final String JavaDoc XML_CREATOR =
121     "<Config name=\"config8\">" +
122     " <Param name=\"param5\" value=\"value5\"/>" +
123     "</Config>";
124
125   private static final String JavaDoc XML_OBJECT_HANDLER =
126     "<config name=\"config9\">" +
127     " <stringBuffer length=\"5\"/>" +
128     "</config>";
129
130
131   private static final String JavaDoc XML_CDATA_CONFIG =
132     "<TextualConfig name=\"config10\">" +
133     " <![CDATA[" +
134     " THIS IS A CDATA CONFIG CONTENT" +
135     " ]]>" +
136     "</TextualConfig>";
137
138   private JDOMProcessor _theProcessor;
139
140   public static void main(String JavaDoc[] args) {
141     TestRunner.run(JDOMProcessorCompositeTest.class);
142   }
143
144   public JDOMProcessorCompositeTest(String JavaDoc aName) {
145     super(aName);
146   }
147
148   public void setUp() {
149     ReflectionFactory anObjectFactory = new ReflectionFactory(
150             new String JavaDoc[] { "org.sapia.util.xml.confix.test", "java.lang" } );
151     _theProcessor = new JDOMProcessor(anObjectFactory);
152   }
153
154   public void testSimpleConfig() throws Exception JavaDoc {
155     ByteArrayInputStream JavaDoc anInput =
156             new ByteArrayInputStream JavaDoc(XML_SIMPLE_CONFIG.getBytes());
157     Object JavaDoc anObject = _theProcessor.process(anInput);
158     assertTrue("The returned object is not a Config", anObject instanceof Config);
159
160     Config aConfig = (Config) anObject;
161     assertEquals("The name of the config is invalid", "config1", aConfig.getName());
162     assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size());
163     Iterator JavaDoc someNamedValues = aConfig.getNamedValues().iterator();
164
165     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
166     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
167     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
168   }
169
170   public void testSimpleConfigWithElement() throws Exception JavaDoc {
171     ByteArrayInputStream JavaDoc anInput =
172             new ByteArrayInputStream JavaDoc(XML_CONFIG_WITH_ELEMENT.getBytes());
173     Object JavaDoc anObject = _theProcessor.process(anInput);
174     assertTrue("The returned object is not a Config", anObject instanceof Config);
175
176     Config aConfig = (Config) anObject;
177     assertEquals("The name of the config is invalid", "config2", aConfig.getName());
178     assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size());
179     Iterator JavaDoc someNamedValues = aConfig.getNamedValues().iterator();
180
181     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
182     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
183     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
184   }
185
186   public void testCompositeConfigWithElement() throws Exception JavaDoc {
187     ByteArrayInputStream JavaDoc anInput =
188             new ByteArrayInputStream JavaDoc(XML_COMPOSITE_CONFIG_WITH_ELEMENT.getBytes());
189     Object JavaDoc anObject = _theProcessor.process(anInput);
190     assertTrue("The returned object is not a Config", anObject instanceof Config);
191
192     Config aConfig = (Config) anObject;
193     assertEquals("The name of the config is invalid", "config2", aConfig.getName());
194     assertEquals("The size of the named value list is invalid", 4, aConfig.getNamedValues().size());
195     Iterator JavaDoc someNamedValues = aConfig.getNamedValues().iterator();
196
197     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
198     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
199     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
200
201     aNamedValue = (NamedValue) someNamedValues.next();
202     assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName());
203     assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue());
204
205     aNamedValue = (NamedValue) someNamedValues.next();
206     assertEquals("The name of the named value is invalid", "param3", aNamedValue.getName());
207     assertEquals("The value of the named value is invalid", "value3", aNamedValue.getValue());
208
209     aNamedValue = (NamedValue) someNamedValues.next();
210     assertEquals("The name of the named value is invalid", "elem-param4", aNamedValue.getName());
211     assertEquals("The value of the named value is invalid", "elem-value4", aNamedValue.getValue());
212   }
213
214   public void testWrappedNamedValue() throws Exception JavaDoc {
215     ByteArrayInputStream JavaDoc anInput =
216             new ByteArrayInputStream JavaDoc(XML_WRAPPED_NAMED_VALUE.getBytes());
217     Object JavaDoc anObject = _theProcessor.process(anInput);
218     assertTrue("The returned object is not a Config", anObject instanceof Config);
219
220     Config aConfig = (Config) anObject;
221     assertEquals("The name of the config is invalid", "config3", aConfig.getName());
222     assertEquals("The size of the named value list is invalid", 2, aConfig.getNamedValues().size());
223     Iterator JavaDoc someNamedValues = aConfig.getNamedValues().iterator();
224
225     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
226     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
227     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
228
229     aNamedValue = (NamedValue) someNamedValues.next();
230     assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName());
231     assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue());
232   }
233
234   public void testTextualConfig() throws Exception JavaDoc {
235     ByteArrayInputStream JavaDoc anInput =
236             new ByteArrayInputStream JavaDoc(XML_TEXTUAL_CONFIG.getBytes());
237     Object JavaDoc anObject = _theProcessor.process(anInput);
238     assertTrue("The returned object is not a TextualConfig", anObject instanceof TextualConfig);
239
240     TextualConfig aTextualConfig = (TextualConfig) anObject;
241     assertEquals("The name of the config is invalid", "config5", aTextualConfig.getName());
242     assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aTextualConfig.getText());
243     assertEquals("The size of the named value list is invalid", 2, aTextualConfig.getNamedValues().size());
244     Iterator JavaDoc someNamedValues = aTextualConfig.getNamedValues().iterator();
245
246     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
247     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
248     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
249
250     aNamedValue = (NamedValue) someNamedValues.next();
251     assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName());
252     assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue());
253   }
254
255
256   public void testCdataConfig() throws Exception JavaDoc {
257     ByteArrayInputStream JavaDoc anInput =
258             new ByteArrayInputStream JavaDoc(XML_CDATA_CONFIG.getBytes());
259     Object JavaDoc anObject = _theProcessor.process(anInput);
260     assertTrue("The returned object is not a TextualConfig", anObject instanceof TextualConfig);
261
262     TextualConfig aTextualConfig = (TextualConfig) anObject;
263     assertEquals("The name of the config is invalid", "config10", aTextualConfig.getName());
264     assertEquals("The content of the config is invalid", "THIS IS A CDATA CONFIG CONTENT", aTextualConfig.getText());
265     assertEquals("The size of the named value list is invalid", 0, aTextualConfig.getNamedValues().size());
266   }
267
268   public void testWrappedTextualConfig() throws Exception JavaDoc {
269     ByteArrayInputStream JavaDoc anInput =
270             new ByteArrayInputStream JavaDoc(XML_TEXTUAL_WRAPPED_CONFIG.getBytes());
271     Object JavaDoc anObject = _theProcessor.process(anInput);
272     assertTrue("The returned object is not a TextualConfig", anObject instanceof WrappedTextualConfig);
273
274     WrappedTextualConfig aWrapper = (WrappedTextualConfig) anObject;
275     assertEquals("The name of the config is invalid", "config6", aWrapper.getTextualConfig().getName());
276     assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aWrapper.getTextualConfig().getText());
277     assertEquals("The size of the named value list is invalid", 2, aWrapper.getTextualConfig().getNamedValues().size());
278     Iterator JavaDoc someNamedValues = aWrapper.getTextualConfig().getNamedValues().iterator();
279
280     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
281     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
282     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
283
284     TextualNamedValue aTextualNamedValue = (TextualNamedValue) someNamedValues.next();
285     assertEquals("The name of the named value is invalid", "param2", aTextualNamedValue.getName());
286     assertEquals("The value of the named value is invalid", "value2", aTextualNamedValue.getValue());
287     assertEquals("The text of the named value is invalid", "TEXT:TextualNamedValue", aTextualNamedValue.getText());
288   }
289
290   public void testJdomHandler() throws Exception JavaDoc {
291     ByteArrayInputStream JavaDoc anInput =
292             new ByteArrayInputStream JavaDoc(XML_JDOM_HANDLER.getBytes());
293     Object JavaDoc anObject = _theProcessor.process(anInput);
294     assertTrue("The returned object is not a TextualConfig", anObject instanceof WrappedTextualConfig);
295
296     WrappedTextualConfig aWrapper = (WrappedTextualConfig) anObject;
297     assertEquals("The name of the config is invalid", "config7", aWrapper.getTextualConfig().getName());
298     assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aWrapper.getTextualConfig().getText());
299     assertEquals("The size of the named value list is invalid", 1, aWrapper.getTextualConfig().getNamedValues().size());
300     Iterator JavaDoc someNamedValues = aWrapper.getTextualConfig().getNamedValues().iterator();
301
302     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
303     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
304     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
305
306     assertEquals("The size of the custom config list is invalid", 1, aWrapper.getTextualConfig().getCustomConfigs().size());
307     Iterator JavaDoc someCustomCongigs = aWrapper.getTextualConfig().getCustomConfigs().iterator();
308
309     CustomConfig aCustomConfig = (CustomConfig) someCustomCongigs.next();
310     assertEquals("The type of the custom config is invalid", "ant", aCustomConfig.getType());
311
312     Element anElement = aCustomConfig.getConfigElement();
313     assertEquals("The name of the element is invalid", "build", anElement.getName());
314     assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size());
315
316     Attribute anAttribute = (Attribute) anElement.getAttributes().iterator().next();
317     assertEquals("The name of the attribute is invalid", "default", anAttribute.getName());
318     assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue());
319     assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size());
320
321     anElement = (Element) anElement.getChildren().iterator().next();
322     assertEquals("The name of the element is invalid", "target", anElement.getName());
323     assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size());
324
325     anAttribute = (Attribute) anElement.getAttributes().iterator().next();
326     assertEquals("The name of the attribute is invalid", "name", anAttribute.getName());
327     assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue());
328     assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size());
329
330     anElement = (Element) anElement.getChildren().iterator().next();
331     assertEquals("The name of the element is invalid", "javac", anElement.getName());
332     assertEquals("The size of the attribute list is invalid", 0, anElement.getAttributes().size());
333   }
334   
335     public void testXMLConsumer() throws Exception JavaDoc {
336         ByteArrayInputStream JavaDoc anInput =
337                         new ByteArrayInputStream JavaDoc(XML_CONSUMER.getBytes());
338         Object JavaDoc anObject = _theProcessor.process(anInput);
339         assertTrue("The returned object is not a TextualConfig", anObject instanceof WrappedTextualConfig);
340
341         WrappedTextualConfig aWrapper = (WrappedTextualConfig) anObject;
342         assertEquals("The name of the config is invalid", "config7", aWrapper.getTextualConfig().getName());
343         assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aWrapper.getTextualConfig().getText());
344         assertEquals("The size of the named value list is invalid", 1, aWrapper.getTextualConfig().getNamedValues().size());
345         Iterator JavaDoc someNamedValues = aWrapper.getTextualConfig().getNamedValues().iterator();
346
347         NamedValue aNamedValue = (NamedValue) someNamedValues.next();
348         assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
349         assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
350
351         assertEquals("The size of the custom config list is invalid", 1, aWrapper.getTextualConfig().getCustomConfigs().size());
352         Iterator JavaDoc someCustomCongigs = aWrapper.getTextualConfig().getCustomConfigs().iterator();
353
354         XMLConsumerConfig aCustomConfig = (XMLConsumerConfig) someCustomCongigs.next();
355         assertEquals("The type of the custom config is invalid", "ant", aCustomConfig.getType());
356
357         Element anElement = aCustomConfig.getConfigElement();
358         assertEquals("The name of the element is invalid", "build", anElement.getName());
359         assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size());
360
361         Attribute anAttribute = (Attribute) anElement.getAttributes().iterator().next();
362         assertEquals("The name of the attribute is invalid", "default", anAttribute.getName());
363         assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue());
364         assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size());
365
366         anElement = (Element) anElement.getChildren().iterator().next();
367         assertEquals("The name of the element is invalid", "target", anElement.getName());
368         assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size());
369
370         anAttribute = (Attribute) anElement.getAttributes().iterator().next();
371         assertEquals("The name of the attribute is invalid", "name", anAttribute.getName());
372         assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue());
373         assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size());
374
375         anElement = (Element) anElement.getChildren().iterator().next();
376         assertEquals("The name of the element is invalid", "javac", anElement.getName());
377         assertEquals("The size of the attribute list is invalid", 0, anElement.getAttributes().size());
378     }
379
380   public void testCreatorConfig() throws Exception JavaDoc {
381     ByteArrayInputStream JavaDoc anInput =
382             new ByteArrayInputStream JavaDoc(XML_CREATOR.getBytes());
383     Object JavaDoc anObject = _theProcessor.process(anInput);
384     assertTrue("The returned object is not a Config", anObject instanceof Config);
385
386     Config aConfig = (Config) anObject;
387     assertEquals("The name of the config is invalid", "config8", aConfig.getName());
388     assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size());
389     Iterator JavaDoc someNamedValues = aConfig.getNamedValues().iterator();
390
391     NamedValue aNamedValue = (NamedValue) someNamedValues.next();
392     assertEquals("The name of the named value is invalid", "param5", aNamedValue.getName());
393     assertEquals("The value of the named value is invalid", "value5", aNamedValue.getValue());
394   }
395
396   public void testObjectHandler() throws Exception JavaDoc {
397     ByteArrayInputStream JavaDoc anInput =
398             new ByteArrayInputStream JavaDoc(XML_OBJECT_HANDLER.getBytes());
399     Object JavaDoc anObject = _theProcessor.process(anInput);
400     assertTrue("The returned object is not a Config", anObject instanceof Config);
401
402     Config aConfig = (Config) anObject;
403     assertEquals("The name of the config is invalid", "config9", aConfig.getName());
404     assertEquals("The size of the named value list is invalid", 0, aConfig.getNamedValues().size());
405
406     assertEquals("The length of the custom object array is invalid", 2, aConfig.getCustomObject().length);
407     assertEquals("The first custom object is invalid", "stringBuffer", aConfig.getCustomObject()[0]);
408     assertEquals("The second custom object is invalid", 5, ((StringBuffer JavaDoc) aConfig.getCustomObject()[1]).length());
409   }
410 }
411
Popular Tags