View Javadoc

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 static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.Iterator;
25  
26  import org.junit.Before;
27  import org.junit.Test;
28  
29  /**
30   * Test class for EnvironmentConfiguration.
31   *
32   * @author <a
33   * href="http://commons.apache.org/configuration/team-list.html">Commons
34   * Configuration team</a>
35   * @version $Id: TestEnvironmentConfiguration.java 1224637 2011-12-25 19:47:21Z oheger $
36   */
37  public class TestEnvironmentConfiguration
38  {
39      /** Stores the configuration to be tested. */
40      private EnvironmentConfiguration config;
41  
42      @Before
43      public void setUp() throws Exception
44      {
45          config = new EnvironmentConfiguration();
46      }
47  
48      /**
49       * Tests whether a newly created configuration contains some properties. (We
50       * expect that at least some properties are set in each environment.)
51       */
52      @Test
53      public void testInit()
54      {
55          boolean found = false;
56          assertFalse("No properties found", config.isEmpty());
57          for (Iterator<String> it = config.getKeys(); it.hasNext();)
58          {
59              String key = it.next();
60              assertTrue("Key not found: " + key, config.containsKey(key));
61              assertNotNull("No value for property " + key, config.getString(key));
62              found = true;
63          }
64          assertTrue("No property keys returned", found);
65      }
66  
67      /**
68       * Tests removing properties. This should not be possible.
69       */
70      @Test(expected = UnsupportedOperationException.class)
71      public void testClearProperty()
72      {
73          String key = config.getKeys().next();
74          config.clearProperty(key);
75      }
76  
77      /**
78       * Tests removing all properties. This should not be possible.
79       */
80      @Test(expected = UnsupportedOperationException.class)
81      public void testClear()
82      {
83          config.clear();
84      }
85  
86      /**
87       * Tries to add another property. This should cause an exception.
88       */
89      @Test(expected = UnsupportedOperationException.class)
90      public void testAddProperty()
91      {
92          config.addProperty("JAVA_HOME", "C:\\java");
93      }
94  
95      /**
96       * Tries to set the value of a property. This should cause an exception.
97       */
98      @Test(expected = UnsupportedOperationException.class)
99      public void testSetProperty()
100     {
101         config.setProperty("JAVA_HOME", "C:\\java");
102     }
103 }