Search in sources :

Example 96 with Foo

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();
    }
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) List(java.util.List) ValidationAware(com.opensymphony.xwork2.interceptor.ValidationAware)

Example 97 with Foo

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);
}
Also used : ModelDrivenAction2(com.opensymphony.xwork2.test.ModelDrivenAction2) SimpleAction(com.opensymphony.xwork2.SimpleAction) Action(com.opensymphony.xwork2.Action) Mock(com.mockobjects.dynamic.Mock)

Example 98 with Foo

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());
}
Also used : CompiledPattern(com.opensymphony.xwork2.util.NamedVariablePatternMatcher.CompiledPattern) Test(org.junit.Test)

Example 99 with Foo

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));
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) StrutsXmlConfigurationProvider(org.apache.struts2.config.StrutsXmlConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) RuntimeConfiguration(com.opensymphony.xwork2.config.RuntimeConfiguration)

Example 100 with Foo

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

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