简书链接:分享一个我写的自动化模型填充测试工具类日志开关工具类
文章字数:921,阅读全文大约需要3分钟
该类可以实现自动类型判断模拟一些比较合理的数据,比如对于时间字段对于昵称字段对于头像字段的假数据自动填充。

在服务器接口还没出来的情况,要模拟模型数据填充,最快的方法当然是我这个测试工具类了.
用法:

1
2
3
4
5
 if (BuildConfig.DEBUG) {
return TestUtils.createModeListAndFillField(DetailPersonModel.class);
}
//或者
TestUtils.createModelAndFillField(model.class);

有一些不严谨,比如对类型的判断,不够我自己够用,各位自己遇到问题的欢迎改改,我再提交一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219


package cn.qssq666.test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import cn.qssq666.quickdevelopframe.db.ReflectUtils;
import cn.qssq666.quickdevelopframe.utils.Prt;

/**
* Created by qssq on 2017/11/3 [email protected] luozheng
*/

public class TestUtils {
private static final String TAG = "TestUtils";
public static String[] strs = new String[]{"我很快乐", "这他妈是为什么", "妈的智障", "又犯二了", "口头禅是什么东西?", "非诚勿扰", "你好世界", "一万个理由"};
public static String[] images = new String[]{"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509690990292&di=6cd521ecac68df8734bbdea15fb98c35&imgtype=0&src=http%3A%2F%2Fwww.zhlzw.com%2FUploadFiles%2FArticle_UploadFiles%2F201204%2F20120412123914329.jpg"
, "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2618776981,1527535159&fm=27&gp=0.jpg"
, "https://ss0.baidu.com/-Po3dSag_xI4khGko9WTAnF6hhy/image/h%3D220/sign=c043c32e8c44ebf87271633de9f9d736/2e2eb9389b504fc27e414a28eedde71190ef6d85.jpg"
, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509691068813&di=3709acd4a6b08c2e9c481023fee85614&imgtype=0&src=http%3A%2F%2Fxnnews.com.cn%2Fwenyu%2Flxsj%2F201709%2FW020170928748935368882.jpg",
"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509690990292&di=ea8d7cb0c080087258a553a999cf612e&imgtype=0&src=http%3A%2F%2Fwww.zhlzw.com%2FUploadFiles%2FArticle_UploadFiles%2F201204%2F20120412123926750.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1509691069176&di=f81ea7c885ea5447cdd481dd811b5c14&imgtype=0&src=http%3A%2F%2Fimg5.duitang.com%2Fuploads%2Fitem%2F201505%2F27%2F20150527064518_CRne2.jpeg"
};
public static String[] nicknames = new String[]{"张三", "李四", "王老五", "小二", "老二", "阿猫", "蠢猪", "萝莉", "白痴", "混蛋", "二逼青年", "逗比"};
public static int[] ints = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
public static long[] durations = new long[]{1000 * 60 + (1000 * 50), 1000 * 90 + (5000 * 50), 1000 * 60 + (1000 * 550), 1000 * 60 + (1000 * 55550)};
public static int[] numbers = new int[]{1000, 2111111, 33434343, 333433444, 33434335, 3343436, 7334334, 3333338};
public static String[] urls = new String[]{"http://baidu.com", "http://qssq666.cn"};
private static long[] times = new long[]{
new Date().getTime(), addTime(Calendar.DAY_OF_WEEK, 1), addTime(Calendar.DAY_OF_WEEK, 55), addTime(Calendar.DAY_OF_WEEK, 10), addTime(Calendar.DAY_OF_WEEK, 133)

};

public static Calendar getCalendarInstance() {
if (instance == null) {
instance = Calendar.getInstance(Locale.CHINA);
}
return instance;
}

private static Calendar instance;

static {

instance.setTimeInMillis(new Date().getTime());

}

private static long setTime(int field, int value) {
getCalendarInstance().set(field, value);
return getCalendarInstance().getTimeInMillis();
}

private static long addTime(int field, int value) {
getCalendarInstance().add(field, value);
return getCalendarInstance().getTimeInMillis();
}


public static String getRandomImage() {
return images[new Random().nextInt(images.length)];
}

public static String getRandomName() {
return nicknames[new Random().nextInt(nicknames.length)];
}

public static String getRandomTitle() {
return strs[new Random().nextInt(strs.length)];
}

public static String getRandomUrl() {
return urls[new Random().nextInt(urls.length)];
}

public static int getRandomInts() {
return ints[new Random().nextInt(ints.length)];
}

public static <T> void createModelAndFillField(List list, Class<T> srcClass, int count) {
for (int i = 0; i < count; i++) {
list.add(createModelAndFillField(srcClass));
}
}

public static <T> List<T> createModeListAndFillField(Class<T> srcClass) {
return createModeListAndFillField(srcClass, 5);
}

public static <T> List<T> createModeListAndFillField(Class<T> srcClass, int count) {
ArrayList list = new ArrayList<>();
createModelAndFillField(list, srcClass, count);
return list;
}

public static <T> T createModelAndFillField(Class<T> srcClass) {
T srcObject = null;
try {
srcObject = srcClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
}
boolean isChildClass = true;
// ArrayList<String> hasCallMethods = new ArrayList<>();
while (srcClass != null && srcClass != Object.class) {
Field[] fieldsSrc = srcClass.getDeclaredFields();

Prt.w(TAG, "正在给类自动赋值:" + srcClass.getName());

for (int i = 0; i < fieldsSrc.length; i++) {
Field fieldSrc = fieldsSrc[i];
if (fieldSrc.isSynthetic()) {
continue;
}
if ("serialVersionUID".equals(fieldSrc.getName())) {
continue;
}
//public static final long com.buyao.buliao.bean.DetailPersonModel.serialVersionUID
String getMethodName = ReflectUtils.getGetName(fieldSrc);

String setMethodName = ReflectUtils.getSetName(fieldSrc);
/* if (hasCallMethods.contains(setMethodName)) {//理论上不存在。没有字段就自然不会调用子类方法。

Prt.w(TAG, "忽略父类,因为子类已经赋值 " + setMethodName);
continue;
}*/

try {

Method getMethod = srcClass.getMethod(getMethodName);
Method setMethod = srcClass.getMethod(setMethodName, getMethod.getReturnType());

Object invoke = null;
switch (fieldSrc.getName()) {
case "face":
case "image":
invoke = TestUtils.getRandomImage();
break;
case "id":
case "userid":
invoke = TestUtils.getRandomInts();
break;
case "url":
invoke = TestUtils.getRandomUrl();
break;
case "name":
case "nickname":
case "username":
case "uname":
invoke = TestUtils.getRandomName();
case "title":
case "content":
invoke = TestUtils.getRandomTitle();
break;
case "regtime":
case "endime":
invoke = TestUtils.getRandomTime();
break;
default:
String name = fieldSrc.getName();
if (name.contains("count") || name.contains("num")) {
invoke = TestUtils.getRandomNumber();
} else if (name.contains("duration")) {
invoke = getRandomDuration();
}
break;

}

Prt.d(TAG, "" + getMethodName + ":" + invoke + ",setMethod:" + setMethod);
if (invoke != null) {
setMethod.invoke(srcObject, invoke);
}


} catch (NoSuchMethodException e) {
e.printStackTrace();

} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

}

srcClass = (Class<T>) srcClass.getSuperclass();

}

Prt.d(TAG, "创建测试对象结果:" + srcObject);
return srcObject;
}

public static int getRandomNumber() {
return numbers[new Random().nextInt(numbers.length)];
}

public static long getRandomDuration() {
return durations[new Random().nextInt(durations.length)];
}

public static long getRandomTime() {
return times[new Random().nextInt(times.length)];
}

}

