Example #1
0
  /**
   * Clones the ActionConfig and its children, replacing various properties with the values of the
   * wildcard-matched strings.
   *
   * @param path The requested path
   * @param orig The original ActionConfig
   * @param vars A Map of wildcard-matched strings
   * @return A cloned ActionConfig with appropriate properties replaced with wildcard-matched values
   */
  @Override
  public ActionConfig convert(String path, ActionConfig orig, Map<String, String> vars) {

    String methodName = convertParam(orig.getMethodName(), vars);

    if (StringUtils.isEmpty(methodName)) {
      methodName = ActionConfig.DEFAULT_METHOD;
    }

    if (!orig.isAllowedMethod(methodName)) {
      return null;
    }

    String className = convertParam(orig.getClassName(), vars);
    String pkgName = convertParam(orig.getPackageName(), vars);

    Map<String, String> params = replaceParameters(orig.getParams(), vars);

    Map<String, ResultConfig> results = new LinkedHashMap<>();
    for (String name : orig.getResults().keySet()) {
      ResultConfig result = orig.getResults().get(name);
      name = convertParam(name, vars);
      ResultConfig r =
          new ResultConfig.Builder(name, convertParam(result.getClassName(), vars))
              .addParams(replaceParameters(result.getParams(), vars))
              .build();
      results.put(name, r);
    }

    List<ExceptionMappingConfig> exs = new ArrayList<ExceptionMappingConfig>();
    for (ExceptionMappingConfig ex : orig.getExceptionMappings()) {
      String name = convertParam(ex.getName(), vars);
      String exClassName = convertParam(ex.getExceptionClassName(), vars);
      String exResult = convertParam(ex.getResult(), vars);
      Map<String, String> exParams = replaceParameters(ex.getParams(), vars);
      ExceptionMappingConfig e =
          new ExceptionMappingConfig.Builder(name, exClassName, exResult)
              .addParams(exParams)
              .build();
      exs.add(e);
    }

    return new ActionConfig.Builder(pkgName, orig.getName(), className)
        .methodName(methodName)
        .addParams(params)
        .addResultConfigs(results)
        .addInterceptors(orig.getInterceptors())
        .addExceptionMappings(exs)
        .location(orig.getLocation())
        .build();
  }
  public void testGlobalResultInheritenceTest() throws Exception {
    ConfigurationProvider provider =
        buildConfigurationProvider(
            "com/opensymphony/xwork2/config/providers/xwork-test-global-result-inheritence.xml");

    ConfigurationManager configurationManager = new ConfigurationManager();
    configurationManager.addContainerProvider(new XWorkConfigurationProvider());
    configurationManager.addContainerProvider(provider);
    Configuration configuration = configurationManager.getConfiguration();

    ActionConfig parentActionConfig =
        configuration.getRuntimeConfiguration().getActionConfig("/base", "parentAction");
    ActionConfig anotherActionConfig =
        configuration.getRuntimeConfiguration().getActionConfig("/base", "anotherAction");
    ActionConfig childActionConfig =
        configuration.getRuntimeConfiguration().getActionConfig("/base", "childAction");

    ResultConfig parentResultConfig1 = parentActionConfig.getResults().get("mockResult1");
    ResultConfig parentResultConfig2 = parentActionConfig.getResults().get("mockResult2");
    ResultConfig anotherResultConfig1 = anotherActionConfig.getResults().get("mockResult1");
    ResultConfig anotherResultConfig2 = anotherActionConfig.getResults().get("mockResult2");
    ResultConfig childResultConfig1 = childActionConfig.getResults().get("mockResult1");
    ResultConfig childResultConfig2 = childActionConfig.getResults().get("mockResult2");

    System.out.println(parentResultConfig1.getParams().get("identity"));
    System.out.println(parentResultConfig2.getParams().get("identity"));
    System.out.println(anotherResultConfig1.getParams().get("identity"));
    System.out.println(anotherResultConfig2.getParams().get("identity"));
    System.out.println(childResultConfig1.getParams().get("identity"));
    System.out.println(childResultConfig2.getParams().get("identity"));

    assertFalse(parentResultConfig1 == anotherResultConfig1);
    assertFalse(parentResultConfig2 == anotherResultConfig2);

    assertFalse(parentResultConfig1 == childResultConfig1);
    assertTrue(parentResultConfig2 == childResultConfig2);
  }
  /**
   * Tests creating action flow override configuration.
   *
   * @throws Exception when something goes wrong.
   */
  @Test
  public void testCorrectFlowOverride() throws Exception {
    ActionProxy proxy = getActionProxy("/correctFlowOverride/correctFlowOverride");
    Assert.assertNotNull(proxy);
    proxy.getInvocation().getInvocationContext().setSession(new HashMap<String, Object>());
    proxy.execute();

    ActionConfig overriddenViewConf =
        configuration
            .getRuntimeConfiguration()
            .getActionConfig("/correctFlowOverride", "savePhone-2ViewOverride");

    Assert.assertNotNull(overriddenViewConf);
    Assert.assertEquals("phone", overriddenViewConf.getMethodName());
    Assert.assertEquals(
        "anotherPhone",
        overriddenViewConf
            .getResults()
            .get(Action.SUCCESS)
            .getParams()
            .get(ServletDispatcherResult.DEFAULT_PARAM));

    ActionConfig actionConfig =
        configuration
            .getRuntimeConfiguration()
            .getActionConfig("/correctFlowOverride", "saveName-1ViewOverride");

    Assert.assertNotNull(actionConfig);
    Assert.assertEquals("executeOverride", actionConfig.getMethodName());
    Assert.assertEquals(
        "name",
        actionConfig
            .getResults()
            .get(Action.SUCCESS)
            .getParams()
            .get(ServletDispatcherResult.DEFAULT_PARAM));
  }