Skip to content

Commit c7c772c

Browse files
committed
Android:APIJSONTest 调通远程执行单元测试
1 parent 9c58574 commit c7c772c

5 files changed

Lines changed: 1523 additions & 30 deletions

File tree

‎APIJSON-Android/APIJSONTest/app/build.gradle‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55
buildToolsVersion '29.0.0'
66
defaultConfig {
77
applicationId "apijson.demo"
8-
minSdkVersion 20
9-
targetSdkVersion 22
8+
minSdkVersion 26
9+
targetSdkVersion 26
1010
versionCode 1
1111
versionName "1.0"
1212
}
@@ -18,6 +18,10 @@ android {
1818
}
1919
productFlavors {
2020
}
21+
compileOptions {
22+
targetCompatibility 1.8
23+
sourceCompatibility 1.8
24+
}
2125
}
2226

2327
dependencies {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package apijson.demo.server;
2+
3+
public class MathUtil {
4+
5+
public static long plus(long a, long b) {
6+
return a + b;
7+
}
8+
public static double plus(double a, double b) {
9+
return a + b;
10+
}
11+
public static double plus(Number a, Number b) {
12+
return plus(a.doubleValue(), b.doubleValue());
13+
}
14+
15+
public static long minus(long a, long b) {
16+
return a - b;
17+
}
18+
public static double minus(double a, double b) {
19+
return a - b;
20+
}
21+
public static double minus(Number a, Number b) {
22+
return minus(a.doubleValue(), b.doubleValue());
23+
}
24+
25+
public static long multiply(long a, long b) {
26+
return a * b;
27+
}
28+
public static double multiply(double a, double b) {
29+
return a * b;
30+
}
31+
public static double multiply(Number a, Number b) {
32+
return multiply(a.doubleValue(), b.doubleValue());
33+
}
34+
35+
public static double divide(long a, long b) {
36+
return divide((double) a, (double) b); //直接相除会白自动强转为 long,1/2 = 0 ! a / b;
37+
}
38+
public static double divide(double a, double b) {
39+
return a / b;
40+
}
41+
public static double divide(Number a, Number b) {
42+
return divide(a.doubleValue(), b.doubleValue());
43+
}
44+
45+
public static long pow(long a, long b) {
46+
return (long) Math.pow(a, b);
47+
}
48+
public static double pow(double a, double b) {
49+
return Math.pow(a, b);
50+
}
51+
public static double pow(Number a, Number b) {
52+
return pow(a.doubleValue(), b.doubleValue());
53+
}
54+
55+
public static double sqrt(long a) {
56+
return Math.sqrt(a);
57+
}
58+
public static double sqrt(double a) {
59+
return Math.sqrt(a);
60+
}
61+
public static double sqrt(Number a) {
62+
return sqrt(a.doubleValue());
63+
}
64+
65+
public static Long computeAsync(long a, long b, TestInterface callback) {
66+
callback.setData("Mock outer interface success!");
67+
Boolean sort = callback.sort();
68+
if (sort != null && sort && a > b) {
69+
callback.minusAsId(b, a);
70+
}
71+
else {
72+
callback.minusAsId(a, b);
73+
}
74+
return callback.getId();
75+
}
76+
77+
public static Number computeAsync(long a, long b, Callback callback) {
78+
79+
new Thread(new Runnable() {
80+
81+
@Override
82+
public void run() {
83+
try {
84+
Thread.sleep(3000);
85+
} catch (Exception e) {}
86+
87+
Boolean sort = callback.sort();
88+
if (sort != null && sort && a > b) {
89+
callback.minusAsId(b, a);
90+
} else {
91+
callback.minusAsId(a, b);
92+
}
93+
94+
callback.setData("Mock inner interface success!");
95+
}
96+
}).start();
97+
98+
return callback.getId();
99+
}
100+
101+
102+
public static <D> D computeAsync(long a, long b, D d, Callback1<D> callback) {
103+
callback.minusAsId(a, b);
104+
callback.setData(d);
105+
return callback.getData();
106+
}
107+
108+
public static <L, D> Callback2<L, D> computeAsync(L a, L b, D d, Callback2<L, D> callback) {
109+
callback.setA(a);
110+
callback.setB(b);
111+
callback.setData(d);
112+
callback.append(a, b);
113+
return callback;
114+
}
115+
116+
//需要用 $ 隔开内部类与它所在的类 apijson.demo.server.MathUtil$Callback
117+
public interface Callback2<L, D> {
118+
void setId(L id);
119+
L getId();
120+
121+
void setData(D data);
122+
D getData();
123+
124+
void setA(L a);
125+
L getA();
126+
127+
void setB(L b);
128+
L getB();
129+
130+
default String append(L a, L b) {
131+
return "a=" + a + "; b=" + b;
132+
}
133+
}
134+
135+
public interface Callback1<D> extends Callback2<Long, D> {
136+
Boolean sort();
137+
138+
default void minusAsId(long a, long b) {
139+
setA(a);
140+
setB(b);
141+
setId(a + b);
142+
}
143+
}
144+
145+
public interface Callback extends Callback1<String> {
146+
static long currentTime() {
147+
return System.currentTimeMillis();
148+
}
149+
150+
@Override
151+
default void minusAsId(long a, long b) {
152+
System.out.println("minusAsId startTime: " + currentTime());
153+
setA(a);
154+
setB(b);
155+
setId(a + b);
156+
System.out.println("minusAsId endTime: " + currentTime());
157+
}
158+
}
159+
160+
}

0 commit comments

Comments
 (0)