KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > configuration > test > SAXConfigurationHandlerTestCase


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Apache Avalon", "Avalon Excalibur", "Avalon
26     Framework" and "Apache Software Foundation" must not be used to endorse
27     or promote products derived from this software without prior written
28     permission. For written permission, please contact apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation and was originally created by
47  Stefano Mazzocchi <stefano@apache.org>. For more information on the Apache
48  Software Foundation, please see <http://www.apache.org/>.
49  
50 */

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 JavaDoc;
59
60 /**
61  * Test the basic public methods of SAXConfigurationHandlerTestCase.
62  *
63  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
64  */

65 public final class SAXConfigurationHandlerTestCase extends TestCase
66 {
67     public SAXConfigurationHandlerTestCase()
68     {
69         this("SAXConfigurationHandler Test Case ");
70     }
71
72     public SAXConfigurationHandlerTestCase( final String JavaDoc name )
73     {
74         super( name );
75     }
76
77     /**
78      * Test the ContentHandler. The XML created should look like this:
79      *
80      * <pre>
81      * &lt;rawName attqName="attValue"&gt;
82      * &lt;child:localName xmlns:child="namespaceURI"&gt;value&lt;/child:localName&gt;
83      * &lt;emptyElement/&gt;
84      * &lt;/rawName&gt;
85      * </pre>
86      */

87     public void testDefaultHandling() throws Exception JavaDoc
88     {
89         SAXConfigurationHandler handler = new SAXConfigurationHandler( );
90
91         final String JavaDoc rootURI = "";
92         final String JavaDoc rootlocal = "rawName";
93         final String JavaDoc rootraw = "rawName";
94         final String JavaDoc childURI = "namespaceURI";
95         final String JavaDoc childlocal = "localName";
96         final String JavaDoc childraw = "child:" + childlocal;
97         final String JavaDoc childvalue = "value";
98         final String JavaDoc attqName = "attqName";
99         final String JavaDoc attValue = "attValue";
100         final String JavaDoc emptylocal = "emptyElement";
101         final String JavaDoc emptyraw = emptylocal;
102         
103         final AttributesImpl JavaDoc emptyAttributes = new AttributesImpl JavaDoc();
104
105         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
106         attributes.addAttribute("",attqName,attqName,
107                                 "CDATA",attValue);
108
109         final AttributesImpl JavaDoc childAttributes = new AttributesImpl JavaDoc();
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 JavaDoc
137     {
138         SAXConfigurationHandler handler = new NamespacedSAXConfigurationHandler( );
139
140         final String JavaDoc rootURI = "";
141         final String JavaDoc rootlocal = "rawName";
142         final String JavaDoc rootraw = "rawName";
143         final String JavaDoc childURI = "namespaceURI";
144         final String JavaDoc childlocal = "localName";
145         final String JavaDoc childraw = "child:" + childlocal;
146         final String JavaDoc childvalue = "value";
147         final String JavaDoc attqName = "attqName";
148         final String JavaDoc attValue = "attValue";
149
150         final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
151         attributes.addAttribute("",attqName,attqName,
152                                 "CDATA",attValue);
153
154         final AttributesImpl JavaDoc childAttributes = new AttributesImpl JavaDoc();
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