You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tiles.apache.org by ap...@apache.org on 2010/01/25 21:12:46 UTC

svn commit: r902965 - in /tiles/framework/trunk/tiles-velocity/src: main/java/org/apache/tiles/velocity/context/ main/java/org/apache/tiles/velocity/template/ test/java/org/apache/tiles/velocity/context/ test/java/org/apache/tiles/velocity/template/

Author: apetrelli
Date: Mon Jan 25 20:12:46 2010
New Revision: 902965

URL: http://svn.apache.org/viewvc?rev=902965&view=rev
Log:
TILES-490
Completed unit tests.

Added:
    tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java   (with props)
    tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java   (with props)
Modified:
    tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/VelocityUtil.java
    tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/BodyBlockDirective.java
    tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/context/VelocityUtilTest.java

Modified: tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/VelocityUtil.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/VelocityUtil.java?rev=902965&r1=902964&r2=902965&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/VelocityUtil.java (original)
+++ tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/context/VelocityUtil.java Mon Jan 25 20:12:46 2010
@@ -60,8 +60,7 @@
                 return "";
             }
 
-            public boolean render(InternalContextAdapter context, Writer writer)
-                    throws IOException {
+            public boolean render(InternalContextAdapter context, Writer writer) {
                 // Does nothing, really!
                 return true;
             }
@@ -99,7 +98,9 @@
      * @param context The Velocity context.
      * @return The parameter stack.
      * @since 2.2.0
+     * @deprecated Use Velocity directives.
      */
+    @Deprecated
     @SuppressWarnings("unchecked")
     public static ArrayStack<Map<String, Object>> getParameterStack(Context context) {
         ArrayStack<Map<String, Object>> stack = (ArrayStack<Map<String, Object>>) context
@@ -140,6 +141,15 @@
         }
     }
 
+    /**
+     * Evaluates the body (child node at position 1) and returns it as a string.
+     *
+     * @param context The Velocity context.
+     * @param node The node to use.
+     * @return The evaluated body.
+     * @throws IOException If something goes wrong.
+     * @since 2.2.2
+     */
     public static String getBodyAsString(InternalContextAdapter context, Node node)
             throws IOException {
         ASTBlock block = (ASTBlock) node.jjtGetChild(1);
@@ -156,12 +166,31 @@
         return body;
     }
 
+    /**
+     * Evaluates the body writing in the passed writer.
+     *
+     * @param context The Velocity context.
+     * @param writer The writer to write into.
+     * @param node The node to use.
+     * @throws IOException If something goes wrong.
+     * @since 2.2.2
+     */
     public static void evaluateBody(InternalContextAdapter context, Writer writer,
             Node node) throws IOException {
         ASTBlock block = (ASTBlock) node.jjtGetChild(1);
         block.render(context, writer);
     }
 
