Search in sources :

Example 6 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class ValidateAction method testParametersDoesNotAffectSession.

public void testParametersDoesNotAffectSession() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("blah", "This is blah");
    params.put("#session.foo", "Foo");
    params.put("\u0023session['user']", "0wn3d");
    params.put("\\u0023session['user']", "0wn3d");
    params.put("\u0023session.user2", "0wn3d");
    params.put("\\u0023session.user2", "0wn3d");
    params.put("('\u0023'%20%2b%20'session['user3']')(unused)", "0wn3d");
    params.put("('\\u0023' + 'session[\\'user4\\']')(unused)", "0wn3d");
    params.put("('\u0023'%2b'session['user5']')(unused)", "0wn3d");
    params.put("('\\u0023'%2b'session['user5']')(unused)", "0wn3d");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext);
    ValueStack stack = proxy.getInvocation().getStack();
    HashMap<String, Object> session = new HashMap<>();
    stack.getContext().put("session", session);
    proxy.execute();
    assertEquals("This is blah", ((SimpleAction) proxy.getAction()).getBlah());
    assertNull(session.get("foo"));
    assertNull(session.get("user"));
    assertNull(session.get("user2"));
    assertNull(session.get("user3"));
    assertNull(session.get("user4"));
    assertNull(session.get("user5"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) OgnlValueStack(com.opensymphony.xwork2.ognl.OgnlValueStack) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 7 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class ValidateAction method testParametersNotAccessProtectedMethods.

public void testParametersNotAccessProtectedMethods() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("theSemiProtectedMap.foo", "This is blah");
    params.put("theProtectedMap.boo", "This is blah");
    HashMap<String, Object> extraContext = new HashMap<>();
    extraContext.put(ActionContext.PARAMETERS, HttpParameters.create(params).build());
    ActionProxy proxy = actionProxyFactory.createActionProxy("", MockConfigurationProvider.PARAM_INTERCEPTOR_ACTION_NAME, null, extraContext);
    proxy.execute();
    SimpleAction action = (SimpleAction) proxy.getAction();
    assertEquals(1, action.getTheProtectedMap().size());
    assertNotNull(action.getTheProtectedMap().get("boo"));
    assertNull(action.getTheProtectedMap().get("foo"));
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SimpleAction(com.opensymphony.xwork2.SimpleAction)

Example 8 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class XmlConfigurationProviderInterceptorsTest method testBasicInterceptors.

public void testBasicInterceptors() throws ConfigurationException {
    final String filename = "com/opensymphony/xwork2/config/providers/xwork-test-interceptors-basic.xml";
    ConfigurationProvider provider = buildConfigurationProvider(filename);
    // setup expectations
    // the test interceptor with a parameter
    Map<String, String> params = new HashMap<>();
    params.put("foo", "expectedFoo");
    InterceptorConfig paramsInterceptor = new InterceptorConfig.Builder("test", MockInterceptor.class.getName()).addParams(params).build();
    // the default interceptor stack
    InterceptorStackConfig defaultStack = new InterceptorStackConfig.Builder("defaultStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).build();
    // the derivative interceptor stack
    InterceptorStackConfig derivativeStack = new InterceptorStackConfig.Builder("derivativeStack").addInterceptor(new InterceptorMapping("noop", objectFactory.buildInterceptor(noopInterceptor, new HashMap<String, String>()))).addInterceptor(new InterceptorMapping("test", objectFactory.buildInterceptor(mockInterceptor, params))).addInterceptor(new InterceptorMapping("logging", objectFactory.buildInterceptor(loggingInterceptor, new HashMap<String, String>()))).build();
    // execute the configuration
    provider.init(configuration);
    provider.loadPackages();
    PackageConfig pkg = configuration.getPackageConfig("default");
    Map interceptorConfigs = pkg.getInterceptorConfigs();
    // assertions for size
    assertEquals(5, interceptorConfigs.size());
    // assertions for interceptors
    assertEquals(noopInterceptor, interceptorConfigs.get("noop"));
    assertEquals(loggingInterceptor, interceptorConfigs.get("logging"));
    assertEquals(paramsInterceptor, interceptorConfigs.get("test"));
    // assertions for interceptor stacks
    assertEquals(defaultStack, interceptorConfigs.get("defaultStack"));
    assertEquals(derivativeStack, interceptorConfigs.get("derivativeStack"));
}
Also used : HashMap(java.util.HashMap) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) InterceptorConfig(com.opensymphony.xwork2.config.entities.InterceptorConfig) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) MockInterceptor(com.opensymphony.xwork2.mock.MockInterceptor) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with Foo

use of com.opensymphony.xwork2.util.Foo in project struts by apache.

the class NumberConverterTest method testStringToNumberConversionPL.

public void testStringToNumberConversionPL() throws Exception {
    // given
    NumberConverter converter = new NumberConverter();
    Map<String, Object> context = createContextWithLocale(new Locale("pl", "PL"));
    SimpleFooAction foo = new SimpleFooAction();
    // when
    Object value = converter.convertValue(context, foo, null, "id", "1234", Integer.class);
    // then
    assertEquals(1234, value);
}
Also used : Locale(java.util.Locale) SimpleFooAction(com.opensymphony.xwork2.SimpleFooAction)

Example 10 with Foo

use of com.opensymphony.xwork2.util.Foo 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)

Aggregations

Foo (com.opensymphony.xwork2.util.Foo)53 HashMap (java.util.HashMap)32 ValueStack (com.opensymphony.xwork2.util.ValueStack)23 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)20 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)19 StrutsException (org.apache.struts2.StrutsException)19 OgnlException (ognl.OgnlException)18 IntrospectionException (java.beans.IntrospectionException)17 InappropriateExpressionException (ognl.InappropriateExpressionException)17 MethodFailedException (ognl.MethodFailedException)17 NoSuchPropertyException (ognl.NoSuchPropertyException)17 ActionProxy (com.opensymphony.xwork2.ActionProxy)16 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)9 ActionContext (com.opensymphony.xwork2.ActionContext)8 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)8 Bar (com.opensymphony.xwork2.util.Bar)8 Map (java.util.Map)8 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)7 ConversionData (com.opensymphony.xwork2.conversion.impl.ConversionData)7 StubValueStack (com.opensymphony.xwork2.StubValueStack)6