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.apache.commons.io.IOUtils;
26  import org.codehaus.plexus.interpolation.Interpolator;
27  import org.codehaus.plexus.interpolation.RecursionInterceptor;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.mockito.Mock;
31  import org.mockito.MockitoAnnotations;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.mockito.ArgumentMatchers.eq;
35  import static org.mockito.ArgumentMatchers.isA;
36  import static org.mockito.Mockito.when;
37  
38  public abstract class AbstractInterpolatorFilterReaderLineEndingTest {
39  
40      @Mock
41      private Interpolator interpolator;
42  
43      @BeforeEach
44      public void onSetup() {
45          MockitoAnnotations.openMocks(this);
46      }
47  
48      @Test
49      void defaults() throws Exception {
50          when(interpolator.interpolate(eq("${a}"), eq(""), isA(RecursionInterceptor.class)))
51                  .thenReturn("DONE_A");
52  
53          Reader in = new StringReader("text without expression");
54          Reader reader = getDollarBracesReader(in, interpolator, "\\");
55          assertEquals("text without expression", IOUtils.toString(reader));
56  
57          in = new StringReader("valid expression ${a}");
58          reader = getDollarBracesReader(in, interpolator, null);
59          assertEquals("valid expression DONE_A", IOUtils.toString(reader));
60  
61          in = new StringReader("empty expression ${}");
62          reader = getDollarBracesReader(in, interpolator, null);
63          assertEquals("empty expression ${}", IOUtils.toString(reader));
64  
65          in = new StringReader("dollar space expression $ {a}");
66          reader = getDollarBracesReader(in, interpolator, "\\");
67          assertEquals("dollar space expression $ {a}", IOUtils.toString(reader));
68  
69          in = new StringReader("space in expression ${ a}");
70          reader = getDollarBracesReader(in, interpolator, "\\");
71          assertEquals("space in expression ${ a}", IOUtils.toString(reader));
72  
73          in = new StringReader("escape dollar with expression \\${a}");
74          reader = getDollarBracesReader(in, interpolator, "\\");
75          assertEquals("escape dollar with expression ${a}", IOUtils.toString(reader));
76  
77          //        in = new StringReader( "escape escape string before expression \\\\${a}" );
78          //        reader = getDollarBracesReader( in, interpolator, "\\" );
79          //        assertEquals( "escape escape string before expression \\DONE_A", IOUtils.toString( reader ) );
80          //
81          //        in = new StringReader( "escape escape string and expression \\\\\\${a}" );
82          //        reader = getDollarBracesReader( in, interpolator, "\\" );
83          //        assertEquals( "escape escape string before expression \\${a}", IOUtils.toString( reader ) );
84  
85          in = new StringReader("unknown expression ${unknown}");
86          reader = getDollarBracesReader(in, interpolator, "\\");
87          assertEquals("unknown expression ${unknown}", IOUtils.toString(reader));
88      }
89  
90      // MSHARED-198: custom delimiters doesn't work as expected
91      @Test
92      void customDelimiters() throws Exception {
93          when(interpolator.interpolate(eq("aaaFILTER.a.MEaaa"), eq(""), isA(RecursionInterceptor.class)))
94                  .thenReturn("DONE");
95          when(interpolator.interpolate(eq("abcFILTER.a.MEabc"), eq(""), isA(RecursionInterceptor.class)))
96                  .thenReturn("DONE");
97  
98          Reader in = new StringReader("aaaFILTER.a.MEaaa");
99          Reader reader = getAaa_AaaReader(in, interpolator);
100 
101         assertEquals("DONE", IOUtils.toString(reader));
102 
103         in = new StringReader("abcFILTER.a.MEabc");
104         reader = getAbc_AbcReader(in, interpolator);
105         assertEquals("DONE", IOUtils.toString(reader));
106     }
107 
108     // MSHARED-235: reader exceeds readAheadLimit
109     @Test
110     void markInvalid() throws IOException {
111         try (Reader reader = getAtReader(new StringReader("@\").replace(p,\"]\").replace(q,\""), interpolator, "\\")) {
112             assertEquals("@\").replace(p,\"]\").replace(q,\"", IOUtils.toString(reader));
113         }
114     }
115 
116     protected abstract Reader getAbc_AbcReader(Reader in, Interpolator interpolator);
117 
118     protected abstract Reader getAaa_AaaReader(Reader in, Interpolator interpolator);
119 
120     protected abstract Reader getDollarBracesReader(Reader in, Interpolator interpolator, String escapeString);
121 
122     protected abstract Reader getAtReader(Reader in, Interpolator interpolator, String escapeString);
123 }