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.Reader;
22  import java.io.StringReader;
23  import java.util.Arrays;
24  import java.util.Collections;
25  import java.util.HashSet;
26  
27  import org.codehaus.plexus.interpolation.Interpolator;
28  import org.codehaus.plexus.interpolation.RecursionInterceptor;
29  import org.junit.jupiter.api.Test;
30  import org.junit.jupiter.api.extension.ExtendWith;
31  import org.mockito.Mock;
32  import org.mockito.junit.jupiter.MockitoExtension;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.mockito.ArgumentMatchers.eq;
36  import static org.mockito.ArgumentMatchers.isA;
37  import static org.mockito.Mockito.when;
38  
39  @ExtendWith(MockitoExtension.class)
40  public class MultiDelimiterInterpolatorFilterReaderLineEndingTest
41          extends AbstractInterpolatorFilterReaderLineEndingTest {
42  
43      @Mock
44      private Interpolator interpolator;
45  
46      @Override
47      protected Reader getAaa_AaaReader(Reader in, Interpolator interpolator) {
48          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
49                  new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
50          reader.setDelimiterSpecs(Collections.singleton("aaa*aaa"));
51          return reader;
52      }
53  
54      @Override
55      protected Reader getAbc_AbcReader(Reader in, Interpolator interpolator) {
56          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
57                  new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
58          reader.setDelimiterSpecs(Collections.singleton("abc*abc"));
59          return reader;
60      }
61  
62      @Override
63      protected Reader getDollarBracesReader(Reader in, Interpolator interpolator, String escapeString) {
64          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
65                  new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
66          reader.setDelimiterSpecs(Collections.singleton("${*}"));
67          reader.setEscapeString(escapeString);
68          return reader;
69      }
70  
71      @Override
72      protected Reader getAtReader(Reader in, Interpolator interpolator, String escapeString) {
73          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
74                  new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
75          reader.setDelimiterSpecs(Collections.singleton("@"));
76          reader.setEscapeString(escapeString);
77          return reader;
78      }
79  
80      // MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
81      @Test
82      public void testLineWithSingleAtAndExpression() throws Exception {
83          when(interpolator.interpolate(eq("${foo}"), eq(""), isA(RecursionInterceptor.class)))
84                  .thenReturn("bar");
85  
86          Reader in = new StringReader("toto@titi.com ${foo}");
87          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
88                  new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
89          reader.setDelimiterSpecs(new HashSet<>(Arrays.asList("${*}", "@")));
90  
91          assertEquals("toto@titi.com bar", IOUtils.toString(reader));
92      }
93  
94      // http://stackoverflow.com/questions/21786805/maven-war-plugin-customize-filter-delimitters-in-webresources/
95      @Test
96      public void testAtDollarExpression() throws Exception {
97          when(interpolator.interpolate(eq("${db.server}"), eq(""), isA(RecursionInterceptor.class)))
98                  .thenReturn("DB_SERVER");
99          when(interpolator.interpolate(eq("${db.port}"), eq(""), isA(RecursionInterceptor.class)))
100                 .thenReturn("DB_PORT");
101         when(interpolator.interpolate(eq("${db.name}"), eq(""), isA(RecursionInterceptor.class)))
102                 .thenReturn("DB_NAME");
103 
104         Reader in = new StringReader("  url=\"jdbc:oracle:thin:\\@${db.server}:${db.port}:${db.name}\"");
105         MultiDelimiterInterpolatorFilterReaderLineEnding reader =
106                 new MultiDelimiterInterpolatorFilterReaderLineEnding(in, interpolator, true);
107         reader.setEscapeString("\\");
108         reader.setDelimiterSpecs(new HashSet<>(Arrays.asList("${*}", "@")));
109 
110         assertEquals("  url=\"jdbc:oracle:thin:@DB_SERVER:DB_PORT:DB_NAME\"", IOUtils.toString(reader));
111     }
112 }