Search in sources :

Example 21 with ActionMapping

use of org.apache.struts.action.ActionMapping in project sonarqube by SonarSource.

the class TestRequestUtils method testCreateActionForm3a.

// Default module -- Dynamic ActionForm should be created
public void testCreateActionForm3a() {
    request.setPathElements("/myapp", "/dynamic.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/dynamic");
    assertNotNull("Found /dynamic mapping", mapping);
    assertNotNull("Mapping has non-null name", mapping.getName());
    assertEquals("Mapping has correct name", "dynamic", mapping.getName());
    assertNotNull("AppConfig has form bean " + mapping.getName(), moduleConfig.findFormBeanConfig(mapping.getName()));
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNotNull("ActionForm returned", form);
    assertTrue("ActionForm of correct type", form instanceof DynaActionForm);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) DynaActionForm(org.apache.struts.action.DynaActionForm) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm)

Example 22 with ActionMapping

use of org.apache.struts.action.ActionMapping in project sonarqube by SonarSource.

the class TestRequestUtils method testCreateActionForm2a.

// Default module -- Standard ActionForm should be created
public void testCreateActionForm2a() {
    request.setPathElements("/myapp", "/static.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/static");
    assertNotNull("Found /static mapping", mapping);
    assertNotNull("Mapping has non-null name", mapping.getName());
    assertEquals("Mapping has correct name", "static", mapping.getName());
    assertNotNull("AppConfig has form bean " + mapping.getName(), moduleConfig.findFormBeanConfig(mapping.getName()));
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNotNull("ActionForm returned", form);
    assertTrue("ActionForm of correct type", form instanceof MockFormBean);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) MockFormBean(org.apache.struts.mock.MockFormBean)

Example 23 with ActionMapping

use of org.apache.struts.action.ActionMapping in project sonar-java by SonarSource.

the class TestRequestUtilsPopulate method testMultipartVisibility.

/**
 * 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() throws Exception {
    String mockMappingName = "mockMapping";
    String stringValue = "Test";
    MockFormBean mockForm = new MockFormBean();
    ActionMapping mapping = new ActionMapping();
    mapping.setName(mockMappingName);
    // Set up the mock HttpServletRequest
    request.setMethod("POST");
    request.setContentType("multipart/form-data");
    request.setAttribute(Globals.MULTIPART_KEY, MockMultipartRequestHandler.class.getName());
    request.setAttribute(Globals.MAPPING_KEY, mapping);
    request.addParameter("stringProperty", stringValue);
    request.addParameter("multipartRequestHandler.mapping.name", "Bad");
    // Check the Mapping/ActionForm before
    assertNull("Multipart Handler already set", mockForm.getMultipartRequestHandler());
    assertEquals("Mapping name not set correctly", mockMappingName, mapping.getName());
    // Try to populate
    try {
        RequestUtils.populate(mockForm, request);
    } catch (ServletException se) {
        // Expected BeanUtils.populate() to throw a NestedNullException
        // which gets wrapped in RequestUtils in a ServletException
        assertEquals("Unexpected type of Exception thrown", "BeanUtils.populate", se.getMessage());
    }
    // Check the Mapping/ActionForm after
    assertNotNull("Multipart Handler Missing", mockForm.getMultipartRequestHandler());
    assertEquals("Mapping name has been modified", mockMappingName, mapping.getName());
}
Also used : ServletException(javax.servlet.ServletException) ActionMapping(org.apache.struts.action.ActionMapping) MockMultipartRequestHandler(org.apache.struts.mock.MockMultipartRequestHandler) MockFormBean(org.apache.struts.mock.MockFormBean)

Example 24 with ActionMapping

use of org.apache.struts.action.ActionMapping in project sonar-java by SonarSource.

the class JavascriptValidatorTag method createDynamicJavascript.

/**
 * Generates the dynamic JavaScript for the form.
 *
 * @param config
 * @param resources
 * @param locale
 * @param form
 */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale, Form form) throws JspException {
    StringBuffer results = new StringBuffer();
    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();
    List actions = this.createActionList(resources, form);
    final String methods = this.createMethods(actions, this.stopOnError(config));
    String formName = form.getName();
    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);
        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));
            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }
        jsFormName = mapping.getAttribute();
    }
    results.append(this.getJavascriptBegin(methods));
    for (Iterator i = actions.iterator(); i.hasNext(); ) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;
        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }
        results.append("    function " + jsFormName + "_" + functionName + " () { \n");
        for (Iterator x = form.getFields().iterator(); x.hasNext(); ) {
            Field field = (Field) x.next();
            // retrieve from scope?))
            if (field.isIndexed() || (field.getPage() != page) || !field.isDependency(va.getName())) {
                continue;
            }
            String message = Resources.getMessage(application, request, messages, locale, va, field);
            message = (message != null) ? message : "";
            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \"" + escapeQuotes(message) + "\", ");
            results.append("new Function (\"varName\", \"");
            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }
                String varValueEscaped = escapeJavascript(varValue);
                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                // So everyone using the latest format doesn't need to
                // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 25 with ActionMapping

use of org.apache.struts.action.ActionMapping in project sonar-java by SonarSource.

the class TestRequestUtils method testCreateActionForm2a.

// Default module -- Standard ActionForm should be created
public void testCreateActionForm2a() {
    request.setPathElements("/myapp", "/static.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/static");
    assertNotNull("Found /static mapping", mapping);
    assertNotNull("Mapping has non-null name", mapping.getName());
    assertEquals("Mapping has correct name", "static", mapping.getName());
    assertNotNull("AppConfig has form bean " + mapping.getName(), moduleConfig.findFormBeanConfig(mapping.getName()));
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNotNull("ActionForm returned", form);
    assertTrue("ActionForm of correct type", form instanceof MockFormBean);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) MockFormBean(org.apache.struts.mock.MockFormBean)

Aggregations

ActionMapping (org.apache.struts.action.ActionMapping)32 ActionForm (org.apache.struts.action.ActionForm)18 DynaActionForm (org.apache.struts.action.DynaActionForm)14 ActionForward (org.apache.struts.action.ActionForward)6 MockFormBean (org.apache.struts.mock.MockFormBean)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 ActionFormBean (org.apache.struts.action.ActionFormBean)4 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)4 FormPropertyConfig (org.apache.struts.config.FormPropertyConfig)4 ForwardConfig (org.apache.struts.config.ForwardConfig)4 ModuleConfigFactory (org.apache.struts.config.ModuleConfigFactory)4 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 List (java.util.List)2 Map (java.util.Map)2 ServletContext (javax.servlet.ServletContext)2 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 JspException (javax.servlet.jsp.JspException)2 Field (org.apache.commons.validator.Field)2