Search in sources :

Example 41 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class DebugTagTest method setStrutsConstant.

/**
 * Overwrite the Struts Constant and reload container
 */
private void setStrutsConstant(final Map<String, String> overwritePropeties) {
    configurationManager.addContainerProvider(new StubConfigurationProvider() {

        @Override
        public boolean needsReload() {
            return true;
        }

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            for (Map.Entry<String, String> stringStringEntry : overwritePropeties.entrySet()) {
                props.setProperty(stringStringEntry.getKey(), stringStringEntry.getValue(), null);
            }
        }
    });
    configurationManager.reload();
    container = configurationManager.getConfiguration().getContainer();
    stack.getActionContext().withContainer(container);
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 42 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class AnnotationValidationConfigurationBuilderTest method createValidationManager.

private AnnotationActionValidatorManager createValidationManager(final Class<? extends ActionSupport> actionClass, Locale locale) throws Exception {
    loadConfigurationProviders(new ConfigurationProvider() {

        public void destroy() {
        }

        public void init(Configuration configuration) throws ConfigurationException {
            configuration.addPackageConfig("default", new PackageConfig.Builder("default").addActionConfig("annotation", new ActionConfig.Builder("", "annotation", actionClass.getName()).build()).build());
        }

        public boolean needsReload() {
            return false;
        }

        public void loadPackages() throws ConfigurationException {
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.constant(StrutsConstants.STRUTS_DEVMODE, true);
        }
    });
    // ActionContext is destroyed during rebuilding configuration
    ActionContext.getContext().withLocale(locale);
    ActionInvocation invocation = new DefaultActionInvocation(ActionContext.getContext().getContextMap(), true);
    container.inject(invocation);
    invocation.init(actionProxyFactory.createActionProxy("", "annotation", null, ActionContext.getContext().getContextMap()));
    AnnotationActionValidatorManager manager = new AnnotationActionValidatorManager();
    container.inject(manager);
    ValidatorFactory vf = container.getInstance(ValidatorFactory.class);
    vf.registerValidator("myValidator", MyValidator.class.getName());
    return manager;
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) Configuration(com.opensymphony.xwork2.config.Configuration) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) DefaultActionInvocation(com.opensymphony.xwork2.DefaultActionInvocation) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

Example 43 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class StrutsResultFactoryTest method testAcceptParams.

