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.IOException;
28  import java.io.Reader;
29  import java.io.StringReader;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.codehaus.plexus.interpolation.Interpolator;
33  import org.codehaus.plexus.interpolation.RecursionInterceptor;
34  import org.junit.Before;
35  import org.junit.Test;
36  import org.mockito.Mock;
37  import org.mockito.MockitoAnnotations;
38  
39  public abstract class AbstractInterpolatorFilterReaderLineEndingTest
40  {
41  
42      @Mock
43      private Interpolator interpolator;
44  
45      @Before
46      public void onSetup()
47      {
48          MockitoAnnotations.initMocks( this );
49      }
50  
51      @Test
52      public void testDefaults()
53          throws Exception
54      {
55          when( interpolator.interpolate( eq( "${a}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DONE_A" );
56  
57          Reader in = new StringReader( "text without expression" );
58          Reader reader = getDollarBracesReader( in, interpolator, "\\" );
59          assertEquals( "text without expression", IOUtils.toString( reader ) );
60  
61          in = new StringReader( "valid expression ${a}" );
62          reader = getDollarBracesReader( in, interpolator, null );
63          assertEquals( "valid expression DONE_A", IOUtils.toString( reader ) );
64  
65          in = new StringReader( "empty expression ${}" );
66          reader = getDollarBracesReader( in, interpolator, null );
67          assertEquals( "empty expression ${}", IOUtils.toString( reader ) );
68  
69          in = new StringReader( "dollar space expression $ {a}" );
70          reader = getDollarBracesReader( in, interpolator, "\\" );
71          assertEquals( "dollar space expression $ {a}", IOUtils.toString( reader ) );
72  
73          in = new StringReader( "space in expression ${ a}" );
74          reader = getDollarBracesReader( in, interpolator, "\\" );
75          assertEquals( "space in expression ${ a}", IOUtils.toString( reader ) );
76  
77          in = new StringReader( "escape dollar with expression \\${a}" );
78          reader = getDollarBracesReader( in, interpolator, "\\" );
79          assertEquals( "escape dollar with expression ${a}", IOUtils.toString( reader ) );
80  
81  //        in = new StringReader( "escape escape string before expression \\\\${a}" );
82  //        reader = getDollarBracesReader( in, interpolator, "\\" );
83  //        assertEquals( "escape escape string before expression \\DONE_A", IOUtils.toString( reader ) );
84  //
85  //        in = new StringReader( "escape escape string and expression \\\\\\${a}" );
86  //        reader = getDollarBracesReader( in, interpolator, "\\" );
87  //        assertEquals( "escape escape string before expression \\${a}", IOUtils.toString( reader ) );
88  
89          in = new StringReader( "unknown expression ${unknown}" );
90          reader = getDollarBracesReader( in, interpolator, "\\" );
91          assertEquals( "unknown expression ${unknown}", IOUtils.toString( reader ) );
92      }
93  
94      // MSHARED-198: custom delimiters doesn't work as expected
95      @Test
96      public void testCustomDelimiters()
97          throws Exception
98      {
99          when( interpolator.interpolate( eq( "aaaFILTER.a.MEaaa" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DONE" );
100         when( interpolator.interpolate( eq( "abcFILTER.a.MEabc" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DONE" );
101 
102         Reader in = new StringReader( "aaaFILTER.a.MEaaa" );
103         Reader reader = getAaa_AaaReader( in, interpolator );
104 
105         assertEquals( "DONE", IOUtils.toString( reader ) );
106 
107         in = new StringReader( "abcFILTER.a.MEabc" );
108         reader = getAbc_AbcReader( in, interpolator );
109         assertEquals( "DONE", IOUtils.toString( reader ) );
110     }
111 
112     // MSHARED-235: reader exceeds readAheadLimit
113     @Test
114     public void testMarkInvalid() throws IOException
115     {
116         try ( Reader reader =
117             getAtReader( new StringReader( "@\").replace(p,\"]\").replace(q,\"" ), interpolator, "\\" ) )
118         {
119             assertEquals( "@\").replace(p,\"]\").replace(q,\"", IOUtils.toString( reader ) );
120         }
121     }
122 
123     protected abstract Reader getAbc_AbcReader( Reader in, Interpolator interpolator );
124 
125     protected abstract Reader getAaa_AaaReader( Reader in, Interpolator interpolator );
126 
127     protected abstract Reader getDollarBracesReader( Reader in, Interpolator interpolator, String escapeString );
128 
129     protected abstract Reader getAtReader( Reader in, Interpolator interpolator, String escapeString );
130 
131 }