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