|
|
|
oheger
|
Author: oheger
Date: Sun Nov 8 17:38:19 2009 New Revision: 833896 URL: http://svn.apache.org/viewvc?rev=833896&view=rev Log: [CONFIGURATION-399] Added EnvironmentLookup class. Added: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java (with props) commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java (with props) Added: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java?rev=833896&view=auto ============================================================================== --- commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java (added) +++ commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java Sun Nov 8 17:38:19 2009 @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.configuration.interpol; + +import org.apache.commons.configuration.EnvironmentConfiguration; +import org.apache.commons.lang.text.StrLookup; + +/** + * <p> + * A specialized lookup implementation that allows access to environment + * variables. + * </p> + * <p> + * This implementation relies on {@link EnvironmentConfiguration} to resolve + * environment variables. It can be used for referencing environment variables + * in configuration files in an easy way, for instance: + * + * <pre> + * java.home = ${env:JAVA_HOME} + * </pre> + * + * </p> + * <p> + * <code>EnvironmentLookup</code> is one of the standard lookups that is + * registered per default for each configuration. + * </p> + * + * @author <a + * href="http://commons.apache.org/configuration/team-list.html">Commons + * Configuration team</a> + * @since 1.7 + * @version $Id$ + */ +public class EnvironmentLookup extends StrLookup +{ + /** Stores the underlying <code>EnvironmentConfiguration</code>. */ + private final EnvironmentConfiguration environmentConfig = new EnvironmentConfiguration(); + + /** + * Performs a lookup for the specified variable. This implementation + * directly delegates to a <code>EnvironmentConfiguration</code>. + * + * @param key the key to lookup + * @return the value of this key or <b>null</b> if it cannot be resolved + */ + public String lookup(String key) + { + return environmentConfig.getString(key); + } +} Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/interpol/EnvironmentLookup.java ------------------------------------------------------------------------------ svn:mime-type = text/plain Added: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java?rev=833896&view=auto ============================================================================== --- commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java (added) +++ commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java Sun Nov 8 17:38:19 2009 @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.configuration.interpol; + +import java.util.Iterator; + +import junit.framework.TestCase; + +import org.apache.commons.configuration.EnvironmentConfiguration; + +/** + * Test class for EnvironmentLookup. + * + * @author <a + * href="http://commons.apache.org/configuration/team-list.html">Commons + * Configuration team</a> + * @version $Id$ + */ +public class TestEnvironmentLookup extends TestCase +{ + /** The lookup to be tested. */ + private EnvironmentLookup lookup; + + protected void setUp() throws Exception + { + super.setUp(); + lookup = new EnvironmentLookup(); + } + + /** + * Tests whether environment variables can be queried. + */ + public void testLookup() + { + EnvironmentConfiguration envConf = new EnvironmentConfiguration(); + for (Iterator it = envConf.getKeys(); it.hasNext();) + { + String var = (String) it.next(); + assertEquals("Wrong value for " + var, envConf.getString(var), + lookup.lookup(var)); + } + } + + /** + * Tries to lookup a non existing property. + */ + public void testLookupNonExisting() + { + assertNull("Got result for non existing environment variable", lookup + .lookup("a non existing variable!")); + } +} Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL Propchange: commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/interpol/TestEnvironmentLookup.java ------------------------------------------------------------------------------ svn:mime-type = text/plain |
||||||||||||||||
| Free Embeddable Forum Powered by Nabble | Help |