KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > dom > test > DefaultDOMHandlerFactoryTestCase


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.xml.dom.test;
18
19 import java.io.StringReader JavaDoc;
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 JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30
31 /**
32  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33  */

34 public class DefaultDOMHandlerFactoryTestCase extends ExcaliburTestCase
35 {
36     
37     private static final String JavaDoc 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 JavaDoc IN = new StringReader JavaDoc( CONTENT );
44     
45     public DefaultDOMHandlerFactoryTestCase( String JavaDoc 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 JavaDoc( IN ), handler );
59             final Document JavaDoc document = handler.getDocument();
60             
61             final Element JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc e )
78         {
79             fail( "Failed to create handler: " + e.getMessage() );
80         }
81     }
82     
83 }
84
Popular Tags