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.codehaus.plexus.interpolation.Interpolator;
34  import org.codehaus.plexus.interpolation.RecursionInterceptor;
35  import org.codehaus.plexus.util.IOUtil;
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      @Before
49      public void onSetup()
50      {
51          MockitoAnnotations.initMocks( this );
52      }
53  
54      @Override
55      protected Reader getAaa_AaaReader( Reader in, Interpolator interpolator )
56      {
57          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
58              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
59          reader.setDelimiterSpecs( Collections.singleton( "aaa*aaa" ) );
60          return reader;
61      }
62  
63      @Override
64      protected Reader getAbc_AbcReader( Reader in, Interpolator interpolator )
65      {
66          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
67              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
68          reader.setDelimiterSpecs( Collections.singleton( "abc*abc" ) );
69          return reader;
70      }
71  
72      @Override
73      protected Reader getDollarBracesReader( Reader in, Interpolator interpolator, String escapeString )
74      {
75          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
76              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
77          reader.setDelimiterSpecs( Collections.singleton( "${*}" ) );
78          reader.setEscapeString( escapeString );
79          return reader;
80      }
81  
82      @Override
83      protected Reader getAtReader( Reader in, Interpolator interpolator, String escapeString )
84      {
85          MultiDelimiterInterpolatorFilterReaderLineEnding reader =
86              new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
87          reader.setDelimiterSpecs( Collections.singleton( "@" ) );
88          reader.setEscapeString( escapeString );
89          return reader;
90      }
91  
92      // MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
93      @Test
94      public void testLineWithSingleAtAndExpression()
95          throws Exception
96      {
97          when( interpolator.interpolate( eq( "${foo}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "bar" );
98  
99          Reader in = new StringReader( "toto@titi.com ${foo}" );
100         MultiDelimiterInterpolatorFilterReaderLineEnding reader =
101             new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
102         reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );
103 
104         assertEquals( "toto@titi.com bar", IOUtil.toString( reader ) );
105     }
106 
107     // http://stackoverflow.com/questions/21786805/maven-war-plugin-customize-filter-delimitters-in-webresources/
108     @Test
109     public void testAtDollarExpression()
110         throws Exception
111     {
112         when( interpolator.interpolate( eq( "${db.server}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_SERVER" );
113         when( interpolator.interpolate( eq( "${db.port}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_PORT" );
114         when( interpolator.interpolate( eq( "${db.name}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DB_NAME" );
115 
116         Reader in = new StringReader( "  url=\"jdbc:oracle:thin:\\@${db.server}:${db.port}:${db.name}\"" );
117         MultiDelimiterInterpolatorFilterReaderLineEnding reader =
118             new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
119         reader.setEscapeString( "\\" );
120         reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );
121 
122         assertEquals( "  url=\"jdbc:oracle:thin:@DB_SERVER:DB_PORT:DB_NAME\"", IOUtil.toString( reader ) );
123     }
124 }