Search in sources :

Example 16 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class InterceptorBuilderTest method testBuildInterceptor_1.

/**
 * Try to test this
 * <interceptor-ref name="interceptorStack1">
 * <param name="interceptor1.param1">interceptor1_value1</param>
 * <param name="interceptor1.param2">interceptor1_value2</param>
 * <param name="interceptor2.param1">interceptor2_value1</param>
 * <param name="interceptor2.param2">interceptor2_value2</param>
 * </interceptor-ref>
 *
 * @throws Exception
 */
public void testBuildInterceptor_1() throws Exception {
    InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("interceptorStack1").build();
    InterceptorConfig interceptorConfig1 = new InterceptorConfig.Builder("interceptor1", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor1").build();
    InterceptorConfig interceptorConfig2 = new InterceptorConfig.Builder("interceptor2", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor2").build();
    PackageConfig packageConfig = new PackageConfig.Builder("package1").namespace("/namespace").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorStackConfig(interceptorStackConfig1).build();
    List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "interceptorStack1", new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = -1358620486812957895L;

        {
            put("interceptor1.param1", "interceptor1_value1");
            put("interceptor1.param2", "interceptor1_value2");
            put("interceptor2.param1", "interceptor2_value1");
            put("interceptor2.param2", "interceptor2_value2");
        }
    }, null, objectFactory);
    assertEquals(interceptorMappings.size(), 2);
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getName(), "interceptor1");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor().getClass(), MockInterceptor1.class);
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam1(), "interceptor1_value1");
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam2(), "interceptor1_value2");
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getName(), "interceptor2");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor().getClass(), MockInterceptor2.class);
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam1(), "interceptor2_value1");
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_value2");
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) List(java.util.List) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 17 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class InterceptorBuilderTest method testBuildInterceptor_3.

/**
 * Try to test this
 * <interceptor-ref name="interceptorStack1">
 * <param name="interceptorStack2.interceptorStack3.interceptorStack4.interceptor1.param1">interceptor1_value1</param>
 * <param name="interceptorStack2.interceptorStack3.interceptorStack4.interceptor1.param2">interceptor1_value2</param>
 * <param name="interceptorStack5.interceptor2.param1">interceptor2_value1</param>
 * <param name="interceptorStack5.interceptor2.param2">interceptor2_value2</param>
 * </interceptor-ref>
 *
 * @throws Exception
 */
public void testBuildInterceptor_3() throws Exception {
    InterceptorConfig interceptorConfig1 = new InterceptorConfig.Builder("interceptor1", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor1").build();
    InterceptorConfig interceptorConfig2 = new InterceptorConfig.Builder("interceptor2", "com.opensymphony.xwork2.config.providers.InterceptorBuilderTest$MockInterceptor2").build();
    InterceptorStackConfig interceptorStackConfig1 = new InterceptorStackConfig.Builder("interceptorStack1").build();
    InterceptorStackConfig interceptorStackConfig2 = new InterceptorStackConfig.Builder("interceptorStack2").build();
    InterceptorStackConfig interceptorStackConfig3 = new InterceptorStackConfig.Builder("interceptorStack3").build();
    InterceptorStackConfig interceptorStackConfig4 = new InterceptorStackConfig.Builder("interceptorStack4").build();
    InterceptorStackConfig interceptorStackConfig5 = new InterceptorStackConfig.Builder("interceptorStack5").build();
    PackageConfig packageConfig = new PackageConfig.Builder("package1").addInterceptorConfig(interceptorConfig1).addInterceptorConfig(interceptorConfig2).addInterceptorStackConfig(interceptorStackConfig1).addInterceptorStackConfig(interceptorStackConfig2).addInterceptorStackConfig(interceptorStackConfig3).addInterceptorStackConfig(interceptorStackConfig4).addInterceptorStackConfig(interceptorStackConfig5).build();
    List interceptorMappings = InterceptorBuilder.constructInterceptorReference(packageConfig, "interceptorStack1", new LinkedHashMap<String, String>() {

        private static final long serialVersionUID = 4675809753780875525L;

        {
            put("interceptorStack2.interceptorStack3.interceptorStack4.interceptor1.param1", "interceptor1_value1");
            put("interceptorStack2.interceptorStack3.interceptorStack4.interceptor1.param2", "interceptor1_value2");
            put("interceptorStack5.interceptor2.param1", "interceptor2_value1");
            put("interceptorStack5.interceptor2.param2", "interceptor2_value2");
        }
    }, null, objectFactory);
    assertEquals(interceptorMappings.size(), 2);
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getName(), "interceptor1");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(0)).getInterceptor().getClass(), MockInterceptor1.class);
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam1(), "interceptor1_value1");
    assertEquals(((MockInterceptor1) ((InterceptorMapping) interceptorMappings.get(0)).getInterceptor()).getParam2(), "interceptor1_value2");
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getName(), "interceptor2");
    assertNotNull(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor());
    assertEquals(((InterceptorMapping) interceptorMappings.get(1)).getInterceptor().getClass(), MockInterceptor2.class);
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam1(), "interceptor2_value1");
    assertEquals(((MockInterceptor2) ((InterceptorMapping) interceptorMappings.get(1)).getInterceptor()).getParam2(), "interceptor2_value2");
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) List(java.util.List) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 18 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class XmlConfigurationProviderActionsTest method testActions.

