2017.6.2:重复计数自动化测试

来源:互联网 发布:比价的那个软件是什么 编辑:程序博客网 时间:2024/06/08 02:10

com.zte.ums.common.pm.common.EjbAnnotationRegister
ejb注解注册,即将标注ejbController注解的controller、method写入对应的map集合:

Map<controllerName, bean> ejbControllerMapMap<controllerName, Map<String, String>> ejbControllerMethodMap
// spring中可直接通过此方法获取指定注解的controller实例Map<String, Object> beanMap = applicationContext.getBeansWithAnnotation(EjbController.class);

package com.zte.ums.common.pm;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import javax.annotation.Resource;import org.apache.commons.lang3.StringUtils;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.zte.ums.common.pm.common.EjbAnnotationRegister;import com.zte.ums.common.pm.exception.PmRfException;import com.zte.ums.uep.api.util.DebugPrn;@RequestMapping("/")@RestControllerpublic class EjbTester {    private static final DebugPrn dmsg = new DebugPrn(EjbTester.class.getName());    private static final String PARAMETER_PREFIX = "parameter_";    @Resource    private EjbAnnotationRegister ejbAnnotationRegister;    @RequestMapping(value = "ejb", method = RequestMethod.POST)    public Object testEjbRest(            @RequestParam(value = "controller", required = true) String controllerName,            @RequestParam(value = "method", required = true) String methodName,            @RequestBody String content) throws PmRfException {        try {        // 解析附加信息,并转化为xml字符串            Object[] parameterObjs = parseParameters(content);            return testEjb(controllerName, methodName, parameterObjs);        } catch (Exception e) {            dmsg.error(e.getMessage(), e);            throw new PmRfException(e.getLocalizedMessage(), e);        }    }    private Object[] parseParameters(String content) throws Exception {        ObjectMapper objectMapper = new ObjectMapper();        if (StringUtils.isBlank(content)) {            return null;        }        Object[] parameterObjs = null;        JsonNode jsonNode = objectMapper.readTree(content);        List<Object> parameterList = new ArrayList<Object>();        int i = 1;        while (true) {            String key = PARAMETER_PREFIX + i;            JsonNode paramJsonNode = jsonNode.get(key);            if (paramJsonNode == null) {                break;            }            parameterList.add(objectMapper.writeValueAsString(paramJsonNode));            i++;        }        if (parameterList.isEmpty() == false) {            parameterObjs = parameterList.toArray(new Object[parameterList.size()]);        }        return parameterObjs;    }    public Object testEjb( String controllerName, String methodName, Object... parameterObjs) throws PmRfException {        try {            Object ejbController = ejbAnnotationRegister.getEjbControllerBean(controllerName);            if (ejbController == null) {                throw new PmRfException("未注册的EJB Controller。" + controllerName);            }            String ejbMethodName = ejbAnnotationRegister.getEjbMethodName(controllerName, methodName);            if (StringUtils.isBlank(ejbMethodName)) {                throw new PmRfException("未注册的EJB Method。" + methodName);            }            Class<?>[] paraClazzes = getClazzes(parameterObjs);            Method method = ejbController.getClass().getMethod(ejbMethodName, paraClazzes);            return method.invoke(ejbController, parameterObjs);        } catch (PmRfException e) {            throw e;        } catch (Exception e) {            dmsg.error(e.getMessage(), e);            throw new PmRfException(e.getLocalizedMessage(), e);        }    }    private Class<?>[] getClazzes(Object... objs) {        if (objs == null) {            return null;        } else {            Class<?>[] paraClazzes = new Class[objs.length];            for (int i = 0; i < paraClazzes.length; i++) {                paraClazzes[i] = objs[i].getClass();            }            return paraClazzes;        }    }    public static void main(String[] args) {        try {            ObjectMapper objectMapper = new ObjectMapper();            try {objectMapper.readTree("[{\"id\":\"1\"},{\"id\":\"2\"}]");                List<String> readTree = objectMapper.readValue("[{\"id\":\"1\"},{\"id\":\"2\"}]", new TypeReference<List<String>>(){});            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }//            ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");//读取bean.xml中的内容//           //            EjbTester ejbTester = ctx.getBean(EjbTester.class);//            Object result = ejbTester.testEjb("taskQueryTester", "queryTaskInfoById", "[ 100 ]");//            EjbController.class.getGenericInterfaces();//            System.out.println(result);        } catch (Exception e) {            e.printStackTrace();        }    }}

加载配置文件的方式:

package com.zte.ums.common.pm.properties;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.Properties;import org.apache.commons.lang3.StringUtils;import org.springframework.core.io.Resource;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import com.zte.ums.uep.api.util.DebugPrn;public class TesterConfig {    private static final DebugPrn dmsg = new DebugPrn(TesterConfig.class.getName());    private static final String PROPERTIES_FILE_NAME = "tester-config.properties";    private static final String UEP_HOST = "ems.host";    public static final String EJB_PM_PORT = "ejb.pm.port";    public static final String EJB_FM_PORT = "ejb.fm.port";    private Properties properties;    private static TesterConfig instance = null;    private TesterConfig() {        properties = new Properties();        try {            properties.load(new FileInputStream(getConfigFilePath()));        } catch (IOException e) {            dmsg.error(e.getMessage(), e);        }    }    public static TesterConfig getInstance() {        if (instance == null) {            synchronized (TesterConfig.class) {                if (instance == null) {                    instance = new TesterConfig();                }            }        }        return instance;    }    public String getProperty(String propertyName) {        String value = properties.getProperty(propertyName);        return StringUtils.trimToEmpty(value);    }    public File getConfigFilePath() throws IOException {        PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();        Resource resource = patternResolver.getResource(PROPERTIES_FILE_NAME);        return new File(resource.getURI());    }    public String getUepHost() {        return getProperty(UEP_HOST);    }}
原创粉丝点击