    1   /*
    2    * Copyright 2001-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License")
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.configuration;
    18
    19  import java.util.Iterator;
    20  import java.util.List;
    21  import java.util.Vector;
    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,v 1.8 2004/08/16 22:16:31 henning Exp $
    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          Assert.assertEquals(1, configuration.getVector("test.boolean").size());
    46      }
    47
    48      public void testBooleanDefaultValue() throws Exception
    49      {
    50          boolean booleanValue = configuration.getBoolean("test.boolean.missing", true);
    51          Assert.assertEquals(true, booleanValue);
    52
    53          Boolean booleanObject = configuration.getBoolean("test.boolean.missing", new Boolean(true));
    54          Assert.assertEquals(new Boolean(true), booleanObject);
    55      }
    56
    57      public void testByte() throws Exception
    58      {
    59          byte testValue = 10;
    60          byte byteValue = configuration.getByte("test.byte");
    61          Assert.assertEquals(testValue, byteValue);
    62          Assert.assertEquals(1, configuration.getList("test.byte").size());
    63          Assert.assertEquals(1, configuration.getVector("test.byte").size());
    64      }
    65
    66      public void testDouble() throws Exception
    67      {
    68          double testValue = 10.25;
    69          double doubleValue = configuration.getDouble("test.double");
    70          Assert.assertEquals(testValue, doubleValue, 0.01);
    71          Assert.assertEquals(1, configuration.getList("test.double").size());
    72          Assert.assertEquals(1, configuration.getVector("test.double").size());
    73      }
    74
    75      public void testDoubleDefaultValue() throws Exception
    76      {
    77          double testValue = 10.25;
    78          double doubleValue = configuration.getDouble("test.double.missing", 10.25);
    79
    80          Assert.assertEquals(testValue, doubleValue, 0.01);
    81      }
    82
    83      public void testFloat() throws Exception
    84      {
    85          float testValue = (float) 20.25;
    86          float floatValue = configuration.getFloat("test.float");
    87          Assert.assertEquals(testValue, floatValue, 0.01);
    88          Assert.assertEquals(1, configuration.getList("test.float").size());
    89          Assert.assertEquals(1, configuration.getVector("test.float").size());
    90      }
    91
    92      public void testFloatDefaultValue() throws Exception
    93      {
    94          float testValue = (float) 20.25;
    95          float floatValue = configuration.getFloat("test.float.missing", testValue);
    96          Assert.assertEquals(testValue, floatValue, 0.01);
    97      }
    98
    99      public void testInteger() throws Exception
    100     {
    101         int intValue = configuration.getInt("test.integer");
    102         Assert.assertEquals(10, intValue);
    103         Assert.assertEquals(1, configuration.getList("test.integer").size());
    104         Assert.assertEquals(1, configuration.getVector("test.integer").size());
    105     }
    106
    107     public void testIntegerDefaultValue() throws Exception
    108     {
    109         int intValue = configuration.getInt("test.integer.missing", 10);
    110         Assert.assertEquals(10, intValue);
    111     }
    112
    113     public void testLong() throws Exception
    114     {
    115         long longValue = configuration.getLong("test.long");
    116         Assert.assertEquals(1000000, longValue);
    117         Assert.assertEquals(1, configuration.getList("test.long").size());
    118         Assert.assertEquals(1, configuration.getVector("test.long").size());
    119     }
    120     public void testLongDefaultValue() throws Exception
    121     {
    122         long longValue = configuration.getLong("test.long.missing", 1000000);
    123         Assert.assertEquals(1000000, longValue);
    124     }
    125
    126     public void testShort() throws Exception
    127     {
    128         short shortValue = configuration.getShort("test.short");
    129         Assert.assertEquals(1, shortValue);
    130         Assert.assertEquals(1, configuration.getList("test.short").size());
    131         Assert.assertEquals(1, configuration.getVector("test.short").size());
    132     }
    133
    134     public void testShortDefaultValue() throws Exception
    135     {
    136         short shortValue = configuration.getShort("test.short.missing", (short) 1);
    137         Assert.assertEquals(1, shortValue);
    138     }
    139
    140     public void testListMissing() throws Exception
    141     {
    142         List list = configuration.getList("missing.list");
    143         Assert.assertTrue("'missing.list' is not empty", list.isEmpty());
    144     }
    145
    146     public void testVectorMissing() throws Exception
    147     {
    148         Vector vector = configuration.getVector("missing.list");
    149         Assert.assertTrue("'missing.list' is not empty", vector.isEmpty());
    150     }
    151
    152     public void testSubset() throws Exception
    153     {
    154         Configuration subset = configuration.subset("test");
    155
    156         // search the "short" key in the subset using the key iterator
    157         boolean foundKeyValue = false;
    158         Iterator it = subset.getKeys();
    159         while (it.hasNext() && !foundKeyValue)
    160         {
    161             String key = (String) it.next();
    162             foundKeyValue = "short".equals(key);
    163         }
    164
    165         Assert.assertTrue("'short' key not found in the subset key iterator", foundKeyValue);
    166     }
    167
    168     public void testIsEmpty() throws Exception
    169     {
    170         Assert.assertTrue("Configuration should not be empty", !configuration.isEmpty());
    171     }
    172
    173 }