1 17 package org.apache.excalibur.xml.dom.test; 18 19 import java.io.StringReader ; 20 21 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase; 22 import org.apache.avalon.framework.component.ComponentException; 23 import org.apache.excalibur.xml.dom.DOMHandler; 24 import org.apache.excalibur.xml.dom.DOMHandlerFactory; 25 import org.apache.excalibur.xml.sax.SAXParser; 26 import org.w3c.dom.Document ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.Node ; 29 import org.xml.sax.InputSource ; 30 31 34 public class DefaultDOMHandlerFactoryTestCase extends ExcaliburTestCase 35 { 36 37 private static final String CONTENT = 38 "<?xml version=\"1.0\"?>" + 39 "<test:root xmlns:test=\"http://localhost/test\">" + 40 "<test:element1/>" + 41 "<test:element2/>" + 42 "</test:root>"; 43 private static final StringReader IN = new StringReader ( CONTENT ); 44 45 public DefaultDOMHandlerFactoryTestCase( String name ) 46 { 47 super( name ); 48 } 49 50 public void testCreateDOMHandler() 51 { 52 try 53 { 54 final SAXParser parser = (SAXParser)manager.lookup( SAXParser.ROLE ); 55 final DOMHandlerFactory handlerFactory = (DOMHandlerFactory)manager.lookup( DOMHandlerFactory.ROLE ); 56 57 final DOMHandler handler = handlerFactory.createDOMHandler(); 58 parser.parse( new InputSource ( IN ), handler ); 59 final Document document = handler.getDocument(); 60 61 final Element root = document.getDocumentElement(); 62 assertEquals( "Wrong root element", "test:root", root.getNodeName() ); 63 assertEquals( "Wrong namespace uri", "http://localhost/test", root.getNamespaceURI() ); 64 65 final Node element1 = root.getFirstChild(); 66 assertEquals( "Child is not an element", Document.ELEMENT_NODE, element1.getNodeType() ); 67 assertEquals( "Wrong first element", "test:element1", element1.getNodeName() ); 68 69 final Node element2 = root.getLastChild(); 70 assertEquals( "Child is not an element", Document.ELEMENT_NODE, element2.getNodeType() ); 71 assertEquals( "Wrong last element", "test:element2", element2.getNodeName() ); 72 } 73 catch ( ComponentException e ) 74 { 75 fail( "Failed to lookup components: " + e.getMessage() ); 76 } 77 catch ( Exception e ) 78 { 79 fail( "Failed to create handler: " + e.getMessage() ); 80 } 81 } 82 83 } 84 | Popular Tags |