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