zoukankan      html  css  js  c++  java
  • Mock数据使用的Util

    package com.xxx.common.util;
    
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    import java.util.*;
    
    /**
     * 测试某些非空数据使用
     * @author xw
     */
    public class MockUtil {
        private static double curD = 100.1;
    
        private static float curF = 10000.2f;
    
        private static int curInt = 1;
    
        private static long curL = 1000;
    
        private static int curS = 1;
    
        private static String[] rs = new String[] { "A", "C", "D", "E", "F", "J", "H", "I", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "c", "d", "e", "f", "j", "h", "i", "k", "l", "m", "n", "o", "p", "q",
                "r", "s", "t", "u", "v", "w", "x", "y", "z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "f" };
    
        private static String getFieldName(String methodName) {
            String s = "set";
            String ups = methodName.toLowerCase();
            String key = null;
            if (ups.indexOf(s) != -1) {
                key = ups.substring(s.length());
            }
            return (key == null) ? null : key;
        }
    
        public static <T> T getJavaBean(Class<T> c) {
    
            T object = null;
            List<Method> allMethods = new ArrayList<Method>();
            List<Field> allFields = new ArrayList<Field>();
            try {
                object = c.newInstance();
                Method[] methods = c.getDeclaredMethods();
                Field[] fields = c.getDeclaredFields();
                allMethods.addAll(Arrays.asList(methods));
                allFields.addAll(Arrays.asList(fields));
                Class superClass = c.getSuperclass();
                while (superClass != null) {
                    allMethods.addAll(Arrays.asList(superClass.getDeclaredMethods()));
                    allFields.addAll(Arrays.asList(superClass.getDeclaredFields()));
                    superClass = superClass.getSuperclass();
                }
                for (Method m : allMethods) {
                    if (m.getName().indexOf("set") != -1) {
                        String field = getFieldName(m.getName());
                        String type = "string";
                        for (Field f : allFields) {
                            if (f.getName().toLowerCase().equals(field)) {
                                type = f.getType().getSimpleName();
                                break;
                            }
                        }
                        m.invoke(object, new Object[] { getValue(type) });
                    }
                }
    
            } catch (Exception e) {
                // e.printStackTrace();
            }
            return object;
        }
    
        public static String getRand(int size) {
            Random random = new Random();
            String rvs = "";
            for (int i = 0; i < size; i++) {
                int status = random.nextInt(size);
                if (status < rs.length && status > 0) {
                    rvs += rs[status];
                }
                else {
                    rvs += "A";
                }
    
            }
            return rvs;
        }
    
        private static Object getValue(String s) {
            Object temp = null;
            String st = s.toLowerCase();
            Random random = new Random(10010);
            if (st.equals("int") || st.equals("integer")) {
                temp = curInt;
                curInt++;
            }
            else if (st.equals("long")) {
                temp = curL;
                curL++;
            }
            else if (st.equals("string")) {
                temp = curS + getRand(6);
                curS++;
            }
            else if (st.equals("double")) {
                temp = curD;
                curD++;
            }
            else if (st.equals("float")) {
                temp = curF;
                curF++;
            }
            else if (st.equals("boolean")) {
                temp = random.nextBoolean();
            }
            else if (st.equals("date")) {
                temp = new Date();
            }
            return (temp == null) ? null : (temp);
        }
    }
    

      mock 数据使用工具

  • 相关阅读:
    C# DataGridView导出到Excel
    面试时,怎样“谈薪”?
    7月11号 列表类型的内置方法及应用
    6月1号 字符编码
    文件相关知识
    Django框架之第二篇app注册、静态文件配置、form表单提交、pycharm链接数据库、Django使用mysql数据库、表字段的增删改查、表数据的增删改查
    函数相关知识
    Django框架之第三篇(路由层)有名/无名分组、反向解析、路由分发、名称空间、伪静态、图书管理系统表设计
    初识Django
    Django框架之第五篇(模型层)单表操作(增删改查)、单表查询和必知必会13条、单表查询之双下划线、Django ORM常用字段和字段参数和关系字段
  • 原文地址:https://www.cnblogs.com/tietazhan/p/9777643.html
Copyright © 2011-2022 走看看