View Javadoc
1   package org.codehaus.plexus.util;
2   
3   /*
4    * Copyright 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 java.io.StringReader;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import junit.framework.TestCase;
24  
25  public class InterpolationFilterReaderTest
26      extends TestCase
27  {
28      /*
29       * Added and commented by jdcasey@03-Feb-2005 because it is a bug in the InterpolationFilterReader.
30       * kenneyw@15-04-2005 fixed the bug.
31       */
32      public void testShouldNotInterpolateExpressionAtEndOfDataWithInvalidEndToken()
33          throws Exception
34      {
35          Map<String, String> m = new HashMap<String, String>();
36          m.put( "test", "TestValue" );
37  
38          String testStr = "This is a ${test";
39  
40          assertEquals( "This is a ${test", interpolate( testStr, m ) );
41      }
42  
43      /*
44       * kenneyw@14-04-2005 Added test to check above fix.
45       */
46      public void testShouldNotInterpolateExpressionWithMissingEndToken()
47          throws Exception
48      {
49          Map<String, String> m = new HashMap<String, String>();
50          m.put( "test", "TestValue" );
51  
52          String testStr = "This is a ${test, really";
53  
54          assertEquals( "This is a ${test, really", interpolate( testStr, m ) );
55      }
56  
57      public void testShouldNotInterpolateWithMalformedStartToken()
58          throws Exception
59      {
60          Map<String, String> m = new HashMap<String, String>();
61          m.put( "test", "testValue" );
62  
63          String foo = "This is a $!test} again";
64  
65          assertEquals( "This is a $!test} again", interpolate( foo, m ) );
66      }
67  
68      public void testShouldNotInterpolateWithMalformedEndToken()
69          throws Exception
70      {
71          Map<String, String> m = new HashMap<String, String>();
72          m.put( "test", "testValue" );
73  
74          String foo = "This is a ${test!} again";
75  
76          assertEquals( "This is a ${test!} again", interpolate( foo, m, "${", "$}" ) );
77      }
78  
79      public void testInterpolationWithMulticharDelimiters()
80          throws Exception
81      {
82          Map<String, String> m = new HashMap<String, String>();
83          m.put( "test", "testValue" );
84  
85          String foo = "This is a ${test$} again";
86  
87          assertEquals( "This is a testValue again", interpolate( foo, m, "${", "$}" ) );
88      }
89  
90      public void testDefaultInterpolationWithNonInterpolatedValueAtEnd()
91          throws Exception
92      {
93          Map<String, String> m = new HashMap<String, String>();
94          m.put( "name", "jason" );
95          m.put( "noun", "asshole" );
96  
97          String foo = "${name} is an ${noun}. ${not.interpolated}";
98  
99          assertEquals( "jason is an asshole. ${not.interpolated}", interpolate( foo, m ) );
100     }
101 
102     public void testDefaultInterpolationWithInterpolatedValueAtEnd()
103         throws Exception
104     {
105         Map<String, String> m = new HashMap<String, String>();
106         m.put( "name", "jason" );
107         m.put( "noun", "asshole" );
108 
109         String foo = "${name} is an ${noun}";
110 
111         assertEquals( "jason is an asshole", interpolate( foo, m ) );
112     }
113 
114     public void testInterpolationWithSpecifiedBoundaryTokens()
115         throws Exception
116     {
117         Map<String, String> m = new HashMap<String, String>();
118         m.put( "name", "jason" );
119         m.put( "noun", "asshole" );
120 
121         String foo = "@name@ is an @noun@. @not.interpolated@ baby @foo@. @bar@";
122 
123         assertEquals( "jason is an asshole. @not.interpolated@ baby @foo@. @bar@", interpolate( foo, m, "@", "@" ) );
124     }
125 
126     public void testInterpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd()
127         throws Exception
128     {
129         Map<String, String> m = new HashMap<String, String>();
130         m.put( "name", "jason" );
131         m.put( "noun", "asshole" );
132 
133         String foo = "@name@ is an @foobarred@";
134 
135         assertEquals( "jason is an @foobarred@", interpolate( foo, m, "@", "@" ) );
136     }
137 
138     public void testInterpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd()
139         throws Exception
140     {
141         Map<String, String> m = new HashMap<String, String>();
142         m.put( "name", "jason" );
143         m.put( "noun", "asshole" );
144 
145         String foo = "@name@ is an @noun@";
146 
147         assertEquals( "jason is an asshole", interpolate( foo, m, "@", "@" ) );
148     }
149 
150     public void testInterpolationWithSpecifiedBoundaryTokensAndAdditionalTokenCharacter()
151         throws Exception
152     {
153         Map<String, String> m = new HashMap<String, String>();
154         m.put( "name", "jason" );
155         m.put( "noun", "asshole" );
156 
157         String foo = "@name@ (known as jason@somewhere) is an @noun@";
158 
159         assertEquals( "jason (known as jason@somewhere) is an asshole", interpolate( foo, m, "@", "@" ) );
160     }
161 
162     // ----------------------------------------------------------------------
163     //
164     // ----------------------------------------------------------------------
165 
166     private String interpolate( String input, Map context )
167         throws Exception
168     {
169         return IOUtil.toString( new InterpolationFilterReader( new StringReader( input ), context ) );
170     }
171 
172     private String interpolate( String input, Map context, String startToken, String endToken )
173         throws Exception
174     {
175         return IOUtil.toString( new InterpolationFilterReader( new StringReader( input ), context, startToken,
176                                                                endToken ) );
177     }
178 }