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