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.BufferedReader;
20  import java.io.IOException;
21  import java.io.StringReader;
22  import java.io.StringWriter;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * Generated by JUnitDoclet, a tool provided by ObjectFab GmbH under LGPL. Please see www.junitdoclet.org, www.gnu.org
31   * and www.objectfab.de for informations about the tool, the licence and the authors.
32   */
33  public class LineOrientedInterpolatingReaderTest
34      extends TestCase
35  {
36      public LineOrientedInterpolatingReaderTest( String name )
37      {
38          super( name );
39      }
40  
41      /**
42       * The JUnit setup method
43       */
44      protected void setUp()
45          throws Exception
46      {
47      }
48  
49      /**
50       * The teardown method for JUnit
51       */
52      protected void tearDown()
53          throws Exception
54      {
55      }
56  
57      /*
58       * Added and commented by jdcasey@03-Feb-2005 because it is a bug in the InterpolationFilterReader.
59       */
60      public void testShouldInterpolateExpressionAtEndOfDataWithInvalidEndToken()
61          throws IOException
62      {
63          String testStr = "This is a ${test";
64          LineOrientedInterpolatingReader iReader =
65              new LineOrientedInterpolatingReader( new StringReader( testStr ),
66                                                   Collections.singletonMap( "test", "TestValue" ) );
67          BufferedReader reader = new BufferedReader( iReader );
68  
69          String result = reader.readLine();
70  
71          assertEquals( "This is a ${test", result );
72      }
73  
74      public void testDefaultInterpolationWithNonInterpolatedValueAtEnd()
75          throws Exception
76      {
77          Map<String, String> m = getStandardMap();
78  
79          String foo = "${name} is an ${noun}. ${not.interpolated}";
80  
81          LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader( new StringReader( foo ), m );
82  
83          StringWriter writer = new StringWriter();
84          IOUtil.copy( reader, writer );
85  
86          String bar = writer.toString();
87          assertEquals( "jason is an asshole. ${not.interpolated}", bar );
88      }
89  
90      private Map<String, String> getStandardMap()
91      {
92          Map<String, String> m = new HashMap<String, String>();
93          m.put( "name", "jason" );
94          m.put( "noun", "asshole" );
95          return m;
96      }
97  
98      public void testDefaultInterpolationWithEscapedExpression()
99          throws Exception
100     {
101         Map<String, String> m = getStandardMap();
102 
103         String foo = "${name} is an ${noun}. \\${noun} value";
104 
105         LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader( new StringReader( foo ), m );
106 
107         StringWriter writer = new StringWriter();
108         IOUtil.copy( reader, writer );
109 
110         String bar = writer.toString();
111         assertEquals( "jason is an asshole. ${noun} value", bar );
112     }
113 
114     public void testDefaultInterpolationWithInterpolatedValueAtEnd()
115         throws Exception
116     {
117         Map<String, String> m = getStandardMap();
118 
119         String foo = "${name} is an ${noun}";
120 
121         LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader( new StringReader( foo ), m );
122 
123         StringWriter writer = new StringWriter();
124         IOUtil.copy( reader, writer );
125 
126         String bar = writer.toString();
127         assertEquals( "jason is an asshole", bar );
128     }
129 
130     public void testInterpolationWithSpecifiedBoundaryTokens()
131         throws Exception
132     {
133         Map<String, String> m = getStandardMap();
134 
135         String foo = "@name@ is an @noun@. @not.interpolated@ baby @foo@. @bar@";
136 
137         LineOrientedInterpolatingReader reader =
138             new LineOrientedInterpolatingReader( new StringReader( foo ), m, "@", "@" );
139 
140         StringWriter writer = new StringWriter();
141         IOUtil.copy( reader, writer );
142 
143         String bar = writer.toString();
144         assertEquals( "jason is an asshole. @not.interpolated@ baby @foo@. @bar@", bar );
145     }
146 
147     public void testInterpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd()
148         throws Exception
149     {
150         Map<String, String> m = getStandardMap();
151 
152         String foo = "@name@ is an @foobarred@";
153 
154         LineOrientedInterpolatingReader reader =
155             new LineOrientedInterpolatingReader( new StringReader( foo ), m, "@", "@" );
156 
157         StringWriter writer = new StringWriter();
158         IOUtil.copy( reader, writer );
159 
160         String bar = writer.toString();
161         assertEquals( "jason is an @foobarred@", bar );
162     }
163 
164     public void testInterpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd()
165         throws Exception
166     {
167         Map<String, String> m = getStandardMap();
168 
169         String foo = "@name@ is an @noun@";
170 
171         LineOrientedInterpolatingReader reader =
172             new LineOrientedInterpolatingReader( new StringReader( foo ), m, "@", "@" );
173 
174         StringWriter writer = new StringWriter();
175         IOUtil.copy( reader, writer );
176 
177         String bar = writer.toString();
178         assertEquals( "jason is an asshole", bar );
179     }
180 }