SLING-5712 - avoid windows file separator issues in ClassResourceVisitor, based on a patch by Emanuele Lombardi, thanks!

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1743376 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitor.java b/src/main/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitor.java
index 7373fee..41f5287 100644
--- a/src/main/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitor.java
+++ b/src/main/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitor.java
@@ -61,8 +61,9 @@
         
         if("file".equals(protocol)) {
             // Get base path and remove ending slash
-            String basePath = clazz.getResource("/").getPath();
-            basePath = basePath.substring(0, basePath.length() - 1);
+            //String basePath = clazz.getResource("/").getPath();
+            //basePath = basePath.substring(0, basePath.length() - 1);
+            final String basePath = new File(clazz.getResource("/").getPath()).getAbsolutePath();
             processFile(basePath, new File(resourceURL.getPath()), p);
             
         } else if("jar".equals(protocol)) {
@@ -107,6 +108,13 @@
         }
     }
     
+    /* Backslashes are valid in Zip-files, BUT Java expects paths to be delimited by forward slashes.
+     * Windows provides file paths using backslashes, which need to be converted here.
+     */
+    static String sanitizeResourceName(String basePath, File resource) {
+        return resource.getAbsolutePath().substring(basePath.length()).replace("\\", "/");
+    }
+    
     private void processFile(String basePath, File f, Processor p) throws IOException {
         if(f.isDirectory()) {
             final String [] names = f.list();
@@ -118,7 +126,7 @@
         } else {
             final InputStream is = new BufferedInputStream(new FileInputStream(f));
             try {
-                p.process(f.getAbsolutePath().substring(basePath.length()), is);
+                p.process(sanitizeResourceName(basePath, f), is);            
             } finally {
                 is.close();
             }
diff --git a/src/test/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitorTest.java b/src/test/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitorTest.java
index af7b77f..c8f7628 100644
--- a/src/test/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitorTest.java
+++ b/src/test/java/org/apache/sling/testing/teleporter/client/ClassResourceVisitorTest.java
@@ -16,8 +16,8 @@
  */
 package org.apache.sling.testing.teleporter.client;
 
-import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
diff --git a/src/test/java/org/apache/sling/testing/teleporter/client/SanitizeResourceNameTest.java b/src/test/java/org/apache/sling/testing/teleporter/client/SanitizeResourceNameTest.java
new file mode 100644
index 0000000..5e0ceb2
--- /dev/null
+++ b/src/test/java/org/apache/sling/testing/teleporter/client/SanitizeResourceNameTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.
+ */
+package org.apache.sling.testing.teleporter.client;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.util.Arrays;
+import java.util.Collection;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+@RunWith(Parameterized.class)
+public class SanitizeResourceNameTest {
+    @Parameterized.Parameters(name = "{0}/{1}")
+    public static Collection<String[]> data() {
+        return Arrays.asList(new String[][] {
+                { "some/base", "r/a/b", "/r/a/b" },
+                { "some\\base", "r\\c\\d", "/r/c/d" },
+                { "some/base", "r\\e\\f", "/r/e/f" },
+                { "some\\base", "r/g/h/i", "/r/g/h/i" },
+        });
+    }
+    
+    private final String basePath;
+    private final String path;
+    private final String expected;
+    
+    public SanitizeResourceNameTest(String basePath, String path, String expected) {
+        this.basePath = File.separator + basePath;
+        this.path = path;
+        this.expected = expected;
+    }
+    
+    @Test
+    public void sanitize() {
+        final String actual = ClassResourceVisitor.sanitizeResourceName(basePath, new File(basePath, path));
+        assertEquals(expected, actual);
+    }
+}
\ No newline at end of file