[MSHARED-599] Escaping the escape string produces incorrect output.

o Updated to correctly suppress exceptions when closing resources.
o Updated to add tests for MSHARED-599.

-- dIESE und die folgenden Zeilen werden ignoriert --

M    src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
M    src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
M    src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
M    src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
M    src/test/java/org/apache/maven/shared/filtering/EscapeStringTest.java
M    src/test/java/org/apache/maven/shared/filtering/IncrementalResourceFilteringTest.java
M    src/test/java/org/apache/maven/shared/filtering/MuliLinesMavenResourcesFilteringTest.java
A    src/test/units-files/MRESOURCES-230
AM   src/test/units-files/MRESOURCES-230/expected.txt
AM   src/test/units-files/MRESOURCES-230/resource.txt


git-svn-id: https://svn.apache.org/repos/asf/maven/shared/trunk@1770766 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
index 5f2251c..1648db5 100644
--- a/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
+++ b/src/main/java/org/apache/maven/shared/filtering/DefaultMavenFileFilter.java
@@ -145,6 +145,10 @@
                 Reader src = readerFilter.filter( fileReader, true, wrappers );
 
                 IOUtil.copy( src, fileWriter );
+                fileReader.close();
+                fileReader = null;
+                fileWriter.close();
+                fileWriter = null;
             }
             finally
             {
diff --git a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
index 6c0e788..d03277c 100644
--- a/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
+++ b/src/main/java/org/apache/maven/shared/filtering/PropertyUtils.java
@@ -48,7 +48,7 @@
      * The properties are resolved iteratively, so if the value of property A refers to property B, then after
      * resolution the value of property B will contain the value of property B.
      * </p>
-     * 
+     *
      * @param propFile The property file to load.
      * @param baseProps Properties containing the initial values to substitute into the properties file.
      * @return Properties object containing the properties in the file with their values fully resolved.
@@ -63,10 +63,13 @@
         }
 
         final Properties fileProps = new Properties();
