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));
}
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();
}
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));
}
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"));
}
Aggregations