Do not instantiate JsonParser 56/99356/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Jan 2022 11:42:49 +0000 (12:42 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 18 Jan 2022 11:43:40 +0000 (12:43 +0100)
JsonParser.parse() should not be used, but rather its equivalent static
methods should be invoked. Migrate tests to do so, fixing a few warnings
in the process.

Change-Id: I6bbfad51c584a5dc0e752bcbb1ed2ec3cdff483f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/AnyXmlSupportTest.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/Bug5446Test.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/Bug7246Test.java
codec/yang-data-codec-gson/src/test/java/org/opendaylight/yangtools/yang/data/codec/gson/TestUtils.java

index 0ffcb02723d85dc98c3091134b509d418ecadc97..284fe600be0d5627bc09d6d773ddb799e15d239a 100644 (file)
@@ -14,7 +14,6 @@ import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadText
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadXmlToNormalizedNodes;
 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.normalizedNodesToJsonString;
 
-import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
 import com.google.gson.stream.JsonReader;
 import java.io.File;
@@ -77,10 +76,7 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
         final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
                 SchemaPath.ROOT);
 
-        final JsonParser parser = new JsonParser();
-        final JsonElement expected = parser.parse(inputJson);
-        final JsonElement actual = parser.parse(serializationResult);
-        assertTrue(expected.equals(actual));
+        assertEquals(JsonParser.parseString(inputJson), JsonParser.parseString(serializationResult));
     }
 
     @Test
@@ -112,11 +108,8 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
         final String serializationResult = normalizedNodesToJsonString(transformedInput, schemaContext,
                 SchemaPath.ROOT);
 
-        final JsonParser parser = new JsonParser();
-        final String expectedJson = loadTextFile("/bug8927/json/composite.json");
-        final JsonElement expected = parser.parse(expectedJson);
-        final JsonElement actual = parser.parse(serializationResult);
-        assertTrue(expected.equals(actual));
+        assertEquals(JsonParser.parseString(loadTextFile("/bug8927/json/composite.json")),
+            JsonParser.parseString(serializationResult));
     }
 
     @Test
