You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2013/05/15 23:48:56 UTC

svn commit: r1483104 - /tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java

Author: markt
Date: Wed May 15 21:48:55 2013
New Revision: 1483104

URL: http://svn.apache.org/r1483104
Log:
Start the modification tests

Modified:
    tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java

Modified: tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java?rev=1483104&r1=1483103&r2=1483104&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java (original)
+++ tomcat/trunk/test/org/apache/catalina/startup/TestHostConfigAutomaticDeployment.java Wed May 15 21:48:55 2013
@@ -29,6 +29,9 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import org.apache.catalina.Context;
+import org.apache.catalina.Lifecycle;
+import org.apache.catalina.LifecycleEvent;
+import org.apache.catalina.LifecycleListener;
 import org.apache.catalina.LifecycleState;
 import org.apache.catalina.core.StandardHost;
 import org.apache.catalina.util.ContextName;
@@ -57,6 +60,10 @@ public class TestHostConfigAutomaticDepl
     private static final int WAR = 3;
     private static final int DIR = 4;
 
+    private static final int NONE = 1;
+    private static final int RELOAD = 2;
+    private static final int REDEPLOY = 3;
+
     private static final String XML_COOKIE_NAME = "XML_CONTEXT";
     private static final String WAR_COOKIE_NAME = "WAR_CONTEXT";
     private static final String DIR_COOKIE_NAME = "DIR_CONTEXT";
@@ -973,6 +980,238 @@ public class TestHostConfigAutomaticDepl
     }
 
 
