Author: markt
Date: Thu Sep 13 21:50:33 2012
New Revision: 1384557

URL: http://svn.apache.org/viewvc?rev=1384557&view=rev
Log:
Add unit tests for DirResourceSets mounted below the web app root. Fixed a 
couple of identified failures including an nasty edge case at the root of the 
mounted DirResourceSet

Added:
    
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java
   (with props)
Modified:
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
    
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
    
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSet.java

Modified: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java?rev=1384557&r1=1384556&r2=1384557&view=diff
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
 (original)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/DirResourceSet.java
 Thu Sep 13 21:50:33 2012
@@ -151,7 +151,13 @@ public class DirResourceSet extends Abst
                     sm.getString("dirResourceSet.writeNpe"));
         }
 
-        File dest = new File(base, path);
+        File dest = null;
+        if (path.startsWith(webAppMount)) {
+            dest = new File(base, path.substring(webAppMount.length()));
+        } else {
+            return false;
+        }
+
         if (dest.exists()) {
             throw new IllegalArgumentException(
                     sm.getString("dirResourceSet.writeExists"));

Modified: 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java?rev=1384557&r1=1384556&r2=1384557&view=diff
==============================================================================
--- 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
 (original)
+++ 
tomcat/sandbox/trunk-resources/java/org/apache/catalina/webresources/FileResource.java
 Thu Sep 13 21:50:33 2012
@@ -35,11 +35,29 @@ public class FileResource extends Abstra
     private static final Log log = LogFactory.getLog(FileResource.class);
 
     private final File resource;
+    private final String name;
 
     public FileResource(WebResourceRoot root, File resource,
             String webAppPath) {
         super(root,webAppPath);
         this.resource = resource;
+
+        if (webAppPath.charAt(webAppPath.length() - 1) == '/') {
+            String realName = resource.getName() + '/';
+            if (webAppPath.endsWith(realName)) {
+                name = resource.getName();
+            } else {
+                // This is the root directory of a mounted ResourceSet
+                // Need to return the mounted name, not the real name
+                int endOfName = webAppPath.length() - 1;
+                name = webAppPath.substring(
+                        webAppPath.lastIndexOf("/", endOfName - 1) + 1,
+                        endOfName);
+            }
+        } else {
+            // Must be a file
+            name = resource.getName();
+        }
     }
 
     @Override
@@ -69,7 +87,7 @@ public class FileResource extends Abstra
 
     @Override
     public String getName() {
-        return resource.getName();
+        return name;
     }
 
     @Override

Modified: 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSet.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSet.java?rev=1384557&r1=1384556&r2=1384557&view=diff
==============================================================================
--- 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSet.java
 (original)
+++ 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSet.java
 Thu Sep 13 21:50:33 2012
@@ -221,7 +221,8 @@ public class TestDirResourceSet {
 
     @Test(expected = IllegalArgumentException.class)
     public void testWriteEmpty() {
-        dirResourceSet.write("", null);
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("", is);
     }
 
     @Test(expected = IllegalArgumentException.class)

Added: 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java
URL: 
http://svn.apache.org/viewvc/tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java?rev=1384557&view=auto
==============================================================================
--- 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java
 (added)
+++ 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java
 Thu Sep 13 21:50:33 2012
@@ -0,0 +1,300 @@
+/*
+ * 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.catalina.webresources;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.catalina.WebResource;
+
+public class TestDirResourceSetMount {
+
+    private DirResourceSet dirResourceSet;
+
+    @Before
+    public void setup() {
+        File f = new File("test/webresources");
+        dirResourceSet = new DirResourceSet(
+                new TesterWebResourceRoot(), f, "/mount", "");
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testGetResourceEmpty() {
+        dirResourceSet.getResource("");
+    }
+
+    @Test
+    public void testGetResourceAbove() {
+        WebResource webResource = dirResourceSet.getResource("/");
+        Assert.assertFalse(webResource.exists());
+    }
+
+    @Test
+    public void testGetResourceRoot() {
+        WebResource webResource = dirResourceSet.getResource("/mount");
+        Assert.assertTrue(webResource.isDirectory());
+        Assert.assertEquals("mount", webResource.getName());
+        Assert.assertEquals("/mount/", webResource.getWebappPath());
+    }
+
+    @Test
+    public void testGetResourceDirA() {
+        WebResource webResource = dirResourceSet.getResource("/mount/d1");
+        Assert.assertTrue(webResource.isDirectory());
+        Assert.assertEquals("d1", webResource.getName());
+        Assert.assertEquals("/mount/d1/", webResource.getWebappPath());
+    }
+
+    @Test
+    public void testGetResourceDirB() {
+        WebResource webResource = dirResourceSet.getResource("/mount/d1/");
+        Assert.assertTrue(webResource.isDirectory());
+        Assert.assertEquals("d1", webResource.getName());
+        Assert.assertEquals("/mount/d1/", webResource.getWebappPath());
+    }
+
+    @Test
+    public void testGetResourceFile() {
+        WebResource webResource =
+                dirResourceSet.getResource("/mount/d1/d1-f1.txt");
+        Assert.assertTrue(webResource.isFile());
+        Assert.assertEquals("d1-f1.txt", webResource.getName());
+        Assert.assertEquals("/mount/d1/d1-f1.txt", 
webResource.getWebappPath());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testListEmpty() {
+        dirResourceSet.list("");
+    }
+
+    @Test
+    public void testListAbove() {
+        String[] results = dirResourceSet.list("/");
+
+        Assert.assertNotNull(results);
+        Assert.assertEquals(0, results.length);
+    }
+
+    @Test
+    public void testListRoot() {
+        String[] results = dirResourceSet.list("/mount");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("d1");
+        expected.add("d2");
+        expected.add("f1.txt");
+        expected.add("f2.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListDirA() {
+        String[] results = dirResourceSet.list("/mount/d1");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("d1-f1.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListDirB() {
+        String[] results = dirResourceSet.list("/mount/d1/");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("d1-f1.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListFile() {
+        String[] results = dirResourceSet.list("/mount/d1/d1-f1.txt");
+
+        Assert.assertNotNull(results);
+        Assert.assertEquals(0, results.length);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testListWebAppPathsEmpty() {
+        dirResourceSet.listWebAppPaths("");
+    }
+
+    @Test
+    public void testListWebAppPathsAbove() {
+        Set<String> results = dirResourceSet.listWebAppPaths("/");
+
+        Assert.assertEquals(0, results.size());
+    }
+
+    @Test
+    public void testListWebAppPathsRoot() {
+        Set<String> results = dirResourceSet.listWebAppPaths("/mount");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("/mount/d1/");
+        expected.add("/mount/d2/");
+        expected.add("/mount/f1.txt");
+        expected.add("/mount/f2.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListWebAppPathsDirA() {
+        Set<String> results = dirResourceSet.listWebAppPaths("/mount/d1");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("/mount/d1/d1-f1.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListWebAppPathsDirB() {
+        Set<String> results = dirResourceSet.listWebAppPaths("/mount/d1/");
+
+        Set<String> expected = new HashSet<>();
+        expected.add("/mount/d1/d1-f1.txt");
+
+        for (String result : results) {
+            Assert.assertTrue(result, expected.remove(result));
+        }
+        Assert.assertEquals(0, expected.size());
+    }
+
+    @Test
+    public void testListWebAppPathsFile() {
+        Set<String> results =
+                dirResourceSet.listWebAppPaths("/mount/d1/d1-f1.txt");
+
+        Assert.assertEquals(0, results.size());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testMkdirEmpty() {
+        dirResourceSet.mkdir("");
+    }
+
+    @Test
+    public void testMkdirAbove() {
+        Assert.assertFalse(dirResourceSet.mkdir("/"));
+    }
+
+    @Test
+    public void testMkdirRoot() {
+        Assert.assertFalse(dirResourceSet.mkdir("/mount"));
+    }
+
+    @Test
+    public void testMkdirDirA() {
+        Assert.assertFalse(dirResourceSet.mkdir("/mount/d1"));
+    }
+
+    @Test
+    public void testMkdirDirB() {
+        Assert.assertFalse(dirResourceSet.mkdir("/mount/d1/"));
+    }
+
+    @Test
+    public void testMkdirFile() {
+        Assert.assertFalse(dirResourceSet.mkdir("/mount/d1/d1-f1.txt"));
+    }
+
+    @Test
+    public void testMkdirNew() {
+        Assert.assertTrue(dirResourceSet.mkdir("/mount/new-test"));
+
+        File file = new File("test/webresources/new-test");
+        Assert.assertTrue(file.isDirectory());
+        Assert.assertTrue(file.delete());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWriteEmpty() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("", is);
+   }
+
+    @Test
+    public void testWriteAbove() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        Assert.assertFalse(dirResourceSet.write("/", is));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWriteRoot() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("/mount", is);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWriteDirA() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("/mount/d1", is);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWriteDirB() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("/mount/d1/", is);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testWriteFile() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        dirResourceSet.write("/mount/d1/d1-f1.txt", is);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testWriteNew() {
+        dirResourceSet.write("/mount/new-test", null);
+    }
+
+    @Test
+    public void testWrite() {
+        InputStream is = new ByteArrayInputStream("test".getBytes());
+        Assert.assertTrue(dirResourceSet.write("/mount/new-test", is));
+
+        File file = new File("test/webresources/new-test");
+        Assert.assertTrue(file.exists());
+        Assert.assertTrue(file.delete());
+    }
+}

Propchange: 
tomcat/sandbox/trunk-resources/test/org/apache/catalina/webresources/TestDirResourceSetMount.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to