KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.ByteArrayInputStream JavaDoc;
54 import java.io.File JavaDoc;
55 import java.io.FileWriter JavaDoc;
56 import java.io.InputStream JavaDoc;
57 import junit.framework.TestCase;
58 import org.apache.avalon.framework.configuration.Configuration;
59 import org.apache.avalon.framework.configuration.ConfigurationException;
60 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
61 import org.xml.sax.SAXException JavaDoc;
62
63 /**
64  * Test that the <code>Configuration</code>s built by
65  * <code>DefaultConfigurationBuilder</code> meet the stated API contracts.
66  *
67  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
68  * @author <a HREF="mailto:rantene@hotmail.com">Ran Tene</a>
69  * @author <a HREF="mailto:leo.sutic@inspireinfrastructure.com">Leo Sutic</a>
70  */

71 public final class DefaultConfigurationBuilderTestCase
72     extends TestCase
73 {
74
75     private DefaultConfigurationBuilder m_builder;
76     private DefaultConfigurationBuilder m_nsBuilder;
77     private File JavaDoc m_file;
78     private File JavaDoc m_nsFile;
79     private File JavaDoc m_testDirectory;
80     private final String JavaDoc m_simpleFileName = "config_simple.xml";
81     private final String JavaDoc m_nsFileName = "config_namespaces.xml";
82     private final String JavaDoc m_path = "test/framework/io/";
83
84     private final String JavaDoc simpleXML =
85     "<?xml version=\"1.0\" ?>"+
86     "<config boolAttr=\"true\" floatAttr=\"1.32\">"+
87     " <elements-a>"+
88     " <element name=\"a\"/>"+
89     " </elements-a>"+
90     " <elements-b>"+
91     " <element name=\"b\"/> "+
92     " </elements-b>"+
93     " <elements-b type=\"type-b\"/>"+
94     " <elements-c>"+
95     " true"+
96     " </elements-c>"+
97     "</config>";
98
99     /**
100      * These assertions apply when the default builder is used to create a
101      * Configuration from <code>simpleXML</code>, ie namespace
102      * support is disabled.
103      */

104     private void simpleAssertions(Configuration conf)
105         throws ConfigurationException
106     {
107         assertEquals( "config", conf.getName() );
108         assertEquals( "getNamespace() should default to \"\"", "", conf.getNamespace() );
109         try {
110             String JavaDoc value = conf.getValue();
111             fail( "Should throw a ConfigurationException, as this element"+
112                     "contains child elements, not a value" );
113         } catch ( ConfigurationException e )
114         {}
115
116         Configuration[] children;
117         children = conf.getChildren();
118         assertEquals( 4, children.length );
119         assertEquals( "elements-a", children[0].getName() );
120         assertEquals( "elements-b", children[1].getName() );
121         assertEquals( "b", children[1].getChild("element", false).getAttribute("name") );
122         assertEquals( "elements-b", children[2].getName() );
123         assertEquals( "elements-c", children[3].getName() );
124
125         final String JavaDoc[] attrNames = conf.getAttributeNames();
126         assertEquals( 2, attrNames.length );
127         assertEquals( "default", conf.getAttribute("nonexistent", "default") );
128         assertEquals( true, conf.getAttributeAsBoolean("boolAttr") );
129         assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 );
130
131         // Check that the auto-node-creation feature is working correctly.
132
assertEquals(
133                      "When a non-existent child is requested, a blank node should be created",
134                      "nonexistent",
135                      conf.getChild( "nonexistent" ).getName()
136                      );
137         assertEquals(
138                      "When a non-existent child is requested, a blank node should be created",
139                      "baz",
140                      conf.getChild( "foo" ).getChild("bar").getChild("baz").getName()
141                      );
142         try {
143             String JavaDoc value = conf.getChild("nonexistent").getValue();
144             fail( "Auto-created child nodes should not have a value" );
145         } catch ( ConfigurationException e )
146         {}
147         assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false )
148                     );
149         assertEquals( "Standard getChild() lookup failed", "elements-b", conf.getChild( "elements-b", false ).getName() );
150         assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) );
151         assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length );
152     }
153
154     private final String JavaDoc nsXML =
155     "<?xml version=\"1.0\" ?>"+
156     "<conf:config"+
157     " boolAttr=\"true\" floatAttr=\"1.32\""+
158     " xmlns:conf=\"http://conf.com\" xmlns:a=\"http://a.com\" xmlns:b=\"http://b.com\" xmlns:c=\"http://c.com\" xmlns:d=\"http://d.com\" xmlns:e=\"http://e.com\">"+
159     " <a:elements-a>"+
160     " <c:element name=\"a\"/>"+
161     " </a:elements-a>"+
162     " <elements-b xmlns=\"http://defaultns.com\">"+
163     " <element name=\"b\"/> "+
164     " </elements-b>"+
165     " <b:elements-b type=\"type-b\"/>"+
166     " <elements-c>"+
167     " true"+
168     " </elements-c>"+
169     " <d:element>d:element</d:element>"+
170     " <e:element>e:element</e:element>"+
171     "</conf:config>";
172     /*
173     "<?xml version=\"1.0\"?>"+
174     "<my-system version='1.3' xmlns:doc=\"http://myco.com/documentation\">"+
175     " <doc:desc>This is a highly fictitious config file</doc:desc>"+
176     " <widget name=\"fooWidget\" initOrder=\"1\" threadsafe=\"true\"/>"+
177     "</my-system>";
178     */

