[MSHARED-198] custom delimiters doesn't work as expected
Fixed for InterpolatorFilterReaderLineEnding as well
MSHARED-198 + MSHARED-199 with real unit-test
Introduced mockito
git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1567794 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 96c59e9..a6e21f4 100644
--- a/pom.xml
+++ b/pom.xml
@@ -133,6 +133,12 @@
</dependency>
<dependency>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-core</artifactId>
+ <version>1.9.5</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
diff --git a/src/main/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEnding.java b/src/main/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEnding.java
index 3b26925..f756046 100644
--- a/src/main/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEnding.java
+++ b/src/main/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEnding.java
@@ -311,7 +311,8 @@
in.skip( beginToken.length() );
ch = in.read();
- int end = endToken.length();
+ int endTokenSize = endToken.length();
+ int end = endTokenSize;
do
{
if ( ch == -1 )
@@ -327,7 +328,7 @@
key.append( (char) ch );
- if ( ch == this.endToken.charAt( end - 1 ) )
+ if ( ch == this.endToken.charAt( endTokenSize - end ) )
{
end--;
if ( end == 0 )
@@ -337,7 +338,7 @@
}
else
{
- end = endToken.length();
+ end = endTokenSize;
}
ch = in.read();
diff --git a/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java b/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
new file mode 100644
index 0000000..6d29c96
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
@@ -0,0 +1,72 @@
+package org.apache.maven.shared.filtering;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.when;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.codehaus.plexus.interpolation.Interpolator;
+import org.codehaus.plexus.interpolation.RecursionInterceptor;
+import org.codehaus.plexus.util.IOUtil;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public abstract class AbstractInterpolatorFilterReaderLineEndingTest
+{
+
+ @Mock
+ private Interpolator interpolator;
+
+ @Before
+ public void onSetup()
+ {
+ MockitoAnnotations.initMocks( this );
+ }
+
+ // MSHARED-198: custom delimiters doesn't work as expected
+ @Test
+ public void testCustomDelimiters()
+ throws Exception
+ {
+ when( interpolator.interpolate( eq( "aaaFILTER.a.MEaaa" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DONE" );
+ when( interpolator.interpolate( eq( "abcFILTER.a.MEabc" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "DONE" );
+
+ Reader in = new StringReader( "aaaFILTER.a.MEaaa" );
+ Reader reader = getAaa_AaaReader( in, interpolator );
+
+ assertEquals( "DONE", IOUtil.toString( reader ) );
+
+ in = new StringReader( "abcFILTER.a.MEabc" );
+ reader = getAbc_AbcReader( in, interpolator );
+ assertEquals( "DONE", IOUtil.toString( reader ) );
+ }
+
+ protected abstract Reader getAbc_AbcReader( Reader in, Interpolator interpolator );
+
+ protected abstract Reader getAaa_AaaReader( Reader in, Interpolator interpolator );
+
+}
\ No newline at end of file
diff --git a/src/test/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEndingTest.java b/src/test/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEndingTest.java
new file mode 100644
index 0000000..86752a3
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/filtering/InterpolatorFilterReaderLineEndingTest.java
@@ -0,0 +1,40 @@
+package org.apache.maven.shared.filtering;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.Reader;
+
+import org.codehaus.plexus.interpolation.Interpolator;
+
+public class InterpolatorFilterReaderLineEndingTest
+ extends AbstractInterpolatorFilterReaderLineEndingTest
+{
+ @Override
+ protected Reader getAaa_AaaReader( Reader in, Interpolator interpolator )
+ {
+ return new InterpolatorFilterReaderLineEnding( in, interpolator, "aaa", "aaa", true );
+ }
+
+ @Override
+ protected Reader getAbc_AbcReader( Reader in, Interpolator interpolator )
+ {
+ return new InterpolatorFilterReaderLineEnding( in, interpolator, "abc", "abc", true );
+ }
+}
diff --git a/src/test/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEndingTest.java b/src/test/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEndingTest.java
new file mode 100644
index 0000000..6901358
--- /dev/null
+++ b/src/test/java/org/apache/maven/shared/filtering/MultiDelimiterInterpolatorFilterReaderLineEndingTest.java
@@ -0,0 +1,84 @@
+package org.apache.maven.shared.filtering;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.when;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+
+import org.codehaus.plexus.interpolation.Interpolator;
+import org.codehaus.plexus.interpolation.RecursionInterceptor;
+import org.codehaus.plexus.util.IOUtil;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class MultiDelimiterInterpolatorFilterReaderLineEndingTest extends AbstractInterpolatorFilterReaderLineEndingTest
+{
+
+ @Mock
+ private Interpolator interpolator;
+
+ @Before
+ public void onSetup()
+ {
+ MockitoAnnotations.initMocks( this );
+ }
+
+ @Override
+ protected Reader getAaa_AaaReader( Reader in, Interpolator interpolator )
+ {
+ MultiDelimiterInterpolatorFilterReaderLineEnding reader = new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
+ reader.setDelimiterSpecs( Collections.singleton( "aaa*aaa" ) );
+ return reader;
+ }
+
+ @Override
+ protected Reader getAbc_AbcReader( Reader in, Interpolator interpolator )
+ {
+ MultiDelimiterInterpolatorFilterReaderLineEnding reader = new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
+ reader.setDelimiterSpecs( Collections.singleton( "abc*abc" ) );
+ return reader;
+ }
+
+ // MSHARED-199: Filtering doesn't work if 2 delimiters are used on the same line, the first one being left open
+ @Test
+ public void testLineWithSingleAtAndExpression()
+ throws Exception
+ {
+ when( interpolator.interpolate( eq( "${foo}" ), eq( "" ), isA( RecursionInterceptor.class ) ) ).thenReturn( "bar" );
+
+ Reader in = new StringReader( "toto@titi.com ${foo}" );
+ MultiDelimiterInterpolatorFilterReaderLineEnding reader =
+ new MultiDelimiterInterpolatorFilterReaderLineEnding( in, interpolator, true );
+ reader.setDelimiterSpecs( new HashSet<String>( Arrays.asList( "${*}", "@" ) ) );
+
+ assertEquals( "toto@titi.com bar", IOUtil.toString( reader ) );
+ }
+
+}