HostConfig passed a context name to ContextName(String) - ensure that this is correct...
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 2 Dec 2010 12:56:56 +0000 (12:56 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 2 Dec 2010 12:56:56 +0000 (12:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1041356 13f79535-47bb-0310-9956-ffa450edef68

java/org/apache/catalina/util/ContextName.java
test/org/apache/catalina/util/TestContextName.java

index d8f0a6b..8c957fd 100644 (file)
@@ -33,35 +33,53 @@ public final class ContextName {
     private final String name;
     
     /**
-     * Creates an instance from a base name, directory name, WAR name or
-     * context.xml name.
+     * Creates an instance from a context name, display name, base name,
+     * directory name, WAR name or context.xml name.
      * 
      * @param name  The name to use as the basis for this object
      */
     public ContextName(String name) {
-        // Remove file extension, if any
-        if (name.toLowerCase(Locale.ENGLISH).endsWith(".war") ||
-                name.toLowerCase(Locale.ENGLISH).endsWith(".xml")) {
-            baseName = name.substring(0, name.length() -4);
-        } else {
-            baseName = name;
+        
+        String tmp1 = name;
+        
+        // Convert Context names and display names to base names
+        
+        // Strip off any leading "/"
+        if (tmp1.startsWith("/")) {
+            tmp1 = tmp1.substring(1);
+        }
+        
+        // Replace any remaining /
+        tmp1.replaceAll("/", FWD_SLASH_REPLACEMENT);
+        
+        // Insert the ROOT name if required
+        if (tmp1.startsWith(VERSION_MARKER) || "".equals(tmp1)) {
+            tmp1 = ROOT_NAME + tmp1;
         }
 
-        String tmp;
+        // Remove any file extensions
+        if (tmp1.toLowerCase(Locale.ENGLISH).endsWith(".war") ||
+                tmp1.toLowerCase(Locale.ENGLISH).endsWith(".xml")) {
+            tmp1 = tmp1.substring(0, tmp1.length() -4);
+        }
+
+        baseName = tmp1;
+        
+        String tmp2;
         // Extract version number
         int versionIndex = baseName.indexOf(VERSION_MARKER);
         if (versionIndex > -1) {
             version = baseName.substring(versionIndex + 2);
-            tmp = baseName.substring(0, versionIndex);
+            tmp2 = baseName.substring(0, versionIndex);
         } else {
             version = "";
-            tmp = baseName;
+            tmp2 = baseName;
         }
 
-        if (ROOT_NAME.equals(tmp)) {
+        if (ROOT_NAME.equals(tmp2)) {
             path = "";
         } else {
-            path = "/" + tmp.replaceAll(FWD_SLASH_REPLACEMENT, "/");
+            path = "/" + tmp2.replaceAll(FWD_SLASH_REPLACEMENT, "/");
         }
         
         if (versionIndex > -1) {
index 1c74f6a..bb08b35 100644 (file)
@@ -137,4 +137,41 @@ public class TestContextName extends TestCase {
         assertEquals("/foo/bar##E", cn16.getDisplayName());
     }
 
+    public void testConstructorStringWithName() {
+        assertEquals("", new ContextName(cn1.getName()).getName());
+        assertEquals("", new ContextName(cn2.getName()).getName());
+        assertEquals("", new ContextName(cn3.getName()).getName());
+        assertEquals("/foo", new ContextName(cn4.getName()).getName());
+        assertEquals("/foo/bar", new ContextName(cn5.getName()).getName());
+        assertEquals("##A", new ContextName(cn6.getName()).getName());
+        assertEquals("##B", new ContextName(cn7.getName()).getName());
+        assertEquals("##C", new ContextName(cn8.getName()).getName());
+        assertEquals("/foo##D", new ContextName(cn9.getName()).getName());
+        assertEquals("/foo/bar##E", new ContextName(cn10.getName()).getName());
+        assertEquals("", new ContextName(cn11.getName()).getName());
+        assertEquals("/foo", new ContextName(cn12.getName()).getName());
+        assertEquals("/foo/bar", new ContextName(cn13.getName()).getName());
+        assertEquals("##A", new ContextName(cn14.getName()).getName());
+        assertEquals("/foo##D", new ContextName(cn15.getName()).getName());
+        assertEquals("/foo/bar##E", new ContextName(cn16.getName()).getName());
+    }
+
+    public void testConstructorStringWithDisplayName() {
+        assertEquals("", new ContextName(cn1.getDisplayName()).getName());
+        assertEquals("", new ContextName(cn2.getDisplayName()).getName());
+        assertEquals("", new ContextName(cn3.getDisplayName()).getName());
+        assertEquals("/foo", new ContextName(cn4.getDisplayName()).getName());
+        assertEquals("/foo/bar", new ContextName(cn5.getDisplayName()).getName());
+        assertEquals("##A", new ContextName(cn6.getDisplayName()).getName());
+        assertEquals("##B", new ContextName(cn7.getDisplayName()).getName());
+        assertEquals("##C", new ContextName(cn8.getDisplayName()).getName());
+        assertEquals("/foo##D", new ContextName(cn9.getDisplayName()).getName());
+        assertEquals("/foo/bar##E", new ContextName(cn10.getDisplayName()).getName());
+        assertEquals("", new ContextName(cn11.getDisplayName()).getName());
+        assertEquals("/foo", new ContextName(cn12.getDisplayName()).getName());
+        assertEquals("/foo/bar", new ContextName(cn13.getDisplayName()).getName());
+        assertEquals("##A", new ContextName(cn14.getDisplayName()).getName());
+        assertEquals("/foo##D", new ContextName(cn15.getDisplayName()).getName());
+        assertEquals("/foo/bar##E", new ContextName(cn16.getDisplayName()).getName());
+    }
 }