    1   /*
    2    * Copyright 2002-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.NoSuchElementException;
    21
    22  import junit.framework.TestCase;
    23
    24  public class TestJNDIEnvironmentValues extends TestCase
    25  {
    26      private JNDIConfiguration conf = null;
    27
    28      public void setUp() throws Exception
    29      {
    30          System.setProperty("java.naming.factory.initial", TestJNDIConfiguration.CONTEXT_FACTORY);
    31
    32          conf = new JNDIConfiguration();
    33          conf.setThrowExceptionOnMissing(true);
    34      }
    35
    36      public void testThrowExceptionOnMissing()
    37      {
    38          assertTrue("Throw Exception Property is not set!", conf.isThrowExceptionOnMissing());
    39      }
    40
    41      public void testSimpleGet() throws Exception
    42      {
    43          String s = conf.getString("test.key");
    44          assertEquals("jndivalue", s);
    45      }
    46
    47      public void testMoreGets() throws Exception
    48      {
    49          String s = conf.getString("test.key");
    50          assertEquals("jndivalue", s);
    51          assertEquals("jndivalue2", conf.getString("test.key2"));
    52          assertEquals(1, conf.getShort("test.short"));
    53      }
    54
    55      public void testGetMissingKey() throws Exception
    56      {
    57          try
    58          {
    59              conf.getString("test.imaginarykey");
    60              fail("Should have thrown NoSuchElementException");
    61          }
    62          catch (NoSuchElementException e)
    63          {
    64              assertTrue(e.getMessage(), e.getMessage().indexOf("test.imaginarykey") != -1);
    65          }
    66      }
    67
    68      public void testGetMissingKeyWithDefault() throws Exception
    69      {
    70          String result = conf.getString("test.imaginarykey", "bob");
    71          assertEquals("bob", result);
    72      }
    73
    74      public void testContainsKey() throws Exception
    75      {
    76          assertTrue(conf.containsKey("test.key"));
    77          assertTrue(!conf.containsKey("test.imaginerykey"));
    78      }
    79
    80      public void testClearProperty()
    81      {
    82          assertNotNull("null short for the 'test.short' key", conf.getShort("test.short", null));
    83          conf.clearProperty("test.short");
    84          assertNull("'test.short' property not cleared", conf.getShort("test.short", null));
    85      }
    86
    87      public void testIsEmpty()
    88      {
    89          assertFalse("the configuration shouldn't be empty", conf.isEmpty());
    90      }
    91
    92      public void testGetKeys() throws Exception
    93      {
    94          boolean found = false;
    95          Iterator it = conf.getKeys();
    96
    97          assertTrue("no key found", it.hasNext());
    98
    99          while (it.hasNext() && !found)
    100         {
    101             found = "test.boolean".equals(it.next());
    102         }
    103
    104         assertTrue("'test.boolean' key not found", found);
    105     }
    106
    107     public void testGetKeysWithUnknownPrefix()
    108     {
    109         // test for a unknown prefix
    110         Iterator it = conf.getKeys("foo.bar");
    111         assertFalse("no key should be found", it.hasNext());
    112     }
    113
    114     public void testGetKeysWithExistingPrefix()
    115     {
    116         // test for an existing prefix
    117         Iterator it = conf.getKeys("test");
    118         boolean found = false;
    119         while (it.hasNext() && !found)
    120         {
    121             found = "test.boolean".equals(it.next());
    122         }
    123
    124         assertTrue("'test.boolean' key not found", found);
    125     }
    126
    127     public void testGetKeysWithKeyAsPrefix()
    128     {
    129         // test for a prefix matching exactly the key of a property
    130         Iterator it = conf.getKeys("test.boolean");
    131         boolean found = false;
    132         while (it.hasNext() && !found)
    133         {
    134             found = "test.boolean".equals(it.next());
    135         }
    136
    137         assertTrue("'test.boolean' key not found", found);
    138     }
    139
    140 }