Skip to content

Commit 017e19c

Browse files
committed
Android:APIJSONTest 新增自动化单元测试 UnitActivity
1 parent 0c68da2 commit 017e19c

10 files changed

Lines changed: 293 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ dependencies {
2626
compile 'com.squareup.okhttp:okhttp:2.1.0'
2727
compile 'com.alibaba:fastjson:1.2.61'
2828
compile 'com.github.APIJSON:apijson-orm:3.9.0'
29+
compile 'com.koushikdutta.async:androidasync:2.+'
2930
}

‎APIJSON-Android/APIJSONTest/app/src/main/AndroidManifest.xml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,17 @@
3434
android:name="apijson.demo.ui.RequestActivity"
3535
android:label="@string/request" />
3636
<activity
37-
android:windowSoftInputMode="adjustPan"
3837
android:name="apijson.demo.ui.AutoActivity"
3938
android:label="@string/auto"
40-
android:screenOrientation="landscape" />
39+
android:screenOrientation="landscape"
40+
android:windowSoftInputMode="adjustPan"
41+
/>
42+
<activity
43+
android:name="apijson.demo.ui.UnitActivity"
44+
android:label="@string/unit"
45+
android:screenOrientation="landscape"
46+
android:windowSoftInputMode="adjustPan"
47+
/>
4148
</application>
4249

