Skip to content

Commit d187343

Browse files
belingueresolamy
authored andcommitted
Fix10 - RegexBasedInterpolatorTest fails when building with maven 3.4.0-SNAPSHOT (#11)
* Fixed MultiDelimiterStringSearchInterpolator escape String code * Modified ReflectionValueExtractor to a more up to date version ReflectionValueExtractor version with capacity to parse expressions with arrays, lists and maps. * Commit to undo the changes I've made since forked the original project. The reason is that I've made a mistake and created a unique PR to solve several issues, which is a bad practice. * Fix #10 - RegexBasedInterpolatorTest fails when building with maven 3.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. * Fix #10 - RegexBasedInterpolatorTest fails when building with maven 3.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. * Reseted branch commit
1 parent 0d3c1d2 commit d187343

File tree

8 files changed

+319
-12
lines changed

8 files changed

+319
-12
lines changed

src/main/java/org/codehaus/plexus/interpolation/EnvarBasedValueSource.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,14 @@ public Object getValue( String expression )
110110

111111
return envars.getProperty( expr );
112112
}
113+
114+
/**
115+
* reset static variables acting as a cache for testing purposes only
116+
*/
117+
static void resetStatics()
118+
{
119+
envarsCaseSensitive = null;
120+
envarsCaseInsensitive = null;
121+
}
113122

114123
}

src/main/java/org/codehaus/plexus/interpolation/fixed/EnvarBasedValueSource.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,13 @@ public Object getValue( String expression, InterpolationState interpolationState
110110
return envars.getProperty( expr );
111111
}
112112

113+
/**
114+
* reset static variables acting as a cache for testing purposes only
115+
*/
116+
static void resetStatics()
117+
{
118+
envarsCaseSensitive = null;
119+
envarsCaseInsensitive = null;
120+
}
121+
113122
}

