Search in sources :

Example 1 with ConfigurationProvider

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

the class XWorkTestCaseHelper method loadConfigurationProviders.

public static ConfigurationManager loadConfigurationProviders(ConfigurationManager configurationManager, ConfigurationProvider... providers) {
    try {
        tearDown(configurationManager);
    } catch (Exception e) {
        throw new RuntimeException("Cannot clean old configuration", e);
    }
    configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
    configurationManager.addContainerProvider(new ContainerProvider() {

        public void destroy() {
        }

        public void init(Configuration configuration) throws ConfigurationException {
        }

        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.setAllowDuplicates(true);
        }
    });
    configurationManager.addContainerProvider(new StrutsDefaultConfigurationProvider());
    for (ConfigurationProvider prov : providers) {
        if (prov instanceof XmlConfigurationProvider) {
            ((XmlConfigurationProvider) prov).setThrowExceptionOnDuplicateBeans(false);
        }
        configurationManager.addContainerProvider(prov);
    }
    Container container = configurationManager.getConfiguration().getContainer();
    // Reset the value stack
    ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
    stack.getActionContext().withContainer(container).withValueStack(stack).bind();
    return configurationManager;
}
Also used : XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsDefaultConfigurationProvider(com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) StrutsDefaultConfigurationProvider(com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)

Example 2 with ConfigurationProvider

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

the class PreResultListenerTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    loadConfigurationProviders(new ConfigurationProvider() {

        Configuration configuration;

        public void destroy() {
        }

        public void init(Configuration config) {
            this.configuration = config;
        }

        public void loadPackages() {
            PackageConfig packageConfig = new PackageConfig.Builder("package").addActionConfig("action", new ActionConfig.Builder("package", "action", SimpleFooAction.class.getName()).build()).build();
            configuration.addPackageConfig("package", packageConfig);
        }

        /**
         * Tells whether the ConfigurationProvider should reload its configuration
         */
        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
            builder.factory(ObjectFactory.class);
        }
    });
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) Configuration(com.opensymphony.xwork2.config.Configuration) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 3 with ConfigurationProvider

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

the class XmlConfigurationProviderExceptionMappingsTest method testActions.

public void testActions() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-exception-mappings.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    List<ExceptionMappingConfig> exceptionMappings = new ArrayList<>();
    HashMap<String, String> parameters = new HashMap<>();
    HashMap<String, ResultConfig> results = new HashMap<>();
    exceptionMappings.add(new ExceptionMappingConfig.Builder("spooky-result", "com.opensymphony.xwork2.SpookyException", "spooky-result").build());
    results.put("spooky-result", new ResultConfig.Builder("spooky-result", MockResult.class.getName()).build());
    Map<String, String> resultParams = new HashMap<>();
    resultParams.put("actionName", "bar.vm");
    results.put("specificLocationResult", new ResultConfig.Builder("specificLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
    ActionConfig expectedAction = new ActionConfig.Builder("default", "Bar", SimpleAction.class.getName()).addParams(parameters).addResultConfigs(results).addExceptionMappings(exceptionMappings).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map actionConfigs = pkg.getActionConfigs();
    // assertions
    assertEquals(1, actionConfigs.size());
    ActionConfig action = (ActionConfig) actionConfigs.get("Bar");
    assertEquals(expectedAction, action);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ConfigurationProvider

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

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

the class XmlConfigurationProviderInvalidFileTest method testInvalidFileThrowsException.

public void testInvalidFileThrowsException() {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-invalid-file.xml";
    try {
        ConfigurationProvider provider = buildConfigurationProvider(filename);
        fail();
    } catch (ConfigurationException e) {
    // this is what we expect
    }
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider)

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