Search in sources :

Example 11 with RuntimeConfiguration

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

the class ConfigurationTest method testMultipleConfigProviders.

public void testMultipleConfigProviders() {
    configurationManager.addContainerProvider(new MockConfigurationProvider());
    try {
        configurationManager.reload();
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail();
    }
    RuntimeConfiguration configuration = configurationManager.getConfiguration().getRuntimeConfiguration();
    // check that it has configuration from xml
    assertNotNull(configuration.getActionConfig("/foo/bar", "Bar"));
    // check that it has configuration from MockConfigurationProvider
    assertNotNull(configuration.getActionConfig("", MockConfigurationProvider.FOO_ACTION_NAME));
}
Also used : MockConfigurationProvider(com.opensymphony.xwork2.config.providers.MockConfigurationProvider)

Example 12 with RuntimeConfiguration

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

the class ConfigurationTest method testMultipleContainerProviders.

public void testMultipleContainerProviders() throws Exception {
    // to start from scratch
    configurationManager.destroyConfiguration();
    // to build basic configuration
    configurationManager.getConfiguration();
    Mock mockContainerProvider = new Mock(ContainerProvider.class);
    mockContainerProvider.expect("init", C.ANY_ARGS);
    mockContainerProvider.expect("register", C.ANY_ARGS);
    mockContainerProvider.matchAndReturn("equals", C.ANY_ARGS, false);
    mockContainerProvider.matchAndReturn("toString", "foo");
    mockContainerProvider.matchAndReturn("destroy", null);
    mockContainerProvider.expectAndReturn("needsReload", true);
    // the order of providers must be changed as just first is checked if reload is needed
    configurationManager.addContainerProvider((ContainerProvider) mockContainerProvider.proxy());
    XmlConfigurationProvider provider = new StrutsXmlConfigurationProvider("xwork-sample.xml");
    container.inject(provider);
    configurationManager.addContainerProvider(provider);
    Configuration config = null;
    try {
        config = configurationManager.getConfiguration();
    } catch (ConfigurationException e) {
        e.printStackTrace();
        fail();
    }
    RuntimeConfiguration configuration = config.getRuntimeConfiguration();
    // check that it has configuration from xml
    assertNotNull(configuration.getActionConfig("/foo/bar", "Bar"));
    mockContainerProvider.verify();
}
Also used : StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) Mock(com.mockobjects.dynamic.Mock)

Example 13 with RuntimeConfiguration

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

the class FetchMetadataInterceptorTest method testSetExemptedPathsInjectionIndirectly.

