001: /*
002: * $Id: TestRequestUtilsPopulate.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.util;
023:
024: import javax.servlet.ServletException;
025:
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: import org.apache.struts.action.ActionMapping;
030: import org.apache.struts.util.RequestUtils;
031: import org.apache.struts.Globals;
032: import org.apache.struts.mock.TestMockBase;
033: import org.apache.struts.mock.MockFormBean;
034: import org.apache.struts.mock.MockMultipartRequestHandler;
035:
036: /**
037: * Unit tests for the RequestUtil's <code>populate</code> method.
038: *
039: * @version $Rev: 471754 $
040: */
041: public class TestRequestUtilsPopulate extends TestMockBase {
042:
043: /**
044: * Defines the testcase name for JUnit.
045: *
046: * @param theName the testcase's name.
047: */
048: public TestRequestUtilsPopulate(String theName) {
049: super (theName);
050: }
051:
052: /**
053: * Start the tests.
054: *
055: * @param theArgs the arguments. Not used
056: */
057: public static void main(String[] theArgs) {
058: junit.awtui.TestRunner
059: .main(new String[] { TestRequestUtilsPopulate.class
060: .getName() });
061: }
062:
063: /**
064: * @return a test suite (<code>TestSuite</code>) that includes all methods
065: * starting with "test"
066: */
067: public static Test suite() {
068: // All methods starting with "test" will be executed in the test suite.
069: return new TestSuite(TestRequestUtilsPopulate.class);
070: }
071:
072: public void setUp() {
073: super .setUp();
074: }
075:
076: public void tearDown() {
077: super .tearDown();
078: }
079:
080: /**
081: * Ensure that the getMultipartRequestHandler cannot be seen in
082: * a subclass of ActionForm.
083: *
084: * The purpose of this test is to ensure that Bug #38534 is fixed.
085: *
086: */
087: public void testMultipartVisibility() throws Exception {
088:
089: String mockMappingName = "mockMapping";
090: String stringValue = "Test";
091:
092: MockFormBean mockForm = new MockFormBean();
093: ActionMapping mapping = new ActionMapping();
094: mapping.setName(mockMappingName);
095:
096: // Set up the mock HttpServletRequest
097: request.setMethod("POST");
098: request.setContentType("multipart/form-data");
099: request.setAttribute(Globals.MULTIPART_KEY,
100: MockMultipartRequestHandler.class.getName());
101: request.setAttribute(Globals.MAPPING_KEY, mapping);
102:
103: request.addParameter("stringProperty", stringValue);
104: request.addParameter("multipartRequestHandler.mapping.name",
105: "Bad");
106:
107: // Check the Mapping/ActionForm before
108: assertNull("Multipart Handler already set", mockForm
109: .getMultipartRequestHandler());
110: assertEquals("Mapping name not set correctly", mockMappingName,
111: mapping.getName());
112:
113: // Try to populate
114: try {
115: RequestUtils.populate(mockForm, request);
116: } catch (ServletException se) {
117: // Expected BeanUtils.populate() to throw a NestedNullException
118: // which gets wrapped in RequestUtils in a ServletException
119: assertEquals("Unexpected type of Exception thrown",
120: "BeanUtils.populate", se.getMessage());
121: }
122:
123: // Check the Mapping/ActionForm after
124: assertNotNull("Multipart Handler Missing", mockForm
125: .getMultipartRequestHandler());
126: assertEquals("Mapping name has been modified", mockMappingName,
127: mapping.getName());
128:
129: }
130:
131: }
|