📄 testactionservlet.java
字号:
/*
* $Id: TestActionServlet.java 471754 2006-11-06 14:55:09Z husted $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.struts.action;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.FormPropertyConfig;
import org.apache.struts.config.ForwardConfig;
import org.apache.struts.config.ModuleConfig;
import org.apache.struts.config.ModuleConfigFactory;
import org.apache.struts.util.MessageResources;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import java.util.List;
/**
* Suite of unit tests for the <code>org.apache.struts.action.ActionServlet</code>
* class.
*/
public class TestActionServlet extends TestCase {
// ----------------------------------------------------- Instance Variables
/**
* The ModuleConfig we'll use.
*/
protected ModuleConfig moduleConfig = null;
/**
* The common form bean we'll use.
*/
protected FormBeanConfig baseFormBean = null;
/**
* The common exception config we'll use.
*/
protected ExceptionConfig baseException = null;
/**
* The common action config we'll use.
*/
protected ActionMapping baseAction = null;
/**
* The common action forward we'll use.
*/
protected ActionForward baseForward = null;
/**
* The ActionServlet we'll test.
*/
protected ActionServlet actionServlet = null;
// ------------------------------------------ Constructors, suite, and main
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestActionServlet(String theName) {
super(theName);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs) {
junit.awtui.TestRunner.main(new String[] {
TestActionServlet.class.getName()
});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite() {
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestActionServlet.class);
}
// ------------------------------------------------- setUp() and tearDown()
/**
* Set up instance variables required by this test case.
*/
public void setUp() throws Exception {
actionServlet = new ActionServlet();
actionServlet.initInternal();
ModuleConfigFactory factoryObject = ModuleConfigFactory.createFactory();
moduleConfig = factoryObject.createModuleConfig("");
// Setup the base form
baseFormBean = new FormBeanConfig();
baseFormBean.setName("baseForm");
baseFormBean.setType("org.apache.struts.action.DynaActionForm");
// Set up id, name, and score
FormPropertyConfig property = new FormPropertyConfig();
property.setName("id");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("name");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
property = new FormPropertyConfig();
property.setName("score");
property.setType("java.lang.String");
baseFormBean.addFormPropertyConfig(property);
// Setup the exception handler
baseException = new ExceptionConfig();
baseException.setType("java.lang.NullPointerException");
baseException.setKey("msg.exception.npe");
// Setup the forward config
baseForward = new ActionForward("success", "/succes.jsp", false);
// Setup the action config
baseAction = new ActionMapping();
baseAction.setPath("/index");
baseAction.setType("org.apache.struts.actions.DummyAction");
baseAction.setName("someForm");
baseAction.setInput("/input.jsp");
baseAction.addForwardConfig(new ActionForward("next", "/next.jsp", false));
baseAction.addForwardConfig(new ActionForward("prev", "/prev.jsp", false));
ExceptionConfig exceptionConfig = new ExceptionConfig();
exceptionConfig.setType("java.sql.SQLException");
exceptionConfig.setKey("msg.exception.sql");
baseAction.addExceptionConfig(exceptionConfig);
// Nothing is registered to our module config until they are needed
}
/**
* Tear down instance variables required by this test case.
*/
public void tearDown() {
moduleConfig = null;
}
// ----------------------------- initInternal() and destroyInternal() tests
/**
* Verify that we can initialize and destroy our internal message
* resources object.
*/
public void testInitDestroyInternal() {
ActionServlet servlet = new ActionServlet();
try {
servlet.initInternal();
} catch (ServletException e) {
fail("initInternal() threw exception: " + e);
}
assertTrue("internal was initialized", servlet.getInternal() != null);
assertTrue("internal of correct type",
servlet.getInternal() instanceof MessageResources);
servlet.destroyInternal();
assertTrue("internal was destroyed", servlet.getInternal() == null);
}
/**
* Test class loader resolution and splitting.
*/
public void notestSplitAndResolvePaths()
throws Exception {
ActionServlet servlet = new ActionServlet();
List list =
servlet.splitAndResolvePaths(
"org/apache/struts/config/struts-config.xml");
assertNotNull(list);
assertTrue("List size should be 1", list.size() == 1);
list =
servlet.splitAndResolvePaths(
"org/apache/struts/config/struts-config.xml, "
+ "org/apache/struts/config/struts-config-1.1.xml");
assertNotNull(list);
assertTrue("List size should be 2, was " + list.size(), list.size() == 2);
list = servlet.splitAndResolvePaths("META-INF/MANIFEST.MF");
assertNotNull(list);
assertTrue("Number of manifests should be more than 5, was "
+ list.size(), list.size() > 5);
// test invalid path
try {
list =
servlet.splitAndResolvePaths(
"org/apache/struts/config/struts-asdfasdfconfig.xml");
fail("Should have thrown an exception on bad path");
} catch (NullPointerException ex) {
// correct behavior since internal error resources aren't loaded
}
}
//----- Test initApplication() method --------------------------------------
/**
* Verify that nothing happens if no "application" property is defined in
* the servlet configuration.
*/
/*
public void testInitApplicationNull() throws ServletException
{
ActionServlet servlet = new ActionServlet();
servlet.init(config);
// Test the initApplication() method
servlet.initApplication();
// As no "application" object is found in the servlet config, no
// attribute should be set in the context
assertTrue(config.getServletContext().getAttribute(Action.MESSAGES_KEY) == null);
}
*/
/**
* Verify that eveything is fine when only a "application" parameter is
* defined in the servlet configuration.
*/
/*
public void testInitApplicationOk1() throws ServletException
{
// initialize config
config.setInitParameter("application", "org.apache.struts.webapp.example.ApplicationResources");
ActionServlet servlet = new ActionServlet();
servlet.init(config);
// Test the initApplication() method
servlet.initApplication();
assertTrue(servlet.application != null);
assertTrue(servlet.application.getReturnNull() == true);
assertTrue(config.getServletContext().getAttribute(Action.MESSAGES_KEY) != null);
assertEquals(servlet.application, config.getServletContext().getAttribute(Action.MESSAGES_KEY));
}
*/
// --------------------------------------------------- FormBeanConfig Tests
/**
* Test that nothing fails if there are no extensions.
*/
public void testInitModuleFormBeansNoExtends()
throws ServletException {
moduleConfig.addFormBeanConfig(baseFormBean);
try {
actionServlet.initModuleExceptionConfigs(moduleConfig);
} catch (Exception e) {
fail("Unexpected exception caught.");
}
}
/**
* Test that initModuleFormBeans throws an exception when a form with a
* null type is present.
*/
public void testInitModuleFormBeansNullFormType()
throws ServletException {
FormBeanConfig formBean = new FormBeanConfig();
formBean.setName("noTypeForm");
moduleConfig.addFormBeanConfig(formBean);
try {
actionServlet.initModuleFormBeans(moduleConfig);
fail("An exception should've been thrown here.");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unrecognized exception thrown: " + e);
}
}
/**
* Test that initModuleFormBeans throws an exception when a form whose
* prop type is null is present.
*/
public void testInitModuleFormBeansNullPropType()
throws ServletException {
moduleConfig.addFormBeanConfig(baseFormBean);
baseFormBean.findFormPropertyConfig("name").setType(null);
try {
actionServlet.initModuleFormBeans(moduleConfig);
fail("An exception should've been thrown here.");
} catch (UnavailableException e) {
// success
} catch (Exception e) {
fail("Unrecognized exception thrown: " + e);
}
}
/**
* Test that processFormBeanExtension() calls processExtends()
*/
public void testProcessFormBeanExtension()
throws ServletException {
CustomFormBeanConfig form = new CustomFormBeanConfig();
actionServlet.processFormBeanExtension(form, moduleConfig);
assertTrue("processExtends() was not called", form.processExtendsCalled);
}
/**
* Make sure processFormBeanConfigClass() returns an instance of the
* correct class if the base config is using a custom class.
*/
public void testProcessFormBeanConfigClass()
throws Exception {
CustomFormBeanConfig customBase = new CustomFormBeanConfig();
customBase.setName("customBase");
moduleConfig.addFormBeanConfig(customBase);
FormBeanConfig customSub = new FormBeanConfig();
customSub.setName("customSub");
customSub.setExtends("customBase");
customSub.setType("org.apache.struts.action.DynaActionForm");
moduleConfig.addFormBeanConfig(customSub);
FormBeanConfig result =
actionServlet.processFormBeanConfigClass(customSub, moduleConfig);
assertTrue("Incorrect class of form bean config",
result instanceof CustomFormBeanConfig);
assertEquals("Incorrect name", customSub.getName(), result.getName());
assertEquals("Incorrect type", customSub.getType(), result.getType());
assertEquals("Incorrect extends", customSub.getExtends(),
result.getExtends());
assertEquals("Incorrect 'restricted' value", customSub.isRestricted(),
result.isRestricted());
assertSame("Result was not registered in the module config", result,
moduleConfig.findFormBeanConfig("customSub"));
}
/**
* Make sure processFormBeanConfigClass() returns what it was given if the
* form passed to it doesn't extend anything.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -