KickJava   Java API By Example, From Geeks To Geeks.

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


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 Apache's log4j classes
9
// --------------------------------
10
import org.apache.log4j.BasicConfigurator;
11 import org.apache.log4j.Level;
12 import org.apache.log4j.LogManager;
13
14 // Import of Junit classes
15
// ---------------------------
16
import junit.framework.TestCase;
17 import junit.textui.TestRunner;
18
19 // Import of Sapia's utility classes
20
// ---------------------------------
21
import org.sapia.util.xml.confix.ReflectionFactory;
22 import org.sapia.util.xml.confix.SAXProcessor;
23
24
25 /**
26  *
27  *
28  * @author Jean-Cedric Desrochers
29  *
30  * <dl>
31  * <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>
32  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
33  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
34  * </dl>
35  */

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