+    /**
+     * Extracts the parameters from the directives, by getting the child at
+     * position 0 supposing it is a map.
+     *
+     * @param context The Velocity context.
+     * @param node The node to use.
+     * @return The extracted parameters.
+     * @since 2.2.2
+     */
+    @SuppressWarnings("unchecked")
     public static Map<String, Object> getParameters(InternalContextAdapter context,
             Node node) {
         ASTMap astMap = (ASTMap) node.jjtGetChild(0);

Modified: tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/BodyBlockDirective.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/BodyBlockDirective.java?rev=902965&r1=902964&r2=902965&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/BodyBlockDirective.java (original)
+++ tiles/framework/trunk/tiles-velocity/src/main/java/org/apache/tiles/velocity/template/BodyBlockDirective.java Mon Jan 25 20:12:46 2010
@@ -61,7 +61,7 @@
         HttpServletRequest request = viewContext.getRequest();
         HttpServletResponse response = viewContext.getResponse();
         ServletContext servletContext = viewContext.getServletContext();
-        start(context, null, params, request, response, servletContext);
+        start(context, writer, params, request, response, servletContext);
         String body = VelocityUtil.getBodyAsString(context, node);
         end(context, writer, params, body, request, response, servletContext);
         return true;

Modified: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/context/VelocityUtilTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/context/VelocityUtilTest.java?rev=902965&r1=902964&r2=902965&view=diff
==============================================================================
--- tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/context/VelocityUtilTest.java (original)
+++ tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/context/VelocityUtilTest.java Mon Jan 25 20:12:46 2010
@@ -22,18 +22,23 @@
 package org.apache.tiles.velocity.context;
 
 import static org.apache.tiles.velocity.context.VelocityUtil.*;
-import static org.junit.Assert.*;
 import static org.easymock.EasyMock.*;
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
 
-import java.util.HashMap;
+import java.io.IOException;
+import java.io.Writer;
 import java.util.Map;
 
 import javax.servlet.ServletContext;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
-import org.apache.tiles.ArrayStack;
 import org.apache.velocity.context.Context;
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.runtime.parser.node.ASTBlock;
+import org.apache.velocity.runtime.parser.node.ASTMap;
+import org.apache.velocity.runtime.parser.node.Node;
 import org.junit.Test;
 
 /**
@@ -47,11 +52,6 @@
     private static final Integer DUMMY_VALUE = new Integer(10);
 
     /**
-     * The parameter stack key.
-     */
-    private static final String PARAMETER_MAP_STACK_KEY = "org.apache.tiles.velocity.PARAMETER_MAP_STACK";
-
-    /**
      * Test method for {@link org.apache.tiles.velocity.context.VelocityUtil
      * #toSimpleBoolean(java.lang.Boolean, boolean)}.
      */
@@ -67,34 +67,6 @@
 
     /**
      * Test method for {@link org.apache.tiles.velocity.context.VelocityUtil
-     * #getParameterStack(org.apache.velocity.context.Context)}.
-     */
-    @Test
-    public void testGetParameterStack() {
-        Context velocityContext = createMock(Context.class);
-
-        expect(velocityContext.get(PARAMETER_MAP_STACK_KEY)).andReturn(null);
-        expect(velocityContext.put(eq(PARAMETER_MAP_STACK_KEY),
-                isA(ArrayStack.class))).andReturn(null);
-        replay(velocityContext);
-        ArrayStack<Map<String, Object>> paramStack = getParameterStack(velocityContext);
-        assertNotNull(paramStack);
-        assertEquals(0, paramStack.size());
-        verify(velocityContext);
-
-        reset(velocityContext);
-
-        paramStack = new ArrayStack<Map<String, Object>>();
-        paramStack.push(new HashMap<String, Object>());
-        expect(velocityContext.get(PARAMETER_MAP_STACK_KEY)).andReturn(paramStack);
-
-        replay(velocityContext);
-        assertEquals(paramStack, getParameterStack(velocityContext));
-        verify(velocityContext);
-    }
-
-    /**
-     * Test method for {@link org.apache.tiles.velocity.context.VelocityUtil
      * #setAttribute(org.apache.velocity.context.Context, javax.servlet.http.HttpServletRequest,
      * javax.servlet.ServletContext, java.lang.String, java.lang.Object, java.lang.String)}.
      */
@@ -166,4 +138,86 @@
         setAttribute(velocityContext, request, servletContext, "myName", value, "application");
         verify(velocityContext, request, servletContext);
     }
