tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject svn commit: r960234 - in /tomcat/taglibs/standard/trunk/impl/src: main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java
Date Sat, 03 Jul 2010 16:24:55 GMT
Author: bayard
Date: Sat Jul  3 16:24:54 2010
New Revision: 960234

URL: http://svn.apache.org/viewvc?rev=960234&view=rev
Log:
Adding test cases for common c:set logic; along with a refactoring of the result into a separate
helper method to ease testing. Patch from Jeremy Boynes: #49546

Modified:
    tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java
    tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java

Modified: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java?rev=960234&r1=960233&r2=960234&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/core/SetSupport.java
Sat Jul  3 16:24:54 2010
@@ -91,22 +91,8 @@ public class SetSupport extends BodyTagS
 
     public int doEndTag() throws JspException {
 
-        Object result;      // what we'll store in scope:var
-
-        // determine the value by...
-        if (value != null) {
-            // ... reading our attribute
-            result = value;
-        } else if (valueSpecified) {
-            // ... accepting an explicit null
-            result = null;
-        } else {
-            // ... retrieving and trimming our body
-            if (bodyContent == null || bodyContent.getString() == null)
-                result = "";
-            else
-                result = bodyContent.getString().trim();
-        }
+        // what we'll store in scope:var
+        Object result = getResult();
 
         // decide what to do with the result
         if (var != null) {
@@ -196,6 +182,27 @@ public class SetSupport extends BodyTagS
         return EVAL_PAGE;
     }
     
+    Object getResult() {
+        Object result;      // what we'll store in scope:var
+
+        // determine the value by...
+        if (value != null) {
+            // ... reading our attribute
+            result = value;
+        } else if (valueSpecified) {
+            // ... accepting an explicit null
+            result = null;
+        } else {
+            // ... retrieving and trimming our body
+            if (bodyContent == null || bodyContent.getString() == null)
+                result = "";
+            else
+                result = bodyContent.getString().trim();
+        }
+
+        return result;
+    }
+    
     /**
      * Convert an object to an expected type according to the conversion
      * rules of the Expression Language.

Modified: tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java?rev=960234&r1=960233&r2=960234&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/core/TestSetSupport.java
Sat Jul  3 16:24:54 2010
@@ -16,6 +16,7 @@
  */
 package org.apache.taglibs.standard.tag.common.core;
 
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -24,6 +25,7 @@ import javax.el.ValueExpression;
 import javax.el.VariableMapper;
 import javax.servlet.jsp.JspException;
 import javax.servlet.jsp.PageContext;
+import javax.servlet.jsp.tagext.BodyContent;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -31,6 +33,8 @@ import static org.easymock.EasyMock.repl
 import static org.easymock.EasyMock.verify;
 
 public class TestSetSupport {
+    private static String VALUE = "Hello";
+    private static final String VAR = "x";
 
     private PageContext pageContext;
     private ELContext elContext;
@@ -39,13 +43,15 @@ public class TestSetSupport {
 
     @Before
     public void setup() {
-        tag = new SetSupport();
         pageContext = createMock(PageContext.class);
         elContext = createMock(ELContext.class);
         vm = createMock(VariableMapper.class);
 
         expect(pageContext.getELContext()).andStubReturn(elContext);
         expect(elContext.getVariableMapper()).andStubReturn(vm);
+
+        tag = new SetSupport();
+        tag.setPageContext(pageContext);
     }
 
     /**
@@ -55,13 +61,12 @@ public class TestSetSupport {
      */
     @Test
     public void test49526WhenNotMapped() throws JspException {
-        tag.setPageContext(pageContext);
-        tag.setVar("x");
-        tag.value = "Hello";
+        tag.setVar(VAR);
+        tag.value = VALUE;
 
         // verify mapper is checked but that no action is taken
-        expect(vm.resolveVariable("x")).andReturn(null);
-        pageContext.setAttribute("x", "Hello", PageContext.PAGE_SCOPE);
+        expect(vm.resolveVariable(VAR)).andReturn(null);
+        pageContext.setAttribute(VAR, VALUE, PageContext.PAGE_SCOPE);
         replay(pageContext, elContext, vm);
         tag.doEndTag();
         verify(pageContext, elContext, vm);
@@ -74,15 +79,14 @@ public class TestSetSupport {
      */
     @Test
     public void test49526WhenAlreadyMapped() throws JspException {
-        tag.setPageContext(pageContext);
-        tag.setVar("x");
-        tag.value = "Hello";
+        tag.setVar(VAR);
+        tag.value = VALUE;
 
         // verify mapper is checked and the mapped variable removed
         ValueExpression ve = createMock(ValueExpression.class);
-        expect(vm.resolveVariable("x")).andReturn(ve);
-        expect(vm.setVariable("x", null)).andReturn(ve);
-        pageContext.setAttribute("x", "Hello", PageContext.PAGE_SCOPE);
+        expect(vm.resolveVariable(VAR)).andReturn(ve);
+        expect(vm.setVariable(VAR, null)).andReturn(ve);
+        pageContext.setAttribute(VAR, VALUE, PageContext.PAGE_SCOPE);
         replay(pageContext, elContext, vm, ve);
         tag.doEndTag();
         verify(pageContext, elContext, vm, ve);
@@ -95,15 +99,53 @@ public class TestSetSupport {
      */
     @Test
     public void test49526WhenNotUsingPageContext() throws JspException {
-        tag.setPageContext(pageContext);
-        tag.setVar("x");
-        tag.value = "Hello";
+        tag.setVar(VAR);
+        tag.value = VALUE;
         tag.setScope("request");
 
         // verify mapper is not checked
-        pageContext.setAttribute("x", "Hello", PageContext.REQUEST_SCOPE);
+        pageContext.setAttribute(VAR, VALUE, PageContext.REQUEST_SCOPE);
         replay(pageContext, elContext, vm);
         tag.doEndTag();
         verify(pageContext, elContext, vm);
     }
+
+    @Test
+    public void testResultFromValueAttribute() {
+        tag.valueSpecified = true;
+        tag.value = VALUE;
+        Assert.assertSame(VALUE, tag.getResult());
+    }
+
+    @Test
+    public void testResultFromNullValueAttribute() {
+        tag.valueSpecified = true;
+        tag.value = null;
+        Assert.assertNull(tag.getResult());
+    }
+
+    @Test
+    public void testResultFromBodyContent() {
+        tag.valueSpecified = false;
+        BodyContent bodyContent = createMock(BodyContent.class);
+        expect(bodyContent.getString()).andStubReturn("  Hello  ");
+        replay(bodyContent);
+        tag.setBodyContent(bodyContent);
+        Assert.assertEquals(VALUE, tag.getResult());
+    }
+
+    @Test
+    public void testResultFromNullBodyContent() {
+        tag.valueSpecified = false;
+        tag.setBodyContent(null);
+        Assert.assertEquals("", tag.getResult());
+    }
+
+    @Test
+    public void testResultFromEmptyBodyContent() {
+        tag.valueSpecified = false;
+        BodyContent bodyContent = createMock(BodyContent.class);
+        expect(bodyContent.getString()).andStubReturn(null);
+        Assert.assertEquals("", tag.getResult());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message