Skip to content

Commit

Permalink
Merge pull request #42945 from Shadow-Devil/fix-deprecated-json-parse…
Browse files Browse the repository at this point in the history
…r-init

[Refactoring] Replace deprecated usage of `new JsonParser`
  • Loading branch information
gimantha committed Sep 12, 2024
2 parents 81e438e + a3f0809 commit 16c9572
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ public static void replaceTemplateName(Path path, String templateName, String pa
Files.write(path, content.getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
throw new RuntimeException("Error while replacing template name in module import statements: " + path, e);
throw new RuntimeException(
"Error while replacing template name in module import statements: " + path, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@
*/
public class BLangNodeTransformerTest {
private static final Logger LOGGER = LoggerFactory.getLogger(BLangNodeTransformerTest.class);
private static final JsonParser JSON_PARSER = new JsonParser();
public static final Path RES_DIR = Paths.get("src/test/resources/node-tree/").toAbsolutePath();

private final JsonParser jsonParser = new JsonParser();

@Test(dataProvider = "testTransformationTestProvider", enabled = false)
public void testTransformation(String configName, String sourcePackage)
throws IOException, IllegalAccessException {
Expand All @@ -67,7 +64,7 @@ public void testTransformation(String configName, String sourcePackage)
BLangPackage bLangPackage = pkg.getCompilation().defaultModuleBLangPackage();
Set<Class<?>> skipList = new HashSet<>();
String jsonStr = generateFieldJson(bLangPackage.getClass(), bLangPackage, skipList, bLangPackage);
JsonObject actualJsonObj = jsonParser.parse(jsonStr).getAsJsonObject();
JsonObject actualJsonObj = JsonParser.parseString(jsonStr).getAsJsonObject();

// Fix test cases replacing expected using responses
// if (!expJsonObj.equals(actualJsonObj)) {
Expand Down Expand Up @@ -242,7 +239,7 @@ private static JsonObject fileContentAsObject(String filePath) {
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
}
return JSON_PARSER.parse(contentAsString).getAsJsonObject();
return JsonParser.parseString(contentAsString).getAsJsonObject();
}

private static Project loadProject(String sourceFilePath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void testWithImportDeclRecovery() {
ImportDeclarationNode importDeclNode = NodeParser.parseImportDeclaration("%import foobar/foo.bar.baz qux;;");
Assert.assertEquals(importDeclNode.kind(), SyntaxKind.IMPORT_DECLARATION);
Assert.assertTrue(importDeclNode.hasDiagnostics());
Assert.assertEquals(importDeclNode.toString(), " INVALID[%]import foobar/foo.bar.baz MISSING[as]qux; INVALID[;]");
Assert.assertEquals(
importDeclNode.toString(), " INVALID[%]import foobar/foo.bar.baz MISSING[as]qux; INVALID[;]");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
*/
public class FileUtils {

private static final JsonParser JSON_PARSER = new JsonParser();

private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);

public static final Path RES_DIR = Paths.get("src/test/resources/").toAbsolutePath();
Expand All @@ -54,7 +52,7 @@ public static JsonObject fileContentAsObject(String filePath) {
} catch (IOException ex) {
LOGGER.error(ex.getMessage());
}
return JSON_PARSER.parse(contentAsString).getAsJsonObject();
return JsonParser.parseString(contentAsString).getAsJsonObject();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
public class CodeLensTest {
private final Path functionsBalPath = FileUtils.RES_DIR.resolve("codelens").resolve("functions.bal");
private Endpoint serviceEndpoint;
private static final JsonParser JSON_PARSER = new JsonParser();
private static final Logger log = LoggerFactory.getLogger(CodeLensTest.class);
private static final Gson GSON = new Gson();

Expand All @@ -65,9 +64,9 @@ private void testCodeLenses(String expectedConfigName, String response) throws I
String expected = getExpectedValue(expectedConfigName);

List<CodeLens> expectedItemList = getFlattenItemList(
JSON_PARSER.parse(expected).getAsJsonObject().getAsJsonArray("result"));
JsonParser.parseString(expected).getAsJsonObject().getAsJsonArray("result"));
List<CodeLens> responseItemList = getFlattenItemList(
JSON_PARSER.parse(response).getAsJsonObject().getAsJsonArray("result"));
JsonParser.parseString(response).getAsJsonObject().getAsJsonArray("result"));

boolean isSubList = getStringListForEvaluation(responseItemList).containsAll(
getStringListForEvaluation(expectedItemList));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public abstract class AbstractCommandExecutionTest {

private final Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();

private final JsonParser parser = new JsonParser();

private final Path resourcesPath = new File(getClass().getClassLoader().getResource("command").getFile()).toPath();

private static final Logger log = LoggerFactory.getLogger(AbstractCommandExecutionTest.class);
Expand Down Expand Up @@ -126,7 +124,7 @@ private JsonObject getCommandResponse(List<Object> args, String command) {
List argsList = argsToJson(args);
ExecuteCommandParams params = new ExecuteCommandParams(command, argsList);
String response = TestUtil.getExecuteCommandResponse(params, this.serviceEndpoint).replace("\\r\\n", "\\n");
JsonObject responseJson = parser.parse(response).getAsJsonObject();
JsonObject responseJson = JsonParser.parseString(response).getAsJsonObject();
responseJson.remove("id");
return responseJson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public class PullModuleExecutorTest {
private Endpoint serviceEndpoint;

private final Gson gson = new Gson();
private final JsonParser parser = new JsonParser();

private final Path sourcesDir = new File(getClass().getClassLoader()
.getResource("command").getFile()).toPath();
Expand Down Expand Up @@ -128,7 +127,7 @@ private JsonObject getCommandResponse(List<Object> args, String command) {
ExecuteCommandParams params = new ExecuteCommandParams(command, argsList);
String response = TestUtil.getExecuteCommandResponse(params, this.serviceEndpoint)
.replace("\\r\\n", "\\n");
JsonObject responseJson = parser.parse(response).getAsJsonObject();
JsonObject responseJson = JsonParser.parseString(response).getAsJsonObject();
responseJson.remove("id");
return responseJson;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public class DiagnosticsTest {

private final Path testRoot = FileUtils.RES_DIR.resolve("diagnostics");

private final JsonParser parser = new JsonParser();

private final Gson gson = new Gson();

private final LanguageServerContext serverContext = new LanguageServerContextImpl();
Expand All @@ -82,7 +80,7 @@ public void test(String config) throws IOException, WorkspaceDocumentException {
JsonObject configJsonObject = FileUtils.fileContentAsObject(configJsonPath);

String response = this.getResponse(configJsonObject);
JsonObject responseJson = parser.parse(response).getAsJsonObject();
JsonObject responseJson = JsonParser.parseString(response).getAsJsonObject();
JsonObject responseDiags = unifyResponse(responseJson);
JsonObject expectedDiags = configJsonObject.get("items").getAsJsonObject();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public class DocumentSymbolTest {

private Endpoint serviceEndpoint;

private JsonParser parser = new JsonParser();

private Path sourcesPath = new File(getClass().getClassLoader().getResource("docsymbol").getFile()).toPath();

private static final Logger log = LoggerFactory.getLogger(DocumentSymbolTest.class);
Expand All @@ -64,7 +62,7 @@ public void testDocumentSymbol(String config, String source) throws IOException
JsonObject configJsonObject = FileUtils.fileContentAsObject(configJsonPath);
JsonObject expected = configJsonObject.get("expected").getAsJsonObject();
String response = TestUtil.getDocumentSymbolResponse(this.serviceEndpoint, sourcePath.toString());
JsonObject jsonResponse = parser.parse(response).getAsJsonObject();
JsonObject jsonResponse = JsonParser.parseString(response).getAsJsonObject();
JsonArray result = jsonResponse.getAsJsonArray("result");
TestUtil.closeDocument(serviceEndpoint, sourcePath);
for (JsonElement element : result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class DocumentSymbolTest {
private Path configRoot;
private Path sourceRoot;
protected Gson gson = new Gson();
protected JsonParser parser = new JsonParser();
protected Endpoint serviceEndpoint;

@BeforeClass
Expand All @@ -44,7 +43,7 @@ public void test(String configPath) throws IOException {
String response = TestUtil.getDocumentSymbolResponse(serviceEndpoint, sourcePath.toString());
TestUtil.closeDocument(serviceEndpoint, sourcePath);
JsonArray expected = resultJson.getAsJsonArray("result");
JsonArray actual = parser.parse(response).getAsJsonObject().getAsJsonArray("result");
JsonArray actual = JsonParser.parseString(response).getAsJsonObject().getAsJsonArray("result");
Assert.assertEquals(actual, expected);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public class ExecutorPositionsTest {
private static final String RANGE = "range";
private static final String START_LINE = "startLine";

private static final JsonParser JSON_PARSER = new JsonParser();

private Path resourceRoot;
private Endpoint serviceEndpoint;

Expand Down Expand Up @@ -82,8 +80,8 @@ private void compareResponse(String expected, String response) {
Path expectedPath = this.resourceRoot.resolve("expected").resolve(expected + ".json");
JsonArray expectedJsonArray =
FileUtils.fileContentAsObject(expectedPath.toAbsolutePath().toString()).getAsJsonArray(EXEC_POSITIONS);
JsonArray actualJsonArray =
JSON_PARSER.parse(response).getAsJsonObject().getAsJsonObject("result").getAsJsonArray(EXEC_POSITIONS);
JsonArray actualJsonArray = JsonParser.parseString(response)
.getAsJsonObject().getAsJsonObject("result").getAsJsonArray(EXEC_POSITIONS);
actualJsonArray.forEach(jsonExec -> {
JsonPrimitive name = jsonExec.getAsJsonObject().getAsJsonPrimitive(NAME);
List<JsonObject> execObjects = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class LSExtensionTestUtil {
private static final String GET_NODE_DEFINITION_BY_POSITION = "ballerinaDocument/syntaxTreeNodeByPosition";

private static final Gson GSON = new Gson();
private static final JsonParser parser = new JsonParser();

/**
* Get the ballerinaDocument/syntaxTree modification response.
Expand Down Expand Up @@ -179,7 +178,7 @@ public static BallerinaSyntaxTreeResponse getBallerinaSyntaxTreeByName(String fi
}

private static JsonObject getResult(CompletableFuture result) {
return parser.parse(TestUtil.getResponseString(result)).getAsJsonObject().getAsJsonObject("result");
return JsonParser.parseString(TestUtil.getResponseString(result)).getAsJsonObject().getAsJsonObject("result");
}

public static BallerinaConnectorListResponse getConnectors(BallerinaConnectorListRequest request,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
*/
public class LineFoldingOnlyTest {

private static final JsonParser JSON_PARSER = new JsonParser();

private final Path resourcesPath =
new File(getClass().getClassLoader().getResource("foldingrange").getFile()).toPath();
private Endpoint serviceEndpoint;
Expand Down Expand Up @@ -64,7 +62,7 @@ private void compareResponse(String expected, String response) {
Path expectedPath = resourcesPath.resolve("expected").resolve(expected);
JsonArray expectedJsonArray =
FileUtils.fileContentAsObject(expectedPath.toAbsolutePath().toString()).getAsJsonArray("result");
JsonArray responseJsonArray = JSON_PARSER.parse(response).getAsJsonObject().getAsJsonArray("result");
JsonArray responseJsonArray = JsonParser.parseString(response).getAsJsonObject().getAsJsonArray("result");
Assert.assertEquals(responseJsonArray, expectedJsonArray, "LineFoldingOnlyTest fails with " + expected
+ " test case.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
public class GotoImplementationTest {
private Endpoint serviceEndpoint;
private String configRoot = "implementation" + CommonUtil.FILE_SEPARATOR + "expected";
private JsonParser jsonParser = new JsonParser();
private static final Logger log = LoggerFactory.getLogger(GotoImplementationTest.class);

@BeforeClass
Expand All @@ -60,7 +59,7 @@ public void testGotoImplementation(String configPath, String source) {
JsonObject position = configJsonObject.getAsJsonObject("position");
Position cursor = new Position(position.get("line").getAsInt(), position.get("col").getAsInt());
String response = TestUtil.getGotoImplementationResponse(serviceEndpoint, sourcePath.toString(), cursor);
JsonObject responseJson = jsonParser.parse(response).getAsJsonObject();
JsonObject responseJson = JsonParser.parseString(response).getAsJsonObject();
responseJson.getAsJsonArray("result").forEach(jsonElement -> jsonElement.getAsJsonObject().remove("uri"));
JsonObject expected = configJsonObject.getAsJsonObject("expected");
Assert.assertEquals(expected, responseJson, "Test Failed for" + configPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class ComponentsTest {

private static final String NAME = "name";
private static final String MODULES = "modules";
private static final JsonParser JSON_PARSER = new JsonParser();
private static final Gson GSON = new Gson();

private Path resourceRoot;
Expand Down Expand Up @@ -79,7 +78,7 @@ private void compareResponse(String expected, String response) {
JsonArray expectedJsonArray =
FileUtils.fileContentAsObject(expectedPath.toAbsolutePath().toString()).getAsJsonArray("result");
JsonArray responseJsonArray =
JSON_PARSER.parse(response).getAsJsonObject().getAsJsonObject("result").getAsJsonArray("packages");
JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("result").getAsJsonArray("packages");

Assert.assertEquals(responseJsonArray.size(), expectedJsonArray.size(), "Package ComponentsTest fails with " +
"incorrect package count.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
* Tests Package Config Schema API in Language Server.
*/
public class ConfigSchemaTest {
private static final JsonParser JSON_PARSER = new JsonParser();

private Path resourceRoot;
private Endpoint serviceEndpoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class MetadataTest {
private static final String PACKAGE_NAME = "packageName";
private static final String PATH = "path";
private static final String KIND = "kind";
private static final JsonParser JSON_PARSER = new JsonParser();

private Path resourceRoot;
private Endpoint serviceEndpoint;
Expand Down Expand Up @@ -68,7 +67,7 @@ private void compareResponse(String projectName, String response) {
Path expectedPath = this.resourceRoot.resolve("metadata").resolve(projectName);
JsonObject expectedJsonObject =
FileUtils.fileContentAsObject(expectedPath.toAbsolutePath().toString()).getAsJsonObject();
JsonObject responseJsonObject = JSON_PARSER.parse(response).getAsJsonObject().getAsJsonObject("result");
JsonObject responseJsonObject = JsonParser.parseString(response).getAsJsonObject().getAsJsonObject("result");
JsonPrimitive packageName = expectedJsonObject.getAsJsonPrimitive(PACKAGE_NAME);
if (packageName != null) {
Assert.assertEquals(responseJsonObject.getAsJsonPrimitive(PACKAGE_NAME), packageName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class ReferenceTest {
private Path configRoot;
private Path sourceRoot;
protected Gson gson = new Gson();
protected JsonParser parser = new JsonParser();
protected Endpoint serviceEndpoint;

@BeforeClass
Expand All @@ -69,7 +68,7 @@ public void test(String configPath, String configDir) throws IOException {
TestUtil.closeDocument(serviceEndpoint, sourcePath);

JsonArray expected = configObject.getAsJsonArray("result");
JsonArray actual = parser.parse(actualStr).getAsJsonObject().getAsJsonArray("result");
JsonArray actual = JsonParser.parseString(actualStr).getAsJsonObject().getAsJsonArray("result");
this.alterExpectedUri(expected);
this.alterActualUri(actual);
Assert.assertEquals(actual, expected);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public class ReferencesTest {
protected Path configRoot;
protected Path sourceRoot;
protected Gson gson = new Gson();
protected JsonParser parser = new JsonParser();
protected Endpoint serviceEndpoint;
private static final Logger log = LoggerFactory.getLogger(ReferencesTest.class);

Expand All @@ -75,7 +74,7 @@ public void test(String configPath) throws IOException {
TestUtil.closeDocument(serviceEndpoint, sourcePath);

JsonArray expected = configObject.getAsJsonArray("result");
JsonArray actual = parser.parse(actualStr).getAsJsonObject().get("result").getAsJsonArray();
JsonArray actual = JsonParser.parseString(actualStr).getAsJsonObject().get("result").getAsJsonArray();
this.alterExpectedUri(expected, sourceRoot);
this.alterActualUri(actual);

Expand All @@ -94,7 +93,7 @@ public void testReferencesWithinStdLib(String configPath) throws IOException, UR
String actualStr = getReferencesResponseWithinStdLib(sourcePath, position);

JsonArray expected = configObject.getAsJsonArray("result");
JsonArray actual = parser.parse(actualStr).getAsJsonObject().get("result").getAsJsonArray();
JsonArray actual = JsonParser.parseString(actualStr).getAsJsonObject().get("result").getAsJsonArray();
this.alterExpectedUri(expected, ballerinaHome);
this.alterActualStdLibUri(actual);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public abstract class AbstractRenameTest {
private Path configRoot;
private Path sourceRoot;
protected Gson gson = new Gson();
protected JsonParser parser = new JsonParser();
protected Endpoint serviceEndpoint;

@BeforeClass
Expand All @@ -60,13 +59,13 @@ public void performTest(String configPath, String varName) throws IOException {
String prepareResponse = TestUtil.getPrepareRenameResponse(sourcePath.toString(), position, serviceEndpoint);

// For invalid positions, response result=null, and is removed by GSON.
Assert.assertEquals(parser.parse(prepareResponse).getAsJsonObject().has("result"), isValidRename,
Assert.assertEquals(JsonParser.parseString(prepareResponse).getAsJsonObject().has("result"), isValidRename,
"Expected valid rename position: " + String.valueOf(isValidRename));

String actualStr = TestUtil.getRenameResponse(sourcePath.toString(), position, varName, serviceEndpoint);
TestUtil.closeDocument(serviceEndpoint, sourcePath);
JsonObject expected = resultJson.getAsJsonObject("result");
JsonObject actual = parser.parse(actualStr).getAsJsonObject().getAsJsonObject("result");
JsonObject actual = JsonParser.parseString(actualStr).getAsJsonObject().getAsJsonObject("result");
if (actual == null) {
actual = new JsonObject();
} else {
Expand Down
Loading

0 comments on commit 16c9572

Please sign in to comment.