From f30167e7c0a83fc742e64337042db3762d0a8f81 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 23 Feb 2025 22:00:22 -0800 Subject: [PATCH 1/3] tests seem to be working, run with strictMode = fale then true --- build.gradle | 43 +++++++++++++++++++ .../org/json/JSONParserConfiguration.java | 2 +- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 2c0fc6d..d8ccd6a 100644 --- a/build.gradle +++ b/build.gradle @@ -53,3 +53,46 @@ publishing { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } + +def originalFile = null; + +task backupCode { + def file = file('src/main/java/org/json/JSONParserConfiguration.java') + originalFile = file.text +} + +task firstTest { + +} + +task modifyCode { + doLast { + // Add your code modification logic here + def file = file('src/main/java/org/json/JSONParserConfiguration.java') + def text = file.text + text = text.replaceAll('oldCode', 'newCode') + file.text = text + } +} + +task compileModifiedCode(type: JavaCompile) { + source = sourceSets.main.java.srcDirs + classpath = sourceSets.main.compileClasspath + destinationDirectory = sourceSets.main.java.outputDir +} + +task secondTest { + +} + +task restoreCode { + def file = file('src/main/java/org/json/JSONParserConfiguration.java') + file.text = originalFile +} + +// and then add it to the task list +backupCode.finalizedBy firstTest +firstTest.finalizedBy modifyCode +modifyCode.finalizedBy compileModifiedCode +compileModifiedCode.finalizedBy secondTest +secondTest.finalizedBy restoreCode \ No newline at end of file diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index d3420a1..1866f30 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -15,7 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration { public JSONParserConfiguration() { super(); this.overwriteDuplicateKey = false; -// this.strictMode = true; + // this.strictMode = true; } /** From 8a86894c63f5535877267d617666e77ead3b9afe Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 2 Mar 2025 11:02:27 -0800 Subject: [PATCH 2/3] test with strict mode enabled and fixed --- README.md | 6 ++++ build.gradle | 97 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1271f8d..336f60c 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,12 @@ Execute the test suite with Gradlew: gradlew clean build test ``` +*Optional* Execute the test suite in strict mode with Gradlew: + +```shell +gradlew testWithStrictMode +``` + # Notes For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md) diff --git a/build.gradle b/build.gradle index d8ccd6a..3961e85 100644 --- a/build.gradle +++ b/build.gradle @@ -53,46 +53,75 @@ publishing { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } +// Add these imports at the top of your build.gradle file +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import java.nio.file.StandardCopyOption -def originalFile = null; +// Your existing build configurations... -task backupCode { - def file = file('src/main/java/org/json/JSONParserConfiguration.java') - originalFile = file.text -} - -task firstTest { - -} - -task modifyCode { +// Add a new task to modify the file +task modifyStrictMode { doLast { - // Add your code modification logic here - def file = file('src/main/java/org/json/JSONParserConfiguration.java') - def text = file.text - text = text.replaceAll('oldCode', 'newCode') - file.text = text + println "Modifying JSONParserConfiguration.java to enable strictMode..." + + def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java') + + if (!filePath.exists()) { + throw new GradleException("Could not find file: ${filePath.absolutePath}") + } + + // Create a backup of the original file + def backupFile = new File(filePath.absolutePath + '.bak') + Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING) + + // Read and modify the file content + def content = filePath.text + def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;') + + // Write the modified content back to the file + filePath.text = modifiedContent + + println "File modified successfully at: ${filePath.absolutePath}" } } -task compileModifiedCode(type: JavaCompile) { - source = sourceSets.main.java.srcDirs - classpath = sourceSets.main.compileClasspath - destinationDirectory = sourceSets.main.java.outputDir +// Add a task to restore the original file +task restoreStrictMode { + doLast { + println "Restoring original JSONParserConfiguration.java..." + + def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java') + def backupFile = new File(filePath.absolutePath + '.bak') + + if (backupFile.exists()) { + Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING) + backupFile.delete() + println "Original file restored successfully at: ${filePath.absolutePath}" + } else { + println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed." + } + } } -task secondTest { +// Create a task to run the workflow +task testWithStrictMode { + dependsOn modifyStrictMode + finalizedBy restoreStrictMode -} - -task restoreCode { - def file = file('src/main/java/org/json/JSONParserConfiguration.java') - file.text = originalFile -} - -// and then add it to the task list -backupCode.finalizedBy firstTest -firstTest.finalizedBy modifyCode -modifyCode.finalizedBy compileModifiedCode -compileModifiedCode.finalizedBy secondTest -secondTest.finalizedBy restoreCode \ No newline at end of file + doLast { + // This will trigger a clean build and run tests with strictMode enabled + if (org.gradle.internal.os.OperatingSystem.current().isWindows()) { + exec { + executable 'cmd' + args '/c', 'gradlew.bat', 'clean', 'build' + } + } else { + exec { + executable './gradlew' + args 'clean', 'build' + } + } + } +} \ No newline at end of file From ae4f4afcc79dec50ff265c56ef34c1d29a32d2f4 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 2 Mar 2025 11:08:00 -0800 Subject: [PATCH 3/3] dont mess with my line --- src/main/java/org/json/JSONParserConfiguration.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/json/JSONParserConfiguration.java b/src/main/java/org/json/JSONParserConfiguration.java index 1866f30..3fbfb8a 100644 --- a/src/main/java/org/json/JSONParserConfiguration.java +++ b/src/main/java/org/json/JSONParserConfiguration.java @@ -15,6 +15,7 @@ public class JSONParserConfiguration extends ParserConfiguration { public JSONParserConfiguration() { super(); this.overwriteDuplicateKey = false; + // DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing // this.strictMode = true; }