KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL 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.ProcessingException;
22 import org.sapia.util.xml.confix.ReflectionFactory;
23 import org.sapia.util.xml.confix.SAXProcessor;
24
25
26 /**
27  *
28  *
29  * @author Jean-Cedric Desrochers
30  *
31  * <dl>
32  * <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>
33  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
34  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
35  * </dl>
36  */

37 public class SAXProcessorSingleTest extends TestCase {
38
39   private static final String JavaDoc XML_NOT_WELL_FORMED =
40     "<ThisIsBrokenXML>";
41
42   private static final String JavaDoc XML_INVALID_CLASS_NAME =
43     "<InvalidClassName />";
44
45   private static final String JavaDoc XML_EMPTY_NAMED_VALUE =
46     "<namedValue />";
47     
48     private static final String JavaDoc XML_CALLBACK =
49         "<Url link=\"http://www.yahoo.com\"/>";
50         
51     private static final String JavaDoc XML_NULL_OBJECT =
52         "<Null />";
53
54   private static final String JavaDoc XML_GENERIC_VALUE =
55     "<GenericObject value=\"a generic value\" />";
56
57   private static final String JavaDoc XML_SIMPLE_NAMED_VALUE =
58     "<NamedValue name=\"param1\" value=\"value1\"/>";
59
60   private static final String JavaDoc XML_WRAPPED_NAMED_VALUE =
61     "<wrappedNamedValue name=\"param1\" value=\"value1\"/>";
62
63   private static final String JavaDoc XML_NAMED_VALUE_WITH_TEXT =
64     "<NamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</NamedValue>";
65
66   private static final String JavaDoc XML_TEXTUAL_NAMED_VALUE =
67     "<textualNamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</textualNamedValue>";
68
69   private static final String JavaDoc XML_WRAPPED_TEXTUAL_NAMED_VALUE =
70     "<WrappedTextualNamedValue name=\"param1\" value=\"value1\"> THIS IS SOME TEXT </WrappedTextualNamedValue>";
71
72   static {
73     BasicConfigurator.configure();
74   }
75
76   private SAXProcessor _theProcessor;
77
78   public static void main(String JavaDoc[] args) {
79     TestRunner.run(SAXProcessorSingleTest.class);
80   }
81
82   private static void disableLog() {
83     LogManager.getRootLogger().setLevel(Level.OFF);
84   }
85
86   private static void enableLog() {
87     LogManager.getRootLogger().setLevel(Level.DEBUG);
88   }
89
90   public SAXProcessorSingleTest(String JavaDoc aName) {
91     super(aName);
92   }
93
94   public void setUp() {
95     ReflectionFactory anObjectFactory = new ReflectionFactory(
96             new String JavaDoc[] { "org.sapia.util.xml.confix.test" } );
97     _theProcessor = new SAXProcessor(anObjectFactory);
98   }
99
100   public void testBrokenXml() throws Exception JavaDoc {
101     disableLog();
102     try {
103       ByteArrayInputStream JavaDoc anInput =
104               new ByteArrayInputStream JavaDoc(XML_NOT_WELL_FORMED.getBytes());
105       Object JavaDoc anObject = _theProcessor.process(anInput);
106       fail("The broken XML should not have been parsed correctly");
107     } catch (ProcessingException pe) {
108     } finally {
109       enableLog();
110     }
111   }
112
113   public void testInvalidClassName() throws Exception JavaDoc {
114     disableLog();
115     try {
116       ByteArrayInputStream JavaDoc anInput =
117               new ByteArrayInputStream JavaDoc(XML_INVALID_CLASS_NAME.getBytes());
118       Object JavaDoc anObject = _theProcessor.process(anInput);
119       if (anObject != null) {
120         fail("The process should have fail because the class InvalidClassNamed doesn't exist");
121       }
122     } catch (ProcessingException pe) {
123     } finally {
124       enableLog();
125     }
126   }
127
128   public void testEmptyNamedValue() throws Exception JavaDoc {
129     ByteArrayInputStream JavaDoc anInput =
130             new ByteArrayInputStream JavaDoc(XML_EMPTY_NAMED_VALUE.getBytes());
131     Object JavaDoc anObject = _theProcessor.process(anInput);
132     assertTrue("The returned object is not a NamedValue", anObject instanceof NamedValue);
133
134     NamedValue aNamedValue = (NamedValue) anObject;
135     assertNull("The name of the named value is invalid", aNamedValue.getName());
136     assertNull("The value of the named value is invalid", aNamedValue.getValue());
137   }
138
139   public void testGenericValue() throws Exception JavaDoc {
140     ByteArrayInputStream JavaDoc anInput =
141             new ByteArrayInputStream JavaDoc(XML_GENERIC_VALUE.getBytes());
142     Object JavaDoc anObject = _theProcessor.process(anInput);
143     assertTrue("The returned object is not a GenericObject", anObject instanceof GenericObject);
144
145     GenericObject aGenericObject = (GenericObject) anObject;
146     assertEquals("The value of the generic object is invalid", "a generic value", aGenericObject.getValue());
147   }
148
149   public void testSimpleNamedValue() throws Exception JavaDoc {
150     ByteArrayInputStream JavaDoc anInput =
151             new ByteArrayInputStream JavaDoc(XML_SIMPLE_NAMED_VALUE.getBytes());
152     Object JavaDoc anObject = _theProcessor.process(anInput);
153     assertTrue("The returned object is not a NamedValue", anObject instanceof NamedValue);
154
155     NamedValue aNamedValue = (NamedValue) anObject;
156     assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName());
157     assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue());
158   }
159
160   public void testWrappedNamedValue() throws Exception JavaDoc {
161     ByteArrayInputStream JavaDoc anInput =
162             new ByteArrayInputStream JavaDoc(XML_WRAPPED_NAMED_VALUE.getBytes());
163     Object JavaDoc anObject = _theProcessor.process(anInput);
164     assertTrue("The returned object is not a WrappedNamedValue", anObject instanceof WrappedNamedValue);
165
166     WrappedNamedValue aWrapper = (WrappedNamedValue) anObject;
167     assertEquals("The name of the wrapped named value is invalid", "param1", aWrapper.getNamedValue().getName());
168     assertEquals("The value of the wrapped named value is invalid", "value1", aWrapper.getNamedValue().getValue());
169   }
170
171   public void testNamedValueWithText() throws Exception JavaDoc {
172     disableLog();
173     try {
174       ByteArrayInputStream JavaDoc anInput =
175               new ByteArrayInputStream JavaDoc(XML_NAMED_VALUE_WITH_TEXT.getBytes());
176       Object JavaDoc anObject = _theProcessor.process(anInput);
177       fail("The NamedValue doesn't have setText()... it should have failed.");
178     } catch (ProcessingException pe) {
179     } finally {
180       enableLog();
181     }
182   }
183
184   public void testTextualNamedValue() throws Exception JavaDoc {
185     ByteArrayInputStream JavaDoc anInput =
186             new ByteArrayInputStream JavaDoc(XML_TEXTUAL_NAMED_VALUE.getBytes());
187     Object JavaDoc anObject = _theProcessor.process(anInput);
188     assertTrue("The returned object is not a TextualNamedValue", anObject instanceof TextualNamedValue);
189
190     TextualNamedValue aTextualNamedValue = (TextualNamedValue) anObject;
191     assertEquals("The name of the textual named value is invalid", "param1", aTextualNamedValue.getName());
192     assertEquals("The value of the textual named value is invalid", "value1", aTextualNamedValue.getValue());
193     assertEquals("The text of the textual named value is invalid", "THIS IS SOME TEXT", aTextualNamedValue.getText());
194   }
195
196   public void testWrappedTextualNamedValue() throws Exception JavaDoc {
197     ByteArrayInputStream JavaDoc anInput =
198             new ByteArrayInputStream JavaDoc(XML_WRAPPED_TEXTUAL_NAMED_VALUE.getBytes());
199     Object JavaDoc anObject = _theProcessor.process(anInput);
200     assertTrue("The returned object is not a WrappedTextualNamedValue", anObject instanceof WrappedTextualNamedValue);
201
202     WrappedTextualNamedValue aWrapper = (WrappedTextualNamedValue) anObject;
203     assertEquals("The name of the textual named value is invalid", "param1", aWrapper.getTextualNamedValue().getName());
204     assertEquals("The value of the textual named value is invalid", "value1", aWrapper.getTextualNamedValue().getValue());
205     assertEquals("The text of the textual named value is invalid", "THIS IS SOME TEXT", aWrapper.getTextualNamedValue().getText());
206   }
207
208     public void testCallback() throws Exception JavaDoc{
209         ByteArrayInputStream JavaDoc anInput =
210                         new ByteArrayInputStream JavaDoc(XML_CALLBACK.getBytes());
211         URL JavaDoc url = (URL JavaDoc)_theProcessor.process(anInput);
212     }
213   
214     public void testNullObject() throws Exception JavaDoc{
215         ByteArrayInputStream JavaDoc anInput =
216                         new ByteArrayInputStream JavaDoc(XML_NULL_OBJECT.getBytes());
217         Object JavaDoc nullObject = _theProcessor.process(anInput);
218         super.assertTrue(nullObject == null);
219     }
220
221   public void testBasicTypes() throws Exception JavaDoc {
222     String JavaDoc anXml = "<BasicTypes" +
223         " byte=\"5\" byteObject=\"32\" " +
224         " short=\"825\" short-object=\"25432\" " +
225         " int=\"75832\" integer-Object=\"1500000\" " +
226         " long=\"3500000000\" long.object=\"919293949596979899\" " +
227         " float=\"52.54\" float.Object=\"899.2535\" " +
228         " Double=\"3.14154853\" DoubleObject=\"832.123456789\" " +
229         " Char=\"a\" Character-Object=\"b\" " +
230         " Boolean=\"true\" Boolean.Object=\"yes\" " +
231         " />";
232     ByteArrayInputStream JavaDoc anInput =
233             new ByteArrayInputStream JavaDoc(anXml.getBytes());
234     Object JavaDoc anObject = _theProcessor.process(anInput);
235     assertTrue("The returned object is not a BasicTypes", anObject instanceof BasicTypes);
236
237     BasicTypes aBasicType = (BasicTypes) anObject;
238     assertEquals("The byte of the basic types is invalid", 5, aBasicType.getByte());
239     assertEquals("The Byte object of the basic types is invalid", 32, aBasicType.getByteObject().byteValue());
240     assertEquals("The short of the basic types is invalid", 825, aBasicType.getShort());
241     assertEquals("The Short object of the basic types is invalid", 25432, aBasicType.getShortObject().shortValue());
242     assertEquals("The int of the basic types is invalid", 75832, aBasicType.getInt());
243     assertEquals("The Integer object of the basic types is invalid", 1500000, aBasicType.getIntegerObject().intValue());
244     assertEquals("The long of the basic types is invalid", 3500000000L, aBasicType.getLong());
245     assertEquals("The Integer object of the basic types is invalid", 919293949596979899L, aBasicType.getLongObject().longValue());
246     assertEquals("The float of the basic types is invalid", 52.54, aBasicType.getFloat(), 0.01);
247     assertEquals("The Float object of the basic types is invalid", 899.2535, aBasicType.getFloatObject().floatValue(), 0.0001);
248     assertEquals("The double of the basic types is invalid", 3.14154853, aBasicType.getDouble(), 0);
249     assertEquals("The Double object of the basic types is invalid", 832.123456789, aBasicType.getDoubleObject().doubleValue(), 0);
250     assertEquals("The char of the basic types is invalid", 'a', aBasicType.getChar());
251     assertEquals("The Character object of the basic types is invalid", 'b', aBasicType.getCharacterObject().charValue());
252     assertEquals("The boolean of the basic types is invalid", true, aBasicType.getBoolean());
253     assertEquals("The Character object of the basic types is invalid", true, aBasicType.getBooleanObject().booleanValue());
254   }
255 }
256
Popular Tags