001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.configuration; 019 020 import static org.junit.Assert.assertFalse; 021 import static org.junit.Assert.assertNotNull; 022 import static org.junit.Assert.assertTrue; 023 024 import java.util.Iterator; 025 026 import org.junit.Before; 027 import org.junit.Test; 028 029 /** 030 * Test class for EnvironmentConfiguration. 031 * 032 * @author <a 033 * href="http://commons.apache.org/configuration/team-list.html">Commons 034 * Configuration team</a> 035 * @version $Id: TestEnvironmentConfiguration.java 1224637 2011-12-25 19:47:21Z oheger $ 036 */ 037 public class TestEnvironmentConfiguration 038 { 039 /** Stores the configuration to be tested. */ 040 private EnvironmentConfiguration config; 041 042 @Before 043 public void setUp() throws Exception 044 { 045 config = new EnvironmentConfiguration(); 046 } 047 048 /** 049 * Tests whether a newly created configuration contains some properties. (We 050 * expect that at least some properties are set in each environment.) 051 */ 052 @Test 053 public void testInit() 054 { 055 boolean found = false; 056 assertFalse("No properties found", config.isEmpty()); 057 for (Iterator<String> it = config.getKeys(); it.hasNext();) 058 { 059 String key = it.next(); 060 assertTrue("Key not found: " + key, config.containsKey(key)); 061 assertNotNull("No value for property " + key, config.getString(key)); 062 found = true; 063 } 064 assertTrue("No property keys returned", found); 065 } 066 067 /** 068 * Tests removing properties. This should not be possible. 069 */ 070 @Test(expected = UnsupportedOperationException.class) 071 public void testClearProperty() 072 { 073 String key = config.getKeys().next(); 074 config.clearProperty(key); 075 } 076 077 /** 078 * Tests removing all properties. This should not be possible. 079 */ 080 @Test(expected = UnsupportedOperationException.class) 081 public void testClear() 082 { 083 config.clear(); 084 } 085 086 /** 087 * Tries to add another property. This should cause an exception. 088 */ 089 @Test(expected = UnsupportedOperationException.class) 090 public void testAddProperty() 091 { 092 config.addProperty("JAVA_HOME", "C:\\java"); 093 } 094 095 /** 096 * Tries to set the value of a property. This should cause an exception. 097 */ 098 @Test(expected = UnsupportedOperationException.class) 099 public void testSetProperty() 100 { 101 config.setProperty("JAVA_HOME", "C:\\java"); 102 } 103 }