| 1 package org.sapia.util.xml.confix.test; 2 3 import java.io.ByteArrayInputStream ; 6 import java.net.URL ; 7 8 import org.apache.log4j.BasicConfigurator; 11 import org.apache.log4j.Level; 12 import org.apache.log4j.LogManager; 13 14 import junit.framework.TestCase; 17 import junit.textui.TestRunner; 18 19 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 37 public class SAXProcessorSingleTest extends TestCase { 38 39 private static final String XML_NOT_WELL_FORMED = 40 "<ThisIsBrokenXML>"; 41 42 private static final String XML_INVALID_CLASS_NAME = 43 "<InvalidClassName />"; 44 45 private static final String XML_EMPTY_NAMED_VALUE = 46 "<namedValue />"; 47 48 private static final String XML_CALLBACK = 49 "<Url link=\"http://www.yahoo.com\"/>"; 50 51 private static final String XML_NULL_OBJECT = 52 "<Null />"; 53 54 private static final String XML_GENERIC_VALUE = 55 "<GenericObject value=\"a generic value\" />"; 56 57 private static final String XML_SIMPLE_NAMED_VALUE = 58 "<NamedValue name=\"param1\" value=\"value1\"/>"; 59 60 private static final String XML_WRAPPED_NAMED_VALUE = 61 "<wrappedNamedValue name=\"param1\" value=\"value1\"/>"; 62 63 private static final String XML_NAMED_VALUE_WITH_TEXT = 64 "<NamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</NamedValue>"; 65 66 private static final String XML_TEXTUAL_NAMED_VALUE = 67 "<textualNamedValue name=\"param1\" value=\"value1\">THIS IS SOME TEXT</textualNamedValue>"; 68 69 private static final String 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 [] 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 aName) { 91 super(aName); 92 } 93 94 public void setUp() { 95 ReflectionFactory anObjectFactory = new ReflectionFactory( 96 new String [] { "org.sapia.util.xml.confix.test" } ); 97 _theProcessor = new SAXProcessor(anObjectFactory); 98 } 99 100 public void testBrokenXml() throws Exception { 101 disableLog(); 102 try { 103 ByteArrayInputStream anInput = 104 new ByteArrayInputStream (XML_NOT_WELL_FORMED.getBytes()); 105 Object 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 { 114 disableLog(); 115 try { 116 ByteArrayInputStream anInput = 117 new ByteArrayInputStream (XML_INVALID_CLASS_NAME.getBytes()); 118 Object 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 { 129 ByteArrayInputStream anInput = 130 new ByteArrayInputStream (XML_EMPTY_NAMED_VALUE.getBytes()); 131 Object 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 { 140 ByteArrayInputStream anInput = 141 new ByteArrayInputStream (XML_GENERIC_VALUE.getBytes()); 142 Object 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 { 150 ByteArrayInputStream anInput = 151 new ByteArrayInputStream (XML_SIMPLE_NAMED_VALUE.getBytes()); 152 Object 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 { 161 ByteArrayInputStream anInput = 162 new ByteArrayInputStream (XML_WRAPPED_NAMED_VALUE.getBytes()); 163 Object 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 { 172 disableLog(); 173 try { 174 ByteArrayInputStream anInput = 175 new ByteArrayInputStream (XML_NAMED_VALUE_WITH_TEXT.getBytes()); 176 Object 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 { 185 ByteArrayInputStream anInput = 186 new ByteArrayInputStream (XML_TEXTUAL_NAMED_VALUE.getBytes()); 187 Object 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 { 197 ByteArrayInputStream anInput = 198 new ByteArrayInputStream (XML_WRAPPED_TEXTUAL_NAMED_VALUE.getBytes()); 199 Object 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 { 209 ByteArrayInputStream anInput = 210 new ByteArrayInputStream (XML_CALLBACK.getBytes()); 211 URL url = (URL )_theProcessor.process(anInput); 212 } 213 214 public void testNullObject() throws Exception { 215 ByteArrayInputStream anInput = 216 new ByteArrayInputStream (XML_NULL_OBJECT.getBytes()); 217 Object nullObject = _theProcessor.process(anInput); 218 super.assertTrue(nullObject == null); 219 } 220 221 public void testBasicTypes() throws Exception { 222 String 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 anInput = 233 new ByteArrayInputStream (anXml.getBytes()); 234 Object 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 |