src/main/java/org/codehaus/plexus/interpolation/os/OperatingSystemUtils.java

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
public final class OperatingSystemUtils
4040
{
4141

42+
private static EnvVarSource envVarSource = new DefaultEnvVarSource();
43+
4244
private OperatingSystemUtils()
4345
{
4446
}
@@ -62,16 +64,54 @@ public static Properties getSystemEnvVars( boolean caseSensitive )
6264
throws IOException
6365
{
6466
Properties envVars = new Properties();
65-
Map<String, String> envs = System.getenv();
67+
Map<String, String> envs = envVarSource.getEnvMap();
6668
for ( String key : envs.keySet() )
6769
{
6870
String value = envs.get( key );
69-
if ( !caseSensitive)
71+
if ( !caseSensitive )
7072
{
7173
key = key.toUpperCase( Locale.ENGLISH );
7274
}
7375
envVars.put( key, value );
7476
}
7577
return envVars;
7678
}
79+
80+
/**
81+
* Set the source object to load the environment variables from.
82+
* Default implementation should suffice. This is mostly for testing.
83+
* @param source the EnvVarSource instance that loads the environment variables.
84+
*
85+
* @since 3.1.2
86+
*/
87+
public static void setEnvVarSource( EnvVarSource source )
88+
{
89+
envVarSource = source;
90+
}
91+
92+
/**
93+
* Defines the functionality to load a Map of environment variables.
94+
*
95+
* @since 3.1.2
96+
*/
97+
public interface EnvVarSource
98+
{
99+
public Map<String, String> getEnvMap();
100+
}
101+
102+
/**
103+
* Default implementation to load environment variables.
104+
*
105+
* @since 3.1.2
106+
*/
107+
public static class DefaultEnvVarSource
108+
implements EnvVarSource
109+
{
110+
111+
public Map<String, String> getEnvMap()
112+
{
113+
return System.getenv();
114+
}
115+
116+
}
77117
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.codehaus.plexus.interpolation;
2+
3+
/*
4+
* Copyright 2007 The Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import static org.junit.Assert.*;
20+
21+
import java.io.IOException;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
public class EnvarBasedValueSourceTest
30+
{
31+
32+
@Before
33+
public void setUp()
34+
{
35+
EnvarBasedValueSource.resetStatics();
36+
}
37+
38+
@Test
39+
public void testNoArgConstructorIsCaseSensitive()
40+
throws IOException
41+
{
42+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
43+
{
44+
public Map<String, String> getEnvMap()
45+
{
46+
HashMap<String, String> map = new HashMap<String, String>();
47+
map.put( "aVariable", "variable" );
48+
return map;
49+
}
50+
} );
51+
52+
EnvarBasedValueSource source = new EnvarBasedValueSource();
53+
54+
assertEquals( "variable", source.getValue( "aVariable" ) );
55+
assertEquals( "variable", source.getValue( "env.aVariable" ) );
56+
assertNull( source.getValue( "AVARIABLE" ) );
57+
assertNull( source.getValue( "env.AVARIABLE" ) );
58+
}
59+
60+
@Test
61+
public void testCaseInsensitive()
62+
throws IOException
63+
{
64+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
65+
{
66+
public Map<String, String> getEnvMap()
67+
{
68+
HashMap<String, String> map = new HashMap<String, String>();
69+
map.put( "aVariable", "variable" );
70+
return map;
71+
}
72+
} );
73+
74+
EnvarBasedValueSource source = new EnvarBasedValueSource( false );
75+
76+
assertEquals( "variable", source.getValue( "aVariable" ) );
77+
assertEquals( "variable", source.getValue( "env.aVariable" ) );
78+
assertEquals( "variable", source.getValue( "AVARIABLE" ) );
79+
assertEquals( "variable", source.getValue( "env.AVARIABLE" ) );
80+
}
81+
82+
@Test
83+
public void testGetRealEnvironmentVariable()
84+
throws IOException
85+
{
86+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.DefaultEnvVarSource() );
87+
88+
EnvarBasedValueSource source = new EnvarBasedValueSource();
89+
90+
String realEnvVar = "JAVA_HOME";
91+
92+
String realValue = System.getenv().get( realEnvVar );
93+
assertNotNull( "Can't run this test until " + realEnvVar + " env variable is set", realValue );
94+
95+
assertEquals( realValue, source.getValue( realEnvVar ) );
96+
}
97+
}

src/test/java/org/codehaus/plexus/interpolation/RegexBasedInterpolatorTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,21 @@
2121
import java.util.Map;
2222
import java.util.Properties;
2323

24+
import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
25+
import org.junit.Before;
26+
2427
import junit.framework.TestCase;
2528

2629
public class RegexBasedInterpolatorTest
2730
extends TestCase
2831
{
2932

33+
@Before
34+
public void setUp()
35+
{
36+
EnvarBasedValueSource.resetStatics();
37+
}
38+
3039
public String getVar()
3140
{
3241
return "testVar";
@@ -81,14 +90,23 @@ public void testShouldResolveByContextValue()
8190
public void testShouldResolveByEnvar()
8291
throws IOException, InterpolationException
8392
{
93+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
94+
{
95+
public Map<String, String> getEnvMap()
96+
{
97+
HashMap<String,String> map = new HashMap<String,String>();
98+
map.put( "SOME_ENV", "variable" );
99+
return map;
100+
}
101+
} );
102+
84103
RegexBasedInterpolator rbi = new RegexBasedInterpolator();
85104

86105
rbi.addValueSource( new EnvarBasedValueSource() );
87106

88-
String result = rbi.interpolate( "this is a ${env.HOME}", "this" );
107+
String result = rbi.interpolate( "this is a ${env.SOME_ENV}", "this" );
89108

90-
assertFalse( "this is a ${HOME}".equals( result ) );
91-
assertFalse( "this is a ${env.HOME}".equals( result ) );
109+
assertEquals( "this is a variable", result );
92110
}
93111

94112
public void testUseAlternateRegex()

src/test/java/org/codehaus/plexus/interpolation/StringSearchInterpolatorTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,21 @@
2424
import java.util.Map;
2525
import java.util.Properties;
2626

27+
import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
28+
import org.junit.Before;
29+
2730
import junit.framework.TestCase;
2831

2932
public class StringSearchInterpolatorTest
3033
extends TestCase
3134
{
3235

36+
@Before
37+
public void setUp()
38+
{
39+
EnvarBasedValueSource.resetStatics();
40+
}
41+
3342
public void testLongDelimitersInContext()
3443
throws InterpolationException
3544
{
@@ -177,14 +186,24 @@ public void testShouldResolveByContextValue()
177186
public void testShouldResolveByEnvar()
178187
throws IOException, InterpolationException
179188
{
189+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
190+
{
191+
public Map<String, String> getEnvMap()
192+
{
193+
HashMap<String,String> map = new HashMap<String,String>();
194+
map.put( "SOME_ENV", "variable" );
195+
map.put( "OTHER_ENV", "other variable" );
196+
return map;
197+
}
198+
} );
199+
180200
StringSearchInterpolator rbi = new StringSearchInterpolator();
181201

182202
rbi.addValueSource( new EnvarBasedValueSource( false ) );
183203

184-
String result = rbi.interpolate( "this is a ${env.HOME} ${env.PATH}" );
204+
String result = rbi.interpolate( "this is a ${env.SOME_ENV} ${env.OTHER_ENV}" );
185205

186-
assertFalse( "this is a ${HOME} ${PATH}".equals( result ) );
187-
assertFalse( "this is a ${env.HOME} ${env.PATH}".equals( result ) );
206+
assertEquals( "this is a variable other variable", result );
188207
}
189208

190209
public void testUsePostProcessor_DoesNotChangeValue()
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.codehaus.plexus.interpolation.fixed;
2+
3+
/*
4+
* Copyright 2007 The Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import static org.junit.Assert.*;
20+
21+
import java.io.IOException;
22+
import java.util.HashMap;
23+
import java.util.Map;
24+
25+
import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
26+
import org.junit.Before;
27+
import org.junit.Test;
28+
29+
public class EnvarBasedValueSourceTest
30+
{
31+
32+
@Before
33+
public void setUp()
34+
{
35+
EnvarBasedValueSource.resetStatics();
36+
}
37+
38+
@Test
39+
public void testNoArgConstructorIsCaseSensitive()
40+
throws IOException
41+
{
42+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
43+
{
44+
public Map<String, String> getEnvMap()
45+
{
46+
HashMap<String, String> map = new HashMap<String, String>();
47+
map.put( "aVariable", "variable" );
48+
return map;
49+
}
50+
} );
51+
52+
EnvarBasedValueSource source = new EnvarBasedValueSource();
53+
54+
assertEquals( "variable", source.getValue( "aVariable", null ) );
55+
assertEquals( "variable", source.getValue( "env.aVariable", null ) );
56+
assertNull( source.getValue( "AVARIABLE", null ) );
57+
assertNull( source.getValue( "env.AVARIABLE", null ) );
58+
}
59+
60+
@Test
61+
public void testCaseInsensitive()
62+
throws IOException
63+
{
64+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.EnvVarSource()
65+
{
66+
public Map<String, String> getEnvMap()
67+
{
68+
HashMap<String, String> map = new HashMap<String, String>();
69+
map.put( "aVariable", "variable" );
70+
return map;
71+
}
72+
} );
73+
74+
EnvarBasedValueSource source = new EnvarBasedValueSource( false );
75+
76+
assertEquals( "variable", source.getValue( "aVariable", null ) );
77+
assertEquals( "variable", source.getValue( "env.aVariable", null ) );
78+
assertEquals( "variable", source.getValue( "AVARIABLE", null ) );
79+
assertEquals( "variable", source.getValue( "env.AVARIABLE", null ) );
80+
}
81+
82+
@Test
83+
public void testGetRealEnvironmentVariable()
84+
throws IOException
85+
{
86+
OperatingSystemUtils.setEnvVarSource( new OperatingSystemUtils.DefaultEnvVarSource() );
87+
88+
EnvarBasedValueSource source = new EnvarBasedValueSource();
89+
90+
String realEnvVar = "JAVA_HOME";
91+
92+
String realValue = System.getenv().get( realEnvVar );
93+
assertNotNull( "Can't run this test until " + realEnvVar + " env variable is set", realValue );
94+
95+
assertEquals( realValue, source.getValue( realEnvVar, null ) );
96+
}
97+
98+
}

0 commit comments

Comments
 (0)