Skip to content

Commit

Permalink
Fix #10 - RegexBasedInterpolatorTest fails when building with maven 3…
Browse files Browse the repository at this point in the history
….4.0-SNAPSHOT

Tests fails because the $HOME env var is no longer defined inside mvn.cmd and is not found (some tests pass even if the env var is not found!).

- Changed the OperatingSystemUtils to allow mocking the Map of env vars.
- Fixed tests to use mocking whenever the env vars are not the focus of the unit test.
- When testing with a REAL env var, it is chosen to be $JAVA_HOME since it is required to be present to run mvn script.
- Added tests to EnvarBasedValueSource and fixed.EnvarBasedValueSource.
  • Loading branch information
belingueres committed Aug 22, 2016
1 parent 50de5ca commit 2307195
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
public final class OperatingSystemUtils
{

private static EnvVarSource envVarSource = new DefaultEnvVarSource();

private OperatingSystemUtils()
{
}
Expand All @@ -62,16 +64,54 @@ public static Properties getSystemEnvVars( boolean caseSensitive )
throws IOException
{
Properties envVars = new Properties();
Map<String, String> envs = System.getenv();
Map<String, String> envs = envVarSource.getEnvMap();
for ( String key : envs.keySet() )
{
String value = envs.get( key );
if ( !caseSensitive)
if ( !caseSensitive )
{
key = key.toUpperCase( Locale.ENGLISH );
}
envVars.put( key, value );
}
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<String, String> getEnvMap();
}

/**
* Default implementation to load environment variables.
*
* @since 3.1.2
*/
public static class DefaultEnvVarSource
implements EnvVarSource
{

public Map<String, String> getEnvMap()
{
return System.getenv();
}

}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> getEnvMap()
{
HashMap<String, String> map = new HashMap<String, String>();
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<String, String> getEnvMap()
{
HashMap<String, String> map = new HashMap<String, String>();
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 ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -81,14 +90,23 @@ public void testShouldResolveByContextValue()
public void testShouldResolveByEnvar()
throws IOException, InterpolationException
{
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
{
public Map<String, String> getEnvMap()
{
HashMap<String,String> map = new HashMap<String,String>();
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,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
{
Expand Down Expand Up @@ -175,14 +184,24 @@ public void testShouldResolveByContextValue()
public void testShouldResolveByEnvar()
throws IOException, InterpolationException
{
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
{
public Map<String, String> getEnvMap()
{
HashMap<String,String> map = new HashMap<String,String>();
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> getEnvMap()
{
HashMap<String, String> map = new HashMap<String, String>();
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<String, String> getEnvMap()
{
HashMap<String, String> map = new HashMap<String, String>();
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 ) );
}

}
Loading

0 comments on commit 2307195

Please sign in to comment.