View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.filtering;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.io.StringReader;
24  
25  import org.codehaus.plexus.interpolation.Interpolator;
26  import org.codehaus.plexus.interpolation.RecursionInterceptor;
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.api.extension.ExtendWith;
29  import org.mockito.Mock;
30  import org.mockito.junit.jupiter.MockitoExtension;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.mockito.ArgumentMatchers.eq;
34  import static org.mockito.ArgumentMatchers.isA;
35  import static org.mockito.Mockito.when;
36  
37  @ExtendWith(MockitoExtension.class)
38  public abstract class AbstractInterpolatorFilterReaderLineEndingTest {
39  
40      @Mock
41      private Interpolator interpolator;
42  
43      @Test
44      public void testDefaults() throws Exception {
45          when(interpolator.interpolate(eq("${a}"), eq(""), isA(RecursionInterceptor.class)))
46                  .thenReturn("DONE_A");
47  
48          Reader in = new StringReader("text without expression");
49          Reader reader = getDollarBracesReader(in, interpolator, "\\");
50          assertEquals("text without expression", IOUtils.toString(reader));
51  
52          in = new StringReader("valid expression ${a}");
53          reader = getDollarBracesReader(in, interpolator, null);
54          assertEquals("valid expression DONE_A", IOUtils.toString(reader));
55  
56          in = new StringReader("empty expression ${}");
57          reader = getDollarBracesReader(in, interpolator, null);
58          assertEquals("empty expression ${}", IOUtils.toString(reader));
59  
60          in = new StringReader("dollar space expression $ {a}");
61          reader = getDollarBracesReader(in, interpolator, "\\");
62          assertEquals("dollar space expression $ {a}", IOUtils.toString(reader));
63  
64          in = new StringReader("space in expression ${ a}");
65          reader = getDollarBracesReader(in, interpolator, "\\");
66          assertEquals("space in expression ${ a}", IOUtils.toString(reader));
67  
68          in = new StringReader("escape dollar with expression \\${a}");
69          reader = getDollarBracesReader(in, interpolator, "\\");
70          assertEquals("escape dollar with expression ${a}", IOUtils.toString(reader));
71  
72          //        in = new StringReader( "escape escape string before expression \\\\${a}" );
73          //        reader = getDollarBracesReader( in, interpolator, "\\" );
74          //        assertEquals( "escape escape string before expression \\DONE_A", toString( reader ) );
75          //
76          //        in = new StringReader( "escape escape string and expression \\\\\\${a}" );
77          //        reader = getDollarBracesReader( in, interpolator, "\\" );
78          //        assertEquals( "escape escape string before expression \\${a}", toString( reader ) );
79  
80          in = new StringReader("unknown expression ${unknown}");
81          reader = getDollarBracesReader(in, interpolator, "\\");
82          assertEquals("unknown expression ${unknown}", IOUtils.toString(reader));
83      }
84  
85      // MSHARED-198: custom delimiters doesn't work as expected
86      @Test
87      public void testCustomDelimiters() throws Exception {
88          when(interpolator.interpolate(eq("aaaFILTER.a.MEaaa"), eq(""), isA(RecursionInterceptor.class)))
89                  .thenReturn("DONE");
90          when(interpolator.interpolate(eq("abcFILTER.a.MEabc"), eq(""), isA(RecursionInterceptor.class)))
91                  .thenReturn("DONE");
92  
93          Reader in = new StringReader("aaaFILTER.a.MEaaa");
94          Reader reader = getAaa_AaaReader(in, interpolator);
95  
96          assertEquals("DONE", IOUtils.toString(reader));
97  
98          in = new StringReader("abcFILTER.a.MEabc");
99          reader = getAbc_AbcReader(in, interpolator);
100         assertEquals("DONE", IOUtils.toString(reader));
101     }
102 
103     // MSHARED-235: reader exceeds readAheadLimit
104     @Test
105     public void testMarkInvalid() throws IOException {
106         try (Reader reader = getAtReader(new StringReader("@\").replace(p,\"]\").replace(q,\""), interpolator, "\\")) {
107             assertEquals("@\").replace(p,\"]\").replace(q,\"", IOUtils.toString(reader));
108         }
109     }
110 
111     protected abstract Reader getAbc_AbcReader(Reader in, Interpolator interpolator);
112 
113     protected abstract Reader getAaa_AaaReader(Reader in, Interpolator interpolator);
114 
115     protected abstract Reader getDollarBracesReader(Reader in, Interpolator interpolator, String escapeString);
116 
117     protected abstract Reader getAtReader(Reader in, Interpolator interpolator, String escapeString);
118 }