1 51 package org.apache.avalon.framework.configuration.test; 52 53 import junit.framework.TestCase; 54 55 import org.apache.avalon.framework.configuration.Configuration; 56 import org.apache.avalon.framework.configuration.NamespacedSAXConfigurationHandler; 57 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 58 import org.xml.sax.helpers.AttributesImpl ; 59 60 65 public final class SAXConfigurationHandlerTestCase extends TestCase 66 { 67 public SAXConfigurationHandlerTestCase() 68 { 69 this("SAXConfigurationHandler Test Case "); 70 } 71 72 public SAXConfigurationHandlerTestCase( final String name ) 73 { 74 super( name ); 75 } 76 77 87 public void testDefaultHandling() throws Exception 88 { 89 SAXConfigurationHandler handler = new SAXConfigurationHandler( ); 90 91 final String rootURI = ""; 92 final String rootlocal = "rawName"; 93 final String rootraw = "rawName"; 94 final String childURI = "namespaceURI"; 95 final String childlocal = "localName"; 96 final String childraw = "child:" + childlocal; 97 final String childvalue = "value"; 98 final String attqName = "attqName"; 99 final String attValue = "attValue"; 100 final String emptylocal = "emptyElement"; 101 final String emptyraw = emptylocal; 102 103 final AttributesImpl emptyAttributes = new AttributesImpl (); 104 105 final AttributesImpl attributes = new AttributesImpl (); 106 attributes.addAttribute("",attqName,attqName, 107 "CDATA",attValue); 108 109 final AttributesImpl childAttributes = new AttributesImpl (); 110 childAttributes.addAttribute("", "child", "xmlns:child", "CDATA", childURI); 111 112 handler.startDocument(); 113 handler.startPrefixMapping( "child", childURI ); 114 handler.startElement( rootURI, rootlocal, rootraw, attributes ); 115 handler.startElement( childURI, 116 childlocal, 117 childraw, 118 childAttributes ); 119 120 handler.characters( childvalue.toCharArray(), 0, childvalue.length() ); 121 handler.endElement( childURI, childlocal, childraw ); 122 handler.startElement( rootURI, emptylocal, emptyraw, emptyAttributes ); 123 handler.endElement( rootURI, emptylocal, emptyraw ); 124 handler.endElement( null, null, rootraw); 125 handler.endPrefixMapping( "child" ); 126 handler.endDocument(); 127 128 final Configuration configuration = handler.getConfiguration(); 129 assertEquals( attValue, configuration.getAttribute(attqName)); 130 assertEquals( childvalue, configuration.getChild(childraw).getValue()); 131 assertEquals( "", configuration.getChild(childraw).getNamespace() ); 132 assertEquals( rootraw, configuration.getName()); 133 assertEquals( "test", configuration.getChild(emptyraw).getValue( "test" ) ); 134 } 135 136 public void testNamespaceHandling() throws Exception 137 { 138 SAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler( ); 139 140 final String rootURI = ""; 141 final String rootlocal = "rawName"; 142 final String rootraw = "rawName"; 143 final String childURI = "namespaceURI"; 144 final String childlocal = "localName"; 145 final String childraw = "child:" + childlocal; 146 final String childvalue = "value"; 147 final String attqName = "attqName"; 148 final String attValue = "attValue"; 149 150 final AttributesImpl attributes = new AttributesImpl (); 151 attributes.addAttribute("",attqName,attqName, 152 "CDATA",attValue); 153 154 final AttributesImpl childAttributes = new AttributesImpl (); 155 childAttributes.addAttribute("", "child", "xmlns:child", "CDATA", childURI); 156 157 handler.startDocument(); 158 handler.startPrefixMapping( "child", childURI ); 159 handler.startElement( rootURI, rootlocal, rootraw, attributes ); 160 handler.startElement( childURI, 161 childlocal, 162 childraw, 163 childAttributes ); 164 165 handler.characters( childvalue.toCharArray(), 0, childvalue.length() ); 166 handler.endElement( childURI, childlocal, childraw ); 167 handler.endElement( null, null, rootraw); 168 handler.endPrefixMapping( "child" ); 169 handler.endDocument(); 170 171 final Configuration configuration = handler.getConfiguration(); 172 assertEquals( attValue, configuration.getAttribute(attqName)); 173 assertEquals( childvalue, configuration.getChild(childlocal).getValue()); 174 assertEquals( childURI, configuration.getChild(childlocal).getNamespace() ); 175 assertEquals( rootraw, configuration.getName()); 176 } 177 } 178 179 180 181 182 183 | Popular Tags |