1 package org.sapia.util.xml.confix.test; 2 3 import java.io.ByteArrayInputStream ; 6 import java.util.Iterator ; 7 8 import junit.framework.TestCase; 11 import junit.textui.TestRunner; 12 13 import org.sapia.util.xml.confix.ReflectionFactory; 16 import org.sapia.util.xml.confix.Dom4jProcessor; 17 import org.jdom.Attribute; 18 import org.jdom.Element; 19 20 21 32 public class Dom4jProcessorCompositeTest extends TestCase { 33 34 private static final String XML_SIMPLE_CONFIG = 35 "<Config name=\"config1\">" + 36 " <NamedValue name=\"param1\" value=\"value1\"/>" + 37 "</Config>"; 38 39 private static final String XML_CONFIG_WITH_ELEMENT = 40 "<Config>" + 41 " <Name>config2</Name>" + 42 " <NamedValue name=\"param1\" value=\"value1\"/>" + 43 "</Config>"; 44 45 private static final String XML_COMPOSITE_CONFIG_WITH_ELEMENT = 46 "<Config>" + 47 " <Name>config2</Name>" + 48 " <NamedValue name=\"param1\" value=\"value1\"/>" + 49 " <NamedValue>" + 50 " <Name>param2</Name>" + 51 " <Value>value2</Value>" + 52 " </NamedValue>" + 53 " <NamedValue name=\"param3\">" + 54 " <Value>value3</Value>" + 55 " </NamedValue>" + 56 " <NamedValue name=\"attrib-param4\" value=\"attrib-value4\">" + 57 " <Name>elem-param4</Name>" + 58 " <Value>elem-value4</Value>" + 59 " </NamedValue>" + 60 "</Config>"; 61 62 private static final String XML_WRAPPED_NAMED_VALUE = 63 "<Config name=\"config3\">" + 64 " <WrappedNamedValue name=\"param1\" value=\"value1\"/>" + 65 " <WrappedNamedValue>" + 66 " <Name>param2</Name>" + 67 " <Value>value2</Value>" + 68 " </WrappedNamedValue>" + 69 "</Config>"; 70 71 private static final String XML_WRAPPED_CONFIG = 72 "<WrappedConfig name=\"config4\">" + 73 " <NamedValue name=\"param1\" value=\"value1\"/>" + 74 " <WrappedNamedValue>" + 75 " <Name>param2</Name>" + 76 " <Value>value2</Value>" + 77 " </WrappedNamedValue>" + 78 "</WrappedConfig>"; 79 80 private static final String XML_TEXTUAL_CONFIG = 81 "<TextualConfig name=\"config5\">" + 82 " <NamedValue name=\"param1\" value=\"value1\"/>" + 83 " <WrappedNamedValue>" + 84 " <Name>param2</Name>" + 85 " <Value>value2</Value>" + 86 " </WrappedNamedValue>" + 87 " THIS IS A FINAL CONFIG CONTENT" + 88 "</TextualConfig>"; 89 90 private static final String XML_TEXTUAL_WRAPPED_CONFIG = 91 "<WrappedTextualConfig name=\"config6\">" + 92 " <NamedValue name=\"param1\" value=\"value1\"/>" + 93 " <WrappedTextualNamedValue>" + 94 " TEXT:TextualNamedValue" + 95 " <Name>param2</Name>" + 96 " <Value>value2</Value>" + 97 " </WrappedTextualNamedValue>" + 98 " THIS IS A FINAL CONFIG CONTENT" + 99 "</WrappedTextualConfig>"; 100 101 102 private static final String XML_JDOM_HANDLER = 103 "<WrappedTextualConfig name=\"config7\">" + 104 " <NamedValue name=\"param1\" value=\"value1\"/>" + 105 " <CustomConfig type=\"ant\">" + 106 " <build default=\"build\"><target name=\"build\"><javac/></target></build>" + 107 " </CustomConfig>" + 108 " THIS IS A FINAL CONFIG CONTENT" + 109 "</WrappedTextualConfig>"; 110 111 private static final String XML_CONSUMER = 112 "<WrappedTextualConfig name=\"config7\">" + 113 " <NamedValue name=\"param1\" value=\"value1\"/>" + 114 " <XMLConsumerConfig type=\"ant\">" + 115 " <build default=\"build\"><target name=\"build\"><javac/></target></build>" + 116 " </XMLConsumerConfig>" + 117 " THIS IS A FINAL CONFIG CONTENT" + 118 "</WrappedTextualConfig>"; 119 120 private static final String XML_CREATOR = 121 "<Config name=\"config8\">" + 122 " <Param name=\"param5\" value=\"value5\"/>" + 123 "</Config>"; 124 125 private static final String XML_OBJECT_HANDLER = 126 "<config name=\"config9\">" + 127 " <stringBuffer length=\"5\"/>" + 128 "</config>"; 129 130 131 private static final String XML_CDATA_CONFIG = 132 "<TextualConfig name=\"config10\">" + 133 " <![CDATA[" + 134 " THIS IS A CDATA CONFIG CONTENT" + 135 " ]]>" + 136 "</TextualConfig>"; 137 138 private Dom4jProcessor _theProcessor; 139 140 public static void main(String [] args) { 141 TestRunner.run(Dom4jProcessorCompositeTest.class); 142 } 143 144 public Dom4jProcessorCompositeTest(String aName) { 145 super(aName); 146 } 147 148 public void setUp() { 149 ReflectionFactory anObjectFactory = new ReflectionFactory( 150 new String [] { "org.sapia.util.xml.confix.test", "java.lang" } ); 151 _theProcessor = new Dom4jProcessor(anObjectFactory); 152 } 153 154 public void testSimpleConfig() throws Exception { 155 ByteArrayInputStream anInput = 156 new ByteArrayInputStream (XML_SIMPLE_CONFIG.getBytes()); 157 Object anObject = _theProcessor.process(anInput); 158 assertTrue("The returned object is not a Config", anObject instanceof Config); 159 160 Config aConfig = (Config) anObject; 161 assertEquals("The name of the config is invalid", "config1", aConfig.getName()); 162 assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size()); 163 Iterator someNamedValues = aConfig.getNamedValues().iterator(); 164 165 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 166 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 167 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 168 } 169 170 public void testSimpleConfigWithElement() throws Exception { 171 ByteArrayInputStream anInput = 172 new ByteArrayInputStream (XML_CONFIG_WITH_ELEMENT.getBytes()); 173 Object anObject = _theProcessor.process(anInput); 174 assertTrue("The returned object is not a Config", anObject instanceof Config); 175 176 Config aConfig = (Config) anObject; 177 assertEquals("The name of the config is invalid", "config2", aConfig.getName()); 178 assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size()); 179 Iterator someNamedValues = aConfig.getNamedValues().iterator(); 180 181 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 182 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 183 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 184 } 185 186 public void testCompositeConfigWithElement() throws Exception { 187 ByteArrayInputStream anInput = 188 new ByteArrayInputStream (XML_COMPOSITE_CONFIG_WITH_ELEMENT.getBytes()); 189 Object anObject = _theProcessor.process(anInput); 190 assertTrue("The returned object is not a Config", anObject instanceof Config); 191 192 Config aConfig = (Config) anObject; 193 assertEquals("The name of the config is invalid", "config2", aConfig.getName()); 194 assertEquals("The size of the named value list is invalid", 4, aConfig.getNamedValues().size()); 195 Iterator someNamedValues = aConfig.getNamedValues().iterator(); 196 197 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 198 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 199 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 200 201 aNamedValue = (NamedValue) someNamedValues.next(); 202 assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName()); 203 assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue()); 204 205 aNamedValue = (NamedValue) someNamedValues.next(); 206 assertEquals("The name of the named value is invalid", "param3", aNamedValue.getName()); 207 assertEquals("The value of the named value is invalid", "value3", aNamedValue.getValue()); 208 209 aNamedValue = (NamedValue) someNamedValues.next(); 210 assertEquals("The name of the named value is invalid", "elem-param4", aNamedValue.getName()); 211 assertEquals("The value of the named value is invalid", "elem-value4", aNamedValue.getValue()); 212 } 213 214 public void testWrappedNamedValue() throws Exception { 215 ByteArrayInputStream anInput = 216 new ByteArrayInputStream (XML_WRAPPED_NAMED_VALUE.getBytes()); 217 Object anObject = _theProcessor.process(anInput); 218 assertTrue("The returned object is not a Config", anObject instanceof Config); 219 220 Config aConfig = (Config) anObject; 221 assertEquals("The name of the config is invalid", "config3", aConfig.getName()); 222 assertEquals("The size of the named value list is invalid", 2, aConfig.getNamedValues().size()); 223 Iterator someNamedValues = aConfig.getNamedValues().iterator(); 224 225 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 226 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 227 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 228 229 aNamedValue = (NamedValue) someNamedValues.next(); 230 assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName()); 231 assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue()); 232 } 233 234 public void testTextualConfig() throws Exception { 235 ByteArrayInputStream anInput = 236 new ByteArrayInputStream (XML_TEXTUAL_CONFIG.getBytes()); 237 Object anObject = _theProcessor.process(anInput); 238 assertTrue("The returned object is not a TextualConfig", anObject instanceof TextualConfig); 239 240 TextualConfig aTextualConfig = (TextualConfig) anObject; 241 assertEquals("The name of the config is invalid", "config5", aTextualConfig.getName()); 242 assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aTextualConfig.getText()); 243 assertEquals("The size of the named value list is invalid", 2, aTextualConfig.getNamedValues().size()); 244 Iterator someNamedValues = aTextualConfig.getNamedValues().iterator(); 245 246 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 247 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 248 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 249 250 aNamedValue = (NamedValue) someNamedValues.next(); 251 assertEquals("The name of the named value is invalid", "param2", aNamedValue.getName()); 252 assertEquals("The value of the named value is invalid", "value2", aNamedValue.getValue()); 253 } 254 255 256 public void testCdataConfig() throws Exception { 257 ByteArrayInputStream anInput = 258 new ByteArrayInputStream (XML_CDATA_CONFIG.getBytes()); 259 Object anObject = _theProcessor.process(anInput); 260 assertTrue("The returned object is not a TextualConfig", anObject instanceof TextualConfig); 261 262 TextualConfig aTextualConfig = (TextualConfig) anObject; 263 assertEquals("The name of the config is invalid", "config10", aTextualConfig.getName()); 264 assertEquals("The content of the config is invalid", "THIS IS A CDATA CONFIG CONTENT", aTextualConfig.getText()); 265 assertEquals("The size of the named value list is invalid", 0, aTextualConfig.getNamedValues().size()); 266 } 267 268 public void testWrappedTextualConfig() throws Exception { 269 ByteArrayInputStream anInput = 270 new ByteArrayInputStream (XML_TEXTUAL_WRAPPED_CONFIG.getBytes()); 271 Object anObject = _theProcessor.process(anInput); 272 assertTrue("The returned object is not a TextualConfig", anObject instanceof WrappedTextualConfig); 273 274 WrappedTextualConfig aWrapper = (WrappedTextualConfig) anObject; 275 assertEquals("The name of the config is invalid", "config6", aWrapper.getTextualConfig().getName()); 276 assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aWrapper.getTextualConfig().getText()); 277 assertEquals("The size of the named value list is invalid", 2, aWrapper.getTextualConfig().getNamedValues().size()); 278 Iterator someNamedValues = aWrapper.getTextualConfig().getNamedValues().iterator(); 279 280 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 281 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 282 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 283 284 TextualNamedValue aTextualNamedValue = (TextualNamedValue) someNamedValues.next(); 285 assertEquals("The name of the named value is invalid", "param2", aTextualNamedValue.getName()); 286 assertEquals("The value of the named value is invalid", "value2", aTextualNamedValue.getValue()); 287 assertEquals("The text of the named value is invalid", "TEXT:TextualNamedValue", aTextualNamedValue.getText()); 288 } 289 290 335 336 public void testXMLConsumer() throws Exception { 337 ByteArrayInputStream anInput = 338 new ByteArrayInputStream (XML_CONSUMER.getBytes()); 339 Object anObject = _theProcessor.process(anInput); 340 assertTrue("The returned object is not a TextualConfig", anObject instanceof WrappedTextualConfig); 341 342 WrappedTextualConfig aWrapper = (WrappedTextualConfig) anObject; 343 assertEquals("The name of the config is invalid", "config7", aWrapper.getTextualConfig().getName()); 344 assertEquals("The content of the config is invalid", "THIS IS A FINAL CONFIG CONTENT", aWrapper.getTextualConfig().getText()); 345 assertEquals("The size of the named value list is invalid", 1, aWrapper.getTextualConfig().getNamedValues().size()); 346 Iterator someNamedValues = aWrapper.getTextualConfig().getNamedValues().iterator(); 347 348 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 349 assertEquals("The name of the named value is invalid", "param1", aNamedValue.getName()); 350 assertEquals("The value of the named value is invalid", "value1", aNamedValue.getValue()); 351 352 assertEquals("The size of the custom config list is invalid", 1, aWrapper.getTextualConfig().getCustomConfigs().size()); 353 Iterator someCustomCongigs = aWrapper.getTextualConfig().getCustomConfigs().iterator(); 354 355 XMLConsumerConfig aCustomConfig = (XMLConsumerConfig) someCustomCongigs.next(); 356 assertEquals("The type of the custom config is invalid", "ant", aCustomConfig.getType()); 357 358 Element anElement = aCustomConfig.getConfigElement(); 359 assertEquals("The name of the element is invalid", "build", anElement.getName()); 360 assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size()); 361 362 Attribute anAttribute = (Attribute) anElement.getAttributes().iterator().next(); 363 assertEquals("The name of the attribute is invalid", "default", anAttribute.getName()); 364 assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue()); 365 assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size()); 366 367 anElement = (Element) anElement.getChildren().iterator().next(); 368 assertEquals("The name of the element is invalid", "target", anElement.getName()); 369 assertEquals("The size of the attribute list is invalid", 1, anElement.getAttributes().size()); 370 371 anAttribute = (Attribute) anElement.getAttributes().iterator().next(); 372 assertEquals("The name of the attribute is invalid", "name", anAttribute.getName()); 373 assertEquals("The value of the attribute is invalid", "build", anAttribute.getValue()); 374 assertEquals("The size of the child list is invalid", 1, anElement.getChildren().size()); 375 376 anElement = (Element) anElement.getChildren().iterator().next(); 377 assertEquals("The name of the element is invalid", "javac", anElement.getName()); 378 assertEquals("The size of the attribute list is invalid", 0, anElement.getAttributes().size()); 379 } 380 381 public void testCreatorConfig() throws Exception { 382 ByteArrayInputStream anInput = 383 new ByteArrayInputStream (XML_CREATOR.getBytes()); 384 Object anObject = _theProcessor.process(anInput); 385 assertTrue("The returned object is not a Config", anObject instanceof Config); 386 387 Config aConfig = (Config) anObject; 388 assertEquals("The name of the config is invalid", "config8", aConfig.getName()); 389 assertEquals("The size of the named value list is invalid", 1, aConfig.getNamedValues().size()); 390 Iterator someNamedValues = aConfig.getNamedValues().iterator(); 391 392 NamedValue aNamedValue = (NamedValue) someNamedValues.next(); 393 assertEquals("The name of the named value is invalid", "param5", aNamedValue.getName()); 394 assertEquals("The value of the named value is invalid", "value5", aNamedValue.getValue()); 395 } 396 397 public void testObjectHandler() throws Exception { 398 ByteArrayInputStream anInput = 399 new ByteArrayInputStream (XML_OBJECT_HANDLER.getBytes()); 400 Object anObject = _theProcessor.process(anInput); 401 assertTrue("The returned object is not a Config", anObject instanceof Config); 402 403 Config aConfig = (Config) anObject; 404 assertEquals("The name of the config is invalid", "config9", aConfig.getName()); 405 assertEquals("The size of the named value list is invalid", 0, aConfig.getNamedValues().size()); 406 407 assertEquals("The length of the custom object array is invalid", 2, aConfig.getCustomObject().length); 408 assertEquals("The first custom object is invalid", "stringBuffer", aConfig.getCustomObject()[0]); 409 assertEquals("The second custom object is invalid", 5, ((StringBuffer ) aConfig.getCustomObject()[1]).length()); 410 } 411 } 412 | Popular Tags |