179
180     /**
181      * These assertions apply when the default builder is used to create a
182      * Configuration from <code>nsXML</code>, ie namespace support is disabled,
183      * but the XML uses namespaces.
184      */

185     private void simpleAssertionsNS(Configuration conf)
186         throws ConfigurationException
187     {
188         assertEquals( "conf:config", conf.getName() );
189         assertEquals( "getNamespace() should default to \"\"", "", conf.getNamespace() );
190         try {
191             String JavaDoc value = conf.getValue();
192             fail( "Should throw a ConfigurationException, as this element"+
193                     "contains child elements, not a value" );
194         } catch ( ConfigurationException e )
195         {}
196
197         Configuration[] children;
198         children = conf.getChildren();
199         assertEquals( 6, children.length );
200         assertEquals( "a:elements-a", children[0].getName() );
201         assertEquals( "elements-b", children[1].getName() );
202         assertEquals( "b", children[1].getChild("element", false).getAttribute("name") );
203         assertEquals( "b:elements-b", children[2].getName() );
204         assertEquals( "elements-c", children[3].getName() );
205
206         final String JavaDoc[] attrNames = conf.getAttributeNames();
207         assertEquals( 8, attrNames.length );
208         assertEquals( "true", conf.getAttribute("boolAttr", null) );
209         assertEquals( true, conf.getAttributeAsBoolean("boolAttr") );
210         assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 );
211         assertEquals( "http://conf.com", conf.getAttribute("xmlns:conf") );
212         assertEquals( "http://a.com", conf.getAttribute("xmlns:a") );
213         assertEquals( "http://b.com", conf.getAttribute("xmlns:b") );
214         assertEquals( "http://c.com", conf.getAttribute("xmlns:c") );
215
216         // Check that the auto-node-creation feature is working correctly.
217
assertEquals(
218                      "When a non-existent child is requested, a blank node should be created",
219                      "nonexistent",
220                      conf.getChild( "nonexistent" ).getName()
221                      );
222         assertEquals(
223                      "When a non-existent child is requested, a blank node should be created",
224                      "baz",
225                      conf.getChild( "foo" ).getChild("bar").getChild("baz").getName()
226                      );
227         try {
228             String JavaDoc value = conf.getChild("nonexistent").getValue();
229             fail( "Auto-created child nodes should not have a value" );
230         } catch ( ConfigurationException e )
231         {}
232         assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false )
233                     );
234         assertEquals( "Standard getChild() lookup failed", "b:elements-b", conf.getChild( "b:elements-b", false ).getName() );
235         assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) );
236         assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length );
237         
238         assertEquals( "d:element", conf.getChild("d:element").getValue() );
239         assertEquals( "e:element", conf.getChild("e:element").getValue() );
240     }
241
242
243     /**
244      * These assertions apply when the namespace-enabled builder is used to
245      * create a Configuration from <code>nsXML</code>, ie namespace support is
246      * enabled, and the XML uses namespaces.
247      */

