/*
 * $Id: TestRequestUtilsPopulate.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.util;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.chain.contexts.ServletActionContext;
import org.apache.struts.mock.MockFormBean;
import org.apache.struts.mock.MockMultipartRequestHandler;
import org.apache.struts.mock.MockServletContext;
import org.apache.struts.mock.TestMockBase;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Unit tests for the RequestUtil's <code>populate</code> method.
 *
 * @version $Rev: 471754 $
 */
public class TestRequestUtilsPopulate extends TestMockBase {

    /**
     * Defines the testcase name for JUnit.
     *
     * @param theName the testcase's name.
     */
    public TestRequestUtilsPopulate(final String theName) {
        super(theName);
    }

    /**
     * Start the tests.
     *
     * @param theArgs the arguments. Not used
     */
    public static void main(final String[] theArgs) {
        junit.textui.TestRunner.main(
            new String[] {TestRequestUtilsPopulate.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(TestRequestUtilsPopulate.class);
    }

    /**
     * Ensure that the getMultipartRequestHandler cannot be seen in
     * a subclass of ActionForm.
     *
     * The purpose of this test is to ensure that Bug #38534 is fixed.
     */
    public void testMultipartVisibility() {

        String mockMappingName = "mockMapping";

        ActionMapping mapping = new ActionMapping();
        mapping.setName(mockMappingName);

        // Set up the mock HttpServletRequest
        this.request.setMethod("POST");
        this.request.setContentType("multipart/form-data");
        this.request.setAttribute(Globals.MULTIPART_KEY,
                MockMultipartRequestHandler.class.getName());
        this.request.setAttribute(Globals.MAPPING_KEY, mapping);

        String stringValue = "Test";
        this.request.addParameter("stringProperty", stringValue);
        this.request.addParameter("multipartRequestHandler.mapping.name", "Bad");

        MockFormBean mockForm = new MockFormBean();
        // Check the Mapping/ActionForm before
        assertNull("Multipart Handler already set", mockForm.getMultipartRequestHandler());
        assertEquals("Mapping name not set correctly", mockMappingName, mapping.getName());

        ServletActionContext ctx = new ServletActionContext(
                new MockServletContext(), this.request, null);

        // Try to populate
        try {
            RequestUtils.populate(mockForm, ctx);
        } catch (final RuntimeException e) {
            // Expected BeanUtils.populate() to throw a NestedNullException
            // which gets wrapped in RequestUtils in a ServletException
            assertEquals("Unexpected type of Exception thrown",
                    "BeanUtils.populate", e.getMessage());
        }

        // Check the Mapping/ActionForm after
        assertNotNull("Multipart Handler Missing", mockForm.getMultipartRequestHandler());
        assertEquals("Mapping name has been modified", mockMappingName, mapping.getName());

    }

}
