Author: mcucchiara Date: Sat Aug 13 09:27:10 2011 New Revision: 1157359 URL: http://svn.apache.org/viewvc?rev=1157359&view=rev Log: * small optimizations * removed duplicate code
Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/AbstractUITagTest.java struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/AnchorTest.java Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/AbstractUITagTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/AbstractUITagTest.java?rev=1157359&r1=1157358&r2=1157359&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/AbstractUITagTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/AbstractUITagTest.java Sat Aug 13 09:27:10 2011 @@ -21,26 +21,19 @@ package org.apache.struts2.views.jsp; +import com.opensymphony.xwork2.ActionContext; +import com.opensymphony.xwork2.util.logging.Logger; +import com.opensymphony.xwork2.util.logging.LoggerFactory; +import org.apache.struts2.ServletActionContext; +import org.apache.struts2.views.jsp.ui.AbstractUITag; + import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.net.URL; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.StringTokenizer; - -import org.apache.struts2.ServletActionContext; -import org.apache.struts2.views.jsp.ui.AbstractUITag; - -import com.opensymphony.xwork2.ActionContext; -import com.opensymphony.xwork2.util.logging.Logger; -import com.opensymphony.xwork2.util.logging.LoggerFactory; +import java.util.*; /** @@ -255,6 +248,17 @@ public abstract class AbstractUITagTest } /** + * Attempt to verify the contents of this.writer against the contents of the resource specified. verify() performs a + * trim on both ends + * + * @param resource the HTML snippet that we want to validate against + * @throws Exception if the validation failed + */ + public void verifyResource(String resource) throws Exception { + verify(this.getClass().getResource(resource)); + } + + /** * Attempt to verify the contents of this.writer against the contents of the URL specified. verify() performs a * trim on both ends * @@ -262,31 +266,7 @@ public abstract class AbstractUITagTest * @throws Exception if the validation failed */ public void verify(URL url) throws Exception { - if (url == null) { - fail("unable to verify a null URL"); - } else if (this.writer == null) { - fail("AbstractJspWriter.writer not initialized. Unable to verify"); - } - - StringBuilder buffer = new StringBuilder(128); - InputStream in = url.openStream(); - byte[] buf = new byte[4096]; - int nbytes; - - while ((nbytes = in.read(buf)) > 0) { - buffer.append(new String(buf, 0, nbytes)); - } - - in.close(); - - /** - * compare the trimmed values of each buffer and make sure they're equivalent. however, let's make sure to - * normalize the strings first to account for line termination differences between platforms. - */ - String writerString = normalize(writer.toString(), true); - String bufferString = normalize(buffer.toString(), true); - - assertEquals(bufferString, writerString); + verify(url,null); } /** @@ -294,10 +274,11 @@ public abstract class AbstractUITagTest * trim on both ends * * @param url the HTML snippet that we want to validate against + * @param excluded * @throws Exception if the validation failed */ public void verify(URL url, String[] excluded) throws Exception { - if (url == null) { + if (url == null) { fail("unable to verify a null URL"); } else if (this.writer == null) { fail("AbstractJspWriter.writer not initialized. Unable to verify"); Modified: struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/AnchorTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/AnchorTest.java?rev=1157359&r1=1157358&r2=1157359&view=diff ============================================================================== --- struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/AnchorTest.java (original) +++ struts/struts2/trunk/core/src/test/java/org/apache/struts2/views/jsp/ui/AnchorTest.java Sat Aug 13 09:27:10 2011 @@ -30,43 +30,31 @@ import org.apache.struts2.views.jsp.Abst public class AnchorTest extends AbstractUITagTest { public void testSimple() throws Exception { - TestAction testAction = (TestAction) action; - testAction.setFoo("bar"); - - AnchorTag tag = new AnchorTag(); - tag.setPageContext(pageContext); + createAction(); - tag.setId("mylink"); + AnchorTag tag = createTag(); tag.setHref("a"); tag.doStartTag(); tag.doEndTag(); - verify(AnchorTest.class.getResource("href-1.txt")); + verifyResource("href-1.txt"); } public void testSimpleBadQuote() throws Exception { - TestAction testAction = (TestAction) action; - testAction.setFoo("bar"); + createAction(); - AnchorTag tag = new AnchorTag(); - tag.setPageContext(pageContext); - - tag.setId("mylink"); + AnchorTag tag = createTag(); tag.setHref("a\""); tag.doStartTag(); tag.doEndTag(); - verify(AnchorTest.class.getResource("href-2.txt")); + verifyResource("href-2.txt"); } public void testDynamicAttribute() throws Exception { - TestAction testAction = (TestAction) action; - testAction.setFoo("bar"); - - AnchorTag tag = new AnchorTag(); - tag.setPageContext(pageContext); + createAction(); - tag.setId("mylink"); + AnchorTag tag = createTag(); tag.setHref("a"); tag.setDynamicAttribute("uri", "dynAttrName", "dynAttrValue"); @@ -74,17 +62,13 @@ public class AnchorTest extends Abstract tag.doStartTag(); tag.doEndTag(); - verify(AnchorTest.class.getResource("Anchor-2.txt")); + verifyResource("Anchor-2.txt"); } public void testDynamicAttributeAsExpression() throws Exception { - TestAction testAction = (TestAction) action; - testAction.setFoo("bar"); + createAction(); - AnchorTag tag = new AnchorTag(); - tag.setPageContext(pageContext); - - tag.setId("mylink"); + AnchorTag tag = createTag(); tag.setHref("a"); tag.setDynamicAttribute("uri", "placeholder", "foo"); @@ -92,6 +76,19 @@ public class AnchorTest extends Abstract tag.doStartTag(); tag.doEndTag(); - verify(AnchorTest.class.getResource("Anchor-3.txt")); + verifyResource("Anchor-3.txt"); + } + + private void createAction() { + TestAction testAction = (TestAction) action; + testAction.setFoo("bar"); + } + + private AnchorTag createTag() { + AnchorTag tag = new AnchorTag(); + tag.setPageContext(pageContext); + + tag.setId("mylink"); + return tag; } }