1+ package com .hjq .http .test ;
2+
3+ import android .content .Context ;
4+
5+ import androidx .test .platform .app .InstrumentationRegistry ;
6+
7+ import com .google .gson .Gson ;
8+ import com .google .gson .GsonBuilder ;
9+ import com .google .gson .internal .bind .TypeAdapters ;
10+ import com .hjq .http .demo .http .json .BooleanTypeAdapter ;
11+ import com .hjq .http .demo .http .json .DoubleTypeAdapter ;
12+ import com .hjq .http .demo .http .json .FloatTypeAdapter ;
13+ import com .hjq .http .demo .http .json .IntegerTypeAdapter ;
14+ import com .hjq .http .demo .http .json .ListTypeAdapter ;
15+ import com .hjq .http .demo .http .json .LongTypeAdapter ;
16+ import com .hjq .http .demo .http .json .StringTypeAdapter ;
17+
18+ import org .junit .After ;
19+ import org .junit .Before ;
20+ import org .junit .Test ;
21+
22+ import java .io .ByteArrayOutputStream ;
23+ import java .io .IOException ;
24+ import java .io .InputStream ;
25+ import java .util .List ;
26+
27+ /**
28+ * Example local unit test, which will execute on the development machine (host).
29+ *
30+ * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31+ */
32+ public class JsonUnitTest {
33+
34+ private Gson mGson ;
35+
36+ @ Before
37+ public void onTestBefore () {
38+ mGson = new GsonBuilder ()
39+ .registerTypeAdapterFactory (TypeAdapters .newFactory (String .class , new StringTypeAdapter ()))
40+ .registerTypeAdapterFactory (TypeAdapters .newFactory (boolean .class , Boolean .class , new BooleanTypeAdapter ()))
41+ .registerTypeAdapterFactory (TypeAdapters .newFactory (int .class , Integer .class , new IntegerTypeAdapter ()))
42+ .registerTypeAdapterFactory (TypeAdapters .newFactory (long .class , Long .class , new LongTypeAdapter ()))
43+ .registerTypeAdapterFactory (TypeAdapters .newFactory (float .class , Float .class , new FloatTypeAdapter ()))
44+ .registerTypeAdapterFactory (TypeAdapters .newFactory (double .class , Double .class , new DoubleTypeAdapter ()))
45+ .registerTypeHierarchyAdapter (List .class , new ListTypeAdapter ())
46+ .create ();
47+ }
48+
49+ @ Test
50+ public void onSpecification () {
51+ Context context = InstrumentationRegistry .getInstrumentation ().getContext ();
52+ String json = getAssetsString (context , "Specification.json" );
53+ mGson .fromJson (json , JsonBean .class );
54+ }
55+
56+ @ Test
57+ public void onNoSpecification () {
58+ Context context = InstrumentationRegistry .getInstrumentation ().getContext ();
59+ String json = getAssetsString (context , "NoSpecification.json" );
60+ mGson .fromJson (json , JsonBean .class );
61+ }
62+
63+ @ After
64+ public void onTestAfter () {
65+ mGson = null ;
66+ }
67+
68+ /**
69+ * 获取资产目录下面文件的字符串
70+ */
71+ private static String getAssetsString (Context context , String file ) {
72+ try {
73+ InputStream inputStream = context .getAssets ().open (file );
74+ ByteArrayOutputStream outStream = new ByteArrayOutputStream ();
75+ byte [] buffer = new byte [512 ];
76+ int length ;
77+ while ((length = inputStream .read (buffer )) != -1 ) {
78+ outStream .write (buffer , 0 , length );
79+ }
80+ outStream .close ();
81+ inputStream .close ();
82+ return outStream .toString ();
83+ } catch (IOException e ) {
84+ e .printStackTrace ();
85+ return null ;
86+ }
87+ }
88+ }
0 commit comments