-        final FileInputStream inStream = new FileInputStream( propFile );
+        FileInputStream inStream = null;
         try
         {
+            inStream = new FileInputStream( propFile );
             fileProps.load( inStream );
+            inStream.close();
+            inStream = null;
         }
         finally
         {
diff --git a/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java b/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
index 89824f4..f381fb6 100644
--- a/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/AbstractInterpolatorFilterReaderLineEndingTest.java
@@ -77,6 +77,14 @@
         reader = getDollarBracesReader( in, interpolator, "\\" );
         assertEquals( "escape dollar with expression ${a}", IOUtil.toString( reader ) );
 
+        in = new StringReader( "escape escape string before expression \\\\${a}" );
+        reader = getDollarBracesReader( in, interpolator, "\\" );
+        assertEquals( "escape escape string before expression \\DONE_A", IOUtil.toString( reader ) );
+
+        in = new StringReader( "escape escape string and expression \\\\\\${a}" );
+        reader = getDollarBracesReader( in, interpolator, "\\" );
+        assertEquals( "escape escape string before expression \\${a}", IOUtil.toString( reader ) );
+
         in = new StringReader( "unknown expression ${unknown}" );
         reader = getDollarBracesReader( in, interpolator, "\\" );
         assertEquals( "unknown expression ${unknown}", IOUtil.toString( reader ) );
diff --git a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
index 058fd57..968ff66 100644
--- a/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/DefaultMavenResourcesFilteringTest.java
@@ -141,6 +141,8 @@
         {
             in = new FileInputStream( new File( outputDirectory, "session-filter-target.txt" ) );
             result.load( in );
+            in.close();
+            in = null;
         }
         finally
         {
@@ -241,6 +243,8 @@
         {
             in = new FileInputStream( new File( outputDirectory, "empty-maven-resources-filtering.txt" ) );
             result.load( in );
+            in.close();
+            in = null;
         }
         finally
         {
@@ -255,6 +259,8 @@
         {
             in = new FileInputStream( new File( outputDirectory, "maven-resources-filtering.txt" ) );
             result.load( in );
+            in.close();
+            in = null;
         }
         finally
         {
@@ -424,6 +430,12 @@
                     return false;
                 }
             }
+
+            expectedIn.close();
+            expectedIn = null;
+
+            currentIn.close();
+            currentIn = null;
         }
         finally
         {
@@ -857,6 +869,47 @@
     }
 
     /**
+     * unit test for MRESOURCES-230 : https://issues.apache.org/jira/browse/MRESOURCES-230
+     */
+    public void testCorrectlyEscapesEscapeString()
+        throws Exception
+    {
+        StubMavenProject mavenProject = new StubMavenProject( new File( "/foo/bar" ) );
+
+        mavenProject.setVersion( "1.0" );
+        mavenProject.addProperty( "a", "DONE_A" );
+
+        MavenResourcesFiltering mavenResourcesFiltering = lookup( MavenResourcesFiltering.class );
+
+        List<Resource> resources = new ArrayList<Resource>();
+        resources.add( new Resource()
+        {
+
+            {
+                setDirectory( getBasedir() + "/src/test/units-files/MRESOURCES-230" );
+                setFiltering( true );
+            }
+
+        } );
+        resources.get( 0 ).addExclude( "expected.txt" );
+
+        File output = new File( outputDirectory, "MRESOURCES-230" );
+        MavenResourcesExecution mavenResourcesExecution =
+            new MavenResourcesExecution( resources, output, mavenProject, "UTF-8", Collections.<String>emptyList(),
+                                         Collections.<String>emptyList(), new StubMavenSession() );
+        mavenResourcesExecution.setIncludeEmptyDirs( true );
+        mavenResourcesExecution.setEscapeString( "\\" );
+
+        mavenResourcesFiltering.filterResources( mavenResourcesExecution );
+
+        final String filtered = FileUtils.fileRead( new File( output, "resource.txt" ), "UTF-8" );
+        final String expected =
+            FileUtils.fileRead( new File( getBasedir() + "/src/test/units-files/MRESOURCES-230/expected.txt" ) );
+
+        assertEquals( expected, filtered );
+    }
+
+    /**
      * unit test for edge cases : https://issues.apache.org/jira/browse/MSHARED-228
      */
     @SuppressWarnings( "serial" )
diff --git a/src/test/java/org/apache/maven/shared/filtering/EscapeStringTest.java b/src/test/java/org/apache/maven/shared/filtering/EscapeStringTest.java
index b0187e2..73cf726 100644
--- a/src/test/java/org/apache/maven/shared/filtering/EscapeStringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/EscapeStringTest.java
@@ -91,8 +91,10 @@
         FileInputStream in = null;
         try
         {
-            String content = IOUtil.toString( new FileInputStream( new File( outputDirectory, "content.xml" ) ) );
-
+            in = new FileInputStream( new File( outputDirectory, "content.xml" ) );
+            String content = IOUtil.toString( in );
+            in.close();
+            in = null;
             assertTrue( content.contains( "<broken-tag>Content with replacement: I am the replacement !</broken-tag>" ) );
             assertTrue( content.contains( "<broken-tag>Content with escaped replacement: Do not ${replaceThis} !</broken-tag>" ) );
         }
diff --git a/src/test/java/org/apache/maven/shared/filtering/IncrementalResourceFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/IncrementalResourceFilteringTest.java
index 10a4216..abe834b 100644
--- a/src/test/java/org/apache/maven/shared/filtering/IncrementalResourceFilteringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/IncrementalResourceFilteringTest.java
@@ -161,10 +161,13 @@
     {
         Properties properties = new Properties();
 
-        InputStream is = new FileInputStream( new File( outputDirectory, relpath ) );
+        InputStream is = null;
         try
         {
+            is = new FileInputStream( new File( outputDirectory, relpath ) );
             properties.load( is );
+            is.close();
+            is = null;
         }
         finally
         {
diff --git a/src/test/java/org/apache/maven/shared/filtering/MuliLinesMavenResourcesFilteringTest.java b/src/test/java/org/apache/maven/shared/filtering/MuliLinesMavenResourcesFilteringTest.java
index f6b87ec..f26e678 100644
--- a/src/test/java/org/apache/maven/shared/filtering/MuliLinesMavenResourcesFilteringTest.java
+++ b/src/test/java/org/apache/maven/shared/filtering/MuliLinesMavenResourcesFilteringTest.java
@@ -96,6 +96,8 @@
         {
             in = new FileInputStream( new File( outputDirectory, "test.properties" ) );
             result.load( in );
+            in.close();
+            in = null;
         }
         finally
         {
diff --git a/src/test/units-files/MRESOURCES-230/expected.txt b/src/test/units-files/MRESOURCES-230/expected.txt
new file mode 100644
index 0000000..90dcf9f
--- /dev/null
+++ b/src/test/units-files/MRESOURCES-230/expected.txt
@@ -0,0 +1,22 @@
+# 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.
+
+\
+\\
+\\\
+\\\DONE_A
+\\\${a}
diff --git a/src/test/units-files/MRESOURCES-230/resource.txt b/src/test/units-files/MRESOURCES-230/resource.txt
new file mode 100644
index 0000000..fe3a91d
--- /dev/null
+++ b/src/test/units-files/MRESOURCES-230/resource.txt
@@ -0,0 +1,22 @@
+# 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.
+
+\\
+\\\\
+\\\\\\
+\\\\\\${a}
+\\\\\\\${a}