View Javadoc
1   package org.apache.maven.shared.filtering;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *    http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.mockito.Matchers.eq;
24  import static org.mockito.Matchers.isA;
25  import static org.mockito.Mockito.when;
26  
27  import java.io.Reader;
28  import java.io.StringReader;
29  import java.util.Arrays;
30  import java.util.Collections;
31  import java.util.HashSet;
32  
33  import org.apache.commons.io.IOUtils;
34  import org.codehaus.plexus.interpolation.Interpolator;
35  import org.codehaus.plexus.interpolation.RecursionInterceptor;
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.mockito.Mock;
39  import org.mockito.MockitoAnnotations;
40  
41  public class MultiDelimiterInterpolatorFilterReaderLineEndingTest
42      extends AbstractInterpolatorFilterReaderLineEndingTest
43  {
44  
45      @Mock
46      private Interpolator interpolator;
47  
48      @Override
49      @Before
50      public void onSetup()
51      {
52          MockitoAnnotations.initMocks( this );
53      }
54  
55      @Override
56      protected Reader getAaa_AaaReader( Reader in, Interpolator interpolator )
57      {
58          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
59              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
60          reader.setDelimiterSpecs( Collections.singleton( "aaa*aaa" ) );
61          return reader;
62      }
63  
64      @Override
65      protected Reader getAbc_AbcReader( Reader in, Interpolator interpolator )
66      {
67          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
68              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
69          reader.setDelimiterSpecs( Collections.singleton( "abc*abc" ) );
70          return reader;
71      }
72  
73      @Override
74      protected Reader getDollarBracesReader( Reader in, Interpolator interpolator, String escapeString )
75      {
76          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
77              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
78          reader.setDelimiterSpecs( Collections.singleton( "${*}" ) );
79          reader.setEscapeString( escapeString );
80          return reader;
81      }
82  
83      @Override
84      protected Reader getAtReader( Reader in, Interpolator interpolator, String escapeString )
85      {
86          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
87              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
88          reader.setDelimiterSpecs( Collections.singleton( "@" ) );
89          reader.setEscapeString( escapeString );
90          return reader;
91      }
92  
93      // MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
94      @Test
95      public void testLineWithSingleAtAndExpression()
96          throws Exception
97      {
98          when( interpolator.interpolate( eq( "${foo}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "bar" );
99  
100         Reader in = new StringReader( "toto@titi.com ${foo}" );
101         MultiDelimiterInterpolatorFilterReaderLineEnding reader =
102             new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
103         reader.setDelimiterSpecs( new HashSet<>( Arrays.asList( "${*}", "@" ) ) );
104 
105         assertEquals( "toto@titi.com bar", IOUtils.toString( reader ) );
106     }
107 
108     // http://stackoverflow.com/questions/21786805/maven-war-plugin-customize-filter-delimitters-in-webresources/
109     @Test
110     public void testAtDollarExpression()
111         throws Exception
112     {
113         when( interpolator.interpolate( eq( "${db.server}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_SERVER" );
114         when( interpolator.interpolate( eq( "${db.port}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_PORT" );
115         when( interpolator.interpolate( eq( "${db.name}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_NAME" );
116 
117         Reader in = new StringReader( "  url=\"jdbc:oracle:thin:\\@${db.server}:${db.port}:${db.name}\"" );
118         MultiDelimiterInterpolatorFilterReaderLineEnding reader =
119             new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
120         reader.setEscapeString( "\\" );
121         reader.setDelimiterSpecs( new HashSet<>( Arrays.asList( "${*}", "@" ) ) );
122 
123         assertEquals( "  url=\"jdbc:oracle:thin:@DB_SERVER:DB_PORT:DB_NAME\"", IOUtils.toString( reader ) );
124     }
125 }