4350
</manifest>

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/AutoActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ protected void onCreate(Bundle savedInstanceState) {
8787

8888

8989
public void copy(View v) {
90-
StringUtil.copyText(context, StringUtil.getString(tvAutoResponse));
90+
StringUtil.copyText(context, StringUtil.getString((TextView) v));
9191
}
9292

9393
public void auto(View v) {

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/RequestActivity.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ public void finish() {
283283

284284
@Override
285285
protected void onDestroy() {
286-
super.onDestroy();
287286
isAlive = false;
287+
super.onDestroy();
288288
}
289289

290290
}

‎APIJSON-Android/APIJSONTest/app/src/main/java/apijson/demo/ui/SelectActivity.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ public boolean onLongClick(View v) {
122122
public void toAuto(View v) {
123123
startActivityForResult(AutoActivity.createIntent(context), REQUEST_TO_AUTO);
124124
}
125+
126+
public void toUnit(View v) {
127+
startActivity(UnitActivity.createIntent(context));
128+
}
129+
125130
public void toUpdateLog(View v) {
126131
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
127132
StringUtil.getCorrectUrl("github.com/TommyLemon/APIJSON/commits/master"))));
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package apijson.demo.ui;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.os.Bundle;
7+
import android.text.TextUtils;
8+
import android.view.View;
9+
import android.widget.TextView;
10+
11+
import com.koushikdutta.async.AsyncServer;
12+
import com.koushikdutta.async.http.Headers;
13+
import com.koushikdutta.async.http.body.AsyncHttpRequestBody;
14+
import com.koushikdutta.async.http.server.AsyncHttpServer;
15+
import com.koushikdutta.async.http.server.AsyncHttpServerRequest;
16+
import com.koushikdutta.async.http.server.AsyncHttpServerResponse;
17+
import com.koushikdutta.async.http.server.HttpServerRequestCallback;
18+
19+
import org.json.JSONException;
20+
import org.json.JSONObject;
21+
22+
import apijson.demo.R;
23+
import apijson.demo.StringUtil;
24+
25+
26+
public class UnitActivity extends Activity implements HttpServerRequestCallback {
27+
private static final String TAG = "UnitActivity";
28+
29+
/**
30+
* @param context
31+
* @return
32+
*/
33+
public static Intent createIntent(Context context) {
34+
return new Intent(context, UnitActivity.class);
35+
}
36+
37+
38+
private AsyncHttpServer server = new AsyncHttpServer();
39+
private AsyncServer mAsyncServer = new AsyncServer();
40+
41+
private Activity context;
42+
private boolean isAlive;
43+
44+
private TextView etUnitPort;
45+
private TextView tvUnitRequest;
46+
private TextView tvUnitResponse;
47+
@Override
48+
protected void onCreate(Bundle savedInstanceState) {
49+
super.onCreate(savedInstanceState);
50+
setContentView(R.layout.unit_activity);
51+
context = this;
52+
isAlive = true;
53+
54+
etUnitPort = findViewById(R.id.etUnitPort);
55+
tvUnitRequest = findViewById(R.id.tvUnitRequest);
56+
tvUnitResponse = findViewById(R.id.tvUnitResponse);
57+
etUnitPort.setText(port);
58+
}
59+
60+
61+
private String port = "8080";
62+
public void start(View v) {
63+
v.setEnabled(false);
64+
port = StringUtil.getString(etUnitPort);
65+
66+
etUnitPort.setText(port + " is starting...");
67+
startServer();
68+
69+
etUnitPort.setText(port + " is listening...");
70+
etUnitPort.setEnabled(false);
71+
v.setEnabled(true);
72+
}
73+
public void stop(View v) {
74+
v.setEnabled(false);
75+
server.stop();
76+
mAsyncServer.stop();
77+
78+
etUnitPort.setText(port);
79+
etUnitPort.setEnabled(true);
80+
v.setEnabled(true);
81+
}
82+
83+
84+
public void copy(View v) {
85+
StringUtil.copyText(context, StringUtil.getString((TextView) v));
86+
}
87+
88+
private void startServer() {
89+
// server.addAction("OPTIONS","*", this);
90+
// server.get("/test", new HttpServerRequestCallback() {
91+
// @Override
92+
// public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) {
93+
// response.send("{\"hello\": \"world!\"}");
94+
// }
95+
// });
96+
97+
// server.post("/get", this);
98+
server.addAction("OPTIONS", "[\\d\\D]*", this);
99+
server.get("[\\d\\D]*", this);
100+
// server.post("/get", this);
101+
server.post("[\\d\\D]*", this);
102+
server.listen(mAsyncServer, Integer.valueOf(port));
103+
104+
}
105+
106+
@Override
107+
public void onRequest(final AsyncHttpServerRequest asyncHttpServerRequest, final AsyncHttpServerResponse asyncHttpServerResponse) {
108+
runOnUiThread(new Runnable() {
109+
110+
@Override
111+
public void run() {
112+
if (isAlive) {
113+
tvUnitRequest.setText(StringUtil.getString(asyncHttpServerRequest) + "\n\n\n\n\n" + StringUtil.getString(tvUnitRequest));
114+
}
115+
}
116+
});
117+
118+
AsyncHttpRequestBody requestBody = asyncHttpServerRequest.getBody();
119+
120+
Headers allHeaders = asyncHttpServerResponse.getHeaders();
121+
Headers reqHeaders = asyncHttpServerRequest.getHeaders();
122+
123+
String corsHeaders = reqHeaders.get("access-control-request-headers");
124+
String corsMethod = reqHeaders.get("access-control-request-method");
125+
126+
// if ("OPTIONS".toLowerCase().equals(asyncHttpServerRequest.getMethod().toLowerCase())) {
127+
128+
String origin = reqHeaders.get("origin");
129+
String cookie = reqHeaders.get("cookie");
130+
131+
allHeaders.set("Access-Control-Allow-Origin", TextUtils.isEmpty(origin) ? "*" : origin);
132+
allHeaders.set("Access-Control-Allow-Credentials", "true");
133+
allHeaders.set("Access-Control-Allow-Headers", TextUtils.isEmpty(corsHeaders) ? "*" : corsHeaders);
134+
allHeaders.set("Access-Control-Allow-Methods", TextUtils.isEmpty(corsMethod) ? "*" : corsMethod);
135+
allHeaders.set("Access-Control-Max-Age", "86400");
136+
if (TextUtils.isEmpty(cookie) == false) {
137+
allHeaders.set("Set-Cookie", cookie + System.currentTimeMillis());
138+
}
139+
// }
140+
141+
try {
142+
JSONObject obj = new JSONObject();
143+
obj.put("code", 200);
144+
obj.put("msg", "success");
145+
asyncHttpServerResponse.code(200);
146+
147+
switch (asyncHttpServerRequest.getPath()) {
148+
case "/invokeMethod":
149+
obj.put("api", "/invokeMethod");
150+
break;
151+
case "/listMethod":
152+
obj.put("api", "/listMethod");
153+
break;
154+
}
155+
156+
asyncHttpServerResponse.send("OPTIONS".toLowerCase().equals(asyncHttpServerRequest.getMethod().toLowerCase()) ? new JSONObject() : obj);
157+
// asyncHttpServerResponse.send("application/json; charset=utf-8", obj.toString());
158+
159+
runOnUiThread(new Runnable() {
160+
161+
@Override
162+
public void run() {
163+
if (isAlive) {
164+
tvUnitResponse.setText(StringUtil.getString(asyncHttpServerResponse) + "\n\n\n\n\n" + StringUtil.getString(tvUnitResponse));
165+
}
166+
}
167+
});
168+
} catch (JSONException e) {
169+
e.printStackTrace();
170+
}
171+
}
172+
173+
@Override
174+
protected void onDestroy() {
175+
isAlive = false;
176+
super.onDestroy();
177+
}
178+
179+
180+
}

‎APIJSON-Android/APIJSONTest/app/src/main/res/layout/select_activity.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@
104104
android:onClick="toAuto"
105105
android:text="@string/auto" />
106106

107+
<Button
108+
style="@style/match_wrap"
109+
android:layout_marginTop="30dp"
110+
android:onClick="toUnit"
111+
android:text="@string/unit" />
112+
107113
<Button
108114
android:onClick="toUpdateLog"
109115
style="@style/match_wrap"
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<LinearLayout
2+
xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
4+
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
5+
android:orientation="vertical"
6+
tools:context=".ui.UnitActivity"
7+
>
8+
9+
<ScrollView
10+
style="@style/match_match"
11+
android:layout_weight="1" >
12+
13+
<LinearLayout
14+
style="@style/match_match"
15+
android:orientation="horizontal" >
16+
17+
<EditText
18+
android:id="@+id/tvUnitRequest"
19+
android:layout_width="0dp"
20+
android:layout_height="wrap_content"
21+
android:layout_weight="1"
22+
android:background="@null"
23+
android:gravity="top|left"
24+
android:hint="request"
25+
android:textSize="14sp" />
26+
27+
<View
28+
android:layout_width="1dp"
29+
android:layout_height="match_parent"
30+
android:background="@android:color/white" />
31+
32+
<TextView
33+
android:id="@+id/tvUnitResponse"
34+
android:layout_width="0dp"
35+
android:layout_height="wrap_content"
36+
android:layout_margin="6dp"
37+
android:layout_weight="2"
38+
android:background="@null"
39+
android:gravity="top|left"
40+
android:hint="response"
41+
android:onClick="copy"
42+
android:textSize="14sp" />
43+
</LinearLayout>
44+
</ScrollView>
45+
46+
<LinearLayout
47+
android:layout_height="wrap_content"
48+
android:layout_width="match_parent"
49+
>
50+
51+
<TextView
52+
android:text="Port: "
53+
android:layout_width="wrap_content"
54+
android:layout_height="wrap_content"
55+
/>
56+
57+
<EditText
58+
android:id="@+id/etUnitPort"
59+
android:hint="8080"
60+
android:inputType="number"
61+
android:maxLength="4"
62+
android:minEms="4"
63+
android:digits="1234567890"
64+
android:layout_width="match_parent"
65+
android:layout_height="wrap_content"
66+
android:layout_weight="1"
67+
android:enabled="true"
68+
/>
69+
70+
<Button
71+
android:id="@+id/tvUnitStart"
72+
android:layout_width="wrap_content"
73+
android:layout_height="wrap_content"
74+
android:text="Start"
75+
android:onClick="start"
76+
/>
77+
78+
<Button
79+
android:id="@+id/tvUnitStop"
80+
android:layout_width="wrap_content"
81+
android:layout_height="wrap_content"
82+
android:text="Stop"
83+
android:onClick="stop"
84+
/>
85+
86+
</LinearLayout>
87+
88+
</LinearLayout>

‎APIJSON-Android/APIJSONTest/app/src/main/res/values-en/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
<string name="request_error">There may be something wrong,you can follow by the steps:\n\n1.Check your net connection\n\n2.Check the url whether it\'s an available ipv4 address\n\n3.Long click the [ %1$s ] button to open the request by web browser\n\n4.Check logs outputed on the target server\n\n5.Try again</string>
2323
<string name="apijson_slogan">APIJSON,&#160;let&#160;interfaces&#160;go&#160;to&#160;hell!</string>
2424
<string name="apijson_info">APIJSON&#160;is&#160;a&#160;JSON&#160;Transmission&#160;Structure&#160;Protocol&#8230;</string>
25+
<string name="unit">Unit Test</string>
2526
</resources>

‎APIJSON-Android/APIJSONTest/app/src/main/res/values/strings.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
<string name="request_error">可能出现了一些问题,可以按照以下步骤解决:\n\n1.检查网络连接\n\n2.检查url是否为一个可用的IPV4地址\n\n3.长按[ %1$s ]按钮用浏览器发送请求\n\n4.查看目标服务器上的log\n\n5.再试一次</string>
2525
<string name="apijson_slogan">APIJSON,&#160;let&#160;interfaces&#160;go&#160;to&#160;hell!</string>
2626
<string name="apijson_info">APIJSON&#160;is&#160;a&#160;JSON&#160;Transmission&#160;Structure&#160;Protocol&#8230;</string>
27+
<string name="unit">自动单元测试</string>
2728

2829
</resources>

0 commit comments

Comments
 (0)