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 1302002 2012-03-17 20:43:46Z sebb $ 030 */ 031 public class NonStringTestHolder 032 { 033 private Configuration configuration; 034 035 public void setConfiguration(Configuration configuration) 036 { 037 this.configuration = configuration; 038 } 039 040 public void testBoolean() throws Exception 041 { 042 boolean booleanValue = configuration.getBoolean("test.boolean"); 043 Assert.assertTrue(booleanValue); 044 Assert.assertEquals(1, configuration.getList("test.boolean").size()); 045 } 046 047 public void testBooleanDefaultValue() throws Exception 048 { 049 boolean booleanValue = configuration.getBoolean("test.boolean.missing", true); 050 Assert.assertTrue(booleanValue); 051 052 Boolean booleanObject = configuration.getBoolean("test.boolean.missing", new Boolean(true)); 053 Assert.assertEquals(new Boolean(true), booleanObject); 054 } 055 056 public void testByte() throws Exception 057 { 058 byte testValue = 10; 059 byte byteValue = configuration.getByte("test.byte"); 060 Assert.assertEquals(testValue, byteValue); 061 Assert.assertEquals(1, configuration.getList("test.byte").size()); 062 } 063 064 public void testDouble() throws Exception 065 { 066 double testValue = 10.25; 067 double doubleValue = configuration.getDouble("test.double"); 068 Assert.assertEquals(testValue, doubleValue, 0.01); 069 Assert.assertEquals(1, configuration.getList("test.double").size()); 070 } 071 072 public void testDoubleDefaultValue() throws Exception 073 { 074 double testValue = 10.25; 075 double doubleValue = configuration.getDouble("test.double.missing", 10.25); 076 077 Assert.assertEquals(testValue, doubleValue, 0.01); 078 } 079 080 public void testFloat() throws Exception 081 { 082 float testValue = (float) 20.25; 083 float floatValue = configuration.getFloat("test.float"); 084 Assert.assertEquals(testValue, floatValue, 0.01); 085 Assert.assertEquals(1, configuration.getList("test.float").size()); 086 } 087 088 public void testFloatDefaultValue() throws Exception 089 { 090 float testValue = (float) 20.25; 091 float floatValue = configuration.getFloat("test.float.missing", testValue); 092 Assert.assertEquals(testValue, floatValue, 0.01); 093 } 094 095 public void testInteger() throws Exception 096 { 097 int intValue = configuration.getInt("test.integer"); 098 Assert.assertEquals(10, intValue); 099 Assert.assertEquals(1, configuration.getList("test.integer").size()); 100 } 101 102 public void testIntegerDefaultValue() throws Exception 103 { 104 int intValue = configuration.getInt("test.integer.missing", 10); 105 Assert.assertEquals(10, intValue); 106 } 107 108 public void testLong() throws Exception 109 { 110 long longValue = configuration.getLong("test.long"); 111 Assert.assertEquals(1000000, longValue); 112 Assert.assertEquals(1, configuration.getList("test.long").size()); 113 } 114 public void testLongDefaultValue() throws Exception 115 { 116 long longValue = configuration.getLong("test.long.missing", 1000000); 117 Assert.assertEquals(1000000, longValue); 118 } 119 120 public void testShort() throws Exception 121 { 122 short shortValue = configuration.getShort("test.short"); 123 Assert.assertEquals(1, shortValue); 124 Assert.assertEquals(1, configuration.getList("test.short").size()); 125 } 126 127 public void testShortDefaultValue() throws Exception 128 { 129 short shortValue = configuration.getShort("test.short.missing", (short) 1); 130 Assert.assertEquals(1, shortValue); 131 } 132 133 public void testListMissing() throws Exception 134 { 135 List<?> list = configuration.getList("missing.list"); 136 Assert.assertTrue("'missing.list' is not empty", list.isEmpty()); 137 } 138 139 public void testSubset() throws Exception 140 { 141 Configuration subset = configuration.subset("test"); 142 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) 147 { 148 String key = it.next(); 149 foundKeyValue = "short".equals(key); 150 } 151 152 Assert.assertTrue("'short' key not found in the subset key iterator", foundKeyValue); 153 } 154 155 public void testIsEmpty() throws Exception 156 { 157 Assert.assertTrue("Configuration should not be empty", !configuration.isEmpty()); 158 } 159 160 }