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