2
2
3
3
import android .app .Activity ;
4
4
import android .content .Context ;
5
+ import android .text .TextUtils ;
6
+
7
+ import com .google .gson .Gson ;
8
+ import com .google .gson .GsonBuilder ;
9
+ import com .google .gson .JsonSyntaxException ;
10
+ import com .orhanobut .hawk .GsonParser ;
5
11
6
12
import junit .framework .TestCase ;
7
13
13
19
import org .robolectric .RobolectricGradleTestRunner ;
14
20
import org .robolectric .annotation .Config ;
15
21
22
+ import java .lang .reflect .Type ;
23
+
24
+ import dalvik .annotation .TestTarget ;
25
+
16
26
import static org .assertj .core .api .Assertions .assertThat ;
17
27
18
28
/**
@@ -30,6 +40,28 @@ public HawkBuilderTest() {
30
40
builder = Hawk .init (context );
31
41
}
32
42
43
+ class CustomParser implements Parser {
44
+ private final Gson gson ;
45
+
46
+ public CustomParser (Gson gson ) {
47
+ this .gson = gson ;
48
+ }
49
+
50
+ @ Override
51
+ public <T > T fromJson (String content , Type type ) throws JsonSyntaxException {
52
+ if (TextUtils .isEmpty (content )) {
53
+ return null ;
54
+ }
55
+ return gson .fromJson (content , type );
56
+ }
57
+
58
+ @ Override
59
+ public String toJson (Object body ) {
60
+ return gson .toJson (body );
61
+ }
62
+
63
+ }
64
+
33
65
@ Before
34
66
public void setup () {
35
67
builder = new HawkBuilder (context );
@@ -149,6 +181,14 @@ public void testDefaultParser() {
149
181
assertThat (builder .getParser ()).isInstanceOf (GsonParser .class );
150
182
}
151
183
184
+ @ Test
185
+ public void testCustomParser () {
186
+ CustomParser parser = new CustomParser (new Gson ());
187
+ builder .setParser (parser )
188
+ .build ();
189
+ assertThat (builder .getParser ()).isInstanceOf (CustomParser .class );
190
+ }
191
+
152
192
@ Test
153
193
public void testDefaultEncoded () {
154
194
builder .build ();
0 commit comments