1 /* 2 * Copyright (C) The JContainer Group. All rights reserved. 3 * 4 * This software is published under the terms of the JContainer 5 * Software License version 1.1, a copy of which has been included 6 * with this distribution in the LICENSE.txt file. 7 */ 8 package org.jcontainer.dna.impl; 9 10 import junit.framework.TestCase; 11 import org.jcontainer.dna.Configuration; 12 import org.xml.sax.SAXException; 13 import org.xml.sax.SAXParseException; 14 import org.xml.sax.helpers.AttributesImpl; 15 16 public class SAXConfigurationHandlerTestCase 17 extends TestCase 18 { 19 public void testGetLocationWithNullLocator() 20 throws Exception 21 { 22 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 23 final String location = handler.getLocationDescription(); 24 assertEquals( "location", "", location ); 25 } 26 27 public void testGetLocationWithNullSystemId() 28 throws Exception 29 { 30 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 31 handler.setDocumentLocator( new MockLocator( null ) ); 32 final String location = handler.getLocationDescription(); 33 assertEquals( "location", "", location ); 34 } 35 36 public void testGetLocationWithNonNullSystemId() 37 throws Exception 38 { 39 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 40 handler.setDocumentLocator( new MockLocator( "file.xml" ) ); 41 final String location = handler.getLocationDescription(); 42 assertEquals( "location", "file.xml", location ); 43 } 44 45 public void testGetLocationWithLineSet() 46 throws Exception 47 { 48 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 49 final MockLocator locator = new MockLocator( "file.xml" ); 50 locator.setLineNumber( 23 ); 51 handler.setDocumentLocator( locator ); 52 final String location = handler.getLocationDescription(); 53 assertEquals( "location", "file.xml:23", location ); 54 } 55 56 public void testGetLocationWithColSet() 57 throws Exception 58 { 59 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 60 final MockLocator locator = new MockLocator( "file.xml" ); 61 locator.setLineNumber( 23 ); 62 locator.setColumnNumber( 15 ); 63 handler.setDocumentLocator( locator ); 64 final String location = handler.getLocationDescription(); 65 assertEquals( "location", "file.xml:23:15", location ); 66 } 67 68 public void testGetLocationWithColSetButLineNotSet() 69 throws Exception 70 { 71 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 72 final MockLocator locator = new MockLocator( "file.xml" ); 73 locator.setColumnNumber( 15 ); 74 handler.setDocumentLocator( locator ); 75 final String location = handler.getLocationDescription(); 76 assertEquals( "location", "file.xml", location ); 77 } 78 79 public void testWarningRethrowsException() 80 throws Exception 81 { 82 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 83 final SAXParseException spe = new SAXParseException( "", null ); 84 try 85 { 86 handler.warning( spe ); 87 } 88 catch( final SAXException se ) 89 { 90 assertEquals( spe, se ); 91 return; 92 } 93 fail( "Expected exception to be thrown" ); 94 } 95 96 public void testErrorRethrowsException() 97 throws Exception 98 { 99 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 100 final SAXParseException spe = new SAXParseException( "", null ); 101 try 102 { 103 handler.error( spe ); 104 } 105 catch( final SAXException se ) 106 { 107 assertEquals( spe, se ); 108 return; 109 } 110 fail( "Expected exception to be thrown" ); 111 } 112 113 public void testFatalRethrowsException() 114 throws Exception 115 { 116 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 117 final SAXParseException spe = new SAXParseException( "", null ); 118 try 119 { 120 handler.fatalError( spe ); 121 } 122 catch( final SAXException se ) 123 { 124 assertEquals( spe, se ); 125 return; 126 } 127 fail( "Expected exception to be thrown" ); 128 } 129 130 public void testCreateSimpleConfiguration() 131 throws Exception 132 { 133 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 134 final String qName = "myElement"; 135 handler.startElement( "", "", qName, new AttributesImpl() ); 136 handler.endElement( "", "", qName ); 137 final Configuration configuration = handler.getConfiguration(); 138 assertEquals( "configuration.name", qName, configuration.getName() ); 139 assertEquals( "configuration.location", "", configuration.getLocation() ); 140 assertEquals( "configuration.path", "", configuration.getPath() ); 141 } 142 143 public void testCreateConfigurationWithValue() 144 throws Exception 145 { 146 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 147 final String qName = "myElement"; 148 final String value = "value"; 149 handler.startElement( "", "", qName, new AttributesImpl() ); 150 handler.characters( value.toCharArray(), 0, value.length() ); 151 handler.endElement( "", "", qName ); 152 final Configuration configuration = handler.getConfiguration(); 153 assertEquals( "configuration.name", qName, configuration.getName() ); 154 assertEquals( "configuration.location", "", configuration.getLocation() ); 155 assertEquals( "configuration.path", "", configuration.getPath() ); 156 assertEquals( "configuration.value", value, configuration.getValue() ); 157 } 158 159 public void testCreateConfigurationWithValueThatIsIntercepted() 160 throws Exception 161 { 162 final SAXConfigurationHandler handler = new MockSAXConfigurationHandler(); 163 final String qName = "myElement"; 164 final String value = "value"; 165 handler.startElement( "", "", qName, new AttributesImpl() ); 166 handler.characters( value.toCharArray(), 0, value.length() ); 167 handler.endElement( "", "", qName ); 168 final Configuration configuration = handler.getConfiguration(); 169 assertEquals( "configuration.name", qName, configuration.getName() ); 170 assertEquals( "configuration.location", "", configuration.getLocation() ); 171 assertEquals( "configuration.path", "", configuration.getPath() ); 172 assertEquals( "configuration.value", MockSAXConfigurationHandler.REPLACEMENT, 173 configuration.getValue() ); 174 } 175 176 public void testCreateConfigurationWithValueInMultipleFragments() 177 throws Exception 178 { 179 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 180 final String qName = "myElement"; 181 final String value = "value"; 182 handler.startElement( "", "", qName, new AttributesImpl() ); 183 handler.characters( value.toCharArray(), 0, value.length() ); 184 handler.characters( value.toCharArray(), 0, value.length() ); 185 handler.endElement( "", "", qName ); 186 final Configuration configuration = handler.getConfiguration(); 187 assertEquals( "configuration.name", qName, configuration.getName() ); 188 assertEquals( "configuration.location", "", configuration.getLocation() ); 189 assertEquals( "configuration.path", "", configuration.getPath() ); 190 assertEquals( "configuration.value", 191 value + value, 192 configuration.getValue() ); 193 } 194 195 public void testCreateConfigurationWithChildElement() 196 throws Exception 197 { 198 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 199 final String qName = "myElement"; 200 final String childName = "myChild"; 201 handler.startElement( "", "", qName, new AttributesImpl() ); 202 handler.startElement( "", "", childName, new AttributesImpl() ); 203 handler.endElement( "", "", childName ); 204 handler.endElement( "", "", qName ); 205 206 final Configuration configuration = handler.getConfiguration(); 207 assertEquals( "configuration.name", qName, configuration.getName() ); 208 assertEquals( "configuration.location", "", configuration.getLocation() ); 209 assertEquals( "configuration.path", "", configuration.getPath() ); 210 final Configuration[] children = configuration.getChildren(); 211 assertEquals( "children.length", 1, children.length ); 212 assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() ); 213 assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() ); 214 assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() ); 215 } 216 217 public void testCreateConfigurationWithDeepChildElements() 218 throws Exception 219 { 220 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 221 final String qName = "myElement"; 222 final String childName = "myChild"; 223 final String grandChildName = "myGrandChild"; 224 handler.startElement( "", "", qName, new AttributesImpl() ); 225 handler.startElement( "", "", childName, new AttributesImpl() ); 226 handler.startElement( "", "", grandChildName, new AttributesImpl() ); 227 handler.endElement( "", "", grandChildName ); 228 handler.endElement( "", "", childName ); 229 handler.endElement( "", "", qName ); 230 231 final Configuration configuration = handler.getConfiguration(); 232 assertEquals( "configuration.name", qName, configuration.getName() ); 233 assertEquals( "configuration.location", "", configuration.getLocation() ); 234 assertEquals( "configuration.path", "", configuration.getPath() ); 235 final Configuration[] children = configuration.getChildren(); 236 assertEquals( "children.length", 1, children.length ); 237 assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() ); 238 assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() ); 239 assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() ); 240 final Configuration[] grandChildren = children[ 0 ].getChildren(); 241 assertEquals( "grandChildren.length", 1, grandChildren.length ); 242 assertEquals( "grandChildren[ 0 ].name", grandChildName, grandChildren[ 0 ].getName() ); 243 assertEquals( "grandChildren[ 0 ].location", "", grandChildren[ 0 ].getLocation() ); 244 assertEquals( "grandChildren[ 0 ].path", "myElement/myChild", grandChildren[ 0 ].getPath() ); 245 } 246 247 248 public void testCreateConfigurationWithChildElementContaingContent() 249 throws Exception 250 { 251 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 252 final String qName = "myElement"; 253 final String childName = "myChild"; 254 final String value = "value"; 255 handler.startElement( "", "", qName, new AttributesImpl() ); 256 handler.startElement( "", "", childName, new AttributesImpl() ); 257 handler.characters( value.toCharArray(), 0, value.length() ); 258 handler.endElement( "", "", childName ); 259 handler.endElement( "", "", qName ); 260 261 final Configuration configuration = handler.getConfiguration(); 262 assertEquals( "configuration.name", qName, configuration.getName() ); 263 assertEquals( "configuration.location", "", configuration.getLocation() ); 264 assertEquals( "configuration.path", "", configuration.getPath() ); 265 final Configuration[] children = configuration.getChildren(); 266 assertEquals( "children.length", 1, children.length ); 267 assertEquals( "children[ 0 ].name", childName, children[ 0 ].getName() ); 268 assertEquals( "children[ 0 ].location", "", children[ 0 ].getLocation() ); 269 assertEquals( "children[ 0 ].path", qName, children[ 0 ].getPath() ); 270 assertEquals( "children[ 0 ].value", value, children[ 0 ].getValue() ); 271 } 272 273 public void testCreateConfigurationWithMixedContent() 274 throws Exception 275 { 276 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 277 final String qName = "myElement"; 278 final String childName = "myChild"; 279 final String value = "value"; 280 handler.startElement( "", "", qName, new AttributesImpl() ); 281 handler.characters( value.toCharArray(), 0, value.length() ); 282 handler.startElement( "", "", childName, new AttributesImpl() ); 283 handler.endElement( "", "", childName ); 284 try 285 { 286 handler.endElement( "", "", qName ); 287 } 288 catch( SAXException e ) 289 { 290 return; 291 } 292 fail( "Expected to fail handling sax events as mixed content" ); 293 } 294 295 public void testClearHandler() 296 throws Exception 297 { 298 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 299 //TODO: This is a really bad unit test - should test internal state 300 handler.clear(); 301 } 302 303 public void testCreateConfigurationContainingEmptySeparator() 304 throws Exception 305 { 306 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 307 final String qName = "myElement"; 308 final String value = " \n \t"; 309 handler.startElement( "", "", qName, new AttributesImpl() ); 310 handler.characters( value.toCharArray(), 0, value.length() ); 311 handler.endElement( "", "", qName ); 312 final Configuration configuration = handler.getConfiguration(); 313 assertEquals( "configuration.name", qName, configuration.getName() ); 314 assertEquals( "configuration.location", "", configuration.getLocation() ); 315 assertEquals( "configuration.path", "", configuration.getPath() ); 316 assertEquals( "configuration.value", null, configuration.getValue( null ) ); 317 } 318 319 public void testCreateConfigurationWithAttributes() 320 throws Exception 321 { 322 final SAXConfigurationHandler handler = new SAXConfigurationHandler(); 323 final String qName = "myElement"; 324 final AttributesImpl attributes = new AttributesImpl(); 325 attributes.addAttribute( "", "", "key", "CDATA", "value" ); 326 handler.startElement( "", "", qName, attributes ); 327 handler.endElement( "", "", qName ); 328 final Configuration configuration = handler.getConfiguration(); 329 assertEquals( "configuration.name", qName, configuration.getName() ); 330 assertEquals( "configuration.location", "", configuration.getLocation() ); 331 assertEquals( "configuration.path", "", configuration.getPath() ); 332 final String[] names = configuration.getAttributeNames(); 333 assertEquals( "names.length", 1, names.length ); 334 assertEquals( "names[0]", "key", names[ 0 ] ); 335 assertEquals( "configuration.getAttribute( names[ 0 ] )", 336 "value", configuration.getAttribute( names[ 0 ] ) ); 337 } 338 339 public void testCreateConfigurationWithAttributesWithInterception() 340 throws Exception 341 { 342 final SAXConfigurationHandler handler = new MockSAXConfigurationHandler(); 343 final String qName = "myElement"; 344 final AttributesImpl attributes = new AttributesImpl(); 345 attributes.addAttribute( "", "", "key", "CDATA", "value" ); 346 handler.startElement( "", "", qName, attributes ); 347 handler.endElement( "", "", qName ); 348 final Configuration configuration = handler.getConfiguration(); 349 assertEquals( "configuration.name", qName, configuration.getName() ); 350 assertEquals( "configuration.location", "", configuration.getLocation() ); 351 assertEquals( "configuration.path", "", configuration.getPath() ); 352 final String[] names = configuration.getAttributeNames(); 353 assertEquals( "names.length", 1, names.length ); 354 assertEquals( "names[0]", "key", names[ 0 ] ); 355 assertEquals( "configuration.getAttribute( names[ 0 ] )", 356 MockSAXConfigurationHandler.REPLACEMENT, 357 configuration.getAttribute( names[ 0 ] ) ); 358 } 359 }

This page was automatically generated by Maven