Search in sources :

Example 6 with ActionForm

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

the class ActionContextBase method findOrCreateActionForm.

/**
     * <p> In the context of the given <code>ModuleConfig</code> and this
     * <code>ActionContext</code>, look for an existing
     * <code>ActionForm</code> in the specified scope. If one is found, return
     * it; otherwise, create a new instance, add it to that scope, and then
     * return it. </p>
     *
     * @param formName  The name attribute of our ActionForm
     * @param scopeName The scope identier (request, session)
     * @return The ActionForm for this request
     * @throws IllegalAccessException   If object cannot be created
     * @throws InstantiationException   If object cannot be created
     * @throws IllegalArgumentException If form config is missing from module
     *                                  or scopeName is invalid
     */
public ActionForm findOrCreateActionForm(String formName, String scopeName, ModuleConfig moduleConfig) throws IllegalAccessException, InstantiationException {
    Map scope = this.getScope(scopeName);
    ActionForm instance;
    FormBeanConfig formBeanConfig = moduleConfig.findFormBeanConfig(formName);
    if (formBeanConfig == null) {
        throw new IllegalArgumentException("No form config found under " + formName + " in module " + moduleConfig.getPrefix());
    }
    instance = (ActionForm) scope.get(formName);
    // ISSUE: Can we recycle the existing instance (if any)?
    if (instance != null) {
        getLogger().trace("Found an instance in scope " + scopeName + "; test for reusability");
        if (formBeanConfig.canReuse(instance)) {
            return instance;
        }
    }
    ActionForm form = formBeanConfig.createActionForm(this);
    // ISSUE: Should we check this call to put?
    scope.put(formName, form);
    return form;
}
Also used : FormBeanConfig(org.apache.struts.config.FormBeanConfig) ActionForm(org.apache.struts.action.ActionForm) Map(java.util.Map)

Example 7 with ActionForm

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

the class TestRequestUtils method testCreateActionForm1a.

// ----------------------------------------------------- createActionForm()
// Default module -- No ActionForm should be created
public void testCreateActionForm1a() {
    request.setPathElements("/myapp", "/noform.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/noform");
    assertNotNull("Found /noform mapping", mapping);
    ActionForm form = RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    assertNull("No ActionForm returned", form);
}
Also used : ActionMapping(org.apache.struts.action.ActionMapping) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm)

Example 8 with ActionForm

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

the class TestRequestUtils method testCreateActionForm4a.

// Default module -- Dynamic ActionForm with initializers
public void testCreateActionForm4a() {
    // Retrieve an appropriately configured DynaActionForm instance
    request.setPathElements("/myapp", "/dynamic0.do", null, null);
    ActionMapping mapping = (ActionMapping) moduleConfig.findActionConfig("/dynamic0");
    assertNotNull("Found /dynamic0 mapping", mapping);
    assertNotNull("Mapping has non-null name", mapping.getName());
    assertEquals("Mapping has correct name", "dynamic0", 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);
    // Validate the property values
    DynaActionForm dform = (DynaActionForm) form;
    Boolean booleanProperty = (Boolean) dform.get("booleanProperty");
    assertTrue("booleanProperty is true", booleanProperty.booleanValue());
    String stringProperty = (String) dform.get("stringProperty");
    assertEquals("stringProperty is correct", "String Property", stringProperty);
    Object value = null;
    value = dform.get("intArray1");
    assertNotNull("intArray1 exists", value);
    assertTrue("intArray1 is int[]", value instanceof int[]);
    int[] intArray1 = (int[]) value;
    assertEquals("intArray1 length is correct", 3, intArray1.length);
    assertEquals("intArray1[0] value is correct", 1, intArray1[0]);
    assertEquals("intArray1[1] value is correct", 2, intArray1[1]);
    assertEquals("intArray1[2] value is correct", 3, intArray1[2]);
    value = dform.get("intArray2");
    assertNotNull("intArray2 exists", value);
    assertTrue("intArray2 is int[]", value instanceof int[]);
    int[] intArray2 = (int[]) value;
    assertEquals("intArray2 length is correct", 5, intArray2.length);
    assertEquals("intArray2[0] value is correct", 0, intArray2[0]);
    assertEquals("intArray2[1] value is correct", 0, intArray2[1]);
    assertEquals("intArray2[2] value is correct", 0, intArray2[2]);
    assertEquals("intArray2[3] value is correct", 0, intArray2[3]);
    assertEquals("intArray2[4] value is correct", 0, intArray2[4]);
    value = dform.get("principal");
    assertNotNull("principal exists", value);
    assertTrue("principal is MockPrincipal", value instanceof MockPrincipal);
    value = dform.get("stringArray1");
    assertNotNull("stringArray1 exists", value);
    assertTrue("stringArray1 is int[]", value instanceof String[]);
    String[] stringArray1 = (String[]) value;
    assertEquals("stringArray1 length is correct", 3, stringArray1.length);
    assertEquals("stringArray1[0] value is correct", "aaa", stringArray1[0]);
    assertEquals("stringArray1[1] value is correct", "bbb", stringArray1[1]);
    assertEquals("stringArray1[2] value is correct", "ccc", stringArray1[2]);
    value = dform.get("stringArray2");
    assertNotNull("stringArray2 exists", value);
    assertTrue("stringArray2 is int[]", value instanceof String[]);
    String[] stringArray2 = (String[]) value;
    assertEquals("stringArray2 length is correct", 3, stringArray2.length);
    assertEquals("stringArray2[0] value is correct", new String(), stringArray2[0]);
    assertEquals("stringArray2[1] value is correct", new String(), stringArray2[1]);
    assertEquals("stringArray2[2] value is correct", new String(), stringArray2[2]);
    // Different form beans should get different property value instances
    Object value1 = null;
    DynaActionForm dform1 = (DynaActionForm) RequestUtils.createActionForm(request, mapping, moduleConfig, null);
    value = dform.get("principal");
    value1 = dform1.get("principal");
    assertEquals("Different form beans get equal instance values", value, value1);
    assertTrue("Different form beans get different instances 1", value != value1);
    value = dform.get("stringArray1");
    value1 = dform1.get("stringArray1");
    assertTrue("Different form beans get different instances 2", value != value1);
    dform1.set("stringProperty", "Different stringProperty value");
    value = dform.get("stringProperty");
    value1 = dform1.get("stringProperty");
    assertTrue("Different form beans get different instances 3", value != value1);
}
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) MockPrincipal(org.apache.struts.mock.MockPrincipal)