+
+    /**
+     * Test method for
+     * {@link VelocityUtil#getBodyAsString(InternalContextAdapter, Node)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    @Test
+    public void testGetBodyAsString() throws IOException {
+        InternalContextAdapter context = createMock(InternalContextAdapter.class);
+        Node node = createMock(Node.class);
+        ASTBlock block = new CustomBlock();
+
+        expect(node.jjtGetChild(1)).andReturn(block);
+
+        replay(context, node);
+        assertEquals("myBody", VelocityUtil.getBodyAsString(context, node));
+        verify(context, node);
+    }
+
+    /**
+     * Test method for
+     * {@link VelocityUtil#evaluateBody(InternalContextAdapter, Writer, Node)}.
+     *
+     * @throws IOException If something goes wrong.
+     */
+    @Test
+    public void testEvaluateBody() throws IOException {
+        InternalContextAdapter context = createMock(InternalContextAdapter.class);
+        Node node = createMock(Node.class);
+        Writer writer = createMock(Writer.class);
+        ASTBlock block = createMock(ASTBlock.class);
+
+        expect(node.jjtGetChild(1)).andReturn(block);
+
+        replay(context, node, writer);
+        VelocityUtil.evaluateBody(context, writer, node);
+        verify(context, node, writer);
+    }
+
+    /**
+     * Test method for
+     * {@link VelocityUtil#getBodyAsString(InternalContextAdapter, Node)}.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testGetParameters() {
+        InternalContextAdapter context = createMock(InternalContextAdapter.class);
+        Node node = createMock(Node.class);
+        ASTMap block = createMock(ASTMap.class);
+        Map<String, Object> params = createMock(Map.class);
+
+        expect(node.jjtGetChild(0)).andReturn(block);
+        expect(block.value(context)).andReturn(params);
+
+        replay(context, node, block, params);
+        assertEquals(params, VelocityUtil.getParameters(context, node));
+        verify(context, node, block, params);
+    }
+
+    /**
+     * Custom block to render a specific string.
+     *
+     * @version $Rev$ $Date$
+     */
+    private static class CustomBlock extends ASTBlock {
+
+        /**
+         * Constructor.
+         */
+        public CustomBlock() {
+            super(1);
+        }
+
+        /** {@inheritDoc} */
+        @Override
+        public boolean render(InternalContextAdapter context, Writer writer)
+                throws IOException {
+            writer.write("myBody");
+            return true;
+        }
+    }
 }