+    /*
+     * Expected behaviour for modification of files.
+     *
+     * Artifacts present      Artifact   Artifacts remaining
+     * XML  WAR  EXT  DIR    Modified    XML  WAR  EXT DIR   Action
+     *  N    N    N    Y       DIR        -    -    -   M     None
+     *  N    Y    N    N       WAR        -    M    -   -    Reload
+     *  N    Y    N    Y       DIR        -    Y    -   M     None
+     *  N    Y    N    Y       WAR        -    M    -   R    Reload
+     *  Y    N    N    N       XML        M    -    -   -   Redeploy
+     *  Y    N    N    Y       DIR        Y    -    -   M     None
+     *  Y    N    N    Y       XML        M    -    -   Y   Redeploy
+     *  Y    N    Y    N       EXT        Y    -    M   -   Reload if WAR
+     *  Y    N    Y    N       XML        M    -    Y   -   Redeploy
+     *  Y    N    Y    Y       DIR        Y    -    Y   M     None
+     *  Y    N    Y    Y       EXT        Y    -    M   R    Reload
+     *  Y    N    Y    Y       XML        M    -    Y   Y   Redeploy
+     *  Y    Y    N    N       WAR        Y    M    -   -    Reload
+     *  Y    Y    N    N       XML        M    Y    -   -   Redeploy
+     *  Y    Y    N    Y       DIR        Y    Y    -   M     None
+     *  Y    Y    N    Y       WAR        Y    M    -   -    Reload
+     *  Y    Y    N    Y       XML        M    Y    -   Y    Reload
+     */
+    @Test
+    public void testModifyDirUpdateDir() throws Exception {
+        doTestModify(false, false, false, false, true, DIR,
+                false, false, true, DIR_COOKIE_NAME, NONE);
+    }
+
+    @Test
+    public void testModifyWarUpdateWar() throws Exception {
+        doTestModify(false, false, false, true, false, WAR,
+                false, true, false, WAR_COOKIE_NAME, REDEPLOY);
+    }
+
+    @Test
+    public void testModifyWarDirUpdateDir() throws Exception {
+        // DIR_COOKIE_NAME since Tomcat is going to assume DIR is expanded WAR
+        doTestModify(false, false, false, true, true, DIR,
+                false, true, true, DIR_COOKIE_NAME, NONE);
+    }
+
+    @Test
+    public void testModifyWarDirUpdateWar() throws Exception {
+        doTestModify(false, false, false, true, true, WAR,
+                false, true, true, WAR_COOKIE_NAME, REDEPLOY);
+    }
+
+    @Test
+    public void testModifyXmlUpdateXml() throws Exception {
+        doTestModify(true, false, false, false, false, XML,
+                true, false, false, XML_COOKIE_NAME, REDEPLOY);
+    }
+
+    private void doTestModify(boolean startXml, boolean startExternalWar,
+            boolean startExternalDir, boolean startWar, boolean startDir,
+            int toModify, boolean resultXml, boolean resultWar,
+            boolean resultDir, String resultCookieName, int resultAction)
+            throws Exception {
+
+        Tomcat tomcat = getTomcatInstance();
+        StandardHost host = (StandardHost) tomcat.getHost();
+
+        // Init
+        File xml = null;
+        File ext = null;
+        File war = null;
+        File dir = null;
+
+        if (startXml && !startExternalWar && !startExternalDir) {
+            xml = new File(host.getConfigBaseFile(), APP_NAME + ".xml");
+            File parent = xml.getParentFile();
+            if (!parent.isDirectory()) {
+                Assert.assertTrue(parent.mkdirs());
+            }
+            Files.copy(XML_SOURCE.toPath(), xml.toPath());
+        }
+        if (startExternalWar) {
+            // Copy the test WAR file to the external directory
+            ext = new File(external, "external" + ".war");
+            Files.copy(WAR_XML_SOURCE.toPath(), ext.toPath());
+
+            // Create the XML file
+            xml = new File(host.getConfigBaseFile(), APP_NAME + ".xml");
+            File parent = xml.getParentFile();
+            if (!parent.isDirectory()) {
+                Assert.assertTrue(parent.mkdirs());
+            }
+
+            try (FileOutputStream fos = new FileOutputStream(xml)) {
+                fos.write(("<Context sessionCookieName=\"" + XML_COOKIE_NAME +
+                        "\" docBase=\"" + ext.getAbsolutePath() +
+                        "\" />").getBytes(B2CConverter.ISO_8859_1));
+            }
+        }
+        if (startExternalDir) {
+            // Copy the test DIR file to the external directory
+            ext = new File(external, "external");
+            recurrsiveCopy(DIR_XML_SOURCE.toPath(), ext.toPath());
+
+            // Create the XML file
+            xml = new File(getTomcatInstance().getHost().getConfigBaseFile(),
+                    APP_NAME + ".xml");
+            File parent = xml.getParentFile();
+            if (!parent.isDirectory()) {
+                Assert.assertTrue(parent.mkdirs());
+            }
+
+            try (FileOutputStream fos = new FileOutputStream(xml)) {
+                fos.write(("<Context sessionCookieName=\"" + XML_COOKIE_NAME +
+                        "\" docBase=\"" + ext.getAbsolutePath() +
+                        "\" />").getBytes(B2CConverter.ISO_8859_1));
+            }
+        }
+        if (startWar) {
+            // Copy the test WAR file to the appBase
+            war = new File(getTomcatInstance().getHost().getAppBaseFile(),
+                    APP_NAME.getBaseName() + ".war");
+            Files.copy(WAR_XML_SOURCE.toPath(), war.toPath());
+        }
+        if (startDir) {
+            // Copy the test DIR file to the appBase
+            dir = new File(getTomcatInstance().getHost().getAppBaseFile(),
+                    APP_NAME.getBaseName());
+            recurrsiveCopy(DIR_XML_SOURCE.toPath(), dir.toPath());
+        }
+
+        if ((startWar || startExternalWar) && !startDir) {
+            host.setUnpackWARs(false);
+        }
+
+        // Deploy the files we copied
+        tomcat.start();
+        host.backgroundProcess();
+
+        // Remove the specified file
+        switch (toModify) {
+            case XML:
+                if (xml == null) {
+                    Assert.fail();
+                } else {
+                    xml.setLastModified(System.currentTimeMillis());
+                }
+                break;
+            case EXT:
+                if (ext == null) {
+                    Assert.fail();
+                } else {
+                    ext.setLastModified(System.currentTimeMillis());
+                }
+                break;
+            case WAR:
+                if (war == null) {
+                    Assert.fail();
+                } else {
+                    war.setLastModified(System.currentTimeMillis());
+                }
+                break;
+            case DIR:
+                if (dir == null) {
+                    Assert.fail();
+                } else {
+                    dir.setLastModified(System.currentTimeMillis());
+                }
+                break;
+            default:
+                Assert.fail();
+        }
+
+        Context oldContext = (Context) host.findChild(APP_NAME.getName());
+        StateTracker tracker = new StateTracker();
+        oldContext.addLifecycleListener(tracker);
+
+        // Trigger an auto-deployment cycle
+        host.backgroundProcess();
+
+        Context newContext = (Context) host.findChild(APP_NAME.getName());
+
+        // Check the results
+        if (resultXml) {
+            if (xml == null) {
+                Assert.fail();
+            } else {
+                Assert.assertTrue(xml.isFile());
+            }
+        }
+        if (resultWar) {
+            if (war == null) {
+                Assert.fail();
+            } else {
+                Assert.assertTrue(war.isFile());
+            }
+        }
+        if (resultDir) {
+            if (dir == null) {
+                Assert.fail();
+            } else {
+                Assert.assertTrue(dir.isDirectory());
+            }
+        }
+
+        if (!resultXml && (startExternalWar || startExternalDir)) {
+            Assert.assertNull(newContext);
+        }
+        if (!resultWar && !resultDir) {
+            if (resultXml) {
+                Assert.assertEquals(LifecycleState.FAILED, newContext.getState());
+            } else {
+                Assert.assertNull(newContext);
+            }
+        }
+
+        if (newContext != null) {
+            Assert.assertEquals(resultCookieName, newContext.getSessionCookieName());
+        }
+
+        if (resultAction == NONE) {
+            Assert.assertSame(oldContext, newContext);
+            Assert.assertEquals("", tracker.getHistory());
+        } else if (resultAction == RELOAD) {
+            Assert.assertSame(oldContext, newContext);
+            Assert.assertEquals("stopstart", tracker.getHistory());
+        } else if (resultAction == REDEPLOY) {
+            Assert.assertNotSame(oldContext, newContext);
+            // No init or start as that will be in a new context object
+            Assert.assertEquals("stopafter_destroy", tracker.getHistory());
+        } else {
+            Assert.fail();
+        }
+    }
+
+
     private static void recurrsiveCopy(final Path src, final Path dest)
             throws IOException {
 
@@ -1004,4 +1243,27 @@ public class TestHostConfigAutomaticDepl
                 return FileVisitResult.CONTINUE;
             }});
     }
+
+
+    private static class StateTracker implements LifecycleListener {
+
+        private StringBuilder stateHistory = new StringBuilder();
+
+        @Override
+        public void lifecycleEvent(LifecycleEvent event) {
+
+            String type = event.getType();
+
+            if (type.equals(Lifecycle.START_EVENT) ||
+                    type.equals(Lifecycle.STOP_EVENT) ||
+                    type.equals(Lifecycle.AFTER_DESTROY_EVENT)) {
+                stateHistory.append(type);
+            }
+        }
+
+
+        public String getHistory() {
+            return stateHistory.toString();
+        }
+    }
 }



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