• 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 数据使用工具

  • 相关阅读:
    如何给wordpress外部链接自动添加nofollow
    wordpress如何批量关闭旧日志留言功能
    如何一次把所有wordpress插件都禁用了
    sql批量获取wordpress所有留言者的邮件地址
    wordpress数据库优化-关闭日志修订
    wordpress数据库优化wp_posts表 OPTIMIZE
    sql批量删除wordpress所有日志修订revision
    sql删除wordpress没用的postmeta记录
    wordpress如何删除没有文章的tags标签
    批量删除wordpress垃圾评论留言
  • 原文地址:https://www.cnblogs.com/tietazhan/p/9777643.html
Copyright © 2020-2023  润新知