1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.struts.util;
20
21 import javax.servlet.ServletException;
22
23 import junit.framework.Test;
24 import junit.framework.TestSuite;
25
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.util.RequestUtils;
28 import org.apache.struts.Globals;
29 import org.apache.struts.mock.TestMockBase;
30 import org.apache.struts.mock.MockFormBean;
31 import org.apache.struts.mock.MockMultipartRequestHandler;
32
33 /***
34 * Unit tests for the RequestUtil's <code>populate</code> method.
35 *
36 * @version $Rev: 421117 $
37 */
38 public class TestRequestUtilsPopulate extends TestMockBase {
39
40 /***
41 * Defines the testcase name for JUnit.
42 *
43 * @param theName the testcase's name.
44 */
45 public TestRequestUtilsPopulate(String theName) {
46 super(theName);
47 }
48
49 /***
50 * Start the tests.
51 *
52 * @param theArgs the arguments. Not used
53 */
54 public static void main(String[] theArgs) {
55 junit.awtui.TestRunner.main(
56 new String[] { TestRequestUtilsPopulate.class.getName()});
57 }
58
59 /***
60 * @return a test suite (<code>TestSuite</code>) that includes all methods
61 * starting with "test"
62 */
63 public static Test suite() {
64
65 return new TestSuite(TestRequestUtilsPopulate.class);
66 }
67
68 public void setUp() {
69 super.setUp();
70 }
71
72 public void tearDown() {
73 super.tearDown();
74 }
75
76 /***
77 * Ensure that the getMultipartRequestHandler cannot be seen in
78 * a subclass of ActionForm.
79 *
80 * The purpose of this test is to ensure that Bug #38534 is fixed.
81 *
82 */
83 public void testMultipartVisibility() throws Exception {
84
85 String mockMappingName = "mockMapping";
86 String stringValue = "Test";
87
88 MockFormBean mockForm = new MockFormBean();
89 ActionMapping mapping = new ActionMapping();
90 mapping.setName(mockMappingName);
91
92
93 request.setMethod("POST");
94 request.setContentType("multipart/form-data");
95 request.setAttribute(Globals.MULTIPART_KEY, MockMultipartRequestHandler.class.getName());
96 request.setAttribute(Globals.MAPPING_KEY, mapping);
97
98 request.addParameter("stringProperty", stringValue);
99 request.addParameter("multipartRequestHandler.mapping.name", "Bad");
100
101
102 assertNull("Multipart Handler already set", mockForm.getMultipartRequestHandler());
103 assertEquals("Mapping name not set correctly", mockMappingName, mapping.getName());
104
105
106 try {
107 RequestUtils.populate(mockForm, request);
108 } catch(ServletException se) {
109
110
111 assertEquals("Unexpected type of Exception thrown", "BeanUtils.populate", se.getMessage());
112 }
113
114
115 assertNotNull("Multipart Handler Missing", mockForm.getMultipartRequestHandler());
116 assertEquals("Mapping name has been modified", mockMappingName, mapping.getName());
117
118 }
119
120 }