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