Skip to content

Commit 6452a6f

Browse files
authored
Merge pull request #955 from marilynel/master
Add testWithStrictMode option to build.gradle
2 parents 75e5a3d + ae4f4af commit 6452a6f

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ Execute the test suite with Gradlew:
9797
gradlew clean build test
9898
```
9999

100+
*Optional* Execute the test suite in strict mode with Gradlew:
101+
102+
```shell
103+
gradlew testWithStrictMode
104+
```
105+
100106
# Notes
101107

102108
For more information, please see [NOTES.md](https://github.com/stleary/JSON-java/blob/master/docs/NOTES.md)

build.gradle

+72
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,75 @@ publishing {
5353
tasks.withType(JavaCompile) {
5454
options.encoding = 'UTF-8'
5555
}
56+
// Add these imports at the top of your build.gradle file
57+
import java.nio.file.Files
58+
import java.nio.file.Path
59+
import java.nio.file.Paths
60+
import java.nio.file.StandardCopyOption
61+
62+
// Your existing build configurations...
63+
64+
// Add a new task to modify the file
65+
task modifyStrictMode {
66+
doLast {
67+
println "Modifying JSONParserConfiguration.java to enable strictMode..."
68+
69+
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
70+
71+
if (!filePath.exists()) {
72+
throw new GradleException("Could not find file: ${filePath.absolutePath}")
73+
}
74+
75+
// Create a backup of the original file
76+
def backupFile = new File(filePath.absolutePath + '.bak')
77+
Files.copy(filePath.toPath(), backupFile.toPath(), StandardCopyOption.REPLACE_EXISTING)
78+
79+
// Read and modify the file content
80+
def content = filePath.text
81+
def modifiedContent = content.replace('// this.strictMode = true;', 'this.strictMode = true;')
82+
83+
// Write the modified content back to the file
84+
filePath.text = modifiedContent
85+
86+
println "File modified successfully at: ${filePath.absolutePath}"
87+
}
88+
}
89+
90+
// Add a task to restore the original file
91+
task restoreStrictMode {
92+
doLast {
93+
println "Restoring original JSONParserConfiguration.java..."
94+
95+
def filePath = project.file('src/main/java/org/json/JSONParserConfiguration.java')
96+
def backupFile = new File(filePath.absolutePath + '.bak')
97+
98+
if (backupFile.exists()) {
99+
Files.copy(backupFile.toPath(), filePath.toPath(), StandardCopyOption.REPLACE_EXISTING)
100+
backupFile.delete()
101+
println "Original file restored successfully at: ${filePath.absolutePath}"
102+
} else {
103+
println "Backup file not found at: ${backupFile.absolutePath}. No restoration performed."
104+
}
105+
}
106+
}
107+
108+
// Create a task to run the workflow
109+
task testWithStrictMode {
110+
dependsOn modifyStrictMode
111+
finalizedBy restoreStrictMode
112+
113+
doLast {
114+
// This will trigger a clean build and run tests with strictMode enabled
115+
if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
116+
exec {
117+
executable 'cmd'
118+
args '/c', 'gradlew.bat', 'clean', 'build'
119+
}
120+
} else {
121+
exec {
122+
executable './gradlew'
123+
args 'clean', 'build'
124+
}
125+
}
126+
}
127+
}

src/main/java/org/json/JSONParserConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public class JSONParserConfiguration extends ParserConfiguration {
1515
public JSONParserConfiguration() {
1616
super();
1717
this.overwriteDuplicateKey = false;
18-
// this.strictMode = true;
18+
// DO NOT DELETE THE FOLLOWING LINE -- it is used for strictMode testing
19+
// this.strictMode = true;
1920
}
2021

2122
/**

0 commit comments

Comments
 (0)