use of com.opensymphony.xwork2.config.ConfigurationProvider 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.ConfigurationProvider in project struts by apache.
the class XmlConfigurationProviderResultsTest method testResultTypes.
public void testResultTypes() throws ConfigurationException {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-results.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename);
// setup expectations
ResultTypeConfig chainResult = new ResultTypeConfig.Builder("chain", ActionChainResult.class.getName()).build();
ResultTypeConfig mockResult = new ResultTypeConfig.Builder("mock", MockResult.class.getName()).build();
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map resultTypes = pkg.getResultTypeConfigs();
// assertions
assertEquals(2, resultTypes.size());
assertEquals("chain", pkg.getDefaultResultType());
assertEquals(chainResult, resultTypes.get("chain"));
assertEquals(mockResult, resultTypes.get("mock"));
}
use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.
the class XmlConfigurationProviderResultsTest method testResultNames.
public void testResultNames() throws ConfigurationException {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-result-names.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename);
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map<String, ActionConfig> actionConfigs = pkg.getActionConfigs();
// assertions
assertNotNull(actionConfigs);
Map<String, ResultConfig> resultConfigs = actionConfigs.get("noname").getResults();
assertEquals(1, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.SUCCESS));
resultConfigs = actionConfigs.get("success").getResults();
assertEquals(1, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.SUCCESS));
resultConfigs = actionConfigs.get("empty").getResults();
assertEquals(1, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.SUCCESS));
resultConfigs = actionConfigs.get("comma").getResults();
assertEquals(1, resultConfigs.size());
assertTrue(resultConfigs.containsKey(" , "));
resultConfigs = actionConfigs.get("error-input").getResults();
assertEquals(2, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.ERROR));
assertTrue(resultConfigs.containsKey(Action.INPUT));
resultConfigs = actionConfigs.get("error-input2").getResults();
assertEquals(2, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.ERROR));
assertTrue(resultConfigs.containsKey(Action.INPUT));
resultConfigs = actionConfigs.get("noname-error-input").getResults();
assertEquals(3, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.SUCCESS));
assertTrue(resultConfigs.containsKey(Action.ERROR));
assertTrue(resultConfigs.containsKey(Action.INPUT));
resultConfigs = actionConfigs.get("noname-error-input2").getResults();
assertEquals(3, resultConfigs.size());
assertTrue(resultConfigs.containsKey(Action.SUCCESS));
assertTrue(resultConfigs.containsKey(Action.ERROR));
assertTrue(resultConfigs.containsKey(Action.INPUT));
}
use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.
the class XmlConfigurationProviderResultsTest method testActions.
public void testActions() throws ConfigurationException {
final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-results.xml";
ConfigurationProvider provider = buildConfigurationProvider(filename);
HashMap<String, String> parameters = new HashMap<>();
HashMap<String, ResultConfig> results = new HashMap<>();
results.put("chainDefaultTypedResult", new ResultConfig.Builder("chainDefaultTypedResult", ActionChainResult.class.getName()).build());
results.put("mockTypedResult", new ResultConfig.Builder("mockTypedResult", 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());
resultParams = new HashMap<>();
resultParams.put("actionName", "foo.vm");
results.put("defaultLocationResult", new ResultConfig.Builder("defaultLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
resultParams = new HashMap<>();
resultParams.put("foo", "bar");
results.put("noDefaultLocationResult", new ResultConfig.Builder("noDefaultLocationResult", ActionChainResult.class.getName()).addParams(resultParams).build());
ActionConfig expectedAction = new ActionConfig.Builder("default", "Bar", SimpleAction.class.getName()).addParams(parameters).addResultConfigs(results).build();
// execute the configuration
provider.init(configuration);
provider.loadPackages();
PackageConfig pkg = configuration.getPackageConfig("default");
Map<String, ActionConfig> actionConfigs = pkg.getActionConfigs();
// assertions
assertEquals(1, actionConfigs.size());
ActionConfig action = actionConfigs.get("Bar");
assertEquals(expectedAction, action);
}
use of com.opensymphony.xwork2.config.ConfigurationProvider in project struts by apache.
the class XmlConfigurationProviderTest method testProvider.
private void testProvider(String configFile) throws Exception {
ConfigurationProvider provider = buildConfigurationProvider(configFile);
container.getInstance(FileManagerFactory.class).getFileManager().setReloadingConfigs(true);
assertFalse(provider.needsReload());
String fullPath = ClassLoaderUtil.getResource(configFile, ConfigurationProvider.class).toString();
int startIndex = fullPath.indexOf(":file:/");
int endIndex = fullPath.indexOf("!/");
String jar = fullPath.substring(startIndex + (":file:/".length() - 1), endIndex).replaceAll("%20", " ");
File file = new File(jar);
assertTrue("File [" + file + "] doesn't exist!", file.exists());
changeFileTime(jar, file);
assertFalse(provider.needsReload());
}
Aggregations