8000 [Issue 79] JacksonFactory.createJsonParser should throw NullPointerEx… · robccan/google-http-java-client@958ac45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 958ac45

Browse files
author
Yaniv Inbar
committed
[Issue 79] JacksonFactory.createJsonParser should throw NullPointerException on null input
http://codereview.appspot.com/5935043/
1 parent f1ee5f4 commit 958ac45

File tree

12 files changed

+78
-1256
lines changed

12 files changed

+78
-1256
lines changed

google-http-client/src/main/java/com/google/api/client/json/JsonFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public abstract class JsonFactory {
4141
* @param in input stream
4242
* @return new instance of a low-level JSON parser
4343
* @throws IOException if failed
44+
* @throws NullPointerException if {@code in} is {@code null}
4445
*/
4546
public abstract JsonParser createJsonParser(InputStream in) throws IOException;
4647

@@ -50,6 +51,7 @@ public abstract class JsonFactory {
5051
* @param value string value
5152
* @return new instance of a low-level JSON parser
5253
* @throws IOException if failed
54+
* @throws NullPointerException if {@code value} is {@code null}
5355
*/
5456
public abstract JsonParser createJsonParser(String value) throws IOException;
5557

@@ -59,6 +61,7 @@ public abstract class JsonFactory {
5961
* @param reader reader
6062
* @return new instance of a low-level JSON parser
6163
* @throws IOException if failed
64+
* @throws NullPointerException if {@code reader} is {@code null}
6265
*/
6366
public abstract JsonParser createJsonParser(Reader reader) throws IOException;
6467

google-http-client/src/main/java/com/google/api/client/json/JsonGenerator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ public abstract class JsonGenerator {
9696
/** Writes a JSON numeric value that has already been encoded properly. */
9797
public abstract void writeNumber(String encodedValue) throws IOException;
9898

99-
/** Serializes the given JSON value object. */
99+
/**
100+
* Serializes the given JSON value object, or if {@code value} is {@code null} it does no
101+
* serialization.
102+
*/
100103
public final void serialize(Object value) throws IOException {
101104
if (value == null) {
102105
return;

google-http-client/src/main/java/com/google/api/client/json/jackson/JacksonFactory.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.client.json.JsonGenerator;
2020
import com.google.api.client.json.JsonParser;
2121
import com.google.api.client.json.JsonToken;
22+
import com.google.common.base.Preconditions;
2223

2324
import java.io.IOException;
2425
import java.io.InputStream;
@@ -60,16 +61,19 @@ public JsonGenerator createJsonGenerator(Writer writer) throws IOException {
6061

6162
@Override
6263
public JsonParser createJsonParser(Reader reader) throws IOException {
64+
Preconditions.checkNotNull(reader);
6365
return new JacksonParser(this, factory.createJsonParser(reader));
6466
}
6567

6668
@Override
6769
public JsonParser createJsonParser(InputStream in) throws IOException {
70+
Preconditions.checkNotNull(in);
6871
return new JacksonParser(this, factory.createJsonParser(in));
6972
}
7073

7174
@Override
7275
public JsonParser createJsonParser(String value) throws IOException {
76+
Preconditions.checkNotNull(value);
7377
return new JacksonParser(this, factory.createJsonParser(value));
7478
}
7579

google-http-client/src/main/java/com/google/api/client/testing/http/MockLowLevelHttpRequest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ public class MockLowLevelHttpRequest extends LowLevelHttpRequest {
4545
/** Map of header name to values. */
4646
private final Map<String, List<String>> headersMap = new HashMap<String, List<String>>();
4747

48+
/**
49+
* HTTP response to return from {@link #execute()}.
50+
*
51+
* <p>
52+
* By default this is a new instance of {@link MockLowLevelHttpResponse}.
53+
* </p>
54+
*/
55+
private MockLowLevelHttpResponse response = new MockLowLevelHttpResponse();
56+
4857
public MockLowLevelHttpRequest() {
4958
}
5059

@@ -68,7 +77,7 @@ public void addHeader(String name, String value) {
6877

6978
@Override
7079
public LowLevelHttpResponse execute() throws IOException {
71-
return new MockLowLevelHttpResponse();
80+
return response;
7281
}
7382

7483
@Override
@@ -112,4 +121,25 @@ public MockLowLevelHttpRequest setUrl(String url) {
112121
public HttpContent getContent() {
113122
return content;
114123
}
124+
125+
/**
126+
* HTTP response to return from {@link #execute()}.
127+
*
128+
* @since 1.8
129+
*/
130+
public MockLowLevelHttpResponse getResponse() {
131+
return response;
132+
}
133+
134+
/**
135+
* Sets the HTTP response to return from {@link #execute()}.
136+
*
137+
* <p>
138+
* By default this is a new instance of {@link MockLowLevelHttpResponse}.
139+
* </p>
140+
*/
141+
public MockLowLevelHttpRequest setResponse(MockLowLevelHttpResponse response) {
142+
this.response = response;
143+
return this;
144+
}
115145
}

google-http-client/src/main/java/com/google/api/client/testing/http/MockLowLevelHttpResponse.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,18 @@ public class MockLowLevelHttpResponse extends LowLevelHttpResponse {
6767
* @param value header value
6868
*/
6969
public void addHeader(String name, String value) {
70-
headerNames.add(name);
71-
headerValues.add(value);
70+
headerNames.add(Preconditions.checkNotNull(name));
71+
headerValues.add(Preconditions.checkNotNull(value));
7272
}
7373

7474
/**
7575
* Sets the response content to the given content string.
7676
*
77-
* @param stringContent content string
77+
* @param stringContent content string or {@code null} for none
7878
*/
7979
public MockLowLevelHttpResponse setContent(String stringContent) {
80-
content = new ByteArrayInputStream(StringUtils.getBytesUtf8(stringContent));
80+
content = stringContent == null ? null : new ByteArrayInputStream(StringUtils.getBytesUtf8(
81+
stringContent));
8182
return this;
8283
}
8384

@@ -218,6 +219,7 @@ public MockLowLevelHttpResponse setContentEncoding(String contentEncoding) {
218219
*/
219220
public MockLowLevelHttpResponse setContentLength(long contentLength) {
220221
this.contentLength = contentLength;
222+
Preconditions.checkArgument(contentLength >= 0);
221223
return this;
222224
}
223225

@@ -232,6 +234,7 @@ public MockLowLevelHttpResponse setContentLength(long contentLength) {
232234
*/
233235
public MockLowLevelHttpResponse setStatusCode(int statusCode) {
234236
this.statusCode = statusCode;
237+
Preconditions.checkArgument(statusCode >= 0);
235238
return this;
236239
}
237240

google-http-client/src/main/java/com/google/api/client/testing/json/AbstractJsonFactoryTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
import java.io.ByteArrayOutputStream;
3636
import java.io.IOException;
37+
import java.io.InputStream;
38+
import java.io.Reader;
3739
import java.math.BigDecimal;
3840
import java.math.BigInteger;
3941
import java.util.ArrayList;
@@ -1092,4 +1094,31 @@ public final void testToPrettyString_FeedApproximate() throws Exception {
10921094
String prettyString = factory.toPrettyString(feed);
10931095
assertEquals(JSON_FEED, factory.toString(factory.fromString(prettyString, Feed.class)));
10941096
}
1097+
1098+
public void testParser_nullInputStream() throws IOException {
1099+
try {
1100+
newFactory().createJsonParser((InputStream) null);
1101+
fail("expected " + NullPointerException.class);
1102+
} catch (NullPointerException e) {
1103+
// expected
1104+
}
1105+
}
1106+
1107+
public void testParser_nullString() throws IOException {
1108+
try {
1109+
newFactory().createJsonParser((String) null);
1110+
fail("expected " + NullPointerException.class);
1111+
} catch (NullPointerException e) {
1112+
// expected
1113+
}
1114+
}
1115+
1116+
public void testParser_nullReader() throws IOException {
1117+
try {
1118+
newFactory().createJsonParser((Reader) null);
1119+
fail("expected " + NullPointerException.class);
1120+
} catch (NullPointerException e) {
1121+
// expected
1122+
}
1123+
}
10951124
}

google-http-client/src/main/java/com/google/api/client/testing/json/AbstractJsonGeneratorTest.java

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0