View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * Copyright 2001-2008 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 java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  import java.util.Properties;
23  
24  import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
25  import org.junit.Before;
26  
27  import junit.framework.TestCase;
28  
29  public class RegexBasedInterpolatorTest
30      extends TestCase
31  {
32  
33      @Before
34      public void setUp()
35      {
36          EnvarBasedValueSource.resetStatics();
37      }
38  
39      public String getVar()
40      {
41          return "testVar";
42      }
43  
44      public void testShouldFailOnExpressionCycle()
45      {
46          Properties props = new Properties();
47          props.setProperty( "key1", "${key2}" );
48          props.setProperty( "key2", "${key1}" );
49  
50          RegexBasedInterpolator rbi = new RegexBasedInterpolator();
51          rbi.addValueSource( new PropertiesBasedValueSource( props ) );
52  
53          try
54          {
55              rbi.interpolate( "${key1}", new SimpleRecursionInterceptor() );
56  
57              fail( "Should detect expression cycle and fail." );
58          }
59          catch ( InterpolationException e )
60          {
61              // expected
62          }
63      }
64  
65      public void testShouldResolveByMy_getVar_Method()
66          throws InterpolationException
67      {
68          RegexBasedInterpolator rbi = new RegexBasedInterpolator();
69          rbi.addValueSource( new ObjectBasedValueSource( this ) );
70          String result = rbi.interpolate( "this is a ${this.var}", "this" );
71  
72          assertEquals( "this is a testVar", result );
73      }
74  
75      public void testShouldResolveByContextValue()
76          throws InterpolationException
77      {
78          RegexBasedInterpolator rbi = new RegexBasedInterpolator();
79  
80          Map context = new HashMap();
81          context.put( "var", "testVar" );
82  
83          rbi.addValueSource( new MapBasedValueSource( context ) );
84  
85          String result = rbi.interpolate( "this is a ${this.var}", "this" );
86  
87          assertEquals( "this is a testVar", result );
88      }
89  
90      public void testShouldResolveByEnvar()
91          throws IOException, InterpolationException
92      {
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 
103         RegexBasedInterpolator rbi = new RegexBasedInterpolator();
104 
105         rbi.addValueSource( new EnvarBasedValueSource() );
106 
107         String result = rbi.interpolate( "this is a ${env.SOME_ENV}", "this" );
108 
109         assertEquals( "this is a variable", result );
110     }
111 
112     public void testUseAlternateRegex()
113         throws Exception
114     {
115         RegexBasedInterpolator rbi = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" );
116 
117         Map context = new HashMap();
118         context.put( "var", "testVar" );
119 
120         rbi.addValueSource( new MapBasedValueSource( context ) );
121 
122         String result = rbi.interpolate( "this is a @{this.var}@", "this" );
123 
124         assertEquals( "this is a testVar", result );
125     }
126 
127     public void testNPEFree()
128         throws Exception
129     {
130         RegexBasedInterpolator rbi = new RegexBasedInterpolator( "\\@\\{(", ")?([^}]+)\\}@" );
131 
132         Map context = new HashMap();
133         context.put( "var", "testVar" );
134 
135         rbi.addValueSource( new MapBasedValueSource( context ) );
136 
137         String result = rbi.interpolate( null );
138 
139         assertEquals( "", result );
140     }    
141     
142     public void testUsePostProcessor_DoesNotChangeValue()
143         throws InterpolationException
144     {
145         RegexBasedInterpolator rbi = new RegexBasedInterpolator();
146         
147         Map context = new HashMap();
148         context.put( "test.var", "testVar" );
149 
150         rbi.addValueSource( new MapBasedValueSource( context ) );
151 
152         rbi.addPostProcessor( new InterpolationPostProcessor()
153         {
154             public Object execute( String expression, Object value )
155             {
156                 return null;
157             }
158         } );
159 
160         String result = rbi.interpolate( "this is a ${test.var}", "" );
161 
162         assertEquals( "this is a testVar", result );
163     }
164 
165     public void testUsePostProcessor_ChangesValue()
166         throws InterpolationException
167     {
168 
169         int loopNumber = 200000;
170         
171         long start = System.currentTimeMillis();
172 
173         RegexBasedInterpolator rbi = new RegexBasedInterpolator();
174 
175         Map context = new HashMap();
176         context.put( "test.var", "testVar" );
177 
178         rbi.addValueSource( new MapBasedValueSource( context ) );
179 
180         rbi.addPostProcessor( new InterpolationPostProcessor()
181         {
182             public Object execute( String expression, Object value )
183             {
184                 return value + "2";
185             }
186         } );        
187         
188         for ( int i = 0, number = loopNumber; i < number; i++ )
189         {
190 
191 
192             String result = rbi.interpolate( "this is a ${test.var}", "" );
193 
194             assertEquals( "this is a testVar2", result );
195         }
196         long end = System.currentTimeMillis();
197 
198         System.out.println( "time without pattern reuse and RegexBasedInterpolator instance reuse " + ( end - start ) );
199 
200         System.gc();
201         
202         start = System.currentTimeMillis();
203         
204         
205 
206         rbi = new RegexBasedInterpolator( true );
207         
208         rbi.addPostProcessor( new InterpolationPostProcessor()
209         {
210             public Object execute( String expression, Object value )
211             {
212                 return value + "2";
213             }
214         } );        
215         
216         rbi.addValueSource( new MapBasedValueSource( context ) );
217         
218         for ( int i = 0, number = loopNumber; i < number; i++ )
219         {
220 
221             String result = rbi.interpolate( "this is a ${test.var}", "" );
222 
223             assertEquals( "this is a testVar2", result );
224         }
225         end = System.currentTimeMillis();
226 
227         System.out.println( "time with pattern reuse and RegexBasedInterpolator instance reuse " + ( end - start ) );
228     }
229 }