001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.configuration;
019:
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import junit.framework.Assert;
024:
025: /**
026: * Pulling out the calls to do the tests so both JUnit and Cactus tests
027: * can share.
028: *
029: * @version $Id: NonStringTestHolder.java 439648 2006-09-02 20:42:10Z oheger $
030: */
031: public class NonStringTestHolder {
032: private Configuration configuration;
033:
034: public void setConfiguration(Configuration configuration) {
035: this .configuration = configuration;
036: }
037:
038: public void testBoolean() throws Exception {
039: boolean booleanValue = configuration.getBoolean("test.boolean");
040: Assert.assertEquals(true, booleanValue);
041: Assert.assertEquals(1, configuration.getList("test.boolean")
042: .size());
043: }
044:
045: public void testBooleanDefaultValue() throws Exception {
046: boolean booleanValue = configuration.getBoolean(
047: "test.boolean.missing", true);
048: Assert.assertEquals(true, booleanValue);
049:
050: Boolean booleanObject = configuration.getBoolean(
051: "test.boolean.missing", new Boolean(true));
052: Assert.assertEquals(new Boolean(true), booleanObject);
053: }
054:
055: public void testByte() throws Exception {
056: byte testValue = 10;
057: byte byteValue = configuration.getByte("test.byte");
058: Assert.assertEquals(testValue, byteValue);
059: Assert.assertEquals(1, configuration.getList("test.byte")
060: .size());
061: }
062:
063: public void testDouble() throws Exception {
064: double testValue = 10.25;
065: double doubleValue = configuration.getDouble("test.double");
066: Assert.assertEquals(testValue, doubleValue, 0.01);
067: Assert.assertEquals(1, configuration.getList("test.double")
068: .size());
069: }
070:
071: public void testDoubleDefaultValue() throws Exception {
072: double testValue = 10.25;
073: double doubleValue = configuration.getDouble(
074: "test.double.missing", 10.25);
075:
076: Assert.assertEquals(testValue, doubleValue, 0.01);
077: }
078:
079: public void testFloat() throws Exception {
080: float testValue = (float) 20.25;
081: float floatValue = configuration.getFloat("test.float");
082: Assert.assertEquals(testValue, floatValue, 0.01);
083: Assert.assertEquals(1, configuration.getList("test.float")
084: .size());
085: }
086:
087: public void testFloatDefaultValue() throws Exception {
088: float testValue = (float) 20.25;
089: float floatValue = configuration.getFloat("test.float.missing",
090: testValue);
091: Assert.assertEquals(testValue, floatValue, 0.01);
092: }
093:
094: public void testInteger() throws Exception {
095: int intValue = configuration.getInt("test.integer");
096: Assert.assertEquals(10, intValue);
097: Assert.assertEquals(1, configuration.getList("test.integer")
098: .size());
099: }
100:
101: public void testIntegerDefaultValue() throws Exception {
102: int intValue = configuration.getInt("test.integer.missing", 10);
103: Assert.assertEquals(10, intValue);
104: }
105:
106: public void testLong() throws Exception {
107: long longValue = configuration.getLong("test.long");
108: Assert.assertEquals(1000000, longValue);
109: Assert.assertEquals(1, configuration.getList("test.long")
110: .size());
111: }
112:
113: public void testLongDefaultValue() throws Exception {
114: long longValue = configuration.getLong("test.long.missing",
115: 1000000);
116: Assert.assertEquals(1000000, longValue);
117: }
118:
119: public void testShort() throws Exception {
120: short shortValue = configuration.getShort("test.short");
121: Assert.assertEquals(1, shortValue);
122: Assert.assertEquals(1, configuration.getList("test.short")
123: .size());
124: }
125:
126: public void testShortDefaultValue() throws Exception {
127: short shortValue = configuration.getShort("test.short.missing",
128: (short) 1);
129: Assert.assertEquals(1, shortValue);
130: }
131:
132: public void testListMissing() throws Exception {
133: List list = configuration.getList("missing.list");
134: Assert
135: .assertTrue("'missing.list' is not empty", list
136: .isEmpty());
137: }
138:
139: public void testSubset() throws Exception {
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: String key = (String) it.next();
147: foundKeyValue = "short".equals(key);
148: }
149:
150: Assert.assertTrue(
151: "'short' key not found in the subset key iterator",
152: foundKeyValue);
153: }
154:
155: public void testIsEmpty() throws Exception {
156: Assert.assertTrue("Configuration should not be empty",
157: !configuration.isEmpty());
158: }
159:
160: }
|