Added: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java?rev=902965&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java (added)
+++ tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java Mon Jan 25 20:12:46 2010
@@ -0,0 +1,109 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.velocity.template;
+
+import static org.junit.Assert.*;
+import static org.easymock.classextension.EasyMock.*;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.runtime.directive.DirectiveConstants;
+import org.apache.velocity.runtime.parser.node.ASTBlock;
+import org.apache.velocity.runtime.parser.node.ASTMap;
+import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.tools.view.ViewToolContext;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests {@link BlockDirective}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class BlockDirectiveTest {
+
+    /**
+     * The directive to test.
+     */
+    private BlockDirective directive;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() {
+        directive = createMock(BlockDirective.class, new Method[0]);
+    }
+
+    /**
+     * Test method for {@link org.apache.tiles.velocity.template.BlockDirective#getType()}.
+     */
+    @Test
+    public void testGetType() {
+        replay(directive);
+        assertEquals(DirectiveConstants.BLOCK, directive.getType());
+        verify(directive);
+    }
+
+    /**
+     * Test method for {@link BlockDirective#render(InternalContextAdapter, Writer, Node)}.
+     * @throws IOException If something goes wrong.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testRenderInternalContextAdapterWriterNode() throws IOException {
+        InternalContextAdapter context = createMock(InternalContextAdapter.class);
+        Writer writer = createMock(Writer.class);
+        Node node = createMock(Node.class);
+        ViewToolContext viewContext = createMock(ViewToolContext.class);
+        HttpServletRequest request = createMock(HttpServletRequest.class);
+        HttpServletResponse response = createMock(HttpServletResponse.class);
+        ServletContext servletContext = createMock(ServletContext.class);
+        ASTMap astMap = createMock(ASTMap.class);
+        ASTBlock block = createMock(ASTBlock.class);
+        Map<String, Object> params = createMock(Map.class);
+
+        expect(context.getInternalUserContext()).andReturn(viewContext);
+        expect(viewContext.getRequest()).andReturn(request);
+        expect(viewContext.getResponse()).andReturn(response);
+        expect(viewContext.getServletContext()).andReturn(servletContext);
+        expect(node.jjtGetChild(0)).andReturn(astMap);
+        expect(astMap.value(context)).andReturn(params);
+        expect(node.jjtGetChild(1)).andReturn(block);
+        expect(block.render(context, writer)).andReturn(true);
+
+        directive.start(context, writer, params, request, response, servletContext);
+        directive.end(context, writer, params, request, response, servletContext);
+
+        replay(directive, context, writer, node, viewContext, servletContext, request, response, astMap, params, block);
+        directive.render(context, writer, node);
+        verify(directive, context, writer, node, viewContext, servletContext, request, response, astMap, params, block);
+    }
+}

Propchange: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BlockDirectiveTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java
URL: http://svn.apache.org/viewvc/tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java?rev=902965&view=auto
==============================================================================
--- tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java (added)
+++ tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java Mon Jan 25 20:12:46 2010
@@ -0,0 +1,111 @@
+/*
+ * $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.tiles.velocity.template;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.classextension.EasyMock.*;
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.lang.reflect.Method;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.velocity.context.InternalContextAdapter;
+import org.apache.velocity.runtime.directive.DirectiveConstants;
+import org.apache.velocity.runtime.parser.node.ASTBlock;
+import org.apache.velocity.runtime.parser.node.ASTMap;
+import org.apache.velocity.runtime.parser.node.Node;
+import org.apache.velocity.tools.view.ViewToolContext;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Tests {@link BodyBlockDirective}.
+ *
+ * @version $Rev$ $Date$
+ */
+public class BodyBlockDirectiveTest {
+
+    /**
+     * The directive to test.
+     */
+    private BodyBlockDirective directive;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @Before
+    public void setUp() {
+        directive = createMock(BodyBlockDirective.class, new Method[0]);
+    }
+
+    /**
+     * Test method for {@link org.apache.tiles.velocity.template.BlockDirective#getType()}.
+     */
+    @Test
+    public void testGetType() {
+        replay(directive);
+        assertEquals(DirectiveConstants.BLOCK, directive.getType());
+        verify(directive);
+    }
+
+    /**
+     * Test method for {@link BlockDirective#render(InternalContextAdapter, Writer, Node)}.
+     * @throws IOException If something goes wrong.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testRenderInternalContextAdapterWriterNode() throws IOException {
+        InternalContextAdapter context = createMock(InternalContextAdapter.class);
+        Writer writer = createMock(Writer.class);
+        Node node = createMock(Node.class);
+        ViewToolContext viewContext = createMock(ViewToolContext.class);
+        HttpServletRequest request = createMock(HttpServletRequest.class);
+        HttpServletResponse response = createMock(HttpServletResponse.class);
+        ServletContext servletContext = createMock(ServletContext.class);
+        ASTMap astMap = createMock(ASTMap.class);
+        ASTBlock block = createMock(ASTBlock.class);
+        Map<String, Object> params = createMock(Map.class);
+
+        expect(context.getInternalUserContext()).andReturn(viewContext);
+        expect(viewContext.getRequest()).andReturn(request);
+        expect(viewContext.getResponse()).andReturn(response);
+        expect(viewContext.getServletContext()).andReturn(servletContext);
+        expect(node.jjtGetChild(0)).andReturn(astMap);
+        expect(astMap.value(context)).andReturn(params);
+        expect(node.jjtGetChild(1)).andReturn(block);
+        expect(block.render(eq(context), isA(StringWriter.class))).andReturn(true);
+
+        directive.start(context, writer, params, request, response, servletContext);
+        directive.end(context, writer, params, null, request, response, servletContext);
+
+        replay(directive, context, writer, node, viewContext, servletContext, request, response, astMap, params, block);
+        directive.render(context, writer, node);
+        verify(directive, context, writer, node, viewContext, servletContext, request, response, astMap, params, block);
+    }
+}

Propchange: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tiles/framework/trunk/tiles-velocity/src/test/java/org/apache/tiles/velocity/template/BodyBlockDirectiveTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL