diff --git a/src/main/java/org/codehaus/plexus/interpolation/EnvarBasedValueSource.java b/src/main/java/org/codehaus/plexus/interpolation/EnvarBasedValueSource.java index 3e10856..6f7df79 100644 --- a/src/main/java/org/codehaus/plexus/interpolation/EnvarBasedValueSource.java +++ b/src/main/java/org/codehaus/plexus/interpolation/EnvarBasedValueSource.java @@ -110,5 +110,14 @@ public Object getValue( String expression ) return envars.getProperty( expr ); } + + /** + * reset static variables acting as a cache for testing purposes only + */ + static void resetStatics() + { + envarsCaseSensitive = null; + envarsCaseInsensitive = null; + } } diff --git a/src/main/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSource.java b/src/main/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSource.java index 3e1f4ef..067ac31 100644 --- a/src/main/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSource.java +++ b/src/main/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSource.java @@ -110,4 +110,13 @@ public Object getValue( String expression, InterpolationState interpolationState return envars.getProperty( expr ); } + /** + * reset static variables acting as a cache for testing purposes only + */ + static void resetStatics() + { + envarsCaseSensitive = null; + envarsCaseInsensitive = null; + } + } diff --git a/src/main/java/org/codehaus/plexus/interpolation/os/OperatingSystemUtils.java b/src/main/java/org/codehaus/plexus/interpolation/os/OperatingSystemUtils.java index 28756e0..d18a914 100644 --- a/src/main/java/org/codehaus/plexus/interpolation/os/OperatingSystemUtils.java +++ b/src/main/java/org/codehaus/plexus/interpolation/os/OperatingSystemUtils.java @@ -39,6 +39,8 @@ public final class OperatingSystemUtils { + private static EnvVarSource envVarSource = new DefaultEnvVarSource(); + private OperatingSystemUtils() { } @@ -62,11 +64,11 @@ public static Properties getSystemEnvVars( boolean caseSensitive ) throws IOException { Properties envVars = new Properties(); - Map envs = System.getenv(); + Map envs = envVarSource.getEnvMap(); for ( String key : envs.keySet() ) { String value = envs.get( key ); - if ( !caseSensitive) + if ( !caseSensitive ) { key = key.toUpperCase( Locale.ENGLISH ); } @@ -74,4 +76,42 @@ public static Properties getSystemEnvVars( boolean caseSensitive ) } return envVars; } + + /** + * Set the source object to load the environment variables from. + * Default implementation should suffice. This is mostly for testing. + * @param source the EnvVarSource instance that loads the environment variables. + * + * @since 3.1.2 + */ + public static void setEnvVarSource( EnvVarSource source ) + { + envVarSource = source; + } + + /** + * Defines the functionality to load a Map of environment variables. + * + * @since 3.1.2 + */ + public interface EnvVarSource + { + public Map getEnvMap(); + } + + /** + * Default implementation to load environment variables. + * + * @since 3.1.2 + */ + public static class DefaultEnvVarSource + implements EnvVarSource + { + + public Map getEnvMap() + { + return System.getenv(); + } + + } } diff --git a/src/test/java/org/codehaus/plexus/interpolation/EnvarBasedValueSourceTest.java b/src/test/java/org/codehaus/plexus/interpolation/EnvarBasedValueSourceTest.java new file mode 100644 index 0000000..bc88e8b --- /dev/null +++ b/src/test/java/org/codehaus/plexus/interpolation/EnvarBasedValueSourceTest.java @@ -0,0 +1,97 @@ +package org.codehaus.plexus.interpolation; + +/* + * Copyright 2007 The Codehaus Foundation. + * + * Licensed 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. + */ + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.codehaus.plexus.interpolation.os.OperatingSystemUtils; +import org.junit.Before; +import org.junit.Test; + +public class EnvarBasedValueSourceTest +{ + + @Before + public void setUp() + { + EnvarBasedValueSource.resetStatics(); + } + + @Test + public void testNoArgConstructorIsCaseSensitive() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "aVariable", "variable" ); + return map; + } + } ); + + EnvarBasedValueSource source = new EnvarBasedValueSource(); + + assertEquals( "variable", source.getValue( "aVariable" ) ); + assertEquals( "variable", source.getValue( "env.aVariable" ) ); + assertNull( source.getValue( "AVARIABLE" ) ); + assertNull( source.getValue( "env.AVARIABLE" ) ); + } + + @Test + public void testCaseInsensitive() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "aVariable", "variable" ); + return map; + } + } ); + + EnvarBasedValueSource source = new EnvarBasedValueSource( false ); + + assertEquals( "variable", source.getValue( "aVariable" ) ); + assertEquals( "variable", source.getValue( "env.aVariable" ) ); + assertEquals( "variable", source.getValue( "AVARIABLE" ) ); + assertEquals( "variable", source.getValue( "env.AVARIABLE" ) ); + } + + @Test + public void testGetRealEnvironmentVariable() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.DefaultEnvVarSource() ); + + EnvarBasedValueSource source = new EnvarBasedValueSource(); + + String realEnvVar = "JAVA_HOME"; + + String realValue = System.getenv().get( realEnvVar ); + assertNotNull( "Can't run this test until " + realEnvVar + " env variable is set", realValue ); + + assertEquals( realValue, source.getValue( realEnvVar ) ); + } +} \ No newline at end of file diff --git a/src/test/java/org/codehaus/plexus/interpolation/RegexBasedInterpolatorTest.java b/src/test/java/org/codehaus/plexus/interpolation/RegexBasedInterpolatorTest.java index 0597564..dcd97d0 100644 --- a/src/test/java/org/codehaus/plexus/interpolation/RegexBasedInterpolatorTest.java +++ b/src/test/java/org/codehaus/plexus/interpolation/RegexBasedInterpolatorTest.java @@ -21,12 +21,21 @@ import java.util.Map; import java.util.Properties; +import org.codehaus.plexus.interpolation.os.OperatingSystemUtils; +import org.junit.Before; + import junit.framework.TestCase; public class RegexBasedInterpolatorTest extends TestCase { + @Before + public void setUp() + { + EnvarBasedValueSource.resetStatics(); + } + public String getVar() { return "testVar"; @@ -81,14 +90,23 @@ public void testShouldResolveByContextValue() public void testShouldResolveByEnvar() throws IOException, InterpolationException { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "SOME_ENV", "variable" ); + return map; + } + } ); + RegexBasedInterpolator rbi = new RegexBasedInterpolator(); rbi.addValueSource( new EnvarBasedValueSource() ); - String result = rbi.interpolate( "this is a ${env.HOME}", "this" ); + String result = rbi.interpolate( "this is a ${env.SOME_ENV}", "this" ); - assertFalse( "this is a ${HOME}".equals( result ) ); - assertFalse( "this is a ${env.HOME}".equals( result ) ); + assertEquals( "this is a variable", result ); } public void testUseAlternateRegex() diff --git a/src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java b/src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java index fe256c3..9065b38 100644 --- a/src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java +++ b/src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java @@ -24,12 +24,21 @@ import java.util.Map; import java.util.Properties; +import org.codehaus.plexus.interpolation.os.OperatingSystemUtils; +import org.junit.Before; + import junit.framework.TestCase; public class StringSearchInterpolatorTest extends TestCase { + @Before + public void setUp() + { + EnvarBasedValueSource.resetStatics(); + } + public void testLongDelimitersInContext() throws InterpolationException { @@ -177,14 +186,24 @@ public void testShouldResolveByContextValue() public void testShouldResolveByEnvar() throws IOException, InterpolationException { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "SOME_ENV", "variable" ); + map.put( "OTHER_ENV", "other variable" ); + return map; + } + } ); + StringSearchInterpolator rbi = new StringSearchInterpolator(); rbi.addValueSource( new EnvarBasedValueSource( false ) ); - String result = rbi.interpolate( "this is a ${env.HOME} ${env.PATH}" ); + String result = rbi.interpolate( "this is a ${env.SOME_ENV} ${env.OTHER_ENV}" ); - assertFalse( "this is a ${HOME} ${PATH}".equals( result ) ); - assertFalse( "this is a ${env.HOME} ${env.PATH}".equals( result ) ); + assertEquals( "this is a variable other variable", result ); } public void testUsePostProcessor_DoesNotChangeValue() diff --git a/src/test/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSourceTest.java b/src/test/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSourceTest.java new file mode 100644 index 0000000..beaf014 --- /dev/null +++ b/src/test/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSourceTest.java @@ -0,0 +1,98 @@ +package org.codehaus.plexus.interpolation.fixed; + +/* + * Copyright 2007 The Codehaus Foundation. + * + * Licensed 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. + */ + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.codehaus.plexus.interpolation.os.OperatingSystemUtils; +import org.junit.Before; +import org.junit.Test; + +public class EnvarBasedValueSourceTest +{ + + @Before + public void setUp() + { + EnvarBasedValueSource.resetStatics(); + } + + @Test + public void testNoArgConstructorIsCaseSensitive() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "aVariable", "variable" ); + return map; + } + } ); + + EnvarBasedValueSource source = new EnvarBasedValueSource(); + + assertEquals( "variable", source.getValue( "aVariable", null ) ); + assertEquals( "variable", source.getValue( "env.aVariable", null ) ); + assertNull( source.getValue( "AVARIABLE", null ) ); + assertNull( source.getValue( "env.AVARIABLE", null ) ); + } + + @Test + public void testCaseInsensitive() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "aVariable", "variable" ); + return map; + } + } ); + + EnvarBasedValueSource source = new EnvarBasedValueSource( false ); + + assertEquals( "variable", source.getValue( "aVariable", null ) ); + assertEquals( "variable", source.getValue( "env.aVariable", null ) ); + assertEquals( "variable", source.getValue( "AVARIABLE", null ) ); + assertEquals( "variable", source.getValue( "env.AVARIABLE", null ) ); + } + + @Test + public void testGetRealEnvironmentVariable() + throws IOException + { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.DefaultEnvVarSource() ); + + EnvarBasedValueSource source = new EnvarBasedValueSource(); + + String realEnvVar = "JAVA_HOME"; + + String realValue = System.getenv().get( realEnvVar ); + assertNotNull( "Can't run this test until " + realEnvVar + " env variable is set", realValue ); + + assertEquals( realValue, source.getValue( realEnvVar, null ) ); + } + +} diff --git a/src/test/java/org/codehaus/plexus/interpolation/fixed/FixedStringSearchInterpolatorTest.java b/src/test/java/org/codehaus/plexus/interpolation/fixed/FixedStringSearchInterpolatorTest.java index 02a7448..d2c519c 100644 --- a/src/test/java/org/codehaus/plexus/interpolation/fixed/FixedStringSearchInterpolatorTest.java +++ b/src/test/java/org/codehaus/plexus/interpolation/fixed/FixedStringSearchInterpolatorTest.java @@ -17,7 +17,6 @@ import static org.codehaus.plexus.interpolation.fixed.FixedStringSearchInterpolator.create; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.fail; import java.io.IOException; @@ -31,11 +30,19 @@ import org.codehaus.plexus.interpolation.InterpolationException; import org.codehaus.plexus.interpolation.InterpolationPostProcessor; import org.codehaus.plexus.interpolation.StringSearchInterpolator; +import org.codehaus.plexus.interpolation.os.OperatingSystemUtils; +import org.junit.Before; import org.junit.Test; public class FixedStringSearchInterpolatorTest { + @Before + public void setUp() + { + EnvarBasedValueSource.resetStatics(); + } + @Test public void testLongDelimitersInContext() { @@ -181,12 +188,22 @@ public void testShouldResolveByContextValue() public void testShouldResolveByEnvar() throws IOException, InterpolationException { + OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource() + { + public Map getEnvMap() + { + HashMap map = new HashMap(); + map.put( "SOME_ENV", "variable" ); + map.put( "OTHER_ENV", "other variable" ); + return map; + } + } ); + FixedStringSearchInterpolator rbi = create( new EnvarBasedValueSource( false ) ); - String result = rbi.interpolate( "this is a ${env.HOME} ${env.PATH}" ); + String result = rbi.interpolate( "this is a ${env.SOME_ENV} ${env.OTHER_ENV}" ); - assertFalse( "this is a ${HOME} ${PATH}".equals( result ) ); - assertFalse( "this is a ${env.HOME} ${env.PATH}".equals( result ) ); + assertEquals( "this is a variable other variable", result ); } @Test