public void testActions() throws Exception {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-actions.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // bar action is very simple, just two params
    params.put("foo", "17");
    params.put("bar", "23");
    params.put("testXW412", "foo.jspa?fooID=${fooID}&something=bar");
    params.put("testXW412Again", "something");
    ActionConfig barAction = new ActionConfig.Builder("", "Bar", SimpleAction.class.getName()).addParams(params).build();
    // foo action is a little more complex, two params, a result and an interceptor stack
    results = new HashMap<>();
    params = new HashMap<>();
    params.put("foo", "18");
    params.put("bar", "24");
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    InterceptorConfig noopInterceptorConfig = new InterceptorConfig.Builder("noop", NoOpInterceptor.class.getName()).build();
    interceptors.add(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptorConfig, new HashMap<String, String>())));
    ActionConfig fooAction = new ActionConfig.Builder("", "Foo", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).build();
    // wildcard action is simple wildcard example
    results = new HashMap<>();
    results.put("*", new ResultConfig.Builder("*", MockResult.class.getName()).build());
    ActionConfig wildcardAction = new ActionConfig.Builder("", "WildCard", SimpleAction.class.getName()).addResultConfigs(results).addInterceptors(interceptors).build();
    // fooBar action is a little more complex, two params, a result and an interceptor stack
    params = new HashMap<String, String>();
    params.put("foo", "18");
    params.put("bar", "24");
    results = new HashMap<>();
    results.put("success", new ResultConfig.Builder("success", MockResult.class.getName()).build());
    ExceptionMappingConfig exceptionConfig = new ExceptionMappingConfig.Builder("runtime", "java.lang.RuntimeException", "exception").build();
    exceptionMappings.add(exceptionConfig);
    ActionConfig fooBarAction = new ActionConfig.Builder("", "FooBar", SimpleAction.class.getName()).addParams(params).addResultConfigs(results).addInterceptors(interceptors).addExceptionMappings(exceptionMappings).build();
    // TestInterceptorParam action tests that an interceptor worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", MockInterceptor.DEFAULT_FOO_VALUE);
    InterceptorConfig mockInterceptorConfig = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).build();
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue");
    interceptorParams.put("foo", "foo123");
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptorConfig, interceptorParams)));
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(7, actionConfigs.size());
    assertEquals(barAction, actionConfigs.get("Bar"));
    assertEquals(fooAction, actionConfigs.get("Foo"));
    assertEquals(wildcardAction, actionConfigs.get("WildCard"));
    assertEquals(fooBarAction, actionConfigs.get("FooBar"));
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
}
Also used : MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) SimpleAction(com.opensymphony.xwork2.SimpleAction) Map(java.util.Map) HashMap(java.util.HashMap)

