    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.configuration;
    19
    20  import java.util.Iterator;
    21  import java.util.List;
    22
    23  import junit.framework.Assert;
    24
    25  /***
    26   * Pulling out the calls to do the tests so both JUnit and Cactus tests
    27   * can share.
    28   *
    29   * @version $Id: NonStringTestHolder.java 439648 2006-09-02 20:42:10Z oheger $
    30   */
    31  public class NonStringTestHolder
    32  {
    33      private Configuration configuration;
    34
    35      public void setConfiguration(Configuration configuration)
    36      {
    37          this.configuration = configuration;
    38      }
    39
    40      public void testBoolean() throws Exception
    41      {
    42          boolean booleanValue = configuration.getBoolean("test.boolean");
    43          Assert.assertEquals(true, booleanValue);
    44          Assert.assertEquals(1, configuration.getList("test.boolean").size());
    45      }
    46
    47      public void testBooleanDefaultValue() throws Exception
    48      {
    49          boolean booleanValue = configuration.getBoolean("test.boolean.missing", true);
    50          Assert.assertEquals(true, booleanValue);
    51
    52          Boolean booleanObject = configuration.getBoolean("test.boolean.missing", new Boolean(true));
    53          Assert.assertEquals(new Boolean(true), booleanObject);
    54      }
    55
    56      public void testByte() throws Exception
    57      {
    58          byte testValue = 10;
    59          byte byteValue = configuration.getByte("test.byte");
    60          Assert.assertEquals(testValue, byteValue);
    61          Assert.assertEquals(1, configuration.getList("test.byte").size());
    62      }
    63
    64      public void testDouble() throws Exception
    65      {
    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());
    70      }
    71
    72      public void testDoubleDefaultValue() throws Exception
    73      {
    74          double testValue = 10.25;
    75          double doubleValue = configuration.getDouble("test.double.missing", 10.25);
    76
    77          Assert.assertEquals(testValue, doubleValue, 0.01);
    78      }
    79
    80      public void testFloat() throws Exception
    81      {
    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());
    86      }
    87
    88      public void testFloatDefaultValue() throws Exception
    89      {
    90          float testValue = (float) 20.25;
    91          float floatValue = configuration.getFloat("test.float.missing", testValue);
    92          Assert.assertEquals(testValue, floatValue, 0.01);
    93      }
    94
    95      public void testInteger() throws Exception
    96      {
    97          int intValue = configuration.getInt("test.integer");
    98          Assert.assertEquals(10, intValue);
    99          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 it = subset.getKeys();
    146         while (it.hasNext() && !foundKeyValue)
    147         {
    148             String key = (String) 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 }