1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License")
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.configuration;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import junit.framework.Assert;
23  
24  /***
25   * Pulling out the calls to do the tests so both JUnit and Cactus tests 
26   * can share.
27   * 
28   * @version $Id: NonStringTestHolder.java 155408 2005-02-26 12:56:39Z dirkv $
29   */
30  public class NonStringTestHolder
31  {
32      private Configuration configuration;
33  
34      public void setConfiguration(Configuration configuration)
35      {
36          this.configuration = configuration;
37      }
38  
39      public void testBoolean() throws Exception
40      {
41          boolean booleanValue = configuration.getBoolean("test.boolean");
42          Assert.assertEquals(true, booleanValue);
43          Assert.assertEquals(1, configuration.getList("test.boolean").size());        
44      }
45  
46      public void testBooleanDefaultValue() throws Exception
47      {
48          boolean booleanValue = configuration.getBoolean("test.boolean.missing", true);
49          Assert.assertEquals(true, booleanValue);
50  
51          Boolean booleanObject = configuration.getBoolean("test.boolean.missing", new Boolean(true));
52          Assert.assertEquals(new Boolean(true), booleanObject);
53      }
54  
55      public void testByte() throws Exception
56      {
57          byte testValue = 10;
58          byte byteValue = configuration.getByte("test.byte");
59          Assert.assertEquals(testValue, byteValue);
60          Assert.assertEquals(1, configuration.getList("test.byte").size());
61      }
62  
63      public void testDouble() throws Exception
64      {
65          double testValue = 10.25;
66          double doubleValue = configuration.getDouble("test.double");
67          Assert.assertEquals(testValue, doubleValue, 0.01);
68          Assert.assertEquals(1, configuration.getList("test.double").size());
69      }
70  
71      public void testDoubleDefaultValue() throws Exception
72      {
73          double testValue = 10.25;
74          double doubleValue = configuration.getDouble("test.double.missing", 10.25);
75  
76          Assert.assertEquals(testValue, doubleValue, 0.01);
77      }
78  
79      public void testFloat() throws Exception
80      {
81          float testValue = (float) 20.25;
82          float floatValue = configuration.getFloat("test.float");
83          Assert.assertEquals(testValue, floatValue, 0.01);
84          Assert.assertEquals(1, configuration.getList("test.float").size());       
85      }
86  
87      public void testFloatDefaultValue() throws Exception
88      {
89          float testValue = (float) 20.25;
90          float floatValue = configuration.getFloat("test.float.missing", testValue);
91          Assert.assertEquals(testValue, floatValue, 0.01);
92      }
93  
94      public void testInteger() throws Exception
95      {
96          int intValue = configuration.getInt("test.integer");
97          Assert.assertEquals(10, intValue);
98          Assert.assertEquals(1, configuration.getList("test.integer").size());
99      }
100 
101     public void testIntegerDefaultValue() throws Exception
102     {
103         int intValue = configuration.getInt("test.integer.missing", 10);
104         Assert.assertEquals(10, intValue);
105     }
106 
107     public void testLong() throws Exception
108     {
109         long longValue = configuration.getLong("test.long");
110         Assert.assertEquals(1000000, longValue);
111         Assert.assertEquals(1, configuration.getList("test.long").size());
112     }
113     public void testLongDefaultValue() throws Exception
114     {
115         long longValue = configuration.getLong("test.long.missing", 1000000);
116         Assert.assertEquals(1000000, longValue);
117     }
118 
119     public void testShort() throws Exception
120     {
121         short shortValue = configuration.getShort("test.short");
122         Assert.assertEquals(1, shortValue);
123         Assert.assertEquals(1, configuration.getList("test.short").size());
124     }
125 
126     public void testShortDefaultValue() throws Exception
127     {
128         short shortValue = configuration.getShort("test.short.missing", (short) 1);
129         Assert.assertEquals(1, shortValue);
130     }
131 
132     public void testListMissing() throws Exception
133     {
134         List list = configuration.getList("missing.list");
135         Assert.assertTrue("'missing.list' is not empty", list.isEmpty());
136     }
137 
138     public void testSubset() throws Exception
139     {
140         Configuration subset = configuration.subset("test");
141 
142         // search the "short" key in the subset using the key iterator
143         boolean foundKeyValue = false;
144         Iterator it = subset.getKeys();
145         while (it.hasNext() && !foundKeyValue)
146         {
147             String key = (String) it.next();
148             foundKeyValue = "short".equals(key);
149         }
150 
151         Assert.assertTrue("'short' key not found in the subset key iterator", foundKeyValue);
152     }
153 
154     public void testIsEmpty() throws Exception
155     {
156         Assert.assertTrue("Configuration should not be empty", !configuration.isEmpty());
157     }
158 
159 }