public void testAcceptParams() throws Exception {
    // given
    initDispatcherWithConfigs("struts-default.xml");
    StrutsResultFactory builder = (StrutsResultFactory) container.getInstance(ResultFactory.class);
    Map<String, String> params = new HashMap<String, String>();
    params.put("accept", "ok");
    params.put("reject", "bad");
    ResultConfig config = new ResultConfig.Builder("struts", MyResult.class.getName()).addParams(params).build();
    // when
    Result result = builder.buildResult(config, ActionContext.getContext().getContextMap());
    // then
    assertEquals("ok", ((MyResult) result).getAccept());
    assertEquals("ok", ((MyResult) result).getReject());
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HashMap(java.util.HashMap) ResultFactory(com.opensymphony.xwork2.factory.ResultFactory) Result(com.opensymphony.xwork2.Result) ParamNameAwareResult(com.opensymphony.xwork2.result.ParamNameAwareResult)

Example 44 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class ActionMappingParametersInterceptorTest method testParametersNoNestedWrapping.

/**
 * Confirm that ActionMappingParametersInterceptor processing methods avoid
 * nested wrapping of parameters.
 */
public void testParametersNoNestedWrapping() {
    final ActionMappingParametersInterceptor ampi = createActionMappingParametersInterceptor();
    final ActionContext ac = ActionContext.getContext();
    final Map<String, Object> parameters = new HashMap<String, Object>() {

        {
            put("fooKey", "fooValue");
            put("barKey", "barValue");
        }
    };
    final Map<String, Object> moreParameters = new HashMap<String, Object>() {

        {
            put("fooKey2", "fooValue2");
            put("barKey2", "barValue3");
        }
    };
    final Map<String, Object> evenMoreParameters = new HashMap<String, Object>() {

        {
            put("fooKey3", "fooValue3");
            put("barKey3", "barValue3");
        }
    };
    // Set up the initial ActionMapping with parameters in the context
    final HttpParameters wrappedParameters = HttpParameters.create(parameters).build();
    final ActionMapping actionMapping = new ActionMapping("testAction", "testNameSpace", "testMethod", parameters);
    ac.put(ServletActionContext.ACTION_MAPPING, actionMapping);
    // Retrieve parameters and confirm both builder result equality and that the contained Object
    // values of each Parameter element are not of type Parameter (i.e. no double-wrapping).
    // Note: ampi.retrieveParameters() only ever returns the parameters associated with the ActionContext's
    // ActionMapping (not changed directly by ampi.addParametersToContext()).
    final HttpParameters retrievedParameters = ampi.retrieveParameters(ac);
    assertNotNull("retrievedParameters null ?", retrievedParameters);
    // Note: Cannot perform equality test on HttpParameters directly as hashCode values differ even
    // when the contents are equivalent.  Instead we compare size and content equality.
    assertEquals("retrievedParameters size not equal to wrappedParameters size ?", retrievedParameters.size(), wrappedParameters.size());
    for (String parameterName : retrievedParameters.keySet()) {
        Parameter retrievedParameter = retrievedParameters.get(parameterName);
        Parameter wrappedParameter = wrappedParameters.get(parameterName);
        assertNotNull("retrievedParameter is null ?", retrievedParameter);
        assertNotNull("wrappedParameter is null ?", wrappedParameter);
        Object retrievedParameterValue = retrievedParameter.getObject();
        Object wrappedParameterValue = wrappedParameter.getObject();
        assertNotNull("retrievedParameterValue is null ?", retrievedParameterValue);
        assertNotNull("wrappedParameterValue is null ?", wrappedParameterValue);
        assertFalse("retrievedParameterValue is type Parameter ?", retrievedParameterValue instanceof Parameter);
        assertEquals("retrievedParameterValue not equal to wrappedParameterValue ?", retrievedParameterValue, wrappedParameterValue);
    }
    // Set up the initial ActionMapping with (artificially) already-wrapped parameters in the context
    final Map<String, Object> wrappedParametersMap = new HashMap<>(wrappedParameters.size());
    for (String parameterName : wrappedParameters.keySet()) {
        wrappedParametersMap.put(parameterName, wrappedParameters.get(parameterName));
    }
    final ActionMapping actionMappingWrapped = new ActionMapping("testAction", "testNameSpace", "testMethod", wrappedParametersMap);
    ac.put(ServletActionContext.ACTION_MAPPING, actionMappingWrapped);
    // Retrieve parameters and confirm that the contained Object values of each Parameter
    // element are not of type Parameter (i.e. no double-wrapping).
    // Note: ampi.retrieveParameters() only ever returns the parameters associated with the ActionContext's
    // ActionMapping (not changed directly by ampi.addParametersToContext()).
    final HttpParameters retrievedWrappedParameters = ampi.retrieveParameters(ac);
    assertNotNull("retrievedWrappedParameters null ?", retrievedWrappedParameters);
    // Note: Cannot perform equality test on HttpParameters directly as hashCode values differ even
    // when the contents are equivalent.  Instead we compare size and content equality.
    assertEquals("retrievedWrappedParameters size not equal to wrappedParametersMap size ?", retrievedWrappedParameters.size(), wrappedParametersMap.size());
    for (String parameterName : retrievedWrappedParameters.keySet()) {
        Parameter retrievedParameter = retrievedWrappedParameters.get(parameterName);
        Object wrappedParameter = wrappedParametersMap.get(parameterName);
        assertNotNull("retrievedParameter is null ?", retrievedParameter);
        assertNotNull("wrappedParameter is null ?", wrappedParameter);
        assertTrue("wrappedParameter is not a Parameter ?", wrappedParameter instanceof Parameter);
        Object retrievedParameterValue = retrievedParameter.getObject();
        Object wrappedParameterValue = ((Parameter) wrappedParameter).getObject();
        assertNotNull("retrievedParameterValue is null ?", retrievedParameterValue);
        assertNotNull("wrappedParameterValue is null ?", wrappedParameterValue);
        assertFalse("retrievedParameterValue is type Parameter ?", retrievedParameterValue instanceof Parameter);
        assertEquals("retrievedParameterValue not equal to wrappedParameterValue ?", retrievedParameterValue, wrappedParameterValue);
    }
    // Add parameters to the context
    ampi.addParametersToContext(ac, parameters);
    // Retrieve parameters and confirm the expected size and that the contained Object
    // values of each Parameter element are not of type Parameter (i.e. no double-wrapping).
    final HttpParameters retrievedACParameters = ac.getParameters();
    assertNotNull("retrievedACParameters null ?", retrievedACParameters);
    assertEquals("retrievedACParameters size not equal to parameters size ?", retrievedACParameters.size(), parameters.size());
    for (String parameterName : retrievedACParameters.keySet()) {
        Parameter retrievedACParameter = retrievedACParameters.get(parameterName);
        assertNotNull("retrievedACParameter is null ?", retrievedACParameter);
        Object parameterValue = retrievedACParameter.getObject();
        assertNotNull("parameterValue is null ?", parameterValue);
        assertFalse("parameterValue is of type Parameter ?", parameterValue instanceof Parameter);
    }
    // Add additional parameters to the context
    ampi.addParametersToContext(ac, moreParameters);
    // Retrieve parameters and confirm the expected size and that the contained Object
    // values of each Parameter element are not of type Parameter (i.e. no double-wrapping).
    final HttpParameters retrievedACMoreParameters = ac.getParameters();
    assertNotNull("retrievedACMoreParameters null ?", retrievedACMoreParameters);
    assertEquals("retrievedACMoreParameters size not equal to combined size (parameters + moreParameters) ?", retrievedACMoreParameters.size(), parameters.size() + moreParameters.size());
    for (String parameterName : retrievedACMoreParameters.keySet()) {
        Parameter retrievedACMoreParameter = retrievedACMoreParameters.get(parameterName);
        assertNotNull("retrievedACMoreParameter is null ?", retrievedACMoreParameter);
        Object parameterValue = retrievedACMoreParameter.getObject();
        assertNotNull("parameterValue (more) is null ?", parameterValue);
        assertFalse("parameterValue (more) is of type Parameter ?", parameterValue instanceof Parameter);
    }
    // Build some "already wrapped" parameters and attempt to add them to the context
    final HttpParameters evenMoreWrappedParameters = HttpParameters.create(evenMoreParameters).build();
    assertNotNull("evenMoreWrappedParameters null ?", evenMoreWrappedParameters);
    assertEquals("evenMoreWrappedParameters size not equal to evenMoreParameters size ?", evenMoreWrappedParameters.size(), evenMoreParameters.size());
    ampi.addParametersToContext(ac, evenMoreWrappedParameters);
    // Retrieve parameters and confirm the expected size and that the contained Object
    // values of each Parameter element are not of type Parameter (i.e. no double-wrapping).
    final HttpParameters retrievedACEvenMoreParameters = ac.getParameters();
    assertNotNull("retrievedACEvenMoreParameters null ?", retrievedACEvenMoreParameters);
    assertEquals("retrievedACEvenMoreParameters size not equal to combined size (parameters + moreParameters + evenMoreParameters) ?", retrievedACEvenMoreParameters.size(), parameters.size() + moreParameters.size() + evenMoreParameters.size());
    for (String parameterName : retrievedACEvenMoreParameters.keySet()) {
        Parameter retrievedACEvenMoreParameter = retrievedACEvenMoreParameters.get(parameterName);
        assertNotNull("retrievedACEvenMoreParameter is null ?", retrievedACEvenMoreParameter);
        Object parameterValue = retrievedACEvenMoreParameter.getObject();
        assertNotNull("parameterValue (even more) is null ?", parameterValue);
        assertFalse("parameterValue (even more) is of type Parameter ?", parameterValue instanceof Parameter);
    }
}
Also used : ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) HttpParameters(org.apache.struts2.dispatcher.HttpParameters) HashMap(java.util.HashMap) Parameter(org.apache.struts2.dispatcher.Parameter) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

Example 45 with Builder

use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.

the class PropertiesConfigurationProviderTest method testRegister_DifferentLocale.

public void testRegister_DifferentLocale() {
    ContainerBuilder builder = new ContainerBuilder();
    builder.constant("foo", "bar");
    builder.constant("struts.locale", "de_DE");
    PropertiesConfigurationProvider prov = new PropertiesConfigurationProvider();
    prov.register(builder, new LocatableProperties());
    Container container = builder.create(true);
    String localeStr = container.getInstance(String.class, StrutsConstants.STRUTS_LOCALE);
    Locale locale = LocaleUtils.toLocale(localeStr);
    assertNotNull(locale);
    assertEquals("DE", locale.getCountry());
    assertEquals("de", locale.getLanguage());
}
Also used : Locale(java.util.Locale) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties)

Aggregations

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)23 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)21 ServletContext (javax.servlet.ServletContext)21 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)19 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)18 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)17 HashSet (java.util.HashSet)14 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)10 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)4 Configuration (com.opensymphony.xwork2.config.Configuration)4 Container (com.opensymphony.xwork2.inject.Container)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)3 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)3