use of com.opensymphony.xwork2.util.Bar 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);
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class FooBarConverter method convertValue.
@Override
public Object convertValue(Map<String, Object> context, Object value, Class toType) {
if (toType == String.class) {
Bar bar = (Bar) value;
return bar.getTitle() + ":" + bar.getSomethingElse();
} else if (toType == Bar.class) {
String valueStr = (String) value;
int loc = valueStr.indexOf(":");
String title = valueStr.substring(0, loc);
String rest = valueStr.substring(loc + 1);
Bar bar = new Bar();
bar.setTitle(title);
bar.setSomethingElse(Integer.parseInt(rest));
return bar;
} else if (toType == Cat.class) {
Cat cat = new Cat();
cat.setName((String) value);
return cat;
} else if (toType == AnnotatedCat.class) {
AnnotatedCat cat = new AnnotatedCat();
cat.setName((String) value);
return cat;
} else {
System.out.println("Don't know how to convert between " + value.getClass().getName() + " and " + toType.getName());
}
return null;
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class ListAction method testStringToCustomTypeUsingCustomConverterFromProperties.
public void testStringToCustomTypeUsingCustomConverterFromProperties() throws Exception {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(new ClassLoader(cl) {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
if ("struts-conversion.properties".equals(name)) {
return new Enumeration<URL>() {
boolean done = false;
public boolean hasMoreElements() {
return !done;
}
public URL nextElement() {
if (done) {
throw new RuntimeException("Conversion configuration loading " + "failed because it asked the enumeration for the next URL " + "too many times");
}
done = true;
return getClass().getResource("/com/opensymphony/xwork2/conversion/impl/test-struts-conversion.properties");
}
};
} else {
return super.getResources(name);
}
}
});
setUp();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
assertNotNull("conversion failed", bar);
assertEquals(123, bar.getSomethingElse());
assertEquals("blah", bar.getTitle());
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class ListAction method testStringToCustomTypeUsingCustomConverter.
public void testStringToCustomTypeUsingCustomConverter() {
// the converter needs to be registered as the Bar.class converter
// it won't be detected from the Foo-conversion.properties
// because the Foo-conversion.properties file is only used when converting a property of Foo
converter.registerConverter(Bar.class.getName(), new FooBarConverter());
Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
assertNotNull("conversion failed", bar);
assertEquals(123, bar.getSomethingElse());
assertEquals("blah", bar.getTitle());
}
use of com.opensymphony.xwork2.util.Bar in project struts by apache.
the class ConversionErrorInterceptorTest method testFieldErrorWithMapKeyAdded.
public void testFieldErrorWithMapKeyAdded() throws Exception {
String fieldName = "foo['1'].intValue";
conversionErrors.put(fieldName, new ConversionData("bar", int.class));
ActionSupport action = new ActionSupport();
mockInvocation.expectAndReturn("getAction", action);
stack.push(action);
mockInvocation.matchAndReturn("getAction", action);
assertNull(action.getFieldErrors().get(fieldName));
interceptor.doIntercept(invocation);
// This fails!
assertTrue(action.hasFieldErrors());
assertNotNull(action.getFieldErrors().get(fieldName));
}
Aggregations