@@ -153,11 +146,9 @@ public class AnyXmlSupportTest extends AbstractComplexJsonTest {
                 .childByArg(new NodeIdentifier(QName.create("bug8927.test", "2017-01-01", "foo")));
         assertNotNull(data);
         final String jsonOutput = normalizedNodesToJsonString(data, schemaContext, SchemaPath.ROOT);
-        final JsonParser parser = new JsonParser();
-        final JsonElement expextedJson = parser.parse(new FileReader(
-            new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8));
-        final JsonElement serializedJson = parser.parse(jsonOutput);
-        assertEquals(expextedJson, serializedJson);
+        assertEquals(JsonParser.parseReader(new FileReader(
+            new File(getClass().getResource(expectedJsonFile).toURI()), StandardCharsets.UTF_8)),
+            JsonParser.parseString(jsonOutput));
     }
 
     private static DOMSource getParsedAnyXmlValue(final NormalizedNode transformedInput, final QName anyxmlName) {
index b1aed2332f3a7f6c931232d0934454f291487cef..c8ec8a2a39aa690790d9d8c390783e6fda81448d 100644 (file)
@@ -64,9 +64,8 @@ public class Bug5446Test {
         final Writer writer = new StringWriter();
         final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, rootNode);
 
-        final JsonParser parser = new JsonParser();
-        final JsonElement serializedJson = parser.parse(jsonOutput);
-        final JsonElement expextedJson = parser.parse(new FileReader(new File(getClass().getResource(
+        final JsonElement serializedJson = JsonParser.parseString(jsonOutput);
+        final JsonElement expextedJson = JsonParser.parseReader(new FileReader(new File(getClass().getResource(
                 "/bug5446/json/foo.json").toURI()), StandardCharsets.UTF_8));
 
         assertEquals(expextedJson, serializedJson);
@@ -78,10 +77,10 @@ public class Bug5446Test {
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), SchemaPath.ROOT, null,
             JsonWriterFactory.createJsonWriter(writer, 2));
-        final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
-        nodeWriter.write(inputStructure);
+        try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+            nodeWriter.write(inputStructure);
+        }
 
-        nodeWriter.close();
         return writer.toString();
     }
 
index 7d25227d46a6dc9f0083a6f863097b7c181a7bc2..46af2239580eab4cecde24aa48ef41fb1dbbf731 100644 (file)
@@ -9,7 +9,6 @@ package org.opendaylight.yangtools.yang.data.codec.gson;
 
 import static org.junit.Assert.assertEquals;
 
-import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
 import java.io.File;
 import java.io.FileReader;
@@ -38,11 +37,6 @@ public class Bug7246Test {
     public void test() throws Exception {
         final EffectiveModelContext schemaContext =
                 YangParserTestUtils.parseYangResource("/bug7246/yang/rpc-test.yang");
-        final JsonParser parser = new JsonParser();
-        final JsonElement expextedJson = parser
-                .parse(new FileReader(new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()),
-                    StandardCharsets.UTF_8));
-
         final ContainerNode inputStructure = ImmutableContainerNodeBuilder.create()
                 .withNodeIdentifier(new NodeIdentifier(qN("my-name")))
                 .withChild(ImmutableNodes.leafNode(new NodeIdentifier(qN("my-name")), "my-value")).build();
@@ -50,9 +44,10 @@ public class Bug7246Test {
         final Writer writer = new StringWriter();
         final String jsonOutput = normalizedNodeToJsonStreamTransformation(schemaContext, rootPath, writer,
                 inputStructure);
-        final JsonElement serializedJson = parser.parse(jsonOutput);
 
-        assertEquals(expextedJson, serializedJson);
+        assertEquals(JsonParser.parseReader(new FileReader(
+            new File(getClass().getResource("/bug7246/json/expected-output.json").toURI()), StandardCharsets.UTF_8)),
+            JsonParser.parseString(jsonOutput));
     }
 
     private static QName qN(final String localName) {
@@ -66,10 +61,9 @@ public class Bug7246Test {
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
                 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), path,
                 XMLNamespace.of(NS), JsonWriterFactory.createJsonWriter(writer, 2));
-        final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
-        nodeWriter.write(inputStructure);
-
-        nodeWriter.close();
+        try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+            nodeWriter.write(inputStructure);
+        }
         return writer.toString();
     }
 }
index 13251a2f94316abb3c209df66bd352c01ab3997c..9271d0ee777a92b92303e2c33408da2d4e1452f7 100644 (file)
@@ -68,11 +68,10 @@ public final class TestUtils {
         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
                 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext), rootPath, null,
                 JsonWriterFactory.createJsonWriter(writer, 2));
-        final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
-        nodeWriter.write(data);
-        nodeWriter.close();
-        final String serializationResult = writer.toString();
-        return serializationResult;
+        try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
+            nodeWriter.write(data);
+        }
+        return writer.toString();
     }
 
     static JsonObject childObject(final JsonObject jsonObject, final String... names) {
@@ -106,7 +105,7 @@ public final class TestUtils {
     }
 
     static JsonObject resolveCont1(final String jsonOutput) {
-        final JsonElement rootElement = new JsonParser().parse(jsonOutput);
+        final JsonElement rootElement = JsonParser.parseString(jsonOutput);
         assertTrue(rootElement.isJsonObject());
         final JsonObject rootObject = rootElement.getAsJsonObject();
         final JsonObject cont1 = childObject(rootObject, "complexjson:cont1", "cont1");
@@ -114,7 +113,7 @@ public final class TestUtils {
     }
 
     static JsonObject resolveCont2(final String jsonOutput) {
-        final JsonElement rootElement = new JsonParser().parse(jsonOutput);
+        final JsonElement rootElement = JsonParser.parseString(jsonOutput);
         assertTrue(rootElement.isJsonObject());
         final JsonObject rootObject = rootElement.getAsJsonObject();
         final JsonObject cont2 = childObject(rootObject, "complexjson:cont2", "cont2");