mockconfigurationprovider.java

来自「在Struts2中的jar包xwork的源代码.版本为2.0.7」· Java 代码 · 共 158 行

JAVA
158
字号
/* * Copyright (c) 2002-2003 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.config.providers;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionChainResult;import com.opensymphony.xwork2.ModelDrivenAction;import com.opensymphony.xwork2.SimpleAction;import com.opensymphony.xwork2.config.Configuration;import com.opensymphony.xwork2.config.ConfigurationException;import com.opensymphony.xwork2.config.ConfigurationProvider;import com.opensymphony.xwork2.config.entities.ActionConfig;import com.opensymphony.xwork2.config.entities.PackageConfig;import com.opensymphony.xwork2.config.entities.ResultConfig;import com.opensymphony.xwork2.config.entities.InterceptorMapping;import com.opensymphony.xwork2.inject.ContainerBuilder;import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;import com.opensymphony.xwork2.interceptor.ParametersInterceptor;import com.opensymphony.xwork2.interceptor.StaticParametersInterceptor;import com.opensymphony.xwork2.mock.MockResult;import com.opensymphony.xwork2.util.location.LocatableProperties;import com.opensymphony.xwork2.validator.ValidationInterceptor;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Properties;/** * MockConfigurationProvider provides a simple configuration class without the need for xml files, etc. for simple testing. * * @author $author$ * @version $Revision: 1213 $ */public class MockConfigurationProvider implements ConfigurationProvider {    public static final String FOO_ACTION_NAME = "foo";    public static final String MODEL_DRIVEN_PARAM_TEST = "modelParamTest";    public static final String MODEL_DRIVEN_PARAM_FILTER_TEST  = "modelParamFilterTest";    public static final String PARAM_INTERCEPTOR_ACTION_NAME = "parametersInterceptorTest";    public static final String VALIDATION_ACTION_NAME = "validationInterceptorTest";    public static final String VALIDATION_ALIAS_NAME = "validationAlias";    public static final String VALIDATION_SUBPROPERTY_NAME = "subproperty";    private Configuration configuration;    /**     * Allows the configuration to clean up any resources used     */    public void destroy() {    }        public void init(Configuration config) {        this.configuration = config;    }    public void loadPackages() {                PackageConfig defaultPackageContext = new PackageConfig("defaultPackage");        HashMap params = new HashMap();        params.put("bar", "5");        HashMap results = new HashMap();        HashMap successParams = new HashMap();        successParams.put("actionName", "bar");        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));        ActionConfig fooActionConfig = new ActionConfig(null, SimpleAction.class, params, results, null);        fooActionConfig.addResultConfig(new ResultConfig(Action.ERROR, MockResult.class.getName()));        fooActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig(FOO_ACTION_NAME, fooActionConfig);        results = new HashMap();        successParams = new HashMap();        successParams.put("actionName", "bar");        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));        List interceptors = new ArrayList();        interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));        ActionConfig paramInterceptorActionConfig = new ActionConfig(null, SimpleAction.class, null, results, interceptors);        paramInterceptorActionConfig.addResultConfig(new ResultConfig(Action.ERROR, MockResult.class.getName()));        paramInterceptorActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig(PARAM_INTERCEPTOR_ACTION_NAME, paramInterceptorActionConfig);        interceptors = new ArrayList();        interceptors.add(new InterceptorMapping("model", new ModelDrivenInterceptor()));        interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));        ActionConfig modelParamActionConfig = new ActionConfig(null, ModelDrivenAction.class, null, null, interceptors);        modelParamActionConfig.addResultConfig(new ResultConfig(Action.SUCCESS, MockResult.class.getName()));        modelParamActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig(MODEL_DRIVEN_PARAM_TEST, modelParamActionConfig);                //List paramFilterInterceptor=new ArrayList();        //paramFilterInterceptor.add(new ParameterFilterInterC)        //ActionConfig modelParamFilterActionConfig = new ActionConfig(null, ModelDrivenAction.class, null, null, interceptors);                results = new HashMap();        successParams = new HashMap();        successParams.put("actionName", "bar");        results.put("success", new ResultConfig("success", ActionChainResult.class.getName(), successParams));        interceptors = new ArrayList();        interceptors.add(new InterceptorMapping("static-params", new StaticParametersInterceptor()));        interceptors.add(new InterceptorMapping("model", new ModelDrivenInterceptor()));        interceptors.add(new InterceptorMapping("params", new ParametersInterceptor()));        interceptors.add(new InterceptorMapping("validation", new ValidationInterceptor()));        //Explicitly set an out-of-range date for DateRangeValidatorTest        params = new HashMap();        params.put("date", new java.util.Date(2002 - 1900, 11, 20));        //Explicitly set an out-of-range double for DoubleRangeValidatorTest        params.put("percentage", new Double(100.0123));        ActionConfig validationActionConfig = new ActionConfig(null, SimpleAction.class, params, results, interceptors);        validationActionConfig.addResultConfig(new ResultConfig(Action.ERROR, MockResult.class.getName()));        validationActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig(VALIDATION_ACTION_NAME, validationActionConfig);        defaultPackageContext.addActionConfig(VALIDATION_ALIAS_NAME, validationActionConfig);        defaultPackageContext.addActionConfig(VALIDATION_SUBPROPERTY_NAME, validationActionConfig);        params = new HashMap();        params.put("percentage", new Double(1.234567));        ActionConfig percentageActionConfig = new ActionConfig(null, SimpleAction.class, params, results, interceptors);        percentageActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig("percentage", percentageActionConfig);        // We need this actionconfig to be the final destination for action chaining        ActionConfig barActionConfig = new ActionConfig(null, SimpleAction.class, null, null, null);        barActionConfig.addResultConfig(new ResultConfig(Action.ERROR, MockResult.class.getName()));        barActionConfig.setPackageName("defaultPackage");        defaultPackageContext.addActionConfig("bar", barActionConfig);        configuration.addPackageConfig("defaultPackage", defaultPackageContext);    }    /**     * Tells whether the ConfigurationProvider should reload its configuration     *     * @return false     */    public boolean needsReload() {        return false;    }    public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {        // TODO Auto-generated method stub            }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?