248     private void nsAssertions(Configuration conf)
249         throws ConfigurationException
250     {
251         assertEquals( "config", conf.getName() );
252         assertEquals( "Namespace not set correctly", "http://conf.com", conf.getNamespace() );
253         try {
254             String JavaDoc value = conf.getValue();
255             fail( "Should throw a ConfigurationException, as this element"+
256                     "contains child elements, not a value" );
257         } catch ( ConfigurationException e )
258         {}
259
260         Configuration[] children;
261         children = conf.getChildren();
262         assertEquals( 6, children.length );
263         assertEquals( "elements-a", children[0].getName() );
264         assertEquals( "http://a.com", children[0].getNamespace() );
265         assertEquals( "elements-b", children[1].getName() );
266         assertEquals( "http://defaultns.com", children[1].getNamespace() );
267         assertEquals( "b", children[1].getChild("element", false).getAttribute("name") );
268         assertEquals( "elements-b", children[2].getName() );
269         assertEquals( "http://b.com", children[2].getNamespace() );
270         assertEquals( "elements-c", children[3].getName() );
271         assertEquals( "", children[3].getNamespace() );
272
273         final String JavaDoc[] attrNames = conf.getAttributeNames();
274         assertEquals( 2, attrNames.length ); // the other 4 are xmlns and so shouldn't appear
275
assertEquals( "true", conf.getAttribute("boolAttr", null) );
276         assertEquals( true, conf.getAttributeAsBoolean("boolAttr") );
277         assertEquals( (float)1.32, conf.getAttributeAsFloat("floatAttr"), 0.0 );
278
279         // Check that the auto-node-creation feature is working correctly.
280
assertEquals(
281                      "When a non-existent child is requested, a blank node should be created",
282                      "nonexistent",
283                      conf.getChild( "nonexistent" ).getName()
284                      );
285         assertEquals(
286                      "When a non-existent child is requested, a blank node should be created",
287                      "baz",
288                      conf.getChild( "foo" ).getChild("bar").getChild("baz").getName()
289                      );
290         try {
291             String JavaDoc value = conf.getChild("nonexistent").getValue();
292             fail( "Auto-created child nodes should not have a value" );
293         } catch ( ConfigurationException e )
294         {}
295         assertEquals( "Turning auto-node-creation off failed", null, conf.getChild( "nonexistent", false )
296                     );
297         assertEquals( "Standard getChild() lookup failed", "elements-b", conf.getChild( "elements-b", false ).getName() );
298         assertEquals( "Boolean value surrounded by whitespace failed", true, conf.getChild("elements-c").getValueAsBoolean( false ) );
299         assertEquals( "A value-containing element should have no child nodes", 0, conf.getChild("elements-c").getChildren().length );
300     }
301
302
303     public DefaultConfigurationBuilderTestCase()
304     {
305         this("DefaultConfigurationBuilder Test Case");
306     }
307
308     public DefaultConfigurationBuilderTestCase( final String JavaDoc name )
309     {
310         super( name );
311         m_testDirectory = (new File JavaDoc( m_path)).getAbsoluteFile();
312         if( !m_testDirectory.exists() )
313         {
314             m_testDirectory.mkdirs();
315         }
316     }
317
318     protected void setUp()
319         throws Exception JavaDoc
320     {
321         m_file = new File JavaDoc( m_testDirectory, m_simpleFileName );
322         m_nsFile = new File JavaDoc( m_testDirectory, m_nsFileName );
323         FileWriter JavaDoc writer = new FileWriter JavaDoc( m_file );
324         writer.write( simpleXML );
325         writer.close();
326         writer = new FileWriter JavaDoc( m_nsFile );
327         writer.write( nsXML );
328         writer.close();
329
330     }
331
332     protected void tearDown()
333         throws Exception JavaDoc
334     {
335         /*FileWriter writer = new FileWriter( m_file );
336         writer.write( "" );
337         writer.close();
338         writer = new FileWriter( m_nsFile );
339         writer.write( "" );
340         writer.close();*/

341         m_builder = null;
342         m_nsBuilder = null;
343     }
344
345
346     public void testBuildFromFileName()
347         throws Exception JavaDoc
348     {
349         m_builder = new DefaultConfigurationBuilder();
350         m_nsBuilder = new DefaultConfigurationBuilder(true); // switch on namespace support
351
Configuration conf = m_builder.buildFromFile( m_path + m_simpleFileName );
352         simpleAssertions( conf );
353         conf = m_builder.buildFromFile( m_path + m_nsFileName );
354         simpleAssertionsNS( conf );
355         conf = m_nsBuilder.buildFromFile( m_path + m_nsFileName );
356         nsAssertions( conf );
357     }
358
359     public void testBuildFromFile()
360         throws Exception JavaDoc
361     {
362         m_builder = new DefaultConfigurationBuilder();
363         m_nsBuilder = new DefaultConfigurationBuilder(true); // switch on namespace support
364
Configuration conf = m_builder.buildFromFile( m_file );
365         simpleAssertions( conf );
366         conf = m_builder.buildFromFile( m_nsFile );
367         simpleAssertionsNS( conf );
368         conf = m_nsBuilder.buildFromFile( m_nsFile );
369         nsAssertions( conf );
370     }
371
372     public void testBuild()
373         throws Exception JavaDoc
374     {
375         m_builder = new DefaultConfigurationBuilder();
376         m_nsBuilder = new DefaultConfigurationBuilder(true); // switch on namespace support
377
Configuration conf = m_builder.build( m_file.toURL().toString() );
378         simpleAssertions( conf );
379         conf = m_builder.buildFromFile( m_nsFile );
380         simpleAssertionsNS( conf );
381         conf = m_nsBuilder.buildFromFile( m_nsFile );
382         nsAssertions( conf );
383     }
384
385     private final String JavaDoc spaceTrimmingCheckXML =
386         "<?xml version=\"1.0\" ?>"+
387         " <config>"+
388         " <trimmed-item>\n"+
389         " value \n"+
390         " </trimmed-item>\n"+
391         " <preserved-item xml:space='preserve'>\n"+
392         " a space&#13; a CR, then a trailing space </preserved-item>\n"+
393         " <first-level-item xml:space='preserve'>\n"+
394         " <second-level-preserved> whitespace around </second-level-preserved>\n"+
395         " </first-level-item>\n"+
396         " <trimmed-again-item>\n"+
397         " value \n"+
398         " </trimmed-again-item>\n"+
399         "</config>";
400     
401     /**
402      * Checks that whitespace is normally stripped but preserved if
403      * space preserving processing instructions are present.
404      */

