use of com.opensymphony.xwork2.util.Foo in project struts by apache.
the class SimpleActionValidationTest method testSubPropertiesAreValidated.
public void testSubPropertiesAreValidated() {
HashMap<String, Object> params = new HashMap<>();
params.put("baz", "10");
// valid values
params.put("foo", "8");
params.put("bar", "7");
params.put("date", "12/23/2002");
params.put("bean.name", "Name should be valid");
// this should cause a message
params.put("bean.count", "100");
HashMap<String, Object> extraContext = new HashMap<>();
extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
try {
ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.VALIDATION_SUBPROPERTY_NAME, null, extraContext);
proxy.execute();
assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());
Map<String, List<String>> errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
List<String> beanCountErrors = errors.get("bean.count");
assertEquals(1, beanCountErrors.size());
String errorMessage = beanCountErrors.get(0);
assertNotNull(errorMessage);
assertEquals("bean.count out of range.", errorMessage);
} catch (Exception e) {
e.printStackTrace();
fail();
}
}
use of com.opensymphony.xwork2.util.Foo in project struts by apache.
the class StrutsLocalizedTextProviderTest method testFindTextInInterface.
public void testFindTextInInterface() throws Exception {
Action action = new ModelDrivenAction2();
Mock mockActionInvocation = new Mock(ActionInvocation.class);
mockActionInvocation.expectAndReturn("getAction", action);
ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInvocation.proxy());
String message = localizedTextProvider.findText(ModelDrivenAction2.class, "test.foo", Locale.getDefault());
assertEquals("Foo!", message);
}
use of com.opensymphony.xwork2.util.Foo in project struts by apache.
the class NamedVariablePatternMatcherTest method testCompile.
@Test
public void testCompile() {
NamedVariablePatternMatcher matcher = new NamedVariablePatternMatcher();
assertNull(matcher.compilePattern(null));
assertNull(matcher.compilePattern(""));
CompiledPattern pattern = matcher.compilePattern("foo");
assertEquals("foo", pattern.getPattern().pattern());
pattern = matcher.compilePattern("foo{jim}");
assertEquals("foo([^/]+)", pattern.getPattern().pattern());
assertEquals("jim", pattern.getVariableNames().get(0));
pattern = matcher.compilePattern("foo{jim}/{bob}");
assertEquals("foo([^/]+)/([^/]+)", pattern.getPattern().pattern());
assertEquals("jim", pattern.getVariableNames().get(0));
assertEquals("bob", pattern.getVariableNames().get(1));
assertTrue(pattern.getPattern().matcher("foostar/jie").matches());
assertFalse(pattern.getPattern().matcher("foo/star/jie").matches());
}
use of com.opensymphony.xwork2.util.Foo 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.util.Foo in project struts by apache.
the class PropertyTest method testDefaultShouldBeOutputIfBeanNotAvailable.
public void testDefaultShouldBeOutputIfBeanNotAvailable() {
final ValueStack stack = ActionContext.getContext().getValueStack();
final Property property = new Property(stack);
property.setDefault("default");
property.setValue("foo");
assertPropertyOutput("default", property);
}
Aggregations