click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r999214 - in /click/trunk/click/framework: src/org/apache/click/service/XmlConfigService.java test/org/apache/click/service/XmlConfigServiceTest.java
Date Tue, 21 Sep 2010 04:34:14 GMT
Author: sabob
Date: Tue Sep 21 04:34:13 2010
New Revision: 999214

URL: http://svn.apache.org/viewvc?rev=999214&view=rev
Log:
improve page class loading. CLK-704

Modified:
    click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java
    click/trunk/click/framework/test/org/apache/click/service/XmlConfigServiceTest.java

Modified: click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java?rev=999214&r1=999213&r2=999214&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java (original)
+++ click/trunk/click/framework/src/org/apache/click/service/XmlConfigService.java Tue Sep
21 04:34:13 2010
@@ -1983,40 +1983,51 @@ public class XmlConfigService implements
                 path = pathValue;
             }
 
-            // Set pageClass
-            String value = element.getAttribute("classname");
-            if (value != null) {
-                if (pagesPackage.trim().length() > 0) {
-                    value = pagesPackage + "." + value;
-                }
-            } else {
+            // Retrieve page classname
+            String classname = element.getAttribute("classname");
+
+            if (classname == null) {
                 String msg = "No classname defined for page path " + path;
                 throw new RuntimeException(msg);
             }
 
+            Class tmpPageClass = null;
+            String classFound = null;
+
             try {
 
-                pageClass = ClickUtils.classForName(value);
+                    // First, lookup classname as provided
+                    tmpPageClass = ClickUtils.classForName(classname);
+                    classFound = classname;
 
             } catch (ClassNotFoundException cnfe) {
+// For backward compatibility prefix classname with package name
+                String prefixedClassname = classname;
+
+                if (pagesPackage.trim().length() > 0) {
+                    prefixedClassname = pagesPackage + "." + classname;
+                }
+
+                try {
+                    // CLK-704
+                    // For backward compatibility, lookup classname prefixed with the package
name
+
+                    tmpPageClass = ClickUtils.classForName(prefixedClassname);
+                    classFound = prefixedClassname;
 
-                // If a pagesPackage was delcared provide a descriptive error message
-                if (StringUtils.isNotBlank(pagesPackage)) {
+                } catch (ClassNotFoundException cnfe2) {
+                    // Throw original exception which used the given classname
                     String msg = "No class was found for the Page classname: '"
-                        + value + "'. Please note that Click automatically adds"
-                        + " the given package '" + pagesPackage + "' to page"
-                        + " classnames. If you need to specify an absolute"
-                        + " classname declare a <pages>...</pages> element"
-                        + " without a package attribute.";
+                        + classname + "'.";
                     throw new RuntimeException(msg, cnfe);
-                } else {
-                    throw cnfe;
                 }
             }
 
+            pageClass = tmpPageClass;
+
             if (!Page.class.isAssignableFrom(pageClass)) {
-                String msg = "Page class " + value
-                             + " is not a subclass of org.apache.click.Page";
+                String msg = "Page class '" + classFound
+                             + "' is not a subclass of org.apache.click.Page";
                 throw new RuntimeException(msg);
             }
 

Modified: click/trunk/click/framework/test/org/apache/click/service/XmlConfigServiceTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/service/XmlConfigServiceTest.java?rev=999214&r1=999213&r2=999214&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/service/XmlConfigServiceTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/service/XmlConfigServiceTest.java Tue
Sep 21 04:34:13 2010
@@ -30,6 +30,7 @@ import java.util.Locale;
 import java.util.Map;
 
 import javax.servlet.ServletContext;
+import javax.servlet.UnavailableException;
 
 import junit.framework.TestCase;
 import org.apache.click.Context;
@@ -364,6 +365,74 @@ public class XmlConfigServiceTest extend
         deleteDir(tmpdir);
     }
 
+    /**
+     * Test that manually loaded pages should specify absolute classnames, but
+     * for backward compatibility fallback to appending package to classname if
+     * absolute classname is not found.
+     *
+     * CLK-704
+     */
+    public void testLoadManualPagesByClassname() throws Exception {
+        File tmpdir = makeTmpDir();
+
+        PrintStream pstr = makeXmlStream(tmpdir, "WEB-INF/click.xml");
+        pstr.println("<click-app>");
+
+        // Dclare the pages package
+                pstr.println("<pages package='org.apache.click.pages'/>");
+        pstr.println("<pages package='org.apache.click.pages'>");
+
+        // Check that page with absolute classname is resolved
+        pstr.println("  <page path='page.htm' classname='org.apache.click.pages.BinaryPage'/>");
+        // For backward compatibility, check that page with classname is resolved as well.
+        // In this case Click will prefix the classname with the package declared above.
+        pstr.println("  <page path='page.htm' classname='BinaryPage'/>");
+        pstr.println("</pages>");
+        pstr.println("</click-app>");
+        pstr.close();
+
+        MockContainer container = new MockContainer(tmpdir.getAbsolutePath());
+        container.start();
+
+        container.stop();
+        deleteDir(tmpdir);
+    }
+
+    /**
+     * Test that manually loaded pages that does not exist, throws appropriate
+     * exception.
+     *
+     * CLK-704
+     */
+    public void testLoadManualNonExistentPageByClassname() throws Exception {
+        File tmpdir = makeTmpDir();
+
+        PrintStream pstr = makeXmlStream(tmpdir, "WEB-INF/click.xml");
+        pstr.println("<click-app>");
+
+        // Dclare the pages package
+                pstr.println("<pages package='org.apache.click.pages'/>");
+        pstr.println("<pages package='org.apache.click.pages'>");
+
+        // Check non existent page
+        pstr.println("  <page path='page.htm' classname='org.apache.click.pages.noSuchPage'/>");
+        pstr.println("</pages>");
+        pstr.println("</click-app>");
+        pstr.close();
+
+        MockContainer container = null;
+        try {
+            container = new MockContainer(tmpdir.getAbsolutePath());
+            container.start();
+            fail("No class called NoSuchPage exists. Container should fail to start up");
+        } catch (Exception expected) {
+        } finally {
+            container.stop();
+        }
+
+        deleteDir(tmpdir);
+    }
+
     public void testHeaders() throws Exception {
         File tmpdir = makeTmpDir();
 



Mime
View raw message