405     public void testSpaceTrimming()
406         throws Exception JavaDoc
407     {
408         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
409         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc( spaceTrimmingCheckXML.getBytes() );
410         Configuration conf = builder.build( in );
411         assertEquals( "Value is trimmed by default",
412                       "value",
413                       conf.getChild( "trimmed-item" ).getValue() );
414         assertEquals( "After trimming turned off value is preserved",
415                       "\n a space\r a CR, then a trailing space ",
416                       conf.getChild( "preserved-item" ).getValue() );
417         assertEquals( "Trimming two levels deep works too",
418                       " whitespace around ",
419                       conf.getChild( "first-level-item" )
420                       .getChild( "second-level-preserved" ).getValue() );
421         assertEquals( "Trimming turned back on",
422                       "value",
423                       conf.getChild( "trimmed-again-item" ).getValue() );
424     }
425
426     
427     private final String JavaDoc mixedContentXML =
428         "<?xml version=\"1.0\" ?>"+
429         "<a>a<a/></a>"
430         ;
431     public void testMixedContentDetection()
432         throws Exception JavaDoc
433     {
434         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
435         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc( mixedContentXML.getBytes() );
436         try
437         {
438             builder.build( in );
439             fail ("Must fail on mixed content");
440         } catch ( SAXException JavaDoc e )
441         {}
442     }
443 }
444
Popular Tags