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