Author: jmitchell Date: Fri Apr 15 21:23:14 2005 New Revision: 161545 URL: http://svn.apache.org/viewcvs?view=rev&rev=161545 Log: Fix for mock objects, bring tests to 97% coverage for TagUtils
Modified: struts/core/trunk/src/share/org/apache/struts/mock/MockFormBean.java struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletRequest.java struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletResponse.java struts/core/trunk/src/share/org/apache/struts/mock/MockPageContext.java struts/core/trunk/src/share/org/apache/struts/util/ResponseUtils.java Modified: struts/core/trunk/src/share/org/apache/struts/mock/MockFormBean.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/mock/MockFormBean.java?view=diff&r1=161544&r2=161545 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/mock/MockFormBean.java (original) +++ struts/core/trunk/src/share/org/apache/struts/mock/MockFormBean.java Fri Apr 15 21:23:14 2005 @@ -33,18 +33,101 @@ public class MockFormBean extends ActionForm { - + /* + * Flag to indicate whether certain methods should complete properly + * or throw an Exception + */ + private boolean throwException = false; + + + private boolean returnNulls = false; + private String defaultValue; + private Double defaultDouble; + private int arrayCount; + protected boolean booleanProperty = false; + + // ------------------- Constructors + public MockFormBean() { this(null); } - + public MockFormBean(boolean throwException, boolean returnNulls){ + super(); + this.throwException = throwException; + this.returnNulls = returnNulls; + } + + public MockFormBean(boolean throwException){ + this.throwException = throwException; + } + + public MockFormBean(boolean throwException, + boolean returnNulls, String defaultValue){ + this(throwException, returnNulls); + this.defaultValue = defaultValue; + + } public MockFormBean(String stringProperty) { this.stringProperty = stringProperty; } - protected boolean booleanProperty = false; + public MockFormBean(boolean throwException, + boolean returnNulls, String defaultValue, int arrayCount){ + this(throwException, returnNulls, defaultValue); + this.arrayCount = arrayCount; + + } + + public MockFormBean(boolean throwException, + boolean returnNulls, Double defaultDouble){ + this(throwException, returnNulls); + this.defaultDouble = defaultDouble; + + } + + // ------------------- public methods + + public String getJustThrowAnException() throws Exception{ + throw new Exception(); + } + + public Object getThrowIllegalAccessException() throws Exception{ + if (true) + throw new IllegalAccessException(); + return null; + } + + public String getStringValue() throws Exception{ + if (throwException) + throw new Exception(); + if (returnNulls) + return null; + return defaultValue; + } + + public String[] getStringArray() throws Exception{ + if (throwException) + throw new Exception(); + if (returnNulls) + return null; + + String[] rtn = new String[arrayCount]; + for (int i = 0; i < rtn.length; i++) { + rtn[i] = defaultValue + i; + } + return rtn; + } + + public Double getDoubleValue() throws Exception{ + if (throwException) + throw new Exception(); + if (returnNulls) + return null; + return defaultDouble; + + } public boolean getBooleanProperty() { return (this.booleanProperty); @@ -59,6 +142,13 @@ HashMap map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); + return (map); + } + + public Map getMapPropertyArrayValues() { + HashMap map = new HashMap(); + map.put("foo1", new String[]{"bar1", "baz1"}); + map.put("foo2", new String[]{"bar2", "baz2"}); return (map); } Modified: struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletRequest.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletRequest.java?view=diff&r1=161544&r2=161545 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletRequest.java (original) +++ struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletRequest.java Fri Apr 15 21:23:14 2005 @@ -299,10 +299,13 @@ } - public HttpSession getSession(boolean create) { + public HttpSession getSession( boolean create ) { if (create && (session == null)) { - return new MockHttpSession(); -// throw new UnsupportedOperationException(); + session = new MockHttpSession(); + + // modified to act like the real deal, + // call with (false) if you want null + // throw new UnsupportedOperationException(); } return (session); } Modified: struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletResponse.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletResponse.java?view=diff&r1=161544&r2=161545 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletResponse.java (original) +++ struts/core/trunk/src/share/org/apache/struts/mock/MockHttpServletResponse.java Fri Apr 15 21:23:14 2005 @@ -25,6 +25,7 @@ import java.util.Locale; import javax.servlet.ServletOutputStream; import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; Modified: struts/core/trunk/src/share/org/apache/struts/mock/MockPageContext.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/mock/MockPageContext.java?view=diff&r1=161544&r2=161545 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/mock/MockPageContext.java (original) +++ struts/core/trunk/src/share/org/apache/struts/mock/MockPageContext.java Fri Apr 15 21:23:14 2005 @@ -20,6 +20,9 @@ package org.apache.struts.mock; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; @@ -55,10 +58,21 @@ public class MockPageContext extends PageContext { + // ----------------------------------------------------- Instance Variables + protected ServletContext application = null; + protected HashMap attributes = new HashMap(); // Page scope attributes + protected ServletConfig config = null; + protected ServletRequest request = null; + protected ServletResponse response = null; + protected HttpSession session = null; + + private boolean throwIOException; + private boolean returnBodyContent; + - // ----------------------------------------------------------- Constructors + // ----------------------------------------------------------- Constructors public MockPageContext() { super(); @@ -72,17 +86,22 @@ setValues(config, request, response); } - - // ----------------------------------------------------- Instance Variables - - - protected ServletContext application = null; - protected HashMap attributes = new HashMap(); // Page scope attributes - protected ServletConfig config = null; - protected ServletRequest request = null; - protected ServletResponse response = null; - protected HttpSession session = null; - + /** + * Construct a new PageContext impl. + * @param throwIOException Determines if the returned JspWriter should + * throw an IOException on any method call. + * @param returnBody Determines if getOut() should return a new + * <code>JspWriter</code> or a <code>BodyContent</code>. + */ + public MockPageContext(boolean throwIOException, boolean returnBody){ + this.throwIOException = throwIOException; + this.returnBodyContent = returnBody; + } + private void checkAndThrow() throws IOException{ + if (throwIOException){ + throw new IOException(); + } + } // --------------------------------------------------------- Public Methods @@ -206,9 +225,78 @@ } + /** + * Custom JspWriter that throws the specified exception + * (supplied on the constructor...if any), else it simply + * returns. + */ public JspWriter getOut() { - throw new UnsupportedOperationException(); + JspWriter jspWriter = new JspWriter(0, false) { + public void print(String s) throws IOException { + checkAndThrow(); + } + public void newLine() throws IOException {checkAndThrow();} + public void print(boolean b) throws IOException {checkAndThrow();} + public void print(char c) throws IOException {checkAndThrow();} + public void print(int i) throws IOException {checkAndThrow();} + public void print(long l) throws IOException {checkAndThrow();} + public void print(float f) throws IOException {checkAndThrow();} + public void print(double d) throws IOException {checkAndThrow();} + public void print(char[] s) throws IOException {checkAndThrow();} + public void print(Object obj) throws IOException {checkAndThrow();} + public void println() throws IOException {checkAndThrow();} + public void println(boolean x) throws IOException {checkAndThrow();} + public void println(char x) throws IOException {checkAndThrow();} + public void println(int x) throws IOException {checkAndThrow();} + public void println(long x) throws IOException {checkAndThrow();} + public void println(float x) throws IOException {checkAndThrow();} + public void println(double x) throws IOException {checkAndThrow();} + public void println(char[] x) throws IOException {checkAndThrow();} + public void println(String x) throws IOException {checkAndThrow();} + public void println(Object x) throws IOException {checkAndThrow();} + public void clear() throws IOException {checkAndThrow();} + public void clearBuffer() throws IOException {checkAndThrow();} + public void flush() throws IOException {checkAndThrow();} + public void close() throws IOException {checkAndThrow();} + public int getRemaining() {return 0;} + public void write(char[] cbuf, int off, int len) throws IOException {checkAndThrow();} + + }; + if (returnBodyContent) + return new BodyContent(jspWriter) { + public Reader getReader() {return null;} + public String getString() {return null;} + public void writeOut(Writer out) throws IOException {checkAndThrow();} + public void newLine() throws IOException {checkAndThrow();} + public void print(boolean b) throws IOException {checkAndThrow();} + public void print(char c) throws IOException {checkAndThrow();} + public void print(int i) throws IOException {checkAndThrow();} + public void print(long l) throws IOException {checkAndThrow();} + public void print(float f) throws IOException {checkAndThrow();} + public void print(double d) throws IOException {checkAndThrow();} + public void print(char[] s) throws IOException {checkAndThrow();} + public void print(String s) throws IOException {checkAndThrow();} + public void print(Object obj) throws IOException {checkAndThrow();} + public void println() throws IOException {checkAndThrow();} + public void println(boolean x) throws IOException {checkAndThrow();} + public void println(char x) throws IOException {checkAndThrow();} + public void println(int x) throws IOException {checkAndThrow();} + public void println(long x) throws IOException {checkAndThrow();} + public void println(float x) throws IOException {checkAndThrow();} + public void println(double x) throws IOException {checkAndThrow();} + public void println(char[] x) throws IOException {checkAndThrow();} + public void println(String x) throws IOException {checkAndThrow();} + public void println(Object x) throws IOException {checkAndThrow();} + public void clear() throws IOException {checkAndThrow();} + public void clearBuffer() throws IOException {checkAndThrow();} + public void close() throws IOException {checkAndThrow();} + public int getRemaining() {return 0;} + public void write(char[] cbuf, int off, int len) throws IOException {checkAndThrow();} + + }; + return jspWriter; } + public Object getPage() { Modified: struts/core/trunk/src/share/org/apache/struts/util/ResponseUtils.java URL: http://svn.apache.org/viewcvs/struts/core/trunk/src/share/org/apache/struts/util/ResponseUtils.java?view=diff&r1=161544&r2=161545 ============================================================================== --- struts/core/trunk/src/share/org/apache/struts/util/ResponseUtils.java (original) +++ struts/core/trunk/src/share/org/apache/struts/util/ResponseUtils.java Fri Apr 15 21:23:14 2005 @@ -82,7 +82,7 @@ * by the corresponding character entities. * * @param value The string to be filtered and returned - * @deprecated + * @deprecated Why???? */ public static String filter(String value) { --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]