KickJava   Java API By Example, From Geeks To Geeks.

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


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