View Javadoc

1   /*
2    * $Id: TestRequestUtilsPopulate.java 421117 2006-07-12 04:35:47Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // All methods starting with "test" will be executed in the test suite.
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          // Set up the mock HttpServletRequest
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         // Check the Mapping/ActionForm before
102         assertNull("Multipart Handler already set",    mockForm.getMultipartRequestHandler());
103         assertEquals("Mapping name not set correctly", mockMappingName, mapping.getName());
104 
105         // Try to populate
106         try {
107             RequestUtils.populate(mockForm, request);
108         } catch(ServletException se) {
109             // Expected BeanUtils.populate() to throw a NestedNullException
110             // which gets wrapped in RequestUtils in a ServletException
111             assertEquals("Unexpected type of Exception thrown", "BeanUtils.populate", se.getMessage());
112         }
113 
114         // Check the Mapping/ActionForm after
115         assertNotNull("Multipart Handler Missing", mockForm.getMultipartRequestHandler());
116         assertEquals("Mapping name has been modified", mockMappingName, mapping.getName());
117 
118     }
119 
120 }