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.StringReader;
21  import java.io.StringWriter;
22  import java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import static org.junit.jupiter.api.Assertions.assertEquals;
29  
30  /**
31   * Generated by JUnitDoclet, a tool provided by ObjectFab GmbH under LGPL. Please see www.junitdoclet.org, www.gnu.org
32   * and www.objectfab.de for informations about the tool, the licence and the authors.
33   *
34   * @author herve
35   * @since 3.4.0
36   */
37  class LineOrientedInterpolatingReaderTest {
38  
39      @Test
40      void shouldInterpolateExpressionAtEndOfDataWithInvalidEndToken() throws Exception {
41          String testStr = "This is a ${test";
42          LineOrientedInterpolatingReader iReader = new LineOrientedInterpolatingReader(
43                  new StringReader(testStr), Collections.singletonMap("test", "TestValue"));
44          BufferedReader reader = new BufferedReader(iReader);
45  
46          String result = reader.readLine();
47  
48          assertEquals("This is a ${test", result);
49      }
50  
51      @Test
52      void defaultInterpolationWithNonInterpolatedValueAtEnd() throws Exception {
53          Map<String, String> m = getStandardMap();
54  
55          String foo = "${name} is an ${noun}. ${not.interpolated}";
56  
57          LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader(new StringReader(foo), m);
58  
59          StringWriter writer = new StringWriter();
60          IOUtil.copy(reader, writer);
61  
62          String bar = writer.toString();
63          assertEquals("jason is an asshole. ${not.interpolated}", bar);
64      }
65  
66      private Map<String, String> getStandardMap() {
67          Map<String, String> m = new HashMap<>();
68          m.put("name", "jason");
69          m.put("noun", "asshole");
70          return m;
71      }
72  
73      @Test
74      void defaultInterpolationWithEscapedExpression() throws Exception {
75          Map<String, String> m = getStandardMap();
76  
77          String foo = "${name} is an ${noun}. \\${noun} value";
78  
79          LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader(new StringReader(foo), m);
80  
81          StringWriter writer = new StringWriter();
82          IOUtil.copy(reader, writer);
83  
84          String bar = writer.toString();
85          assertEquals("jason is an asshole. ${noun} value", bar);
86      }
87  
88      @Test
89      void defaultInterpolationWithInterpolatedValueAtEnd() throws Exception {
90          Map<String, String> m = getStandardMap();
91  
92          String foo = "${name} is an ${noun}";
93  
94          LineOrientedInterpolatingReader reader = new LineOrientedInterpolatingReader(new StringReader(foo), m);
95  
96          StringWriter writer = new StringWriter();
97          IOUtil.copy(reader, writer);
98  
99          String bar = writer.toString();
100         assertEquals("jason is an asshole", bar);
101     }
102 
103     @Test
104     void interpolationWithSpecifiedBoundaryTokens() throws Exception {
105         Map<String, String> m = getStandardMap();
106 
107         String foo = "@name@ is an @noun@. @not.interpolated@ baby @foo@. @bar@";
108 
109         LineOrientedInterpolatingReader reader =
110                 new LineOrientedInterpolatingReader(new StringReader(foo), m, "@", "@");
111 
112         StringWriter writer = new StringWriter();
113         IOUtil.copy(reader, writer);
114 
115         String bar = writer.toString();
116         assertEquals("jason is an asshole. @not.interpolated@ baby @foo@. @bar@", bar);
117     }
118 
119     @Test
120     void interpolationWithSpecifiedBoundaryTokensWithNonInterpolatedValueAtEnd() throws Exception {
121         Map<String, String> m = getStandardMap();
122 
123         String foo = "@name@ is an @foobarred@";
124 
125         LineOrientedInterpolatingReader reader =
126                 new LineOrientedInterpolatingReader(new StringReader(foo), m, "@", "@");
127 
128         StringWriter writer = new StringWriter();
129         IOUtil.copy(reader, writer);
130 
131         String bar = writer.toString();
132         assertEquals("jason is an @foobarred@", bar);
133     }
134 
135     @Test
136     void interpolationWithSpecifiedBoundaryTokensWithInterpolatedValueAtEnd() throws Exception {
137         Map<String, String> m = getStandardMap();
138 
139         String foo = "@name@ is an @noun@";
140 
141         LineOrientedInterpolatingReader reader =
142                 new LineOrientedInterpolatingReader(new StringReader(foo), m, "@", "@");
143 
144         StringWriter writer = new StringWriter();
145         IOUtil.copy(reader, writer);
146 
147         String bar = writer.toString();
148         assertEquals("jason is an asshole", bar);
149     }
150 }