Example 19 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class FormTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    validationInterceptor = new ValidationInterceptor();
    validationInterceptor.setIncludeMethods("*");
    ActionConfig config = new ActionConfig.Builder("", "name", "").addInterceptor(new InterceptorMapping("validationInterceptor", validationInterceptor)).build();
    ActionInvocation invocation = EasyMock.createNiceMock(ActionInvocation.class);
    ActionProxy proxy = EasyMock.createNiceMock(ActionProxy.class);
    EasyMock.expect(invocation.getProxy()).andReturn(proxy).anyTimes();
    EasyMock.expect(invocation.getAction()).andReturn(null).anyTimes();
    EasyMock.expect(invocation.invoke()).andReturn(Action.SUCCESS).anyTimes();
    EasyMock.expect(proxy.getMethod()).andReturn("execute").anyTimes();
    EasyMock.expect(proxy.getConfig()).andReturn(config).anyTimes();
    EasyMock.replay(invocation);
    EasyMock.replay(proxy);
    Map<String, ActionConfig> defaultNamespace = configuration.getRuntimeConfiguration().getActionConfigs().get("");
    defaultNamespace.put("actionName", config);
    ((DefaultActionMapper) container.getInstance(ActionMapper.class)).setAllowDynamicMethodCalls("true");
    ActionContext.getContext().withActionInvocation(invocation);
}
Also used : ValidationInterceptor(com.opensymphony.xwork2.validator.ValidationInterceptor) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) DefaultActionMapper(org.apache.struts2.dispatcher.mapper.DefaultActionMapper) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) ActionProxy(com.opensymphony.xwork2.ActionProxy) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultActionMapper(org.apache.struts2.dispatcher.mapper.DefaultActionMapper) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Example 20 with InterceptorMapping

use of com.opensymphony.xwork2.config.entities.InterceptorMapping in project struts by apache.

the class DispatcherTest method testInterceptorDestroy.

public void testInterceptorDestroy() throws Exception {
    Mock mockInterceptor = new Mock(Interceptor.class);
    mockInterceptor.matchAndReturn("hashCode", 0);
    mockInterceptor.expect("destroy");
    InterceptorMapping interceptorMapping = new InterceptorMapping("test", (Interceptor) mockInterceptor.proxy());
    InterceptorStackConfig isc = new InterceptorStackConfig.Builder("test").addInterceptor(interceptorMapping).build();
    PackageConfig packageConfig = new PackageConfig.Builder("test").addInterceptorStackConfig(isc).build();
    Map<String, PackageConfig> packageConfigs = new HashMap<String, PackageConfig>();
    packageConfigs.put("test", packageConfig);
    Mock mockContainer = new Mock(Container.class);
    mockContainer.matchAndReturn("getInstance", C.args(C.eq(ObjectFactory.class)), new ObjectFactory());
    String reloadConfigs = container.getInstance(String.class, StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD);
    mockContainer.expectAndReturn("getInstance", C.args(C.eq(String.class), C.eq(StrutsConstants.STRUTS_CONFIGURATION_XML_RELOAD)), reloadConfigs);
    Mock mockConfiguration = new Mock(Configuration.class);
    mockConfiguration.matchAndReturn("getPackageConfigs", packageConfigs);
    mockConfiguration.matchAndReturn("getContainer", mockContainer.proxy());
    mockConfiguration.expect("destroy");
    ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.setConfiguration((Configuration) mockConfiguration.proxy());
    Dispatcher dispatcher = new MockDispatcher(new MockServletContext(), new HashMap<String, String>(), configurationManager);
    dispatcher.init();
    dispatcher.cleanup();
    mockInterceptor.verify();
    mockContainer.verify();
    mockConfiguration.verify();
}
Also used : HashMap(java.util.HashMap) Mock(com.mockobjects.dynamic.Mock) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) MockServletContext(org.springframework.mock.web.MockServletContext) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) ConfigurationManager(com.opensymphony.xwork2.config.ConfigurationManager)

Aggregations

InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)29 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)12 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 ArrayList (java.util.ArrayList)10 HashMap (java.util.HashMap)8 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)7 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)7 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)6 List (java.util.List)6 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)5 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)5 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)4 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)4 Map (java.util.Map)4 SimpleAction (com.opensymphony.xwork2.SimpleAction)3 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)3 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)3 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)3 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)3