Author: kkolinko
Date: Sun Jul 13 14:13:27 2014
New Revision: 1610203

URL: http://svn.apache.org/r1610203
Log:
Fix an error with removal of the last context version in 
removeContextVersion(), add test.
The issue was reported for Tomcat 7 in "Time for 7.0.55" thread on dev mailing 
list.
It is backport of r1610197.

Modified:
    tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/mapper/Mapper.java
    tomcat/tc7.0.x/trunk/test/org/apache/tomcat/util/http/mapper/TestMapper.java

Modified: 
tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/mapper/Mapper.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/mapper/Mapper.java?rev=1610203&r1=1610202&r2=1610203&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/mapper/Mapper.java 
(original)
+++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/util/http/mapper/Mapper.java 
Sun Jul 13 14:13:27 2014
@@ -330,7 +330,7 @@ public final class Mapper {
             ContextVersion[] newContextVersions =
                 new ContextVersion[contextVersions.length - 1];
             if (removeMap(contextVersions, newContextVersions, version)) {
-                if (context.versions.length == 0) {
+                if (newContextVersions.length == 0) {
                     // Remove the context
                     ContextList newContextList = 
contextList.removeContext(path);
                     if (newContextList != null) {
@@ -776,7 +776,7 @@ public final class Mapper {
 
         ContextVersion contextVersion = null;
         ContextVersion[] contextVersions = context.versions;
-        int versionCount = contextVersions.length;
+        final int versionCount = contextVersions.length;
         if (versionCount > 1) {
             Object[] contextObjects = new Object[contextVersions.length];
             for (int i = 0; i < contextObjects.length; i++) {

Modified: 
tomcat/tc7.0.x/trunk/test/org/apache/tomcat/util/http/mapper/TestMapper.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/tomcat/util/http/mapper/TestMapper.java?rev=1610203&r1=1610202&r2=1610203&view=diff
==============================================================================
--- 
tomcat/tc7.0.x/trunk/test/org/apache/tomcat/util/http/mapper/TestMapper.java 
(original)
+++ 
tomcat/tc7.0.x/trunk/test/org/apache/tomcat/util/http/mapper/TestMapper.java 
Sun Jul 13 14:13:27 2014
@@ -21,6 +21,7 @@ import java.util.concurrent.atomic.Atomi
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 
@@ -226,6 +227,88 @@ public class TestMapper extends LoggingB
     }
 
     @Test
+    public void testAddRemoveContextVersion() throws Exception {
+        final String hostName = "iowejoiejfoiew";
+        final int iowPos = 3;
+        final String contextPath = "/foo/bar";
+        final int contextPos = 2;
+
+        MappingData mappingData = new MappingData();
+        MessageBytes hostMB = MessageBytes.newInstance();
+        MessageBytes uriMB = MessageBytes.newInstance();
+        hostMB.setString(hostName);
+        uriMB.setString("/foo/bar/blah/bobou/foo");
+
+        // Verifying configuration created by setUp()
+        Mapper.Host mappedHost = mapper.hosts[iowPos];
+        assertEquals(hostName, mappedHost.name);
+        Mapper.Context mappedContext = 
mappedHost.contextList.contexts[contextPos];
+        assertEquals(contextPath, mappedContext.name);
+        assertEquals(1, mappedContext.versions.length);
+        assertEquals("0", mappedContext.versions[0].name);
+        Object oldHost = mappedHost.object;
+
+        mappingData.recycle();
+        mapper.map(hostMB, uriMB, null, mappingData);
+        assertEquals("blah7", mappingData.host.toString());
+        assertEquals("context2", mappingData.context.toString());
+
+        Object newContext = "newContext";
+        mapper.addContextVersion(
+                hostName,
+                oldHost,
+                contextPath,
+                "1",
+                newContext,
+                null,
+                null,
+                Arrays.asList(new WrapperMappingInfo[] { new 
WrapperMappingInfo(
+                        "/", "default", false, false) }));
+
+        assertEquals(2, mappedContext.versions.length);
+        assertEquals("0", mappedContext.versions[0].name);
+        assertEquals("1", mappedContext.versions[1].name);
+        mappingData.recycle();
+        mapper.map(hostMB, uriMB, null, mappingData);
+        assertEquals("newContext", mappingData.context.toString());
+
+        mapper.removeContextVersion(hostName, contextPath, "0");
+
+        assertEquals(1, mappedContext.versions.length);
+        assertEquals("1", mappedContext.versions[0].name);
+        mappingData.recycle();
+        mapper.map(hostMB, uriMB, null, mappingData);
+        assertEquals("newContext", mappingData.context.toString());
+
+        mapper.removeContextVersion(hostName, contextPath, "1");
+
+        assertNotSame(mappedContext, 
mappedHost.contextList.contexts[contextPos]);
+        assertEquals("/foo/bar/bla", 
mappedHost.contextList.contexts[contextPos].name);
+        mappingData.recycle();
+        mapper.map(hostMB, uriMB, null, mappingData);
+        assertEquals("context1", mappingData.context.toString());
+
+        mapper.addContextVersion(
+                hostName,
+                oldHost,
+                contextPath,
+                "0",
+                newContext,
+                null,
+                null,
+                Arrays.asList(new WrapperMappingInfo[] { new 
WrapperMappingInfo(
+                        "/", "default", false, false) }));
+        mappedContext = mappedHost.contextList.contexts[contextPos];
+
+        assertEquals(contextPath, mappedContext.name);
+        assertEquals(1, mappedContext.versions.length);
+        assertEquals("0", mappedContext.versions[0].name);
+        mappingData.recycle();
+        mapper.map(hostMB, uriMB, null, mappingData);
+        assertEquals("newContext", mappingData.context.toString());
+    }
+
+    @Test
     public void testContextListConcurrencyBug56653() throws Exception {
         final Object host = new Object(); // "localhost";
         final Object contextRoot = new Object(); // "ROOT";



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

Reply via email to