Author: hlship
Date: Wed Nov  5 09:33:18 2008
New Revision: 711633

URL: http://svn.apache.org/viewvc?rev=711633&view=rev
Log:
TAP5-184: Improve error reporting when a javascript asset is intended to be 
included on page which has no <html> element

Modified:
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
    
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
    
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java?rev=711633&r1=711632&r2=711633&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/DocumentLinkerImpl.java
 Wed Nov  5 09:33:18 2008
@@ -79,13 +79,6 @@
         // If the document failed to render entirely, that's a different 
problem and is reported elsewhere.        
         if (root == null) return;
 
-        // This only applies when the document is an HTML document. This may 
need to change in the
-        // future, perhaps configurable, to allow for html and xhtml and 
perhaps others. Does SVG
-        // use stylesheets?
-
-
-        if (!root.getName().equals("html"))
-            throw new 
RuntimeException(ServicesMessages.documentMissingHTMLRoot());
 
         if (!stylesheets.isEmpty())
             addStylesheetsToHead(root, includedStylesheets);
@@ -93,8 +86,24 @@
         addScriptElementsToBody(root);
     }
 
+    private void validateRoot(Element root)
+    {
+        // This only applies when the document is an HTML document. This may 
need to change in the
+        // future, perhaps configurable, to allow for html and xhtml and 
perhaps others. Does SVG
+        // use stylesheets?
+
+        String rootElementName = root.getName();
+
+        if (!rootElementName.equals("html"))
+            throw new 
RuntimeException(ServicesMessages.documentMissingHTMLRoot(rootElementName));
+    }
+
     private void addScriptElementsToBody(Element root)
     {
+        if (scripts.isEmpty() && scriptBlock.length() == 0) return;
+
+        validateRoot(root);
+
         Element body = root.find("body");
 
         // Create the body element is it is somehow missing.
@@ -166,12 +175,16 @@
      */
     protected void addStylesheetsToHead(Element root, List<IncludedStylesheet> 
stylesheets)
     {
+        int count = stylesheets.size();
+
+        if (count == 0) return;
+
+        validateRoot(root);
+
         Element head = root.find("head");
 
         if (head == null) head = root.elementAt(0, "head");
 
-        int count = stylesheets.size();
-
         for (int i = 0; i < count; i++)
             stylesheets.get(i).add(head, i);
     }

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java?rev=711633&r1=711632&r2=711633&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/services/ServicesMessages.java
 Wed Nov  5 09:33:18 2008
@@ -418,8 +418,8 @@
         return MESSAGES.format("event-not-handled", eventName, 
element.getCompleteId());
     }
 
-    static String documentMissingHTMLRoot()
+    static String documentMissingHTMLRoot(String rootElementName)
     {
-        return MESSAGES.get("document-missing-html-root");
+        return MESSAGES.format("document-missing-html-root", rootElementName);
     }
 }

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties?rev=711633&r1=711632&r2=711633&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/main/resources/org/apache/tapestry5/internal/services/ServicesStrings.properties
 Wed Nov  5 09:33:18 2008
@@ -95,4 +95,4 @@
 no-such-method=Class %s does not contain a method named '%s()'.
 forbid-instantiate-component-class=Component class %s may not be instantiated 
directly.  You should use an @InjectPage or @InjectComponent annotation instead.
 event-not-handled=Request event '%s' (on component %s) was not handled; you 
must provide a matching event handler method in the component or in one of its 
containers.
-document-missing-html-root=The rendered document does not contain a root 
<html> element, which is necessary for the linking of JavaScript and stylesheet 
resources.
+document-missing-html-root=The root element of the rendered document was <%s>, 
not <html>. A root element of <html> is needed when linking JavaScript and 
stylesheet resources.

Modified: 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java?rev=711633&r1=711632&r2=711633&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/internal/services/DocumentLinkerImplTest.java
 Wed Nov  5 09:33:18 2008
@@ -26,7 +26,7 @@
         assertEquals(document.toString(), readFile(file));
     }
 
-    @Test(expectedExceptions = RuntimeException.class)
+    @Test
     public void exception_if_missing_html_root_element()
     {
         Document document = new Document();
@@ -35,10 +35,21 @@
 
         DocumentLinkerImpl linker = new DocumentLinkerImpl(true, false);
 
+        // Only checked if there's something to link.
+
         linker.addScript("foo.js");
         linker.addScript("doSomething();");
 
-        linker.updateDocument(document);
+        try
+        {
+            linker.updateDocument(document);
+            unreachable();
+        }
+        catch (RuntimeException ex)
+        {
+            assertEquals(ex.getMessage(),
+                         "The root element of the rendered document was 
<not-html>, not <html>. A root element of <html> is needed when linking 
JavaScript and stylesheet resources.");
+        }
     }
 
     @Test


Reply via email to