1 package org.sapia.util.xml.confix.test; 2 3 import java.io.ByteArrayInputStream ; 6 import java.util.Iterator ; 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.confix.ReflectionFactory; 22 import org.sapia.util.xml.confix.SAXProcessor; 23 24 25 36 public class SAXProcessorCompositeTest extends TestCase { 37 38 private static final String XML_SIMPLE_CONFIG = 39 "<Config name=\"config1\">" + 40 " <NamedValue name=\"param1\" value=\"value1\"/>" + 41 "</Config>"; 42 43 private static final String XML_CONFIG_WITH_ELEMENT = 44 "<Config>" + 45 " <Name>config2</Name>" + 46 " <NamedValue name=\"param1\" value=\"value1\"/>" + 47 "</Config>"; 48 49 private static final String 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 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 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 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 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 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 XML_CREATOR = 116 "<Config name=\"config8\">" + 117 " <Param name=\"param5\" value=\"value5\"/>" + 118 "</Config>"; 119 120 private static final String XML_OBJECT_HANDLER = 121 "<config name=\"config9\">" + 122 " <stringBuffer length=\"5\"/>" + 123 "</config>"; 124 125 private static final String 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 [] 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 aName) { 152 super(aName); 153 } 154 155 public void setUp() { 156 ReflectionFactory anObjectFactory = new ReflectionFactory( 157 new String [] { "org.sapia.util.xml.confix.test", "java.lang" } ); 158 _theProcessor = new SAXProcessor(anObjectFactory); 159 } 160 161 public void testSimpleConfig() throws Exception { 162 ByteArrayInputStream anInput = 163 new ByteArrayInputStream (XML_SIMPLE_CONFIG.getBytes()); 164 Object 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 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 { 178 ByteArrayInputStream anInput = 179 new ByteArrayInputStream (XML_CONFIG_WITH_ELEMENT.getBytes()); 180 Object 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 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 { 194 ByteArrayInputStream anInput = 195 new ByteArrayInputStream (XML_COMPOSITE_CONFIG_WITH_ELEMENT.getBytes()); 196 Object 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 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 { 222 ByteArrayInputStream anInput = 223 new ByteArrayInputStream (XML_WRAPPED_NAMED_VALUE.getBytes()); 224 Object 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 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 { 242 ByteArrayInputStream anInput = 243 new ByteArrayInputStream (XML_TEXTUAL_CONFIG.getBytes()); 244 Object 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 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 { 263 ByteArrayInputStream anInput = 264 new ByteArrayInputStream (XML_CDATA_CONFIG.getBytes()); 265 Object 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 { 275 ByteArrayInputStream anInput = 276 new ByteArrayInputStream (XML_TEXTUAL_WRAPPED_CONFIG.getBytes()); 277 Object 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 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 { 297 } 340 341 public void testCreatorConfig() throws Exception { 342 ByteArrayInputStream anInput = 343 new ByteArrayInputStream (XML_CREATOR.getBytes()); 344 Object 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 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 { 358 ByteArrayInputStream anInput = 359 new ByteArrayInputStream (XML_OBJECT_HANDLER.getBytes()); 360 Object 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 ) aConfig.getCustomObject()[1]).length()); 370 } 371 } 372 | Popular Tags |