@@ -8,18 +8,52 @@ import org.scalatest.{Matchers, WordSpec}
8
8
import org .scalatest .prop .PropertyChecks
9
9
10
10
class PackageSpec extends WordSpec with Matchers with PropertyChecks {
11
- " read " should {
11
+ " readFromStream " should {
12
12
" parse JSON from the provided input stream" in {
13
13
readFromStream(getClass.getResourceAsStream(" user_api_response.json" ))(codec) shouldBe user
14
14
}
15
+ " throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
16
+ assert(intercept[JsonParseException ](readFromStream(new ByteArrayInputStream (httpMessage))(codec)).getMessage ==
17
+ """ expected '{', offset: 0x00000000, buf:
18
+ | +-------------------------------------------------+
19
+ | | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
20
+ |+----------+-------------------------------------------------+------------------+
21
+ || 00000000 | 48 54 54 50 2f 31 2e 30 20 32 30 30 20 4f 4b 0a | HTTP/1.0 200 OK. |
22
+ || 00000010 | 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 | Content-Type: ap |
23
+ || 00000020 | 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0a 43 | plication/json.C |
24
+ |+----------+-------------------------------------------------+------------------+""" .stripMargin)
25
+ }
26
+ " throw an exception in case of the provided params are invalid" in {
27
+ intercept[NullPointerException ](readFromStream(new ByteArrayInputStream (compactJson))(null ))
28
+ intercept[NullPointerException ](readFromStream(null )(codec))
29
+ }
30
+ }
31
+ " readFromArray" should {
15
32
" parse JSON from the byte array" in {
16
33
readFromArray(compactJson)(codec) shouldBe user
17
34
}
35
+ " throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
36
+ assert(intercept[JsonParseException ](readFromArray(httpMessage)(codec)).getMessage ==
37
+ """ expected '{', offset: 0x00000000, buf:
38
+ | +-------------------------------------------------+
39
+ | | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
40
+ |+----------+-------------------------------------------------+------------------+
41
+ || 00000000 | 48 54 54 50 2f 31 2e 30 20 32 30 30 20 4f 4b 0a | HTTP/1.0 200 OK. |
42
+ || 00000010 | 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 61 70 | Content-Type: ap |
43
+ || 00000020 | 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 0a 43 | plication/json.C |
44
+ |+----------+-------------------------------------------------+------------------+""" .stripMargin)
45
+ }
46
+ " throw an exception in case of the provided params are invalid" in {
47
+ intercept[NullPointerException ](readFromArray(compactJson)(null ))
48
+ intercept[NullPointerException ](readFromArray(null .asInstanceOf [Array [Byte ]])(codec))
49
+ }
50
+ }
51
+ " readFromSubArray" should {
18
52
" parse JSON from the byte array within specified positions" in {
19
53
readFromSubArray(httpMessage, 66 , httpMessage.length)(codec) shouldBe user
20
54
}
21
55
" throw an exception if cannot parse input with message containing input offset & hex dump of affected part" in {
22
- assert(intercept[JsonParseException ](readFromArray (httpMessage)(codec)).getMessage ==
56
+ assert(intercept[JsonParseException ](readFromSubArray (httpMessage, 0 , httpMessage.length )(codec)).getMessage ==
23
57
""" expected '{', offset: 0x00000000, buf:
24
58
| +-------------------------------------------------+
25
59
| | 0 1 2 3 4 5 6 7 8 9 a b c d e f |
@@ -30,13 +64,8 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
30
64
|+----------+-------------------------------------------------+------------------+""" .stripMargin)
31
65
}
32
66
" throw an exception in case of the provided params are invalid" in {
33
- intercept[NullPointerException ](readFromArray(compactJson)(null ))
34
- intercept[NullPointerException ](readFromStream(new ByteArrayInputStream (compactJson))(null ))
35
67
intercept[NullPointerException ](readFromSubArray(httpMessage, 66 , httpMessage.length)(null ))
36
- intercept[NullPointerException ](readFromArray(null .asInstanceOf [Array [Byte ]])(codec))
37
68
intercept[NullPointerException ](readFromSubArray(null .asInstanceOf [Array [Byte ]], 0 , 50 )(codec))
38
- intercept[NullPointerException ](readFromStream(null .asInstanceOf [InputStream ])(codec))
39
- intercept[NullPointerException ](readFromStream(new ByteArrayInputStream (compactJson), null )(codec))
40
69
intercept[NullPointerException ](readFromSubArray(httpMessage, 66 , httpMessage.length, null )(codec))
41
70
assert(intercept[ArrayIndexOutOfBoundsException ](readFromSubArray(httpMessage, 50 , 200 )(codec))
42
71
.getMessage.contains(" `to` should be positive and not greater than `buf` length" ))
@@ -98,9 +127,7 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
98
127
intercept[NullPointerException ](scanJsonArrayFromStream(inputStream)(npe)(codec))
99
128
}
100
129
}
101
- " write" should {
102
- val buf = new Array [Byte ](150 )
103
-
130
+ " writeToStream" should {
104
131
" serialize an object to the provided output stream" in {
105
132
val out1 = new ByteArrayOutputStream ()
106
133
writeToStream(user, out1)(codec)
@@ -109,10 +136,25 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
109
136
writeToStream(user, out2, WriterConfig (indentionStep = 2 ))(codec)
110
137
out2.toString(" UTF-8" ) shouldBe toString(prettyJson)
111
138
}
139
+ " throw i/o exception in case of the provided params are invalid" in {
140
+ intercept[NullPointerException ](writeToStream(user, new ByteArrayOutputStream ())(null ))
141
+ intercept[NullPointerException ](writeToStream(user, null .asInstanceOf [OutputStream ])(codec))
142
+ intercept[NullPointerException ](writeToStream(user, new ByteArrayOutputStream (), null )(codec))
143
+ }
144
+ }
145
+ " writeToArray" should {
112
146
" serialize an object to a new instance of byte array" in {
113
147
toString(writeToArray(user)(codec)) shouldBe toString(compactJson)
114
148
toString(writeToArray(user, WriterConfig (indentionStep = 2 ))(codec)) shouldBe toString(prettyJson)
115
149
}
150
+ " throw i/o exception in case of the provided params are invalid" in {
151
+ intercept[NullPointerException ](writeToArray(user)(null ))
152
+ intercept[NullPointerException ](writeToArray(user, null .asInstanceOf [WriterConfig ])(codec))
153
+ }
154
+ }
155
+ " writeToPreallocatedArray" should {
156
+ val buf = new Array [Byte ](150 )
157
+
116
158
" serialize an object to the provided byte array from specified position" in {
117
159
val from1 = 10
118
160
val to1 = writeToPreallocatedArray(user, buf, from1)(codec)
@@ -126,13 +168,8 @@ class PackageSpec extends WordSpec with Matchers with PropertyChecks {
126
168
.getMessage.contains(" `buf` length exceeded" ))
127
169
}
128
170
" throw i/o exception in case of the provided params are invalid" in {
129
- intercept[NullPointerException ](writeToArray(user)(null ))
130
- intercept[NullPointerException ](writeToStream(user, new ByteArrayOutputStream ())(null ))
131
171
intercept[NullPointerException ](writeToPreallocatedArray(user, buf, 0 )(null ))
132
- intercept[NullPointerException ](writeToStream(user, null .asInstanceOf [OutputStream ])(codec))
133
172
intercept[NullPointerException ](writeToPreallocatedArray(user, null , 50 )(codec))
134
- intercept[NullPointerException ](writeToArray(user, null .asInstanceOf [WriterConfig ])(codec))
135
- intercept[NullPointerException ](writeToStream(user, new ByteArrayOutputStream (), null )(codec))
136
173
intercept[NullPointerException ](writeToPreallocatedArray(user, buf, 0 , null )(codec))
137
174
assert(intercept[ArrayIndexOutOfBoundsException ](writeToPreallocatedArray(user, new Array [Byte ](10 ), 50 )(codec))
138
175
.getMessage.contains(" `from` should be positive and not greater than `buf` length" ))
0 commit comments