Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java 21] Remove Gson usage from Testerina Runtime #43425

Draft
wants to merge 2 commits into
base: java21
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions bvm/ballerina-rt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ dependencies {
//// metrics
dist project(':metrics-extensions:ballerina-metrics-extension')

dist "com.google.code.gson:gson:${project.gsonVersion}"

// Transaction related third party jars
dist "com.atomikos:transactions-jta:${project.atomikosTransactionsJtaVersion}"
dist "com.atomikos:atomikos-util:${project.atomikosUtilVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
*/
package org.ballerinalang.test.runtime;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.ballerina.projects.util.ProjectConstants;
import io.ballerina.runtime.api.utils.JsonUtils;
import io.ballerina.runtime.api.utils.StringUtils;
import io.ballerina.runtime.api.values.BArray;
import io.ballerina.runtime.api.values.BMap;
import io.ballerina.runtime.api.values.BString;
import io.ballerina.runtime.internal.values.BmpStringValue;
import org.ballerinalang.test.runtime.entity.MockFunctionReplaceVisitor;
import org.ballerinalang.test.runtime.entity.ModuleStatus;
import org.ballerinalang.test.runtime.entity.TestReport;
Expand Down Expand Up @@ -108,13 +112,12 @@ public static void main(String[] args) throws IOException {
} else {
br = Files.newBufferedReader(testSuiteJsonPath, StandardCharsets.UTF_8);
}
Gson gson = new Gson();
Map<String, TestSuite> testSuiteMap = gson.fromJson(br,
new TypeToken<Map<String, TestSuite>>() { }.getType());
Object jsonObj = JsonUtils.parse(br, JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING);
BMap<BString, Object> testSuiteMap = (BMap<BString, Object>) jsonObj;
Comment on lines +115 to +116
Copy link
Contributor

@HindujaB HindujaB Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Object jsonObj = JsonUtils.parse(br, JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING);
BMap<BString, Object> testSuiteMap = (BMap<BString, Object>) jsonObj;
BMap<BString, Object> testSuiteMap = (BMap<BString, Object>) JsonUtils.parse(br,
JsonUtils.NonStringValueProcessingMode.FROM_JSON_STRING);

Because we only use jsonObj in one place.

if (!testSuiteMap.isEmpty()) {
for (Map.Entry<String, TestSuite> entry : testSuiteMap.entrySet()) {
String moduleName = entry.getKey();
TestSuite testSuite = entry.getValue();
for (Map.Entry<BString, Object> entry : testSuiteMap.entrySet()) {
String moduleName = entry.getKey().toString();
TestSuite testSuite = testSuiteGenerator((BMap<BString, Object>) entry.getValue());
String packageName = testSuite.getPackageName();
out.println("\n\t" + (moduleName.equals(packageName) ?
(moduleName.equals(TesterinaConstants.DOT) ? testSuite.getSourceFileName() : moduleName)
Expand Down Expand Up @@ -176,9 +179,7 @@ private static void writeStatusToJsonFile(ModuleStatus moduleStatus, Path tmpJso
}
try (FileOutputStream fileOutputStream = new FileOutputStream(jsonFile)) {
try (Writer writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8)) {
Gson gson = new Gson();
String json = gson.toJson(moduleStatus);
writer.write(new String(json.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
JsonUtils.serialize(JsonUtils.convertToJson(moduleStatus), writer);
}
}
}
Expand Down Expand Up @@ -369,4 +370,39 @@ public static ClassLoader getClassLoader() {
return classLoader;
}

private static TestSuite testSuiteGenerator(BMap<BString, Object> map) {
String packageName = map.get(StringUtils.fromString("packageName")).toString();
String orgName = map.get(StringUtils.fromString("orgName")).toString();
String version = map.get(StringUtils.fromString("version")).toString();
String sourceRootPath = map.get(StringUtils.fromString("sourceRootPath")).toString();
String packageId = map.get(StringUtils.fromString("packageId")).toString();
String testPackageId = map.get(StringUtils.fromString("testPackageId")).toString();
String executeFilePath = map.get(StringUtils.fromString("executeFilePath")).toString();
BMap<BString,?> mockFunctionNamesMap = (BMap<BString,?>) map.get(StringUtils.fromString("mockFunctionNamesMap"));
BMap<BString,?> testUtilityFunctions = (BMap<BString,?>) map.get(StringUtils.fromString("testUtilityFunctions"));
List<Object> testExecutionDependencies = Arrays.stream(
((BArray) map.get(StringUtils.fromString("testExecutionDependencies"))).getValues()).toList();
String sourceFileName = null;
TestSuite testSuite = new TestSuite(packageId, testPackageId, packageName,orgName, version,executeFilePath);
testSuite.setSourceRootPath(sourceRootPath);
if (map.get(StringUtils.fromString("sourceFileName")) != null) {
sourceFileName = map.get(StringUtils.fromString("sourceFileName")).toString();
}
testSuite.setSourceFileName(sourceFileName);
for (BString key : mockFunctionNamesMap.getKeys()) {
testSuite.addMockFunction(key.toString(), mockFunctionNamesMap.get(key).toString());
}
for (BString key : testUtilityFunctions.getKeys()) {
testSuite.addTestUtilityFunction(key.toString(), testUtilityFunctions.get(key).toString());
}
List<Path> paths = new ArrayList<>();
for (Object item : testExecutionDependencies) {
if (item != null) {
paths.add(Paths.get(((BString) item).getValue()));
}
}
testSuite.addTestExecutionDependencies(paths);
return testSuite;
}

}
Loading