Search in sources :

Example 1 with Bar

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);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) MockResult(com.opensymphony.xwork2.mock.MockResult) HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ArrayList(java.util.ArrayList) SimpleAction(com.opensymphony.xwork2.SimpleAction) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) ExceptionMappingConfig(com.opensymphony.xwork2.config.entities.ExceptionMappingConfig) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Bar

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;
}
Also used : AnnotatedCat(com.opensymphony.xwork2.util.AnnotatedCat) Bar(com.opensymphony.xwork2.util.Bar) AnnotatedCat(com.opensymphony.xwork2.util.AnnotatedCat) Cat(com.opensymphony.xwork2.util.Cat)

Example 3 with Bar

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());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with Bar

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());
}
Also used : Bar(com.opensymphony.xwork2.util.Bar)

Example 5 with Bar

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));
}
Also used : ConversionData(com.opensymphony.xwork2.conversion.impl.ConversionData)

Aggregations

HashMap (java.util.HashMap)17 Bar (com.opensymphony.xwork2.util.Bar)14 Foo (com.opensymphony.xwork2.util.Foo)13 ValueStack (com.opensymphony.xwork2.util.ValueStack)12 ActionProxy (com.opensymphony.xwork2.ActionProxy)9 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)9 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)7 ValidationAware (com.opensymphony.xwork2.interceptor.ValidationAware)7 List (java.util.List)6 Map (java.util.Map)6 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)5 Mock (com.mockobjects.dynamic.Mock)4 ActionContext (com.opensymphony.xwork2.ActionContext)4 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)4 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)3 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)3 MockResult (com.opensymphony.xwork2.mock.MockResult)3 ModelDrivenAction2 (com.opensymphony.xwork2.test.ModelDrivenAction2)3 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)3 ActionSupport (com.opensymphony.xwork2.ActionSupport)2