You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2008/12/23 00:01:28 UTC

svn commit: r728821 - in /tapestry/tapestry5/trunk: tapestry-core/src/test/java/org/apache/tapestry5/integration/ tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/ tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/s...

Author: hlship
Date: Mon Dec 22 15:01:27 2008
New Revision: 728821

URL: http://svn.apache.org/viewvc?rev=728821&view=rev
Log:
Add unit tests for reloading of classes, templates and message catalogs.

Added:
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/ReloadTests.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/AppModule.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.properties
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.properties
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/web.xml
Modified:
    tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/AbstractIntegrationTestSuite.java
    tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/JettyRunner.java

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/ReloadTests.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/ReloadTests.java?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/ReloadTests.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/ReloadTests.java Mon Dec 22 15:01:27 2008
@@ -0,0 +1,160 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.integration;
+
+import javassist.*;
+import org.apache.tapestry5.test.AbstractIntegrationTestSuite;
+import org.testng.annotations.Test;
+
+import java.io.*;
+
+/**
+ * Integration tests designed to test Tapestry's ability to dynamically reload component classes, templates and message
+ * catalogs.
+ */
+public class ReloadTests extends AbstractIntegrationTestSuite
+{
+    private final File webappDir;
+    private final File webinfDir;
+    private final File classesDir;
+    private final File pagesDir;
+
+    private static final String PACKAGE = "org.apache.tapestry5.integration.reload.pages";
+
+    public ReloadTests() throws Exception
+    {
+        String uid = Long.toHexString(System.currentTimeMillis());
+
+        webappDir = new File(System.getProperty("java.io.tmpdir"), uid);
+
+        webinfDir = new File(webappDir, "WEB-INF");
+
+        classesDir = new File(webinfDir, "classes");
+
+        pagesDir = new File(classesDir, PACKAGE.replace('.', '/'));
+
+        pagesDir.mkdirs();
+
+        copy("web.xml", webinfDir, "web.xml");
+        copy("Index.1.tml", webappDir, "Index.tml");
+        copy("Index.1.properties", pagesDir, "Index.properties");
+
+        createIndexClass(100);
+
+        setWebappRoot(webappDir.getAbsolutePath());
+
+        System.err.println("Created: " + webappDir);
+    }
+
+    private void createIndexClass(int number) throws NotFoundException, CannotCompileException, IOException
+    {
+        ClassPool pool = new ClassPool(null);
+
+        pool.appendSystemPath();
+
+        CtClass ctClass = pool.makeClass(PACKAGE + ".Index");
+
+        CtMethod method = new CtMethod(pool.get("int"), "getNumber", null, ctClass);
+
+        method.setBody("return " + number + ";");
+
+        ctClass.addMethod(method);
+
+        ctClass.writeFile(classesDir.getAbsolutePath());
+    }
+
+
+    /**
+     * Copies a source file (from the classpath) to a directory as a new file name.
+     *
+     * @param sourceFile source file (within in the reload package)
+     * @param dir        directory to copy to
+     * @param targetFile name of file   to be created or overwritten
+     */
+    private void copy(String sourceFile, File dir, String targetFile) throws IOException
+    {
+        File output = new File(dir, targetFile);
+
+        FileOutputStream fos = new FileOutputStream(output);
+
+        InputStream in = getClass().getResourceAsStream("reload/" + sourceFile);
+
+        copy(in, fos);
+
+        in.close();
+        fos.close();
+    }
+
+    private void copy(InputStream in, FileOutputStream fos) throws IOException
+    {
+        BufferedInputStream bis = new BufferedInputStream(in);
+        BufferedOutputStream bos = new BufferedOutputStream(fos);
+
+        byte[] buffer = new byte[1000];
+
+        while (true)
+        {
+            int length = bis.read(buffer);
+
+            if (length < 0) break;
+
+            bos.write(buffer, 0, length);
+        }
+
+        bos.flush();
+    }
+
+    @Test
+    public void reload_class() throws Exception
+    {
+        open(BASE_URL);
+
+        assertText("property", "100");
+
+        createIndexClass(200);
+
+        open(BASE_URL);
+
+        assertText("property", "200");
+    }
+
+    @Test
+    public void reload_template() throws Exception
+    {
+        open(BASE_URL);
+
+        assertText("template", "Initial Template Version");
+
+        copy("Index.2.tml", webappDir, "Index.tml");
+
+        open(BASE_URL);
+
+        assertText("template", "Updated Template Version");
+    }
+
+    @Test
+    public void reload_message_catalog() throws Exception
+    {
+        open(BASE_URL);
+
+        assertText("message", "Initial Message");
+
+        copy("Index.2.properties", pagesDir, "Index.properties");
+
+        open(BASE_URL);
+
+        assertText("message", "Updated Message");
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/AppModule.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/AppModule.java?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/AppModule.java (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/reload/services/AppModule.java Mon Dec 22 15:01:27 2008
@@ -0,0 +1,26 @@
+// Copyright 2008 The Apache Software Foundation
+//
+// Licensed 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.tapestry5.integration.reload.services;
+
+import org.apache.tapestry5.SymbolConstants;
+import org.apache.tapestry5.ioc.MappedConfiguration;
+
+public class AppModule
+{
+    public static void contributeApplicationDefaults(MappedConfiguration<String, String> conf)
+    {
+        conf.add(SymbolConstants.FILE_CHECK_INTERVAL, "0ms");
+    }
+}

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.properties?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.properties (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.properties Mon Dec 22 15:01:27 2008
@@ -0,0 +1,2 @@
+
+greeting=Initial Message

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.tml?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.1.tml Mon Dec 22 15:01:27 2008
@@ -0,0 +1,12 @@
+<html>
+    <body>
+        <dl>
+            <dt>Property:</dt>
+            <dd id="property">${number}</dd>
+            <dt>Template:</dt>
+            <dd id="template">Initial Template Version</dd>
+            <dt>Message:</dt>
+            <dd id="message">${message:greeting}</dd>
+        </dl>
+    </body>
+</html>
\ No newline at end of file

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.properties
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.properties?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.properties (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.properties Mon Dec 22 15:01:27 2008
@@ -0,0 +1,2 @@
+
+greeting=Updated Message

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.tml?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.tml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/Index.2.tml Mon Dec 22 15:01:27 2008
@@ -0,0 +1,12 @@
+<html>
+    <body>
+        <dl>
+            <dt>Property:</dt>
+            <dd id="property">${number}</dd>
+            <dt>Template:</dt>
+            <dd id="template">Updated Template Version</dd>
+            <dt>Message:</dt>
+            <dd id="message">${message:greeting}</dd>
+        </dl>
+    </body>
+</html>
\ No newline at end of file

Added: tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/web.xml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/web.xml?rev=728821&view=auto
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/web.xml (added)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/resources/org/apache/tapestry5/integration/reload/web.xml Mon Dec 22 15:01:27 2008
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   Copyright 2008 The Apache Software Foundation
+
+   Licensed 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.
+-->
+
+<!DOCTYPE web-app
+        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+        "http://java.sun.com/dtd/web-app_2_3.dtd">
+<web-app>
+    <display-name>Reload App</display-name>
+    <context-param>
+        <param-name>tapestry.app-package</param-name>
+        <param-value>org.apache.tapestry5.integration.reload</param-value>
+    </context-param>
+    <filter>
+        <filter-name>app</filter-name>
+        <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
+    </filter>
+    <filter-mapping>
+        <filter-name>app</filter-name>
+        <url-pattern>/*</url-pattern>
+    </filter-mapping>
+</web-app>

Modified: tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/AbstractIntegrationTestSuite.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/AbstractIntegrationTestSuite.java?rev=728821&r1=728820&r2=728821&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/AbstractIntegrationTestSuite.java (original)
+++ tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/AbstractIntegrationTestSuite.java Mon Dec 22 15:01:27 2008
@@ -66,7 +66,8 @@
 
     public static final String SUBMIT = "//input[@type='submit']";
 
-    private final String webappRoot;
+
+    private String webappRoot;
 
     private final String seleniumBrowserCommand;
 
@@ -949,4 +950,14 @@
         for (String s : linkText)
             clickAndWait(String.format("link=%s", s));
     }
+
+    public String getWebappRoot()
+    {
+        return webappRoot;
+    }
+
+    public void setWebappRoot(String webappRoot)
+    {
+        this.webappRoot = webappRoot;
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/JettyRunner.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/JettyRunner.java?rev=728821&r1=728820&r2=728821&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/JettyRunner.java (original)
+++ tapestry/tapestry5/trunk/tapestry-test/src/main/java/org/apache/tapestry5/test/JettyRunner.java Mon Dec 22 15:01:27 2008
@@ -102,13 +102,15 @@
         try
         {
 
-            String warPath = new File(workingDir, this.warPath).getPath();
+            String webappPath = warPath.startsWith("/")
+                                ? warPath
+                                : new File(workingDir, this.warPath).getPath();
             String webDefaults = new File(workingDir, "src/test/conf/webdefault.xml").getPath();
 
             File keystoreFile = new File(workingDir, "src/test/conf/keystore");
             String keystore = keystoreFile.getPath();
 
-            System.out.printf("Starting Jetty instance on port %d (%s mapped to %s)\n", port, contextPath, warPath);
+            System.out.printf("Starting Jetty instance on port %d (%s mapped to %s)\n", port, contextPath, webappPath);
 
             Server server = new Server();
 
@@ -131,7 +133,7 @@
             NCSARequestLog log = new NCSARequestLog();
             server.setRequestLog(log);
 
-            WebApplicationContext context = server.addWebApplication(contextPath, warPath);
+            WebApplicationContext context = server.addWebApplication(contextPath, webappPath);
 
             context.setDefaultsDescriptor(webDefaults);