日志开关工具类用于解决Log在编译后一直打印问题优化性能也方便控制.使用时把所有的Log.替换为Prt. 然后把导入包的也批量替换一下就完成了优化。怎么替换需要技巧的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
*
* .::::.
* .::::::::.
* ::::::::::: by [email protected]
* ..:::::::::::'
* '::::::::::::'
* .::::::::::
* '::::::::::::::..
* ..::::::::::::.
* ``::::::::::::::::
* ::::``:::::::::' .:::.
* ::::' ':::::' .::::::::.
* .::::' :::: .:::::::'::::.
* .:::' ::::: .:::::::::' ':::::.
* .::' :::::.:::::::::' ':::::.
* .::' ::::::::::::::' ``::::.
* ...::: ::::::::::::' ``::.
* ```` ':. ':::::::::' ::::..
* '.:::::' ':'````..
*
*/

package cn.qssq666.quickdevelopframe.utils;

import android.util.Log;

/**
* Created by qssq on 2017/8/8 [email protected]
*/

public class Prt {

private static final int ERROR_FLAG = -1;
private static final String ERROR_STR = "Prt";
public static boolean LOGGABLE = true;

public static int v(String tag, String msg) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.v(tag, msg);
}

public static int v(String tag, String msg, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.v(tag, msg, tr);
}

public static int d(String tag, String msg) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.d(tag, msg);
}

public static int d(String tag, String msg, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.d(tag, msg, tr);
}

public static int i(String tag, String msg) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.i(tag, msg);
}

public static int i(String tag, String msg, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.i(tag, msg, tr);
}

public static int w(String tag, String msg) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.w(tag, msg);
}

public static int w(String tag, String msg, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.w(tag, msg, tr);
}

public static int w(String tag, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.w(tag, tr);

}

public static int e(String tag, String msg) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.e(tag, msg);
}

public static int e(String tag, String msg, Throwable tr) {
if (!LOGGABLE) {
return ERROR_FLAG;
}
return Log.e(tag, msg, tr);
}


public static String getStackTraceString(Throwable t) {
if (!LOGGABLE) {
return ERROR_STR;
}
return Log.getStackTraceString(t);
}
}