public void testSetExemptedPathsInjectionIndirectly() throws Exception {
    // Perform a multi-step test to confirm (indirectly) that the method parameter injection of setExemptedPaths() for
    // the FetchMetadataInterceptor is functioning as expected, when configured appropriately.
    // Ensure we're using the specific test configuration, not the default simple configuration.
    XmlConfigurationProvider configurationProvider = new StrutsXmlConfigurationProvider("struts-testing.xml");
    container.inject(configurationProvider);
    loadConfigurationProviders(configurationProvider);
    // The test configuration in "struts-testing.xml" should define a "default" package.  That "default" package should contain a "defaultInterceptorStack" containing
    // a "fetchMetadata" interceptor parameter "fetchMetadata.setExemptedPaths".  If the parameter method injection is working correctly for the FetchMetadataInterceptor,
    // the exempted paths should be set appropriately for the interceptor instances, once the configuration is loaded into the container.
    final PackageConfig defaultPackageConfig = configuration.getPackageConfig("default");
    final InterceptorStackConfig defaultInterceptorStackConfig = (InterceptorStackConfig) defaultPackageConfig.getInterceptorConfig("defaultInterceptorStack");
    final Collection<InterceptorMapping> defaultInterceptorStackInterceptors = defaultInterceptorStackConfig.getInterceptors();
    assertFalse("'defaultInterceptorStack' interceptors in struts-testing.xml is empty ?", defaultInterceptorStackInterceptors.isEmpty());
    InterceptorMapping configuredFetchMetadataInterceptorMapping = null;
    Iterator<InterceptorMapping> interceptorIterator = defaultInterceptorStackInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping not present after loading 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping loaded from 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    FetchMetadataInterceptor configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, "foo");
    request.setContextPath("/foo");
    assertEquals("Expected interceptor to NOT accept this request [/foo]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/fetchMetadataExemptedGlobal");
    assertNotEquals("Expected interceptor to accept this request [/fetchMetadataExemptedGlobal]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    request.setContextPath("/someOtherPath");
    assertNotEquals("Expected interceptor to accept this request [/someOtherPath]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // The test configuration in "struts-testing.xml" should also contain three actions configured differently for the "fetchMetadata" interceptor.
    // "fetchMetadataExempted" has an override exemption matching its action name, "fetchMetadataNotExempted" has an override exemption NOT matching its action name,
    // and "fetchMetadataExemptedGlobal" has an action name matching an exemption defined in "defaultInterceptorStack".
    final RuntimeConfiguration runtimeConfiguration = configuration.getRuntimeConfiguration();
    final ActionConfig fetchMetadataExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExempted");
    final ActionConfig fetchMetadataNotExemptedActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataNotExempted");
    final ActionConfig fetchMetadataExemptedGlobalActionConfig = runtimeConfiguration.getActionConfig("/", "fetchMetadataExemptedGlobal");
    assertNotNull("'fetchMetadataExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataNotExempted' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    assertNotNull("'fetchMetadataExemptedGlobal' action config not present in 'struts-testing.xml' ?", fetchMetadataExemptedActionConfig);
    // Test fetchMetadata interceptor for the "fetchMetadataExempted" action.
    Collection<InterceptorMapping> currentActionInterceptors = fetchMetadataExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping for action 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataNotExempted" action.
    currentActionInterceptors = fetchMetadataNotExemptedActionConfig.getInterceptors();
    assertFalse("'fetchMetadataNotExempted' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataNotExempted' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExempted' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataNotExemptedActionConfig.getName());
    request.setContextPath("/" + fetchMetadataNotExemptedActionConfig.getName());
    assertEquals("Expected interceptor to NOT accept this request [" + "/" + fetchMetadataNotExemptedActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
    // Test fetchMetadata interceptor for the "fetchMetadataExemptedGlobal" action.
    currentActionInterceptors = fetchMetadataExemptedGlobalActionConfig.getInterceptors();
    assertFalse("'fetchMetadataExemptedGlobal' interceptors in struts-testing.xml is empty ?", currentActionInterceptors.isEmpty());
    configuredFetchMetadataInterceptorMapping = null;
    interceptorIterator = currentActionInterceptors.iterator();
    while (interceptorIterator.hasNext()) {
        InterceptorMapping currentMapping = interceptorIterator.next();
        if (currentMapping != null && "fetchMetadata".equals(currentMapping.getName())) {
            configuredFetchMetadataInterceptorMapping = currentMapping;
            break;
        }
    }
    assertNotNull("'fetchMetadata' interceptor mapping for action 'fetchMetadataExemptedGlobal' not present in 'struts-testing.xml' ?", configuredFetchMetadataInterceptorMapping);
    assertTrue("'fetchMetadata' interceptor mapping 'fetchMetadataExemptedGlobal' in 'struts-testing.xml' produced a non-FetchMetadataInterceptor type ?", configuredFetchMetadataInterceptorMapping.getInterceptor() instanceof FetchMetadataInterceptor);
    configuredFetchMetadataInterceptor = (FetchMetadataInterceptor) configuredFetchMetadataInterceptorMapping.getInterceptor();
    request.removeHeader(SEC_FETCH_SITE_HEADER);
    request.addHeader(SEC_FETCH_SITE_HEADER, fetchMetadataExemptedGlobalActionConfig.getName());
    request.setContextPath("/" + fetchMetadataExemptedGlobalActionConfig.getName());
    assertNotEquals("Expected interceptor to accept this request [" + "/" + fetchMetadataExemptedGlobalActionConfig.getName() + "]", SC_FORBIDDEN, configuredFetchMetadataInterceptor.intercept(mai));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 14 with RuntimeConfiguration

use of com.opensymphony.xwork2.config.RuntimeConfiguration 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)

Aggregations

RuntimeConfiguration (com.opensymphony.xwork2.config.RuntimeConfiguration)10 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)9 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)6 StrutsXmlConfigurationProvider (org.apache.struts2.config.StrutsXmlConfigurationProvider)6 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)2 ContainerProvider (com.opensymphony.xwork2.config.ContainerProvider)2 DefaultConfiguration (com.opensymphony.xwork2.config.impl.DefaultConfiguration)2 XmlConfigurationProvider (com.opensymphony.xwork2.config.providers.XmlConfigurationProvider)2 DefaultFileManager (com.opensymphony.xwork2.util.fs.DefaultFileManager)2 DefaultFileManagerFactory (com.opensymphony.xwork2.util.fs.DefaultFileManagerFactory)2 ArrayList (java.util.ArrayList)2 Mock (com.mockobjects.dynamic.Mock)1 SimpleAction (com.opensymphony.xwork2.SimpleAction)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)1 MockConfigurationProvider (com.opensymphony.xwork2.config.providers.MockConfigurationProvider)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Set (java.util.Set)1