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 junit.framework.TestCase;
21  
22  import javax.naming.InitialContext;
23  
24  /***
25   * Test to see if the JNDIConfiguration works properly.  Currently excluded
26   * in the project.xml unitTest section as our JNDI provider doesn't
27   * properly support the listBindings() method.
28   *
29   * This does work fine with Tomcat's JNDI provider however.
30   *
31   * @version $Id: TestJNDIConfiguration.java 439648 2006-09-02 20:42:10Z oheger $
32   */
33  public class TestJNDIConfiguration extends TestCase {
34  
35      public static final String CONTEXT_FACTORY =
36              "org.apache.commons.configuration.MockStaticMemoryInitialContextFactory";
37  
38      private JNDIConfiguration conf;
39      private NonStringTestHolder nonStringTestHolder;
40  
41      public void setUp() throws Exception {
42  
43          System.setProperty("java.naming.factory.initial", CONTEXT_FACTORY);
44  
45          conf = new JNDIConfiguration();
46  
47          nonStringTestHolder = new NonStringTestHolder();
48          nonStringTestHolder.setConfiguration(conf);
49      }
50  
51      public void testBoolean() throws Exception {
52          nonStringTestHolder.testBoolean();
53      }
54  
55      public void testBooleanDefaultValue() throws Exception {
56          nonStringTestHolder.testBooleanDefaultValue();
57      }
58  
59      public void testByte() throws Exception {
60          nonStringTestHolder.testByte();
61      }
62  
63      public void testDouble() throws Exception {
64          nonStringTestHolder.testDouble();
65      }
66  
67      public void testDoubleDefaultValue() throws Exception {
68          nonStringTestHolder.testDoubleDefaultValue();
69      }
70  
71      public void testFloat() throws Exception {
72          nonStringTestHolder.testFloat();
73      }
74  
75      public void testFloatDefaultValue() throws Exception {
76          nonStringTestHolder.testFloatDefaultValue();
77      }
78  
79      public void testInteger() throws Exception {
80          nonStringTestHolder.testInteger();
81      }
82  
83      public void testIntegerDefaultValue() throws Exception {
84          nonStringTestHolder.testIntegerDefaultValue();
85      }
86  
87      public void testLong() throws Exception {
88          nonStringTestHolder.testLong();
89      }
90  
91      public void testLongDefaultValue() throws Exception {
92          nonStringTestHolder.testLongDefaultValue();
93      }
94  
95      public void testShort() throws Exception {
96          nonStringTestHolder.testShort();
97      }
98  
99      public void testShortDefaultValue() throws Exception {
100         nonStringTestHolder.testShortDefaultValue();
101     }
102 
103     public void testListMissing() throws Exception {
104         nonStringTestHolder.testListMissing();
105     }
106 
107     public void testSubset() throws Exception {
108         nonStringTestHolder.testSubset();
109     }
110 
111     public void testProperties() throws Exception {
112         Object o = conf.getProperty("test.boolean");
113         assertNotNull(o);
114         assertEquals("true", o.toString());
115     }
116 
117     public void testContainsKey()
118     {
119         String key = "test.boolean";
120         assertTrue("'" + key + "' not found", conf.containsKey(key));
121 
122         conf.clearProperty(key);
123         assertFalse("'" + key + "' still found", conf.containsKey(key));
124     }
125 
126     public void testChangePrefix()
127     {
128         assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
129         assertEquals("'boolean' property", null, conf.getString("boolean"));
130 
131         // change the prefix
132         conf.setPrefix("test");
133         assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
134         assertEquals("'boolean' property", "true", conf.getString("boolean"));
135     }
136 
137     public void testResetRemovedProperties() throws Exception
138     {
139         assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
140 
141         // remove the property
142         conf.clearProperty("test.boolean");
143         assertEquals("'test.boolean' property", null, conf.getString("test.boolean"));
144 
145         // change the context
146         conf.setContext(new InitialContext());
147 
148         // get the property
149         assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
150     }
151 
152     public void testConstructor() throws Exception
153     {
154         // test the constructor accepting a context
155         conf = new JNDIConfiguration(new InitialContext());
156 
157         assertEquals("'test.boolean' property", "true", conf.getString("test.boolean"));
158 
159         // test the constructor accepting a context and a prefix
160         conf = new JNDIConfiguration(new InitialContext(), "test");
161 
162         assertEquals("'boolean' property", "true", conf.getString("boolean"));
163     }
164 
165 }