Skip to content

Commit 645913c

Browse files
committed
Add abstract JSON test and tests for Jackson2 and gson (#665)
1 parent 41f8324 commit 645913c

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.api.client.json.gson;
18+
19+
import com.google.api.client.json.JsonFactory;
20+
import com.google.api.client.test.json.AbstractJsonParserTest;
21+
22+
public class GsonParserTest extends AbstractJsonParserTest {
23+
24+
@Override
25+
protected JsonFactory newJsonFactory() {
26+
return new GsonFactory();
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.api.client.json.jackson2;
18+
19+
import com.google.api.client.json.JsonFactory;
20+
import com.google.api.client.test.json.AbstractJsonParserTest;
21+
22+
public class JacksonParserTest extends AbstractJsonParserTest {
23+
24+
@Override
25+
protected JsonFactory newJsonFactory() {
26+
return new JacksonFactory();
27+
}
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.api.client.test.json;
18+
19+
import com.google.api.client.json.GenericJson;
20+
import com.google.api.client.json.JsonFactory;
21+
import com.google.api.client.json.JsonObjectParser;
22+
import java.io.ByteArrayInputStream;
23+
import java.io.IOException;
24+
import java.io.InputStream;
25+
import java.math.BigDecimal;
26+
import java.nio.charset.StandardCharsets;
27+
import junit.framework.TestCase;
28+
29+
public abstract class AbstractJsonParserTest extends TestCase {
30+
31+
protected abstract JsonFactory newJsonFactory();
32+
33+
private static String TEST_JSON =
34+
"{\"strValue\": \"bar\", \"intValue\": 123, \"boolValue\": false}";
35+
private static String TEST_JSON_BIG_DECIMAL = "{\"bigDecimalValue\": 1559341956102}";
36+
37+
public void testParse_basic() throws IOException {
38+
JsonObjectParser parser = new JsonObjectParser(newJsonFactory());
39+
InputStream inputStream = new ByteArrayInputStream(TEST_JSON.getBytes(StandardCharsets.UTF_8));
40+
GenericJson json = parser.parseAndClose(inputStream, StandardCharsets.UTF_8, GenericJson.class);
41+
42+
assertTrue(json.get("strValue") instanceof String);
43+
assertEquals("bar", json.get("strValue"));
44+
assertTrue(json.get("intValue") instanceof BigDecimal);
45+
assertEquals(new BigDecimal(123), json.get("intValue"));
46+
assertTrue(json.get("boolValue") instanceof Boolean);
47+
assertEquals(Boolean.FALSE, json.get("boolValue"));
48+
}
49+
50+
public void testParse_bigDecimal() throws IOException {
51+
JsonObjectParser parser = new JsonObjectParser(newJsonFactory());
52+
InputStream inputStream =
53+
new ByteArrayInputStream(TEST_JSON_BIG_DECIMAL.getBytes(StandardCharsets.UTF_8));
54+
GenericJson json = parser.parseAndClose(inputStream, StandardCharsets.UTF_8, GenericJson.class);
55+
56+
assertTrue(json.get("bigDecimalValue") instanceof BigDecimal);
57+
assertEquals(new BigDecimal("1559341956102"), json.get("bigDecimalValue"));
58+
}
59+
}

0 commit comments

Comments
 (0)