tapestry-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jkuhn...@apache.org
Subject svn commit: r405765 - in /tapestry/tapestry4/trunk/framework/src: java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
Date Wed, 10 May 2006 13:55:14 GMT
Author: jkuhnert
Date: Wed May 10 06:53:43 2006
New Revision: 405765

URL: http://svn.apache.org/viewcvs?rev=405765&view=rev
Log:
Added more unit tests

Added:
    tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
Modified:
    tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java

Modified: tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java?rev=405765&r1=405764&r2=405765&view=diff
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
(original)
+++ tapestry/tapestry4/trunk/framework/src/java/org/apache/tapestry/services/impl/DojoAjaxResponseBuilder.java
Wed May 10 06:53:43 2006
@@ -72,6 +72,22 @@
     private Map _writers = new HashMap();
     
     /**
+     * Creates a builder with a pre-configured {@link IMarkupWriter}. 
+     * Currently only used for testing.
+     * 
+     * @param writer
+     *          The markup writer to render all "good" content to.
+     * @param parts
+     *          A set of string ids of the components that may have 
+     *          their responses rendered.
+     */
+    public DojoAjaxResponseBuilder(IMarkupWriter writer, List parts)
+    {
+        _writer = writer;
+        if (parts != null) _parts.addAll(parts);
+    }
+    
+    /**
      * Creates a new response builder with the required services it needs
      * to render the response when {@link #renderResponse(IRequestCycle)} is called.
      * 
@@ -244,6 +260,7 @@
                 && contains((IComponent)render))
         {
             render.render(getComponentWriter((IComponent)render), cycle);
+            return;
         }
         
         render.render(NullWriter.getSharedInstance(), cycle);
@@ -280,8 +297,9 @@
         //with xml element response begin/ends. This is very
         //important.
         IMarkupWriter nestedWriter = _writer.getNestedWriter();
+        nestedWriter.begin("response");
         nestedWriter.attribute("id", id);
-        if (type != null) 
+        if (type != null)
             nestedWriter.attribute("type", type);
         
         _writers.put(id, nestedWriter);
@@ -321,7 +339,10 @@
             
             nw.end();
             
-            _writer.printRaw(ScriptUtils.ensureValidScriptTags(nw.getBuffer()));
+            if (!isScriptWriter(key))
+                _writer.printRaw(ScriptUtils.ensureValidScriptTags(nw.getBuffer()));
+            else
+                _writer.printRaw(nw.getBuffer());
         }
         
         //end response
@@ -330,6 +351,27 @@
     }
     
     /**
+     * Determines if the specified markup writer key is one of
+     * the pre-defined script keys from ResponseBuilder.
+     * 
+     * @param key
+     *          The key to check.
+     * @return True, if key is one of the ResponseBuilder keys. 
+     *         (BODY_SCRIPT,INCLUDE_SCRIPT,INITIALIZATION_SCRIPT)
+     */
+    boolean isScriptWriter(String key)
+    {
+        if (key == null) return false;
+        
+        if (ResponseBuilder.BODY_SCRIPT.equals(key)
+                || ResponseBuilder.INCLUDE_SCRIPT.equals(key)
+                || ResponseBuilder.INITIALIZATION_SCRIPT.equals(key))
+            return true;
+        
+        return false;
+    }
+    
+    /**
      * Grabs the incoming parameters needed for json responses, most notable the
      * {@link ServiceConstants#UPDATE_PARTS} parameter.
      * 
@@ -367,9 +409,7 @@
         String id = null;
         //form components have id's generated to ensure uniqueness
         if (comp instanceof IFormComponent)
-            id = ((IFormComponent)comp).getClientId();
-        if (id != null)
-            return id;
+            return ((IFormComponent)comp).getClientId();
         
         id = comp.getId();
         if (comp.getBinding("id") != null

Added: tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
URL: http://svn.apache.org/viewcvs/tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java?rev=405765&view=auto
==============================================================================
--- tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
(added)
+++ tapestry/tapestry4/trunk/framework/src/test/org/apache/tapestry/services/impl/DojoAjaxResponseBuilderTest.java
Wed May 10 06:53:43 2006
@@ -0,0 +1,307 @@
+// Copyright May 9, 2006 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.tapestry.services.impl;
+
+import java.io.CharArrayWriter;
+import java.io.PrintWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.hivemind.test.HiveMindTestCase;
+import org.apache.tapestry.IComponent;
+import org.apache.tapestry.IMarkupWriter;
+import org.apache.tapestry.IRender;
+import org.apache.tapestry.IRequestCycle;
+import org.apache.tapestry.NestedMarkupWriter;
+import org.apache.tapestry.engine.NullWriter;
+import org.apache.tapestry.markup.MarkupFilter;
+import org.apache.tapestry.markup.MarkupWriterImpl;
+import org.apache.tapestry.markup.UTFMarkupFilter;
+import org.apache.tapestry.services.ResponseBuilder;
+
+
+/**
+ * Tests functionality of {@link DojoAjaxResponseBuilder}.
+ * 
+ * @author jkuhnert
+ */
+@SuppressWarnings("cast")
+public class DojoAjaxResponseBuilderTest extends HiveMindTestCase
+{
+
+    private static CharArrayWriter _writer;
+    
+    private PrintWriter newPrintWriter()
+    {
+        _writer = new CharArrayWriter();
+
+        return new PrintWriter(_writer);
+    }
+
+    protected void tearDown() throws Exception
+    {
+        _writer = null;
+
+        super.tearDown();
+    }
+
+    private void assertOutput(String expected)
+    {
+        assertEquals(expected, _writer.toString());
+
+        _writer.reset();
+    }
+    
+    public void testNullRender() 
+    {
+        IRender render = (IRender)newMock(IRender.class);
+        IRequestCycle cycle = (IRequestCycle)newMock(IRequestCycle.class);
+        
+        ResponseBuilder builder = new DojoAjaxResponseBuilder(null, null);
+        
+        render.render(NullWriter.getSharedInstance(), cycle);
+        
+        replayControls();
+        
+        builder.render(null, render, cycle);
+        
+        verifyControls();
+        
+        assertSame(builder.getWriter(), null);
+    }
+    
+    public void testNormalRender()
+    {
+        IRender render = (IRender)newMock(IRender.class);
+        IRequestCycle cycle = (IRequestCycle)newMock(IRequestCycle.class);
+        IMarkupWriter writer = (IMarkupWriter)newMock(IMarkupWriter.class);
+        
+        ResponseBuilder builder = new DojoAjaxResponseBuilder(writer, null);
+        
+        render.render(NullWriter.getSharedInstance(), cycle);
+        
+        replayControls();
+        
+        builder.render(null, render, cycle);
+        
+        verifyControls();
+        
+        assertSame(builder.getWriter(), writer);
+    }
+    
+    public void testPartialRender()
+    {
+        IRender render = (IRender)newMock(IRender.class);
+        
+        IComponent comp1 = (IComponent)newMock(IComponent.class);
+        IRequestCycle cycle = (IRequestCycle)newMock(IRequestCycle.class);
+        IMarkupWriter writer = (IMarkupWriter)newMock(IMarkupWriter.class);
+        NestedMarkupWriter nested = (NestedMarkupWriter)newMock(NestedMarkupWriter.class);
+        
+        List parts = new ArrayList();
+        parts.add("id1");
+        
+        DojoAjaxResponseBuilder builder = new DojoAjaxResponseBuilder(writer, parts);
+        
+        render.render(NullWriter.getSharedInstance(), cycle);
+        
+        comp1.getId();
+        setReturnValue(comp1, "id1");
+        
+        comp1.getBinding("id");
+        setReturnValue(comp1, null);
+        
+        comp1.getId();
+        setReturnValue(comp1, "id1");
+        
+        comp1.getBinding("id");
+        setReturnValue(comp1, null);
+        
+        comp1.getId();
+        setReturnValue(comp1, "id1");
+        
+        comp1.getBinding("id");
+        setReturnValue(comp1, null);
+        
+        writer.getNestedWriter();
+        setReturnValue(writer, nested);
+        
+        nested.begin("response");
+        nested.attribute("id", "id1");
+        nested.attribute("type", DojoAjaxResponseBuilder.ELEMENT_TYPE);
+        
+        comp1.render(nested, cycle);
+        
+        replayControls();
+        
+        builder.render(null, render, cycle);
+        
+        assertTrue(builder.contains(comp1));
+        assertTrue(IComponent.class.isInstance(comp1));
+        
+        builder.render(null, comp1, cycle);
+        
+        verifyControls();
+        
+        assertSame(builder.getWriter(), writer);
+    }
+    
+    public void testAllowedScripts()
+    {
+        IComponent comp = (IComponent)newMock(IComponent.class);
+        List parts = new ArrayList();
+        parts.add("comp1");
+        
+        ResponseBuilder builder = new DojoAjaxResponseBuilder(null, parts);
+        
+        comp.getId();
+        setReturnValue(comp, "comp");
+        comp.getBinding("id");
+        setReturnValue(comp, null);
+        
+        comp.getId();
+        setReturnValue(comp, "comp1");
+        comp.getBinding("id");
+        setReturnValue(comp, null);
+        
+        comp.getId();
+        setReturnValue(comp, "comp");
+        comp.getBinding("id");
+        setReturnValue(comp, null);
+        
+        replayControls();
+        
+        assertFalse(builder.isBodyScriptAllowed(comp));
+        assertTrue(builder.isExternalScriptAllowed(comp));
+        assertFalse(builder.isInitializationScriptAllowed(comp));
+        
+        verifyControls();
+    }
+    
+    public void testWriteBodyScript()
+    {
+        MarkupFilter filter = new UTFMarkupFilter();
+        PrintWriter writer = newPrintWriter();
+        IRequestCycle cycle = (IRequestCycle)newMock(IRequestCycle.class);
+        
+        replayControls();
+        
+        IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
+        DojoAjaxResponseBuilder builder = new DojoAjaxResponseBuilder(mw, null);
+        
+        String bscript = "var e=4;";
+        String imageInit = "image initializations";
+        String preload = "preloadedvarname";
+        
+        verifyControls();
+        replayControls();
+        
+        builder.beginResponse();
+        
+        builder.beginBodyScript(cycle);
+        
+        builder.writeImageInitializations(imageInit, preload, cycle);
+        
+        builder.writeBodyScript(bscript, cycle);
+        
+        builder.endBodyScript(cycle);
+        
+        builder.endResponse();
+        
+        assertOutput("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"
[\n" + 
+                "<!ENTITY nbsp \'&#160;\'>\n" + 
+                "]>\n" + 
+                "<ajax-response><response id=\"bodyscript\" type=\"script\"><script>\n"
+ 
+                "//<![CDATA[\n" + 
+                "\n" + 
+                "\n" + 
+                "var preloadedvarname = new Array();\n" + 
+                "if (document.images)\n" + 
+                "{\n" + 
+                "image initializations}\n" + 
+                "var e=4;\n" + 
+                "//]]>\n" + 
+                "</script></response></ajax-response>");
+        
+        verifyControls();
+    }
+    
+    public void testWriteExternalScripts()
+    {
+        MarkupFilter filter = new UTFMarkupFilter();
+        PrintWriter writer = newPrintWriter();
+        IRequestCycle cycle = (IRequestCycle)newMock(IRequestCycle.class);
+        
+        replayControls();
+        
+        IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
+        DojoAjaxResponseBuilder builder = new DojoAjaxResponseBuilder(mw, null);
+        
+        String script1 = "http://noname/js/package.js";
+        String script2 = "http://noname/js/package.js";
+        
+        verifyControls();
+        replayControls();
+        
+        builder.beginResponse();
+        
+        builder.writeExternalScript(script1, cycle);
+        
+        builder.writeExternalScript(script2, cycle);
+        
+        builder.endResponse();
+        
+        assertOutput("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"
[\n" + 
+                "<!ENTITY nbsp \'&#160;\'>\n" + 
+                "]>\n" + 
+                "<ajax-response><response id=\"includescript\" type=\"script\"><script
type=\"text/javascript\" src=\"http://noname/js/package.js\"></script>\n" + 
+                "<script type=\"text/javascript\" src=\"http://noname/js/package.js\"></script>\n"
+ 
+                "</response></ajax-response>");
+        
+        verifyControls();
+    }
+    
+    public void testWriteInitializationScript()
+    {
+        MarkupFilter filter = new UTFMarkupFilter();
+        PrintWriter writer = newPrintWriter();
+        
+        replayControls();
+        
+        IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
+        DojoAjaxResponseBuilder builder = new DojoAjaxResponseBuilder(mw, null);
+        
+        String script = "doThisInInit();";
+        
+        verifyControls();
+        replayControls();
+        
+        builder.beginResponse();
+        
+        builder.writeInitializationScript(script);
+        
+        builder.endResponse();
+        
+        assertOutput("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC
\"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"
[\n" + 
+                "<!ENTITY nbsp \'&#160;\'>\n" + 
+                "]>\n" + 
+                "<ajax-response><response id=\"initializationscript\" type=\"script\"><script>\n"
+ 
+                "//<![CDATA[\n" + 
+                "doThisInInit();\n" + 
+                "//]]>\n" + 
+                "</script></response></ajax-response>");
+        
+        verifyControls();
+    }
+}



Mime
View raw message