1 package org.sapia.util.xml.confix.test; 2 3 import java.io.ByteArrayInputStream ; 6 import java.net.URL ; 7 8 9 import junit.framework.TestCase; 12 import junit.textui.TestRunner; 13 14 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 32 public class JDOMProcessorSingleTest extends TestCase { 33 34 private static final String XML_NOT_WELL_FORMED = 35 "<ThisIsBrokenXML>"; 36 37 private static final String XML_INVALID_CLASS_NAME = 38 "<InvalidClassName />"; 39 40 private static final String XML_EMPTY_NAMED_VALUE = 41 "<namedValue />"; 42 43 private static final String XML_CALLBACK = 44 "<Url link=\"http://www.yahoo.com\"/>"; 45 46 private static final String XML_NULL_OBJECT = 47 "<Null />"; 48 49 private static final String XML_GENERIC_VALUE = 50 "<genericObject value=\"generic\"/>"; 51 52 private static final String XML_SIMPLE_NAMED_VALUE = 53 "<NamedValue name=\"param1\" value=\"value1\"/>"; 54 55 private static final String XML_WRAPPED_NAMED_VALUE = 56 "<wrappedNamedValue name=\"param1\" value=\"value1\"/>"; 57 58 private static final String XML_NAMED_VALUE_WITH_TEXT = 59 "<NamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</NamedValue>"; 60 61 private static final String XML_TEXTUAL_NAMED_VALUE = 62 "<textualNamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</textualNamedValue>"; 63 64 private static final String 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 [] args) { 70 TestRunner.run(JDOMProcessorSingleTest.class); 71 } 72 73 public JDOMProcessorSingleTest(String aName) { 74 super(aName); 75 } 76 77 public void setUp() { 78 ReflectionFactory anObjectFactory = new ReflectionFactory( 79 new String [] { "org.sapia.util.xml.confix.test" } ); 80 _theProcessor = new JDOMProcessor(anObjectFactory); 81 } 82 83 public void testBrokenXml() throws Exception { 84 try { 85 ByteArrayInputStream anInput = 86 new ByteArrayInputStream (XML_NOT_WELL_FORMED.getBytes()); 87 Object 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 { 94 try { 95 ByteArrayInputStream anInput = 96 new ByteArrayInputStream (XML_INVALID_CLASS_NAME.getBytes()); 97 Object 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 { 104 ByteArrayInputStream anInput = 105 new ByteArrayInputStream (XML_EMPTY_NAMED_VALUE.getBytes()); 106 Object 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 { 115 ByteArrayInputStream anInput = 116 new ByteArrayInputStream (XML_SIMPLE_NAMED_VALUE.getBytes()); 117 Object 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 { 126 ByteArrayInputStream anInput = 127 new ByteArrayInputStream (XML_GENERIC_VALUE.getBytes()); 128 Object 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 { 136 ByteArrayInputStream anInput = 137 new ByteArrayInputStream (XML_WRAPPED_NAMED_VALUE.getBytes()); 138 Object 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 { 147 try { 148 ByteArrayInputStream anInput = 149 new ByteArrayInputStream (XML_NAMED_VALUE_WITH_TEXT.getBytes()); 150 Object 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 { 157 ByteArrayInputStream anInput = 158 new ByteArrayInputStream (XML_TEXTUAL_NAMED_VALUE.getBytes()); 159 Object 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 { 169 ByteArrayInputStream anInput = 170 new ByteArrayInputStream (XML_WRAPPED_TEXTUAL_NAMED_VALUE.getBytes()); 171 Object 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 { 181 ByteArrayInputStream anInput = 182 new ByteArrayInputStream (XML_CALLBACK.getBytes()); 183 URL url = (URL )_theProcessor.process(anInput); 184 } 185 186 public void testNullObject() throws Exception { 187 ByteArrayInputStream anInput = 188 new ByteArrayInputStream (XML_NULL_OBJECT.getBytes()); 189 Object nullObject = _theProcessor.process(anInput); 190 super.assertTrue(nullObject == null); 191 } 192 193 194 public void testBasicTypes() throws Exception { 195 String 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 anInput = 206 new ByteArrayInputStream (anXml.getBytes()); 207 Object 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 |