1 51 package org.apache.avalon.framework.configuration.test; 52 53 import java.io.ByteArrayInputStream ; 54 import java.io.File ; 55 import java.io.FileWriter ; 56 import java.io.InputStream ; 57 import junit.framework.TestCase; 58 import org.apache.avalon.framework.configuration.Configuration; 59 import org.apache.avalon.framework.configuration.ConfigurationException; 60 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 61 import org.xml.sax.SAXException ; 62 63 71 public final class DefaultConfigurationBuilderTestCase 72 extends TestCase 73 { 74 75 private DefaultConfigurationBuilder m_builder; 76 private DefaultConfigurationBuilder m_nsBuilder; 77 private File m_file; 78 private File m_nsFile; 79 private File m_testDirectory; 80 private final String m_simpleFileName = "config_simple.xml"; 81 private final String m_nsFileName = "config_namespaces.xml"; 82 private final String m_path = "test/framework/io/"; 83 84 private final String simpleXML = 85 "<?xml version=\"1.0\" ?>"+ 86 "<config boolAttr=\"true\" floatAttr=\"1.32\">"+ 87 " <elements-a>"+ 88 " <element name=\"a\"/>"+ 89 " </elements-a>"+ 90 " <elements-b>"+ 91 " <element name=\"b\"/> "+ 92 " </elements-b>"+ 93 " <elements-b type=\"type-b\"/>"+ 94 " <elements-c>"+ 95 " true"+ 96 " </elements-c>"+ 97 "</config>"; 98 99 104 private void simpleAssertions(Configuration conf) 105 throws ConfigurationException 106 { 107 assertEquals( "config", conf.getName() ); 108 assertEquals( "getNamespace() should default to \"\"", "", conf.getNamespace() ); 109 try { 110 String value = conf.getValue(); 111 fail( "Should throw a ConfigurationException, as this element"+ 112 "contains child elements, not a value" ); 113 } catch ( ConfigurationException e ) 114 {} 115 116 Configuration[] children; 117 children = conf.getChildren(); 118 assertEquals( 4, children.length ); 119 assertEquals( "elements-a", children[0].getName() ); 120 assertEquals( "elements-b", children[1].getName() ); 121 assertEquals( "b", children[1].getChild("element", false).getAttribute("name") ); 122 assertEquals( "elements-b", children[2].getName() ); 123 assertEquals( "elements-c", children[3].getName() ); 124 125 final String [] attrNames = conf.getAttributeNames(); 126 assertEquals( 2, attrNames.length ); 127 assertEquals( "default", conf.getAttribute("nonexistent", "default") ); 128 assertEquals( true, conf.getAttributeAsBoolean("boolAttr") ); 129 assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 ); 130 131 assertEquals( 133 "When a non-existent child is requested, a blank node should be created", 134 "nonexistent", 135 conf.getChild( "nonexistent" ).getName() 136 ); 137 assertEquals( 138 "When a non-existent child is requested, a blank node should be created", 139 "baz", 140 conf.getChild( "foo" ).getChild("bar").getChild("baz").getName() 141 ); 142 try { 143 String value = conf.getChild("nonexistent").getValue(); 144 fail( "Auto-created child nodes should not have a value" ); 145 } catch ( ConfigurationException e ) 146 {} 147 assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false ) 148 ); 149 assertEquals( "Standard getChild() lookup failed", "elements-b", conf.getChild( "elements-b", false ).getName() ); 150 assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) ); 151 assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length ); 152 } 153 154 private final String nsXML = 155 "<?xml version=\"1.0\" ?>"+ 156 "<conf:config"+ 157 " boolAttr=\"true\" floatAttr=\"1.32\""+ 158 " xmlns:conf=\"http://conf.com\" xmlns:a=\"http://a.com\" xmlns:b=\"http://b.com\" xmlns:c=\"http://c.com\" xmlns:d=\"http://d.com\" xmlns:e=\"http://e.com\">"+ 159 " <a:elements-a>"+ 160 " <c:element name=\"a\"/>"+ 161 " </a:elements-a>"+ 162 " <elements-b xmlns=\"http://defaultns.com\">"+ 163 " <element name=\"b\"/> "+ 164 " </elements-b>"+ 165 " <b:elements-b type=\"type-b\"/>"+ 166 " <elements-c>"+ 167 " true"+ 168 " </elements-c>"+ 169 " <d:element>d:element</d:element>"+ 170 " <e:element>e:element</e:element>"+ 171 "</conf:config>"; 172 179 180 185 private void simpleAssertionsNS(Configuration conf) 186 throws ConfigurationException 187 { 188 assertEquals( "conf:config", conf.getName() ); 189 assertEquals( "getNamespace() should default to \"\"", "", conf.getNamespace() ); 190 try { 191 String value = conf.getValue(); 192 fail( "Should throw a ConfigurationException, as this element"+ 193 "contains child elements, not a value" ); 194 } catch ( ConfigurationException e ) 195 {} 196 197 Configuration[] children; 198 children = conf.getChildren(); 199 assertEquals( 6, children.length ); 200 assertEquals( "a:elements-a", children[0].getName() ); 201 assertEquals( "elements-b", children[1].getName() ); 202 assertEquals( "b", children[1].getChild("element", false).getAttribute("name") ); 203 assertEquals( "b:elements-b", children[2].getName() ); 204 assertEquals( "elements-c", children[3].getName() ); 205 206 final String [] attrNames = conf.getAttributeNames(); 207 assertEquals( 8, attrNames.length ); 208 assertEquals( "true", conf.getAttribute("boolAttr", null) ); 209 assertEquals( true, conf.getAttributeAsBoolean("boolAttr") ); 210 assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 ); 211 assertEquals( "http://conf.com", conf.getAttribute("xmlns:conf") ); 212 assertEquals( "http://a.com", conf.getAttribute("xmlns:a") ); 213 assertEquals( "http://b.com", conf.getAttribute("xmlns:b") ); 214 assertEquals( "http://c.com", conf.getAttribute("xmlns:c") ); 215 216 assertEquals( 218 "When a non-existent child is requested, a blank node should be created", 219 "nonexistent", 220 conf.getChild( "nonexistent" ).getName() 221 ); 222 assertEquals( 223 "When a non-existent child is requested, a blank node should be created", 224 "baz", 225 conf.getChild( "foo" ).getChild("bar").getChild("baz").getName() 226 ); 227 try { 228 String value = conf.getChild("nonexistent").getValue(); 229 fail( "Auto-created child nodes should not have a value" ); 230 } catch ( ConfigurationException e ) 231 {} 232 assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false ) 233 ); 234 assertEquals( "Standard getChild() lookup failed", "b:elements-b", conf.getChild( "b:elements-b", false ).getName() ); 235 assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) ); 236 assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length ); 237 238 assertEquals( "d:element", conf.getChild("d:element").getValue() ); 239 assertEquals( "e:element", conf.getChild("e:element").getValue() ); 240 } 241 242 243 248 private void nsAssertions(Configuration conf) 249 throws ConfigurationException 250 { 251 assertEquals( "config", conf.getName() ); 252 assertEquals( "Namespace not set correctly", "http://conf.com", conf.getNamespace() ); 253 try { 254 String value = conf.getValue(); 255 fail( "Should throw a ConfigurationException, as this element"+ 256 "contains child elements, not a value" ); 257 } catch ( ConfigurationException e ) 258 {} 259 260 Configuration[] children; 261 children = conf.getChildren(); 262 assertEquals( 6, children.length ); 263 assertEquals( "elements-a", children[0].getName() ); 264 assertEquals( "http://a.com", children[0].getNamespace() ); 265 assertEquals( "elements-b", children[1].getName() ); 266 assertEquals( "http://defaultns.com", children[1].getNamespace() ); 267 assertEquals( "b", children[1].getChild("element", false).getAttribute("name") ); 268 assertEquals( "elements-b", children[2].getName() ); 269 assertEquals( "http://b.com", children[2].getNamespace() ); 270 assertEquals( "elements-c", children[3].getName() ); 271 assertEquals( "", children[3].getNamespace() ); 272 273 final String [] attrNames = conf.getAttributeNames(); 274 assertEquals( 2, attrNames.length ); assertEquals( "true", conf.getAttribute("boolAttr", null) ); 276 assertEquals( true, conf.getAttributeAsBoolean("boolAttr") ); 277 assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 ); 278 279 assertEquals( 281 "When a non-existent child is requested, a blank node should be created", 282 "nonexistent", 283 conf.getChild( "nonexistent" ).getName() 284 ); 285 assertEquals( 286 "When a non-existent child is requested, a blank node should be created", 287 "baz", 288 conf.getChild( "foo" ).getChild("bar").getChild("baz").getName() 289 ); 290 try { 291 String value = conf.getChild("nonexistent").getValue(); 292 fail( "Auto-created child nodes should not have a value" ); 293 } catch ( ConfigurationException e ) 294 {} 295 assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false ) 296 ); 297 assertEquals( "Standard getChild() lookup failed", "elements-b", conf.getChild( "elements-b", false ).getName() ); 298 assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) ); 299 assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length ); 300 } 301 302 303 public DefaultConfigurationBuilderTestCase() 304 { 305 this("DefaultConfigurationBuilder Test Case"); 306 } 307 308 public DefaultConfigurationBuilderTestCase( final String name ) 309 { 310 super( name ); 311 m_testDirectory = (new File ( m_path)).getAbsoluteFile(); 312 if( !m_testDirectory.exists() ) 313 { 314 m_testDirectory.mkdirs(); 315 } 316 } 317 318 protected void setUp() 319 throws Exception 320 { 321 m_file = new File ( m_testDirectory, m_simpleFileName ); 322 m_nsFile = new File ( m_testDirectory, m_nsFileName ); 323 FileWriter writer = new FileWriter ( m_file ); 324 writer.write( simpleXML ); 325 writer.close(); 326 writer = new FileWriter ( m_nsFile ); 327 writer.write( nsXML ); 328 writer.close(); 329 330 } 331 332 protected void tearDown() 333 throws Exception 334 { 335 341 m_builder = null; 342 m_nsBuilder = null; 343 } 344 345 346 public void testBuildFromFileName() 347 throws Exception 348 { 349 m_builder = new DefaultConfigurationBuilder(); 350 m_nsBuilder = new DefaultConfigurationBuilder(true); Configuration conf = m_builder.buildFromFile( m_path + m_simpleFileName ); 352 simpleAssertions( conf ); 353 conf = m_builder.buildFromFile( m_path + m_nsFileName ); 354 simpleAssertionsNS( conf ); 355 conf = m_nsBuilder.buildFromFile( m_path + m_nsFileName ); 356 nsAssertions( conf ); 357 } 358 359 public void testBuildFromFile() 360 throws Exception 361 { 362 m_builder = new DefaultConfigurationBuilder(); 363 m_nsBuilder = new DefaultConfigurationBuilder(true); Configuration conf = m_builder.buildFromFile( m_file ); 365 simpleAssertions( conf ); 366 conf = m_builder.buildFromFile( m_nsFile ); 367 simpleAssertionsNS( conf ); 368 conf = m_nsBuilder.buildFromFile( m_nsFile ); 369 nsAssertions( conf ); 370 } 371 372 public void testBuild() 373 throws Exception 374 { 375 m_builder = new DefaultConfigurationBuilder(); 376 m_nsBuilder = new DefaultConfigurationBuilder(true); Configuration conf = m_builder.build( m_file.toURL().toString() ); 378 simpleAssertions( conf ); 379 conf = m_builder.buildFromFile( m_nsFile ); 380 simpleAssertionsNS( conf ); 381 conf = m_nsBuilder.buildFromFile( m_nsFile ); 382 nsAssertions( conf ); 383 } 384 385 private final String spaceTrimmingCheckXML = 386 "<?xml version=\"1.0\" ?>"+ 387 " <config>"+ 388 " <trimmed-item>\n"+ 389 " value \n"+ 390 " </trimmed-item>\n"+ 391 " <preserved-item xml:space='preserve'>\n"+ 392 " a space a CR, then a trailing space </preserved-item>\n"+ 393 " <first-level-item xml:space='preserve'>\n"+ 394 " <second-level-preserved> whitespace around </second-level-preserved>\n"+ 395 " </first-level-item>\n"+ 396 " <trimmed-again-item>\n"+ 397 " value \n"+ 398 " </trimmed-again-item>\n"+ 399 "</config>"; 400 401 405 public void testSpaceTrimming() 406 throws Exception 407 { 408 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 409 InputStream in = new ByteArrayInputStream ( spaceTrimmingCheckXML.getBytes() ); 410 Configuration conf = builder.build( in ); 411 assertEquals( "Value is trimmed by default", 412 "value", 413 conf.getChild( "trimmed-item" ).getValue() ); 414 assertEquals( "After trimming turned off value is preserved", 415 "\n a space\r a CR, then a trailing space ", 416 conf.getChild( "preserved-item" ).getValue() ); 417 assertEquals( "Trimming two levels deep works too", 418 " whitespace around ", 419 conf.getChild( "first-level-item" ) 420 .getChild( "second-level-preserved" ).getValue() ); 421 assertEquals( "Trimming turned back on", 422 "value", 423 conf.getChild( "trimmed-again-item" ).getValue() ); 424 } 425 426 427 private final String mixedContentXML = 428 "<?xml version=\"1.0\" ?>"+ 429 "<a>a<a/></a>" 430 ; 431 public void testMixedContentDetection() 432 throws Exception 433 { 434 DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(); 435 InputStream in = new ByteArrayInputStream ( mixedContentXML.getBytes() ); 436 try 437 { 438 builder.build( in ); 439 fail ("Must fail on mixed content"); 440 } catch ( SAXException e ) 441 {} 442 } 443 } 444 | Popular Tags |