Search in sources :

Example 1 with NoOpInterceptor

use of org.apache.struts2.interceptor.NoOpInterceptor in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testBasicInterceptors.

public void testBasicInterceptors() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-basic.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // the test interceptor with a parameter
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    InterceptorConfig paramsInterceptor = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).addParams(params).build();
    // the default interceptor stack
    InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
    // the derivative interceptor stack
    InterceptorStackConfig derivativeStack = new InterceptorStackConfig.Builder("derivativeStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).addInterceptor(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>()))).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map interceptorConfigs = pkg.getInterceptorConfigs();
    // assertions for size
    assertEquals(5, interceptorConfigs.size());
    // assertions for interceptors
    assertEquals(noopInterceptor, interceptorConfigs.get("noop"));
    assertEquals(loggingInterceptor, interceptorConfigs.get("logging"));
    assertEquals(paramsInterceptor, interceptorConfigs.get("test"));
    // assertions for interceptor stacks
    assertEquals(defaultStack, interceptorConfigs.get("defaultStack"));
    assertEquals(derivativeStack, interceptorConfigs.get("derivativeStack"));
}
Also used : HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with NoOpInterceptor

use of org.apache.struts2.interceptor.NoOpInterceptor in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testInterceptorParamOverriding.

public void testInterceptorParamOverriding() throws Exception {
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    params.put("expectedFoo", "expectedFooValue");
    InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
    ArrayList<InterceptorMapping> interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack.getInterceptors());
    ActionConfig intAction = new ActionConfig.Builder("", "TestInterceptorParam", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    // TestInterceptorParamOverride action tests that an interceptor with a param override worked
    HashMap<String, String> interceptorParams = new HashMap<>();
    interceptorParams.put("expectedFoo", "expectedFooValue2");
    interceptorParams.put("foo", "foo123");
    InterceptorStackConfig defaultStack2 = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, interceptorParams))).build();
    interceptors = new ArrayList<>();
    interceptors.addAll(defaultStack2.getInterceptors());
    ActionConfig intOverAction = new ActionConfig.Builder("", "TestInterceptorParamOverride", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-params.xml");
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(2, actionConfigs.size());
    assertEquals(intAction, actionConfigs.get("TestInterceptorParam"));
    assertEquals(intOverAction, actionConfigs.get("TestInterceptorParamOverride"));
    ActionConfig ac = (ActionConfig) actionConfigs.get("TestInterceptorParamOverride");
    assertEquals(defaultStack.getInterceptors(), ac.getInterceptors());
    ActionConfig ac2 = (ActionConfig) actionConfigs.get("TestInterceptorParam");
    assertEquals(defaultStack2.getInterceptors(), ac2.getInterceptors());
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with NoOpInterceptor

use of org.apache.struts2.interceptor.NoOpInterceptor in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testInterceptorDefaultRefs.

public void testInterceptorDefaultRefs() throws ConfigurationException {
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-defaultref.xml");
    container.inject(provider);
    loadConfigurationProviders(provider);
    // expectations - the inherited interceptor stack
    // default package
    ArrayList<InterceptorMapping> interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>())));
    ActionConfig actionWithOwnRef = new ActionConfig.Builder("", "ActionWithOwnRef", SimpleAction.class.getName()).addInterceptors(interceptors).build();
    ActionConfig actionWithDefaultRef = new ActionConfig.Builder("", "ActionWithDefaultRef", SimpleAction.class.getName()).addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    // sub package
    // this should inherit
    ActionConfig actionWithNoRef = new ActionConfig.Builder("", "ActionWithNoRef", SimpleAction.class.getName()).addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    interceptors = new ArrayList<>();
    interceptors.add(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>())));
    ActionConfig anotherActionWithOwnRef = new ActionConfig.Builder("", "AnotherActionWithOwnRef", SimpleAction.class.getName()).addInterceptor(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>()))).build();
    RuntimeConfiguration runtimeConfig = configurationManager.getConfiguration().getRuntimeConfiguration();
    // assertions
    assertEquals(actionWithOwnRef, runtimeConfig.getActionConfig("", "ActionWithOwnRef"));
    assertEquals(actionWithDefaultRef, runtimeConfig.getActionConfig("", "ActionWithDefaultRef"));
    assertEquals(actionWithNoRef, runtimeConfig.getActionConfig("", "ActionWithNoRef"));
    assertEquals(anotherActionWithOwnRef, runtimeConfig.getActionConfig("", "AnotherActionWithOwnRef"));
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping)

Example 4 with NoOpInterceptor

use of org.apache.struts2.interceptor.NoOpInterceptor in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testInterceptorInheritance.

public void testInterceptorInheritance() throws ConfigurationException {
    // expectations - the inherited interceptor stack
    InterceptorStackConfig inheritedStack = new InterceptorStackConfig.Builder("subDefaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    ConfigurationProvider provider = buildConfigurationProvider("com/opensymphony/xwork2/config/providers/xwork-test-interceptor-inheritance.xml");
    // assertions
    PackageConfig defaultPkg = configuration.getPackageConfig("default");
    assertEquals(2, defaultPkg.getInterceptorConfigs().size());
    PackageConfig subPkg = configuration.getPackageConfig("subPackage");
    assertEquals(1, subPkg.getInterceptorConfigs().size());
    assertEquals(3, subPkg.getAllInterceptorConfigs().size());
    assertEquals(inheritedStack, subPkg.getInterceptorConfigs().get("subDefaultStack"));
    // expectations - the inherited interceptor stack
    inheritedStack = new InterceptorStackConfig.Builder("subSubDefaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).build();
    PackageConfig subSubPkg = configuration.getPackageConfig("subSubPackage");
    assertEquals(1, subSubPkg.getInterceptorConfigs().size());
    assertEquals(4, subSubPkg.getAllInterceptorConfigs().size());
    assertEquals(inheritedStack, subSubPkg.getInterceptorConfigs().get("subSubDefaultStack"));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Aggregations

InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)4 HashMap (java.util.HashMap)4 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)4 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)3 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)3 SimpleAction (com.opensymphony.xwork2.SimpleAction)2 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)1 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)1 MockInterceptor (com.opensymphony.xwork2.mock.MockInterceptor)1