craigmcc 2002/07/01 18:55:27 Modified: . build-tests.xml src/test/org/apache/struts/mock MockHttpServletRequest.java MockHttpServletResponse.java Added: src/test/org/apache/struts/mock MockPageContext.java MockServletConfig.java TestMockBase.java src/test/org/apache/struts/util FormBean.java TestRequestUtils.java Log: Partial set of unit tests for org.apache.struts.util.RequestUtils -- so far, includes tests for absoluteURL(), computeParameters(), computeURL(), and serverURL(). Committing this to celebrate the first bugfix that the new tests helped me find (5942). Struts developers -- before committing changes, please make sure that "ant test.junit" executes with no test failures. In addition, if you have set up at least one of Tomcat 3.2, 4.0, or 4.1, you should run "ant test.tomcat.all" to run the Cactus-based tests on whichever versions you have configured. Revision Changes Path 1.8 +15 -1 jakarta-struts/build-tests.xml Index: build-tests.xml =================================================================== RCS file: /home/cvs/jakarta-struts/build-tests.xml,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- build-tests.xml 30 Jun 2002 01:49:21 -0000 1.7 +++ build-tests.xml 2 Jul 2002 01:55:27 -0000 1.8 @@ -243,6 +243,13 @@ </fileset> </batchtest> + <!-- org.apache.struts.util package Tests --> + <batchtest fork="yes"> + <fileset dir="${out.test.dir}/classes"> + <include name="org/apache/struts/util/Test*.class" /> + </fileset> + </batchtest> + </junit> </target> @@ -502,10 +509,17 @@ <classpath refid="compile.classpath"/> <formatter type="plain" usefile="false"/> - <!-- Initiate test executions --> + <!-- Tests for org.apache.struts.action --> <test name="org.apache.struts.action.TestDynaActionForm"/> <test name="org.apache.struts.action.TestDynaActionFormClass"/> <test name="org.apache.struts.config.TestApplicationConfig"/> + + <!-- Tests for org.apache.struts.util --> + <batchtest fork="yes"> + <fileset dir="${out.test.dir}/classes"> + <include name="org/apache/struts/util/Test*.class"/> + </fileset> + </batchtest> </junit> 1.2 +8 -8 jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletRequest.java Index: MockHttpServletRequest.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletRequest.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MockHttpServletRequest.java 1 Jul 2002 22:10:35 -0000 1.1 +++ MockHttpServletRequest.java 2 Jul 2002 01:55:27 -0000 1.2 @@ -485,22 +485,22 @@ public String getScheme() { - throw new UnsupportedOperationException(); + return ("http"); } public String getServerName() { - throw new UnsupportedOperationException(); + return ("localhost"); } public int getServerPort() { - throw new UnsupportedOperationException(); + return (8080); } public boolean isSecure() { - throw new UnsupportedOperationException(); + return (false); } 1.2 +8 -8 jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletResponse.java Index: MockHttpServletResponse.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockHttpServletResponse.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- MockHttpServletResponse.java 1 Jul 2002 22:10:35 -0000 1.1 +++ MockHttpServletResponse.java 2 Jul 2002 01:55:27 -0000 1.2 @@ -131,22 +131,22 @@ public String encodeRedirectUrl(String url) { - throw new UnsupportedOperationException(); + return (encodeRedirectURL(url)); } public String encodeRedirectURL(String url) { - throw new UnsupportedOperationException(); + return (url); } public String encodeUrl(String url) { - throw new UnsupportedOperationException(); + return (encodeURL(url)); } public String encodeURL(String url) { - throw new UnsupportedOperationException(); + return (url); } 1.1 jakarta-struts/src/test/org/apache/struts/mock/MockPageContext.java Index: MockPageContext.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockPageContext.java,v 1.1 2002/07/02 01:55:27 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/07/02 01:55:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.struts.mock; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.BodyContent; /** * <p>Mock <strong>ServletContext</strong> object for low-level unit tests * of Struts controller components. Coarser grained tests should be * implemented in terms of the Cactus framework, instead of the mock * object classes.</p> * * <p><strong>WARNING</strong> - Only the minimal set of methods needed to * create unit tests is provided, plus additional methods to configure this * object as necessary. Methods for unsupported operations will throw * <code>UnsupportedOperationException</code>.</p> * * <p><strong>WARNING</strong> - Because unit tests operate in a single * threaded environment, no synchronization is performed.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/07/02 01:55:27 $ */ public class MockPageContext extends PageContext { // ----------------------------------------------------------- Constructors public MockPageContext() { super(); } public MockPageContext(ServletConfig config, ServletRequest request, ServletResponse response) { super(); 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; // --------------------------------------------------------- Public Methods public void setValues(ServletConfig config, ServletRequest request, ServletResponse response) { this.config = config; if (config != null) { this.application = config.getServletContext(); } else { this.application = null; } this.request = request; this.response = response; if (request != null) { session = ((HttpServletRequest) request).getSession(false); } else { this.session = null; } } // ---------------------------------------------------- PageContext Methods public Object findAttribute(String name) { Object value = getAttribute(name, PageContext.PAGE_SCOPE); if (value == null) { value = getAttribute(name, PageContext.REQUEST_SCOPE); } if (value == null) { value = getAttribute(name, PageContext.SESSION_SCOPE); } if (value == null) { value = getAttribute(name, PageContext.APPLICATION_SCOPE); } return (value); } public void forward(String path) { throw new UnsupportedOperationException(); } public Object getAttribute(String name) { return (getAttribute(name, PageContext.PAGE_SCOPE)); } public Object getAttribute(String name, int scope) { if (scope == PageContext.PAGE_SCOPE) { return (attributes.get(name)); } else if (scope == PageContext.REQUEST_SCOPE) { if (request != null) { return (request.getAttribute(name)); } else { return (null); } } else if (scope == PageContext.SESSION_SCOPE) { if (session != null) { return (session.getAttribute(name)); } else { return (null); } } else if (scope == PageContext.APPLICATION_SCOPE) { if (application != null) { return (application.getAttribute(name)); } else { return (null); } } else { throw new IllegalArgumentException("Invalid scope " + scope); } } public Enumeration getAttributeNamesInScope(int scope) { if (scope == PageContext.PAGE_SCOPE) { return (new MockEnumeration(attributes.keySet().iterator())); } else if (scope == PageContext.REQUEST_SCOPE) { if (request != null) { return (request.getAttributeNames()); } else { return (new MockEnumeration(Collections.EMPTY_LIST.iterator())); } } else if (scope == PageContext.SESSION_SCOPE) { if (session != null) { return (session.getAttributeNames()); } else { return (new MockEnumeration(Collections.EMPTY_LIST.iterator())); } } else if (scope == PageContext.APPLICATION_SCOPE) { if (application != null) { return (application.getAttributeNames()); } else { return (new MockEnumeration(Collections.EMPTY_LIST.iterator())); } } else { throw new IllegalArgumentException("Invalid scope " + scope); } } public int getAttributesScope(String name) { if (attributes.get(name) != null) { return (PageContext.PAGE_SCOPE); } else if ((request != null) && (request.getAttribute(name) != null)) { return (PageContext.REQUEST_SCOPE); } else if ((session != null) && (session.getAttribute(name) != null)) { return (PageContext.SESSION_SCOPE); } else if ((application != null) && (application.getAttribute(name) != null)) { return (PageContext.APPLICATION_SCOPE); } else { return (0); } } public Exception getException() { throw new UnsupportedOperationException(); } public JspWriter getOut() { throw new UnsupportedOperationException(); } public Object getPage() { throw new UnsupportedOperationException(); } public ServletRequest getRequest() { return (this.request); } public ServletResponse getResponse() { return (this.response); } public ServletConfig getServletConfig() { return (this.config); } public ServletContext getServletContext() { return (this.application); } public HttpSession getSession() { return (this.session); } public void handlePageException(Exception e) { throw new UnsupportedOperationException(); } public void handlePageException(Throwable t) { throw new UnsupportedOperationException(); } public void include(String path) { throw new UnsupportedOperationException(); } public void initialize(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoFlush) { throw new UnsupportedOperationException(); } public JspWriter popBody() { throw new UnsupportedOperationException(); } public BodyContent pushBody() { throw new UnsupportedOperationException(); } public void release() { throw new UnsupportedOperationException(); } public void removeAttribute(String name) { int scope = getAttributesScope(name); if (scope != 0) { removeAttribute(name, scope); } } public void removeAttribute(String name, int scope) { if (scope == PageContext.PAGE_SCOPE) { attributes.remove(name); } else if (scope == PageContext.REQUEST_SCOPE) { if (request != null) { request.removeAttribute(name); } } else if (scope == PageContext.SESSION_SCOPE) { if (session != null) { session.removeAttribute(name); } } else if (scope == PageContext.APPLICATION_SCOPE) { if (application != null) { application.removeAttribute(name); } } else { throw new IllegalArgumentException("Invalid scope " + scope); } } public void setAttribute(String name, Object value) { setAttribute(name, value, PageContext.PAGE_SCOPE); } public void setAttribute(String name, Object value, int scope) { if (scope == PageContext.PAGE_SCOPE) { attributes.put(name, value); } else if (scope == PageContext.REQUEST_SCOPE) { if (request != null) { request.setAttribute(name, value); } } else if (scope == PageContext.SESSION_SCOPE) { if (session != null) { session.setAttribute(name, value); } } else if (scope == PageContext.APPLICATION_SCOPE) { if (application != null) { application.setAttribute(name, value); } } else { throw new IllegalArgumentException("Invalid scope " + scope); } } } 1.1 jakarta-struts/src/test/org/apache/struts/mock/MockServletConfig.java Index: MockServletConfig.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockServletConfig.java,v 1.1 2002/07/02 01:55:27 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/07/02 01:55:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.struts.mock; import java.io.InputStream; import java.net.URL; import java.util.Enumeration; import java.util.HashMap; import java.util.Set; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; /** * <p>Mock <strong>ServletConfig</strong> object for low-level unit tests * of Struts controller components. Coarser grained tests should be * implemented in terms of the Cactus framework, instead of the mock * object classes.</p> * * <p><strong>WARNING</strong> - Only the minimal set of methods needed to * create unit tests is provided, plus additional methods to configure this * object as necessary. Methods for unsupported operations will throw * <code>UnsupportedOperationException</code>.</p> * * <p><strong>WARNING</strong> - Because unit tests operate in a single * threaded environment, no synchronization is performed.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/07/02 01:55:27 $ */ public class MockServletConfig implements ServletConfig { // ----------------------------------------------------------- Constructors public MockServletConfig() { super(); } public MockServletConfig(ServletContext context) { super(); setServletContext(context); } // ----------------------------------------------------- Instance Variables protected ServletContext context = null; protected HashMap parameters = new HashMap(); // --------------------------------------------------------- Public Methods public void addInitParameter(String name, String value) { parameters.put(name, value); } public void setServletContext(ServletContext context) { this.context = context; } // ------------------------------------------------- ServletContext Methods public String getInitParameter(String name) { return ((String) parameters.get(name)); } public Enumeration getInitParameterNames() { return (new MockEnumeration(parameters.keySet().iterator())); } public ServletContext getServletContext() { return (this.context); } public String getServletName() { return ("action"); } } 1.1 jakarta-struts/src/test/org/apache/struts/mock/TestMockBase.java Index: TestMockBase.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/mock/TestMockBase.java,v 1.1 2002/07/02 01:55:27 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/07/02 01:55:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.struts.mock; import junit.framework.*; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForward; import org.apache.struts.config.ApplicationConfig; import org.apache.struts.config.ForwardConfig; /** * <p>Convenience base class for unit tests of the * <code>org.apache.struts.util</code> package, and others that require * a runtime environment similar to what the Struts controller servlet * sets up. The <code>setUp()</code> method establishes * a consistent basic environment for the various tests. The only * tests included in this class are simple validations that the basic * environment was set up correctly.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/07/02 01:55:27 $ */ public class TestMockBase extends TestCase { // ----------------------------------------------------------------- Basics public TestMockBase(String name) { super(name); } public static void main(String args[]) { junit.awtui.TestRunner.main (new String[] { TestMockBase.class.getName() } ); } public static Test suite() { return (new TestSuite(TestMockBase.class)); } // ----------------------------------------------------- Instance Variables protected ApplicationConfig appConfig = null; protected ApplicationConfig appConfig2 = null; protected MockServletConfig config = null; protected MockServletContext context = null; protected MockPageContext page = null; protected MockPrincipal principal = null; protected MockHttpServletRequest request = null; protected MockHttpServletResponse response = null; protected MockHttpSession session = null; // ----------------------------------------------------- Setup and Teardown public void setUp() { // Set up the servlet API objects for a test scenario context = new MockServletContext(); config = new MockServletConfig(context); session = new MockHttpSession(context); request = new MockHttpServletRequest(session); principal = new MockPrincipal("username", new String[] { "admin", "manager" }); request.setUserPrincipal(principal); response = new MockHttpServletResponse(); page = new MockPageContext(config, request, response); // Set up application configuration for the default subapp appConfig = new ApplicationConfig(""); context.setAttribute(Action.APPLICATION_KEY, appConfig); appConfig.addForwardConfig (new ActionForward("foo", "/bar.jsp", false)); // Set up application configuration for the second subapp appConfig2 = new ApplicationConfig("/2"); context.setAttribute(Action.APPLICATION_KEY + "/2", appConfig2); appConfig2.addForwardConfig (new ActionForward("foo", "/baz.jsp", false)); // NOTE - we do not initialize the request attribute // for the selected subapp so that fallbacks to the // default subapp can be tested. To select a subapp, // tests should set the request attribute Action.APPLICATION_KEY // to the ApplicationConfig instance for the selected subapp } public void tearDown() { appConfig = null; config = null; context = null; page = null; principal = null; request = null; response = null; session = null; } // ------------------------------------------------------- Individual Tests // Test the basic setup of the mock object environment public void testUtilBaseEnvironment() { // Validate the servlet API objects and inter-relationships assertNotNull("config is present", config); assertNotNull("context is present", context); assertNotNull("page is present", page); assertNotNull("principal is present", principal); assertNotNull("request is present", request); assertNotNull("response is present", response); assertNotNull("session is present", session); assertEquals("page-->config", config, page.getServletConfig()); assertEquals("config-->context", context, config.getServletContext()); assertEquals("page-->context", context, page.getServletContext()); assertEquals("page-->request", request, page.getRequest()); assertEquals("page-->response", response, page.getResponse()); assertEquals("page-->session", session, page.getSession()); assertEquals("request-->principal", principal, request.getUserPrincipal()); assertEquals("request-->session", session, request.getSession()); assertEquals("session-->context", context, session.getServletContext()); // Validate the configuration for the default subapp assertNotNull("appConfig is present", appConfig); assertEquals("context-->appConfig", appConfig, context.getAttribute(Action.APPLICATION_KEY)); // Validate the configuration for the second subapp assertNotNull("appConfig2 is present", appConfig2); assertEquals("context-->appConfig2", appConfig2, context.getAttribute(Action.APPLICATION_KEY + "/2")); } } 1.1 jakarta-struts/src/test/org/apache/struts/util/FormBean.java Index: FormBean.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/util/FormBean.java,v 1.1 2002/07/02 01:55:27 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/07/02 01:55:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.struts.util; import java.util.HashMap; import java.util.Map; import org.apache.struts.action.ActionForm; /** * <p>General purpose form bean for unit tests.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/07/02 01:55:27 $ */ public class FormBean extends ActionForm { public FormBean() { this(null); } public FormBean(String stringProperty) { this.stringProperty = stringProperty; } protected boolean booleanProperty = false; public boolean getBooleanProperty() { return (this.booleanProperty); } public void setBooleanProperty(boolean booleanProperty) { this.booleanProperty = booleanProperty; } public Map getMapProperty() { HashMap map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); return (map); } protected String stringProperty = null; public String getStringProperty() { return (this.stringProperty); } public void setStringProperty(String stringProperty) { this.stringProperty = stringProperty; } } 1.1 jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java Index: TestRequestUtils.java =================================================================== /* * $Header: /home/cvs/jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java,v 1.1 2002/07/02 01:55:27 craigmcc Exp $ * $Revision: 1.1 $ * $Date: 2002/07/02 01:55:27 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2001 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.struts.util; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import javax.servlet.jsp.JspException; import junit.framework.*; import org.apache.struts.action.Action; import org.apache.struts.taglib.html.Constants; import org.apache.struts.mock.TestMockBase; /** * <p>Unit tests for <code>org.apache.struts.util.RequestUtils</code>.</p> * * @author Craig R. McClanahan * @version $Revision: 1.1 $ $Date: 2002/07/02 01:55:27 $ */ public class TestRequestUtils extends TestMockBase { // ----------------------------------------------------------------- Basics public TestRequestUtils(String name) { super(name); } public static void main(String args[]) { junit.awtui.TestRunner.main (new String[] { TestRequestUtils.class.getName() } ); } public static Test suite() { return (new TestSuite(TestRequestUtils.class)); } // ----------------------------------------------------- Instance Variables // ----------------------------------------------------- Setup and Teardown public void setUp() { super.setUp(); } public void tearDown() { super.tearDown(); } // ------------------------------------------------------- Individual Tests // ---------------------------------------------------------- absoluteURL() public void testAbsoluteURL() { request.setPathElements("/myapp", "action.do", null, null); String url = null; try { url = RequestUtils.absoluteURL(request, "/foo/bar.jsp").toString(); assertEquals("absoluteURL is correct", "http://localhost:8080/myapp/foo/bar.jsp", url); } catch (MalformedURLException e) { fail("Threw MalformedURLException: " + e); } } // ---------------------------------------------------- computeParameters() // No parameters and no transaction token public void testComputeParameters0a() { Map map = null; try { map = RequestUtils.computeParameters(page, null, null, null, null, null, null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNull("Map is null", map); } // No parameters but add transaction token public void testComputeParameters0b() { session.setAttribute(Action.TRANSACTION_TOKEN_KEY, "token"); Map map = null; try { map = RequestUtils.computeParameters (page, null, null, null, null, null, null, null, true); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("One parameter in the returned map", 1, map.size()); assertTrue("Transaction token parameter present", map.containsKey(Constants.TOKEN_KEY)); assertEquals("Transaction token parameter value", "token", (String) map.get(Constants.TOKEN_KEY)); } // Single parameter -- name public void testComputeParameters1a() { session.setAttribute("attr", "bar"); Map map = null; try { map = RequestUtils.computeParameters (page, "foo", "attr", null, null, null, null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("One parameter in the returned map", 1, map.size()); assertTrue("Parameter present", map.containsKey("foo")); assertEquals("Parameter value", "bar", (String) map.get("foo")); } // Single parameter -- scope + name public void testComputeParameters1b() { request.setAttribute("attr", "bar"); Map map = null; try { map = RequestUtils.computeParameters (page, "foo", "attr", null, "request", null, null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("One parameter in the returned map", 1, map.size()); assertTrue("Parameter present", map.containsKey("foo")); assertEquals("Parameter value", "bar", (String) map.get("foo")); } // Single parameter -- scope + name + property public void testComputeParameters1c() { request.setAttribute("attr", new FormBean("bar")); Map map = null; try { map = RequestUtils.computeParameters (page, "foo", "attr", "stringProperty", "request", null, null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("One parameter in the returned map", 1, map.size()); assertTrue("Parameter present", map.containsKey("foo")); assertEquals("Parameter value", "bar", (String) map.get("foo")); } // Provided map -- name public void testComputeParameters2a() { Map map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); session.setAttribute("attr", map); try { map = RequestUtils.computeParameters (page, null, null, null, null, "attr", null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("Two parameter in the returned map", 2, map.size()); assertTrue("Parameter foo1 present", map.containsKey("foo1")); assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1")); assertTrue("Parameter foo2 present", map.containsKey("foo2")); assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2")); } // Provided map -- scope + name public void testComputeParameters2b() { Map map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); request.setAttribute("attr", map); try { map = RequestUtils.computeParameters (page, null, null, null, null, "attr", null, "request", false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("Two parameter in the returned map", 2, map.size()); assertTrue("Parameter foo1 present", map.containsKey("foo1")); assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1")); assertTrue("Parameter foo2 present", map.containsKey("foo2")); assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2")); } // Provided map -- scope + name + property public void testComputeParameters2c() { request.setAttribute("attr", new FormBean()); Map map = null; try { map = RequestUtils.computeParameters (page, null, null, null, null, "attr", "mapProperty", "request", false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("Two parameter in the returned map", 2, map.size()); assertTrue("Parameter foo1 present", map.containsKey("foo1")); assertEquals("Parameter foo1 value", "bar1", (String) map.get("foo1")); assertTrue("Parameter foo2 present", map.containsKey("foo2")); assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2")); } // Provided map -- name with one key and two values public void testComputeParameters2d() { Map map = new HashMap(); map.put("foo", new String[] { "bar1", "bar2" }); session.setAttribute("attr", map); try { map = RequestUtils.computeParameters (page, null, null, null, null, "attr", null, null, false); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("One parameter in the returned map", 1, map.size()); assertTrue("Parameter foo present", map.containsKey("foo")); assertTrue("Parameter foo value type", map.get("foo") instanceof String[]); String values[] = (String[]) map.get("foo"); assertEquals("Values count", 2, values.length); } // Kitchen sink combination of parameters with a merge public void testComputeParameters3a() { request.setAttribute("attr", new FormBean("bar3")); session.setAttribute(Action.TRANSACTION_TOKEN_KEY, "token"); Map map = null; try { map = RequestUtils.computeParameters (page, "foo1", "attr", "stringProperty", "request", "attr", "mapProperty", "request", true); } catch (JspException e) { fail("JspException: " + e); } assertNotNull("Map is not null", map); assertEquals("Three parameter in the returned map", 3, map.size()); assertTrue("Parameter foo1 present", map.containsKey("foo1")); assertTrue("Parameter foo1 value type", map.get("foo1") instanceof String[]); String values[] = (String[]) map.get("foo1"); assertEquals("Values count", 2, values.length); assertTrue("Parameter foo2 present", map.containsKey("foo2")); assertEquals("Parameter foo2 value", "bar2", (String) map.get("foo2")); assertTrue("Transaction token parameter present", map.containsKey(Constants.TOKEN_KEY)); assertEquals("Transaction token parameter value", "token", (String) map.get(Constants.TOKEN_KEY)); } // ----------------------------------------------------------- computeURL() // Default subapp -- Forward only public void testComputeURL1a() { request.setPathElements("/myapp", "action.do", null, null); String url = null; try { url = RequestUtils.computeURL (page, "foo", null, null, null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "/myapp/bar.jsp", url); } // Default subapp -- Href only public void testComputeURL1b() { String url = null; try { url = RequestUtils.computeURL (page, null, "http://foo.com/bar", null, null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "http://foo.com/bar", url); } // Default subapp -- Page only public void testComputeURL1c() { request.setPathElements("/myapp", "action.do", null, null); String url = null; try { url = RequestUtils.computeURL (page, null, null, "/bar", null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "/myapp/bar", url); } // Second subapp -- Forward only public void testComputeURL2a() { request.setAttribute(Action.APPLICATION_KEY, appConfig2); request.setPathElements("/myapp", "/2/action.do", null, null); String url = null; try { url = RequestUtils.computeURL (page, "foo", null, null, null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "/myapp/2/baz.jsp", url); } // Second subapp -- Href only public void testComputeURL2b() { request.setAttribute(Action.APPLICATION_KEY, appConfig2); String url = null; try { url = RequestUtils.computeURL (page, null, "http://foo.com/bar", null, null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "http://foo.com/bar", url); } // Second subapp -- Page only public void testComputeURL2c() { request.setAttribute(Action.APPLICATION_KEY, appConfig2); request.setPathElements("/myapp", "action.do", null, null); String url = null; try { url = RequestUtils.computeURL (page, null, null, "/bar", null, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "/myapp/2/bar", url); } // Add parameters only public void testComputeURL3a() { request.setPathElements("/myapp", "action.do", null, null); Map map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); String url = null; try { url = RequestUtils.computeURL (page, null, null, "/bar", map, null, false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertTrue("url value", url.equals("/myapp/bar?foo1=bar1&foo2=bar2") || url.equals("/myapp/bar?foo2=bar2&foo1=bar1")); } // Add anchor only public void testComputeURL3b() { request.setPathElements("/myapp", "action.do", null, null); String url = null; try { url = RequestUtils.computeURL (page, null, null, "/bar", null, "anchor", false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertEquals("url value", "/myapp/bar#anchor", url); } // Add parameters + anchor public void testComputeURL3c() { request.setPathElements("/myapp", "action.do", null, null); Map map = new HashMap(); map.put("foo1", "bar1"); map.put("foo2", "bar2"); String url = null; try { url = RequestUtils.computeURL (page, null, null, "/bar", map, "anchor", false); } catch (MalformedURLException e) { fail("MalformedURLException: " + e); } assertNotNull("url present", url); assertTrue("url value", url.equals("/myapp/bar?foo1=bar1&foo2=bar2#anchor") || url.equals("/myapp/bar?foo2=bar2&foo1=bar1#anchor")); } // ------------------------------------------------------------ serverURL() public void testServerURL() { String url = null; try { url = RequestUtils.serverURL(request).toString(); } catch (MalformedURLException e) { fail("Threw MalformedURLException: " + e); } assertNotNull("serverURL is present", url); assertEquals("serverURL value", "http://localhost:8080", url); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>