Search in sources :

Example 26 with ConfigurationProvider

use of com.opensymphony.xwork2.config.ConfigurationProvider 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 27 with ConfigurationProvider

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

the class ChainResultTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // ensure we're using the default configuration, not simple config
    XmlConfigurationProvider configurationProvider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(configurationProvider);
    loadConfigurationProviders(configurationProvider);
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider)

Example 28 with ConfigurationProvider

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

the class PrefixBasedActionProxyFactoryTest method setUp.

@Override
public void setUp() throws Exception {
    ConfigurationProvider[] providers = new ConfigurationProvider[] { new StrutsXmlConfigurationProvider("xwork-sample.xml"), new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, "prefix1", new Factory() {

                public Object create(Context context) throws Exception {
                    return new Prefix1Factory();
                }

                public Class type() {
                    return Prefix1Factory.class;
                }
            }, Scope.SINGLETON);
        }
    }, new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, "prefix2", new Factory() {

                public Object create(Context context) throws Exception {
                    return new Prefix2Factory();
                }

                public Class type() {
                    return Prefix2Factory.class;
                }
            }, Scope.SINGLETON);
        }
    } };
    loadConfigurationProviders(providers);
    factory = new PrefixBasedActionProxyFactory();
    factory.setContainer(container);
}
Also used : Context(com.opensymphony.xwork2.inject.Context) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) Factory(com.opensymphony.xwork2.inject.Factory) ActionProxyFactory(com.opensymphony.xwork2.ActionProxyFactory) DefaultActionProxyFactory(com.opensymphony.xwork2.DefaultActionProxyFactory) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

Example 29 with ConfigurationProvider

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

the class ConfigurationManagerTest method testDestroyConfiguration.

public void testDestroyConfiguration() throws Exception {
    class State {

        public boolean isDestroyed1 = false;

        public boolean isDestroyed2 = false;
    }
    final State state = new State();
    ConfigurationManager configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.addContainerProvider(new ConfigurationProvider() {

        public void destroy() {
            throw new RuntimeException("testing testing 123");
        }

        public void init(Configuration configuration) throws ConfigurationException {
        }

        public void loadPackages() throws ConfigurationException {
        }

        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, Properties props) throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
        }
    });
    configurationManager.addContainerProvider(new ConfigurationProvider() {

        public void destroy() {
            state.isDestroyed1 = true;
        }

        public void init(Configuration configuration) throws ConfigurationException {
        }

        public void loadPackages() throws ConfigurationException {
        }

        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, Properties props) throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
        }
    });
    configurationManager.addContainerProvider(new ConfigurationProvider() {

        public void destroy() {
            throw new RuntimeException("testing testing 123");
        }

        public void init(Configuration configuration) throws ConfigurationException {
        }

        public void loadPackages() throws ConfigurationException {
        }

        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, Properties props) throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
        }
    });
    configurationManager.addContainerProvider(new ConfigurationProvider() {

        public void destroy() {
            state.isDestroyed2 = true;
        }

        public void init(Configuration configuration) throws ConfigurationException {
        }

        public void loadPackages() throws ConfigurationException {
        }

        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, Properties props) throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
        }
    });
    assertFalse(state.isDestroyed1);
    assertFalse(state.isDestroyed2);
    configurationManager.clearContainerProviders();
    assertTrue(state.isDestroyed1);
    assertTrue(state.isDestroyed2);
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) StrutsDefaultConfigurationProvider(com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider) Properties(java.util.Properties) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

Example 30 with ConfigurationProvider

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

the class ActionInvocationTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    // ensure we're using the default configuration, not simple config
    XmlConfigurationProvider configurationProvider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(configurationProvider);
    loadConfigurationProviders(configurationProvider);
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider)

Aggregations

ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)41 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)22 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)21 Map (java.util.Map)12 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)11 XmlConfigurationProvider (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider)10 HashMap (java.util.HashMap)9 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)7 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)6 File (java.io.File)6 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)5 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)5 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)5 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)4 MockResult (com.opensymphony.xwork2.mock.MockResult)4 DefaultUnknownHandlerManager (com.opensymphony.xwork2.DefaultUnknownHandlerManager)3 SimpleAction (com.opensymphony.xwork2.SimpleAction)3 UnknownHandlerManager (com.opensymphony.xwork2.UnknownHandlerManager)3 Configuration (com.opensymphony.xwork2.config.Configuration)3 RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)3