craigmcc 2002/07/01 21:23:14
Modified: src/share/org/apache/struts/action ActionFormBean.java
src/test/org/apache/struts/mock TestMockBase.java
src/test/org/apache/struts/util TestRequestUtils.java
Added: src/test/org/apache/struts/mock MockFormBean.java
Removed: src/test/org/apache/struts/util FormBean.java
Log:
Add RequestUtils unit tests for createActionForm, requestURL, and
selectApplication().
Revision Changes Path
1.6 +31 -4
jakarta-struts/src/share/org/apache/struts/action/ActionFormBean.java
Index: ActionFormBean.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/action/ActionFormBean.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- ActionFormBean.java 25 Jun 2002 00:42:44 -0000 1.5
+++ ActionFormBean.java 2 Jul 2002 04:23:13 -0000 1.6
@@ -86,4 +86,31 @@
public class ActionFormBean extends FormBeanConfig {
+ /**
+ * Construct an instance with default vaslues.
+ */
+ public ActionFormBean() {
+
+ super();
+
+ }
+
+
+ /**
+ * Construct an instance with the specified values.
+ *
+ * @param name Form bean name
+ * @param type Fully qualified class name
+ * @param dynamic Is this a DynaActionForm bean?
+ */
+ public ActionFormBean(String name, String type, boolean dynamic) {
+
+ super();
+ setName(name);
+ setType(type);
+ setDynamic(dynamic);
+
+ }
+
+
}
1.2 +123 -13 jakarta-struts/src/test/org/apache/struts/mock/TestMockBase.java
Index: TestMockBase.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/TestMockBase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestMockBase.java 2 Jul 2002 01:55:27 -0000 1.1
+++ TestMockBase.java 2 Jul 2002 04:23:14 -0000 1.2
@@ -65,9 +65,11 @@
import junit.framework.*;
import org.apache.struts.action.Action;
+import org.apache.struts.action.ActionFormBean;
import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
import org.apache.struts.config.ApplicationConfig;
-import org.apache.struts.config.ForwardConfig;
+import org.apache.struts.config.FormPropertyConfig;
@@ -136,24 +138,132 @@
response = new MockHttpServletResponse();
page = new MockPageContext(config, request, response);
- // Set up application configuration for the default subapp
+ // Set up application configurations for our supported subapps
+ setUpDefaultApp();
+ setUpSecondApp();
+
+ // 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
+
+ }
+
+
+ protected void setUpDefaultApp() {
+
+ ActionFormBean formBean = null;
+ ActionForward forward = null;
+ ActionMapping mapping = null;
+
appConfig = new ApplicationConfig("");
context.setAttribute(Action.APPLICATION_KEY, appConfig);
+
+ // Forward "foo" to "/bar.jsp"
appConfig.addForwardConfig
(new ActionForward("foo", "/bar.jsp", false));
- // Set up application configuration for the second subapp
+ // Form Bean "static" is a standard ActionForm subclass
+ formBean = new ActionFormBean
+ ("static",
+ "org.apache.struts.mock.MockFormBean",
+ false);
+ appConfig.addFormBeanConfig(formBean);
+
+ // Action "/static" uses the "static" form bean in request scope
+ mapping = new ActionMapping();
+ mapping.setInput("/static.jsp");
+ mapping.setName("static");
+ mapping.setPath("/static");
+ mapping.setScope("request");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig.addActionConfig(mapping);
+
+ // Form Bean "dynamic" is a DynaActionForm with the same properties
+ formBean = new ActionFormBean
+ ("dynamic",
+ "org.apache.struts.action.DynaActionForm",
+ true);
+ formBean.addFormPropertyConfig
+ (new FormPropertyConfig("booleanProperty", "boolean", "false"));
+ formBean.addFormPropertyConfig
+ (new FormPropertyConfig("stringProperty", "java.lang.String",
+ null));
+ appConfig.addFormBeanConfig(formBean);
+
+ // Action "/dynamic" uses the "dynamic" form bean in session scope
+ mapping = new ActionMapping();
+ mapping.setInput("/dynamic.jsp");
+ mapping.setName("dynamic");
+ mapping.setPath("/dynamic");
+ mapping.setScope("session");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig.addActionConfig(mapping);
+
+ // Action "/noform" has no form bean associated with it
+ mapping = new ActionMapping();
+ mapping.setPath("/noform");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig.addActionConfig(mapping);
+
+ }
+
+
+ protected void setUpSecondApp() {
+
+ ActionFormBean formBean = null;
+ ActionMapping mapping = null;
+
appConfig2 = new ApplicationConfig("/2");
context.setAttribute(Action.APPLICATION_KEY + "/2", appConfig2);
+
+ // Forward "foo" to "/baz.jsp" (different from default)
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
+ // Form Bean "static" is a standard ActionForm subclass (same as default)
+ formBean = new ActionFormBean
+ ("static",
+ "org.apache.struts.mock.MockFormBean",
+ false);
+ appConfig2.addFormBeanConfig(formBean);
+
+ // Action "/static" uses the "static" form bean in request scope (same as
default)
+ mapping = new ActionMapping();
+ mapping.setInput("/static.jsp");
+ mapping.setName("static");
+ mapping.setPath("/static");
+ mapping.setScope("request");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig2.addActionConfig(mapping);
+
+ // Form Bean "dynamic2" is a DynaActionForm with the same properties
+ formBean = new ActionFormBean
+ ("dynamic2",
+ "org.apache.struts.action.DynaActionForm",
+ true);
+ formBean.addFormPropertyConfig
+ (new FormPropertyConfig("booleanProperty", "boolean", "false"));
+ formBean.addFormPropertyConfig
+ (new FormPropertyConfig("stringProperty", "java.lang.String",
+ null));
+ appConfig2.addFormBeanConfig(formBean);
+
+ // Action "/dynamic2" uses the "dynamic2" form bean in session scope
+ mapping = new ActionMapping();
+ mapping.setInput("/dynamic2.jsp");
+ mapping.setName("dynamic2");
+ mapping.setPath("/dynamic2");
+ mapping.setScope("session");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig2.addActionConfig(mapping);
+
+ // Action "/noform" has no form bean associated with it (same as default)
+ mapping = new ActionMapping();
+ mapping.setPath("/noform");
+ mapping.setType("org.apache.struts.mock.MockAction");
+ appConfig2.addActionConfig(mapping);
}
1.1 jakarta-struts/src/test/org/apache/struts/mock/MockFormBean.java
Index: MockFormBean.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-struts/src/test/org/apache/struts/mock/MockFormBean.java,v 1.1
2002/07/02 04:23:14 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2002/07/02 04:23:14 $
*
* ====================================================================
*
* 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.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 04:23:14 $
*/
public class MockFormBean extends ActionForm {
public MockFormBean() {
this(null);
}
public MockFormBean(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.2 +229 -14
jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java
Index: TestRequestUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/test/org/apache/struts/util/TestRequestUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TestRequestUtils.java 2 Jul 2002 01:55:27 -0000 1.1
+++ TestRequestUtils.java 2 Jul 2002 04:23:14 -0000 1.2
@@ -70,7 +70,13 @@
import javax.servlet.jsp.JspException;
import junit.framework.*;
import org.apache.struts.action.Action;
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionMapping;
+import org.apache.struts.action.DynaActionForm;
+import org.apache.struts.action.RequestProcessor;
+import org.apache.struts.config.ApplicationConfig;
import org.apache.struts.taglib.html.Constants;
+import org.apache.struts.mock.MockFormBean;
import org.apache.struts.mock.TestMockBase;
@@ -132,7 +138,7 @@
public void testAbsoluteURL() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
String url = null;
try {
url = RequestUtils.absoluteURL(request, "/foo/bar.jsp").toString();
@@ -240,7 +246,7 @@
// Single parameter -- scope + name + property
public void testComputeParameters1c() {
- request.setAttribute("attr", new FormBean("bar"));
+ request.setAttribute("attr", new MockFormBean("bar"));
Map map = null;
try {
@@ -329,7 +335,7 @@
// Provided map -- scope + name + property
public void testComputeParameters2c() {
- request.setAttribute("attr", new FormBean());
+ request.setAttribute("attr", new MockFormBean());
Map map = null;
try {
@@ -387,7 +393,7 @@
// Kitchen sink combination of parameters with a merge
public void testComputeParameters3a() {
- request.setAttribute("attr", new FormBean("bar3"));
+ request.setAttribute("attr", new MockFormBean("bar3"));
session.setAttribute(Action.TRANSACTION_TOKEN_KEY, "token");
Map map = null;
@@ -430,7 +436,7 @@
// Default subapp -- Forward only
public void testComputeURL1a() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
String url = null;
try {
url = RequestUtils.computeURL
@@ -471,7 +477,7 @@
// Default subapp -- Page only
public void testComputeURL1c() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
String url = null;
try {
url = RequestUtils.computeURL
@@ -536,7 +542,7 @@
public void testComputeURL2c() {
request.setAttribute(Action.APPLICATION_KEY, appConfig2);
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
String url = null;
try {
url = RequestUtils.computeURL
@@ -557,7 +563,7 @@
// Add parameters only
public void testComputeURL3a() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
Map map = new HashMap();
map.put("foo1", "bar1");
map.put("foo2", "bar2");
@@ -581,7 +587,7 @@
// Add anchor only
public void testComputeURL3b() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
String url = null;
try {
url = RequestUtils.computeURL
@@ -602,7 +608,7 @@
// Add parameters + anchor
public void testComputeURL3c() {
- request.setPathElements("/myapp", "action.do", null, null);
+ request.setPathElements("/myapp", "/action.do", null, null);
Map map = new HashMap();
map.put("foo1", "bar1");
map.put("foo2", "bar2");
@@ -623,12 +629,221 @@
}
+ // ----------------------------------------------------- createActionForm()
+ // Default subapp -- No ActionForm should be created
+ public void testCreateActionForm1a() {
+
+ request.setPathElements("/myapp", "/noform.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig.findActionConfig("/noform");
+ assertNotNull("Found /noform mapping", mapping);
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig, null);
+ assertNull("No ActionForm returned", form);
+
+ }
+
+
+ // Second subapp -- No ActionForm should be created
+ public void testCreateActionForm1b() {
+
+ request.setPathElements("/myapp", "/2/noform.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig2.findActionConfig("/noform");
+ assertNotNull("Found /noform mapping", mapping);
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig2, null);
+ assertNull("No ActionForm returned", form);
+
+ }
+
+
+ // Default subapp -- Standard ActionForm should be created
+ public void testCreateActionForm2a() {
+
+ request.setPathElements("/myapp", "/static.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig.findActionConfig("/static");
+ assertNotNull("Found /static mapping", mapping);
+ assertNotNull("Mapping has non-null name",
+ mapping.getName());
+ assertEquals("Mapping has correct name",
+ "static",
+ mapping.getName());
+ assertNotNull("AppConfig has form bean " + mapping.getName(),
+ appConfig.findFormBeanConfig(mapping.getName()));
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig, null);
+ assertNotNull("ActionForm returned", form);
+ assertTrue("ActionForm of correct type",
+ form instanceof MockFormBean);
+
+ }
+
+
+ // Second subapp -- Standard ActionForm should be created
+ public void testCreateActionForm2b() {
+
+ request.setPathElements("/myapp", "/2/static.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig2.findActionConfig("/static");
+ assertNotNull("Found /static mapping", mapping);
+ assertNotNull("Mapping has non-null name",
+ mapping.getName());
+ assertEquals("Mapping has correct name",
+ "static",
+ mapping.getName());
+ assertNotNull("AppConfig has form bean " + mapping.getName(),
+ appConfig.findFormBeanConfig(mapping.getName()));
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig2, null);
+ assertNotNull("ActionForm returned", form);
+ assertTrue("ActionForm of correct type",
+ form instanceof MockFormBean);
+
+ }
+
+
+ // Default subapp -- Dynamic ActionForm should be created
+ public void testCreateActionForm3a() {
+
+ request.setPathElements("/myapp", "/dynamic.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig.findActionConfig("/dynamic");
+ assertNotNull("Found /dynamic mapping", mapping);
+ assertNotNull("Mapping has non-null name",
+ mapping.getName());
+ assertEquals("Mapping has correct name",
+ "dynamic",
+ mapping.getName());
+ assertNotNull("AppConfig has form bean " + mapping.getName(),
+ appConfig.findFormBeanConfig(mapping.getName()));
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig, null);
+ assertNotNull("ActionForm returned", form);
+ assertTrue("ActionForm of correct type",
+ form instanceof DynaActionForm);
+
+ }
+
+
+ // Second subapp -- Dynamic ActionForm should be created
+ public void testCreateActionForm3b() {
+
+ request.setPathElements("/myapp", "/2/dynamic2.do", null, null);
+ ActionMapping mapping = (ActionMapping)
+ appConfig2.findActionConfig("/dynamic2");
+ assertNotNull("Found /dynamic2 mapping", mapping);
+ assertNotNull("Mapping has non-null name",
+ mapping.getName());
+ assertEquals("Mapping has correct name",
+ "dynamic2",
+ mapping.getName());
+ assertNotNull("AppConfig has form bean " + mapping.getName(),
+ appConfig2.findFormBeanConfig(mapping.getName()));
+ ActionForm form = RequestUtils.createActionForm
+ (request, mapping, appConfig2, null);
+ assertNotNull("ActionForm returned", form);
+ assertTrue("ActionForm of correct type",
+ form instanceof DynaActionForm);
+
+ }
+
+
+ // ----------------------------------------------------------- requestURL()
+
+
+ public void testRequestURL() {
+
+ request.setPathElements("/myapp", "/foo.do", null, null);
+ String url = null;
+ try {
+ url = RequestUtils.requestURL(request).toString();
+ } catch (MalformedURLException e) {
+ fail("MalformedURLException: " + e);
+ }
+ assertNotNull("URL was returned", url);
+ assertEquals("URL value",
+ "http://localhost:8080/myapp/foo.do",
+ url);
+
+ }
+
+
+ // ---------------------------------------------------- selectApplication()
+
+
+ // Map to the default subapp -- direct
+ public void testSelectApplication1a() {
+
+ request.setPathElements("/myapp", "/noform.do", null, null);
+ RequestUtils.selectApplication(request, context);
+ ApplicationConfig appConfig = (ApplicationConfig)
+ request.getAttribute(Action.APPLICATION_KEY);
+ assertNotNull("Selected an application", appConfig);
+ assertEquals("Selected correct application",
+ "", appConfig.getPrefix());
+ // FIXME - check application resources?
+
+ }
+
+
+ // Map to the second webapp -- direct
+ public void testSelectApplication1b() {
+
+ request.setPathElements("/myapp", "/2/noform.do", null, null);
+ RequestUtils.selectApplication(request, context);
+ ApplicationConfig appConfig = (ApplicationConfig)
+ request.getAttribute(Action.APPLICATION_KEY);
+ assertNotNull("Selected an application", appConfig);
+ assertEquals("Selected correct application",
+ "/2", appConfig.getPrefix());
+ // FIXME - check application resources?
+
+ }
+
+
+ // Map to the default subapp -- include
+ public void testSelectApplication2a() {
+
+ request.setPathElements("/myapp", "/2/noform.do", null, null);
+ request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH,
+ "/noform.do");
+ RequestUtils.selectApplication(request, context);
+ ApplicationConfig appConfig = (ApplicationConfig)
+ request.getAttribute(Action.APPLICATION_KEY);
+ assertNotNull("Selected an application", appConfig);
+ assertEquals("Selected correct application",
+ "", appConfig.getPrefix());
+ // FIXME - check application resources?
+
+ }
+
+
+ // Map to the second subapp -- include
+ public void testSelectApplication2b() {
+
+ request.setPathElements("/myapp", "/noform.do", null, null);
+ request.setAttribute(RequestProcessor.INCLUDE_SERVLET_PATH,
+ "/2/noform.do");
+ RequestUtils.selectApplication(request, context);
+ ApplicationConfig appConfig = (ApplicationConfig)
+ request.getAttribute(Action.APPLICATION_KEY);
+ assertNotNull("Selected an application", appConfig);
+ assertEquals("Selected correct application",
+ "/2", appConfig.getPrefix());
+ // FIXME - check application resources?
+
+ }
+
+
// ------------------------------------------------------------ serverURL()
+ // Basic test on values in mock objects
public void testServerURL() {
String url = null;
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>