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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.configuration;
20
import java.util.Iterator;
21
import java.util.List;
23
import junit.framework.Assert;
26
* Pulling out the calls to do the tests so both JUnit and Cactus tests
29
* @version $Id: NonStringTestHolder.java 1302002 2012-03-17 20:43:46Z sebb $
31
public class NonStringTestHolder
33
private Configuration configuration;
35
public void setConfiguration(Configuration configuration)
37
this.configuration = configuration;
40
public void testBoolean() throws Exception
42
boolean booleanValue = configuration.getBoolean("test.boolean");
43
Assert.assertTrue(booleanValue);
44
Assert.assertEquals(1, configuration.getList("test.boolean").size());
47
public void testBooleanDefaultValue() throws Exception
49
boolean booleanValue = configuration.getBoolean("test.boolean.missing", true);
50
Assert.assertTrue(booleanValue);
52
Boolean booleanObject = configuration.getBoolean("test.boolean.missing", new Boolean(true));
53
Assert.assertEquals(new Boolean(true), booleanObject);
56
public void testByte() throws Exception
59
byte byteValue = configuration.getByte("test.byte");
60
Assert.assertEquals(testValue, byteValue);
61
Assert.assertEquals(1, configuration.getList("test.byte").size());
64
public void testDouble() throws Exception
66
double testValue = 10.25;
67
double doubleValue = configuration.getDouble("test.double");
68
Assert.assertEquals(testValue, doubleValue, 0.01);
69
Assert.assertEquals(1, configuration.getList("test.double").size());
72
public void testDoubleDefaultValue() throws Exception
74
double testValue = 10.25;
75
double doubleValue = configuration.getDouble("test.double.missing", 10.25);
77
Assert.assertEquals(testValue, doubleValue, 0.01);
80
public void testFloat() throws Exception
82
float testValue = (float) 20.25;
83
float floatValue = configuration.getFloat("test.float");
84
Assert.assertEquals(testValue, floatValue, 0.01);
85
Assert.assertEquals(1, configuration.getList("test.float").size());
88
public void testFloatDefaultValue() throws Exception
90
float testValue = (float) 20.25;
91
float floatValue = configuration.getFloat("test.float.missing", testValue);
92
Assert.assertEquals(testValue, floatValue, 0.01);
95
public void testInteger() throws Exception
97
int intValue = configuration.getInt("test.integer");
98
Assert.assertEquals(10, intValue);
99
Assert.assertEquals(1, configuration.getList("test.integer").size());
102
public void testIntegerDefaultValue() throws Exception
104
int intValue = configuration.getInt("test.integer.missing", 10);
105
Assert.assertEquals(10, intValue);
108
public void testLong() throws Exception
110
long longValue = configuration.getLong("test.long");
111
Assert.assertEquals(1000000, longValue);
112
Assert.assertEquals(1, configuration.getList("test.long").size());
114
public void testLongDefaultValue() throws Exception
116
long longValue = configuration.getLong("test.long.missing", 1000000);
117
Assert.assertEquals(1000000, longValue);
120
public void testShort() throws Exception
122
short shortValue = configuration.getShort("test.short");
123
Assert.assertEquals(1, shortValue);
124
Assert.assertEquals(1, configuration.getList("test.short").size());
127
public void testShortDefaultValue() throws Exception
129
short shortValue = configuration.getShort("test.short.missing", (short) 1);
130
Assert.assertEquals(1, shortValue);
133
public void testListMissing() throws Exception
135
List<?> list = configuration.getList("missing.list");
136
Assert.assertTrue("'missing.list' is not empty", list.isEmpty());
139
public void testSubset() throws Exception
141
Configuration subset = configuration.subset("test");
143
// search the "short" key in the subset using the key iterator
144
boolean foundKeyValue = false;
145
Iterator<String> it = subset.getKeys();
146
while (it.hasNext() && !foundKeyValue)
148
String key = it.next();
149
foundKeyValue = "short".equals(key);
152
Assert.assertTrue("'short' key not found in the subset key iterator", foundKeyValue);
155
public void testIsEmpty() throws Exception
157
Assert.assertTrue("Configuration should not be empty", !configuration.isEmpty());