Example 9 with ActionForm

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

the class TestCopyFormToContext method testLookupByNameAndSessionScope.

public void testLookupByNameAndSessionScope() throws Exception {
    CopyFormToContext command = new CopyFormToContext();
    String formName = "bar";
    command.setFormName(formName);
    command.setScope("session");
    command.setToKey(POST_EXECUTION_CONTEXT_KEY);
    assertNull(context.get(POST_EXECUTION_CONTEXT_KEY));
    assertNull(context.getRequestScope().get(formName));
    assertNull(context.getSessionScope().get(formName));
    command.execute(context);
    assertNotNull(context.get(POST_EXECUTION_CONTEXT_KEY));
    assertNull(context.getRequestScope().get(formName));
    assertNotNull(context.getSessionScope().get(formName));
    assertSame(context.get(POST_EXECUTION_CONTEXT_KEY), context.getSessionScope().get(formName));
    ActionForm theForm = (ActionForm) context.get(POST_EXECUTION_CONTEXT_KEY);
    assertTrue(theForm instanceof DynaActionForm);
    DynaActionForm dForm = (DynaActionForm) theForm;
    assertEquals("test", dForm.get("property"));
}
Also used : DynaActionForm(org.apache.struts.action.DynaActionForm) ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm)

Example 10 with ActionForm

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

the class TestCopyFormToContext method testCopyToDefaultContextKey.

public void testCopyToDefaultContextKey() throws Exception {
    CopyFormToContext command = new CopyFormToContext();
    String formName = "foo";
    command.setFormName(formName);
    command.setScope("request");
    assertNull(context.getActionForm());
    assertNull(context.getRequestScope().get(POST_EXECUTION_CONTEXT_KEY));
    assertNull(context.getSessionScope().get(POST_EXECUTION_CONTEXT_KEY));
    command.execute(context);
    assertNotNull(context.getActionForm());
    assertNotNull(context.getRequestScope().get(formName));
    assertNull(context.getSessionScope().get(formName));
    assertSame(context.getActionForm(), context.getRequestScope().get(formName));
    ActionForm theForm = (ActionForm) context.getActionForm();
    assertTrue(theForm instanceof MockFormBean);
}
Also used : ActionForm(org.apache.struts.action.ActionForm) DynaActionForm(org.apache.struts.action.DynaActionForm) MockFormBean(org.apache.struts.mock.MockFormBean)

Aggregations

ActionForm (org.apache.struts.action.ActionForm)25 DynaActionForm (org.apache.struts.action.DynaActionForm)12 ActionMapping (org.apache.struts.action.ActionMapping)9 MockFormBean (org.apache.struts.mock.MockFormBean)5 ActionConfig (org.apache.struts.config.ActionConfig)4 Map (java.util.Map)3 FormBeanConfig (org.apache.struts.config.FormBeanConfig)3 ServletActionContext (org.apache.struts.chain.contexts.ServletActionContext)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 DynaBean (org.apache.commons.beanutils.DynaBean)1 MutableDynaClass (org.apache.commons.beanutils.MutableDynaClass)1 Action (org.apache.struts.action.Action)1 ActionErrors (org.apache.struts.action.ActionErrors)1