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