KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > parameters > test > ParameterTestCase


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.parameters.test;
52
53 import java.io.ByteArrayInputStream JavaDoc;
54 import java.util.Properties JavaDoc;
55
56 import junit.framework.TestCase;
57
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.apache.avalon.framework.parameters.ParameterException;
62 import org.apache.avalon.framework.parameters.Parameters;
63
64 /**
65  * TestCase for Parameter.
66  * FIXME: Write messages for each assertion.
67  * Writing message in English is very difficult for me :-(.
68  *
69  * @author <a HREF="mailto:colus@isoft.co.kr">Eung-ju Park</a>
70  */

71 public class ParameterTestCase
72     extends TestCase
73 {
74     private static final String JavaDoc EOL = "\n";
75
76     public ParameterTestCase( final String JavaDoc name )
77     {
78         super( name );
79     }
80
81     public void testRemoveParameter()
82     {
83         final Parameters parameters = new Parameters();
84         parameters.setParameter( "key1", "value1" );
85         assertEquals( 1, parameters.getNames().length );
86         parameters.setParameter( "key1", null );
87         assertTrue( ! parameters.isParameter( "key1" ) );
88         assertEquals( 0, parameters.getNames().length );
89     }
90
91     public void testIsParameter()
92     {
93         final Parameters parameters = new Parameters();
94         parameters.setParameter( "key1", "value1" );
95         assertTrue( parameters.isParameter( "key1" ) );
96         assertTrue( ! parameters.isParameter( "key2" ) );
97     }
98
99     public void testGetParameter()
100     {
101         final Parameters parameters = new Parameters();
102         parameters.setParameter( "key1", "value1" );
103
104         try
105         {
106             assertEquals( "value1", parameters.getParameter( "key1" ) );
107         }
108         catch ( final ParameterException pe )
109         {
110             fail( pe.getMessage() );
111         }
112
113         try
114         {
115             parameters.getParameter( "key2" );
116             fail( "Not inserted parameter 'key2' exists" );
117         }
118         catch( final ParameterException pe )
119         {
120             //OK
121
}
122
123         assertEquals( "value1", parameters.getParameter( "key1", "value1-1" ) );
124
125         assertEquals( "value2", parameters.getParameter( "key2", "value2" ) );
126     }
127
128     public void testFromConfiguration()
129     {
130         final ByteArrayInputStream JavaDoc confInput = new ByteArrayInputStream JavaDoc( (
131             "<?xml version=\"1.0\"?>" + EOL +
132             "<test>" + EOL +
133             "<parameter name=\"key1\" value=\"value1\"/>" + EOL +
134             "<parameter name=\"key2\" value=\"value2\"/>" + EOL +
135             "<parameter name=\"key3\" value=\"value3\"/>" + EOL +
136             "</test>" ).getBytes() );
137
138         try
139         {
140             final DefaultConfigurationBuilder builder =
141                 new DefaultConfigurationBuilder();
142             final Configuration configuration = builder.build( confInput );
143
144             final Parameters parameters =
145                 Parameters.fromConfiguration( configuration );
146
147             assertEquals( "value1", parameters.getParameter( "key1" ) );
148             assertEquals( "value2", parameters.getParameter( "key2" ) );
149             assertEquals( "value3", parameters.getParameter( "key3" ) );
150         }
151         catch ( final ConfigurationException ce )
152         {
153             fail( "Converting failed: " + ce.getMessage() );
154         }
155         catch ( final Exception JavaDoc e )
156         {
157             fail( e.getMessage() );
158         }
159     }
160
161     public void testFromProperties()
162     {
163         final Properties JavaDoc properties = new Properties JavaDoc();
164         properties.put( "key1", "value1" );
165         properties.put( "key2", "value2" );
166         properties.put( "key3", "value3" );
167
168         final Parameters parameters = Parameters.fromProperties( properties );
169
170         try
171         {
172             assertEquals( "value1", parameters.getParameter( "key1" ) );
173             assertEquals( "value2", parameters.getParameter( "key2" ) );
174             assertEquals( "value3", parameters.getParameter( "key3" ) );
175         }
176         catch ( final ParameterException pe )
177         {
178             fail( pe.getMessage() );
179         }
180     }
181 }
182
Popular Tags