Skip to content

Commit

Permalink
use try-with-resource
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 19, 2023
1 parent a54248f commit 1567f6c
Showing 17 changed files with 570 additions and 642 deletions.
30 changes: 8 additions & 22 deletions testsrc/org/mozilla/javascript/tests/Bug482203Test.java
Original file line number Diff line number Diff line change
@@ -15,16 +15,11 @@
public class Bug482203Test extends TestCase {

public void testJsApi() throws Exception {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
cx.setOptimizationLevel(-1);
Script script =
cx.compileReader(
new InputStreamReader(
Bug482203Test.class.getResourceAsStream("Bug482203.js")),
"",
1,
null);
InputStreamReader in =
new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js"));
Script script = cx.compileReader(in, "", 1, null);
Scriptable scope = cx.initStandardObjects();
script.exec(cx, scope);
int counter = 0;
@@ -38,22 +33,15 @@ public void testJsApi() throws Exception {
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} finally {
Context.exit();
}
}

public void testJavaApi() throws Exception {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
cx.setOptimizationLevel(-1);
Script script =
cx.compileReader(
new InputStreamReader(
Bug482203Test.class.getResourceAsStream("Bug482203.js")),
"",
1,
null);
InputStreamReader in =
new InputStreamReader(Bug482203Test.class.getResourceAsStream("Bug482203.js"));
Script script = cx.compileReader(in, "", 1, null);
Scriptable scope = cx.initStandardObjects();
cx.executeScriptWithContinuations(script, scope);
int counter = 0;
@@ -67,8 +55,6 @@ public void testJavaApi() throws Exception {
}
assertEquals(counter, 5);
assertEquals(Double.valueOf(3), ScriptableObject.getProperty(scope, "result"));
} finally {
Context.exit();
}
}
}
396 changes: 191 additions & 205 deletions testsrc/org/mozilla/javascript/tests/ContinuationsApiTest.java

Large diffs are not rendered by default.

6 changes: 1 addition & 5 deletions testsrc/org/mozilla/javascript/tests/ExternalArrayTest.java
Original file line number Diff line number Diff line change
@@ -124,12 +124,8 @@ public void testNativeDoubleArray() {
private void runScript(String script, int opt) {
try {
cx.setOptimizationLevel(opt);
FileReader rdr = new FileReader(script);

try {
try (FileReader rdr = new FileReader(script)) {
cx.evaluateReader(root, rdr, script, 1, null);
} finally {
rdr.close();
}
} catch (IOException ioe) {
assertFalse("I/O Error: " + ioe, true);
21 changes: 7 additions & 14 deletions testsrc/org/mozilla/javascript/tests/HashCollisionTest.java
Original file line number Diff line number Diff line change
@@ -15,9 +15,8 @@ public class HashCollisionTest {
@BeforeClass
public static void loadFile() throws IOException {
StringWriter out = new StringWriter();
FileReader in = new FileReader(mediumInput);

try {
try (FileReader in = new FileReader(mediumInput)) {
char[] buf = new char[16392];
int rc;
do {
@@ -28,9 +27,6 @@ public static void loadFile() throws IOException {
} while (rc > 0);

collisions = out.toString();

} finally {
in.close();
}
}

@@ -41,15 +37,12 @@ public static void loadFile() throws IOException {
*/
@Test
public void testMediumCollisions() throws IOException {
FileReader scriptIn = new FileReader("testsrc/jstests/hash-collisions.js");
Context cx = Context.enter();
try {
Global glob = new Global(cx);
glob.put("collisions", glob, collisions);
cx.evaluateReader(glob, scriptIn, "hash-collisons.js", 1, null);
} finally {
Context.exit();
scriptIn.close();
try (FileReader scriptIn = new FileReader("testsrc/jstests/hash-collisions.js")) {
try (Context cx = Context.enter()) {
Global glob = new Global(cx);
glob.put("collisions", glob, collisions);
cx.evaluateReader(glob, scriptIn, "hash-collisons.js", 1, null);
}
}
}
}
25 changes: 5 additions & 20 deletions testsrc/org/mozilla/javascript/tests/InitializationTest.java
Original file line number Diff line number Diff line change
@@ -11,63 +11,48 @@ public class InitializationTest {

@Test
public void testStandard() {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
ScriptableObject root = cx.initStandardObjects();
Object result = cx.evaluateString(root, BASIC_SCRIPT, "basic", 1, null);
assertEquals("Hello, World!", result);
} finally {
Context.exit();
}
}

@Test
public void testStandardES6() {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject root = cx.initStandardObjects();
Object result = cx.evaluateString(root, BASIC_SCRIPT, "basic", 1, null);
assertEquals("Hello, World!", result);
} finally {
Context.exit();
}
}

@Test
public void testSafeStandard() {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
ScriptableObject root = cx.initSafeStandardObjects();
Object result = cx.evaluateString(root, BASIC_SCRIPT, "basic", 1, null);
assertEquals("Hello, World!", result);
} finally {
Context.exit();
}
}

@Test
public void testStandardSealed() {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
ScriptableObject root = cx.initStandardObjects(null, true);
Object result = cx.evaluateString(root, BASIC_SCRIPT, "basic", 1, null);
assertEquals("Hello, World!", result);
} finally {
Context.exit();
}
}

@Test
public void testStandardSealedES6() {
Context cx = Context.enter();
try {
try (Context cx = Context.enter()) {
cx.setLanguageVersion(Context.VERSION_ES6);
ScriptableObject root = cx.initStandardObjects(null, true);
Object result = cx.evaluateString(root, BASIC_SCRIPT, "basic", 1, null);
assertEquals("Hello, World!", result);
} finally {
Context.exit();
}
}
}
10 changes: 3 additions & 7 deletions testsrc/org/mozilla/javascript/tests/Issue176Test.java
Original file line number Diff line number Diff line change
@@ -19,13 +19,9 @@ public class Issue176Test extends TestCase {
public void testThrowing() throws Exception {
cx = Context.enter();
try {
Script script =
cx.compileReader(
new InputStreamReader(
Bug482203Test.class.getResourceAsStream("Issue176.js")),
"Issue176.js",
1,
null);
InputStreamReader in =
new InputStreamReader(Bug482203Test.class.getResourceAsStream("Issue176.js"));
Script script = cx.compileReader(in, "Issue176.js", 1, null);
scope = cx.initStandardObjects();
scope.put("host", scope, this);
script.exec(cx, scope); // calls our methods
12 changes: 6 additions & 6 deletions testsrc/org/mozilla/javascript/tests/JavaIterableTest.java
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ public JavaIterableTest() {

@Test
public void testMap() {
Map map = new LinkedHashMap();
Map<Object, Object> map = new LinkedHashMap<>();
String script =
"var a = [];\n"
+ "for (var { key, value } of value.entrySet()) a.push(key, value);\n"
@@ -61,23 +61,23 @@ public void testMap() {
NativeArray res = (NativeArray) runScript("Array.from(value.entrySet())", map);
assertEquals(3, res.size());

Map.Entry e0 = (Map.Entry) res.get(0);
Map.Entry<Object, Object> e0 = (Map.Entry<Object, Object>) res.get(0);
assertEquals("a", e0.getKey());
assertEquals("b", e0.getValue());

Map.Entry e1 = (Map.Entry) res.get(1);
Map.Entry<Object, Object> e1 = (Map.Entry<Object, Object>) res.get(1);
assertEquals(123.0, Context.toNumber(e1.getKey()));
assertEquals(234.0, Context.toNumber(e1.getValue()));

Map.Entry e2 = (Map.Entry) res.get(2);
Map.Entry<Object, Object> e2 = (Map.Entry<Object, Object>) res.get(2);
assertEquals(o, e2.getKey());
assertEquals(o, e2.getValue());
}
}

@Test
public void testList() {
List list = new ArrayList();
List<Object> list = new ArrayList<>();
String script = "var a = [];\n" + "for (var e of value) a.push(e);\n" + "a";

NativeArray resEmpty = (NativeArray) runScript(script, list);
@@ -107,7 +107,7 @@ public void testList() {

@Test
public void testSet() {
Set set = new LinkedHashSet();
Set<Object> set = new LinkedHashSet<>();
String script = "var a = [];\n" + "for (var e of value) a.push(e);\n" + "a";

NativeArray resEmpty = (NativeArray) runScript(script, set);
88 changes: 45 additions & 43 deletions testsrc/org/mozilla/javascript/tests/MozillaSuiteTest.java
Original file line number Diff line number Diff line change
@@ -198,53 +198,55 @@ public void runMozillaTest() throws Exception {
* print out a list of all the tests that pass.
*/
public static void main(String[] args) throws IOException {
PrintStream out = new PrintStream("fix-tests-files.sh");
try {
for (int i = 0; i < OPT_LEVELS.length; i++) {
int optLevel = OPT_LEVELS[i];
File testDir = getTestDir();
File[] allTests =
TestUtils.recursiveListFiles(
testDir,
new FileFilter() {
public boolean accept(File pathname) {
return ShellTest.DIRECTORY_FILTER.accept(pathname)
|| ShellTest.TEST_FILTER.accept(pathname);
}
});
HashSet<File> diff = new HashSet<File>(Arrays.asList(allTests));
File testFiles[] = getTestFiles(optLevel);
diff.removeAll(Arrays.asList(testFiles));
ArrayList<String> skippedPassed = new ArrayList<String>();
int absolutePathLength = testDir.getAbsolutePath().length() + 1;
for (File testFile : diff) {
try {
(new MozillaSuiteTest(testFile, optLevel)).runMozillaTest();
// strip off testDir
String canonicalized =
testFile.getAbsolutePath().substring(absolutePathLength);
canonicalized = canonicalized.replace('\\', '/');
skippedPassed.add(canonicalized);
} catch (Throwable t) {
// failed, so skip
try (PrintStream out = new PrintStream("fix-tests-files.sh")) {
try {
for (int i = 0; i < OPT_LEVELS.length; i++) {
int optLevel = OPT_LEVELS[i];
File testDir = getTestDir();
File[] allTests =
TestUtils.recursiveListFiles(
testDir,
new FileFilter() {
@Override
public boolean accept(File pathname) {
return ShellTest.DIRECTORY_FILTER.accept(pathname)
|| ShellTest.TEST_FILTER.accept(pathname);
}
});
HashSet<File> diff = new HashSet<File>(Arrays.asList(allTests));
File testFiles[] = getTestFiles(optLevel);
diff.removeAll(Arrays.asList(testFiles));
ArrayList<String> skippedPassed = new ArrayList<String>();
int absolutePathLength = testDir.getAbsolutePath().length() + 1;
for (File testFile : diff) {
try {
(new MozillaSuiteTest(testFile, optLevel)).runMozillaTest();
// strip off testDir
String canonicalized =
testFile.getAbsolutePath().substring(absolutePathLength);
canonicalized = canonicalized.replace('\\', '/');
skippedPassed.add(canonicalized);
} catch (Throwable t) {
// failed, so skip
}
}
}
// "skippedPassed" now contains all the tests that are currently
// skipped but now pass. Print out shell commands to update the
// appropriate *.tests file.
if (skippedPassed.size() > 0) {
out.println("cat >> " + getTestFilename(optLevel) + " <<EOF");
String[] sorted = skippedPassed.toArray(new String[0]);
Arrays.sort(sorted);
for (int j = 0; j < sorted.length; j++) {
out.println(sorted[j]);
// "skippedPassed" now contains all the tests that are currently
// skipped but now pass. Print out shell commands to update the
// appropriate *.tests file.
if (skippedPassed.size() > 0) {
out.println("cat >> " + getTestFilename(optLevel) + " <<EOF");
String[] sorted = skippedPassed.toArray(new String[0]);
Arrays.sort(sorted);
for (int j = 0; j < sorted.length; j++) {
out.println(sorted[j]);
}
out.println("EOF");
}
out.println("EOF");
}
System.out.println("Done.");
} finally {
out.close();
}
System.out.println("Done.");
} finally {
out.close();
}
}
}
10 changes: 4 additions & 6 deletions testsrc/org/mozilla/javascript/tests/NativeJsonTest.java
Original file line number Diff line number Diff line change
@@ -18,14 +18,12 @@ public class NativeJsonTest {
public void stringifyResultAndResultType() {
String jsScript =
"function f(){ return JSON.stringify({property1:\"hello\", array1:[{subobject:1}]}); } f();";
Context jsContext = Context.enter();
try {
Scriptable jsScope = jsContext.initStandardObjects();
Object result = jsContext.evaluateString(jsScope, jsScript, "myscript.js", 1, null);

try (Context cx = Context.enter()) {
Scriptable jsScope = cx.initStandardObjects();
Object result = cx.evaluateString(jsScope, jsScript, "myscript.js", 1, null);
assertEquals("{\"property1\":\"hello\",\"array1\":[{\"subobject\":1}]}", result);
assertEquals("java.lang.String", result.getClass().getName());
} finally {
Context.exit();
}
}
}
Original file line number Diff line number Diff line change
@@ -39,27 +39,20 @@ protected boolean hasFeature(Context cx, int featureIndex) {
}
};

Context cx = factory.enterContext();
try {
try (Context cx = factory.enterContext()) {
cx.setLanguageVersion(Context.VERSION_1_8);
cx.setOptimizationLevel(opt);
cx.setGeneratingDebug(true);

Global global = new Global(cx);
Scriptable root = cx.newObject(global);

FileReader rdr =
new FileReader("testsrc/jstests/extensions/stack-traces-mozilla-lf.js");

try {
try (FileReader rdr =
new FileReader("testsrc/jstests/extensions/stack-traces-mozilla-lf.js")) {
cx.evaluateReader(root, rdr, "stack-traces-mozilla-lf.js", 1, null);
} finally {
rdr.close();
}
} catch (IOException ioe) {
assertFalse("I/O Error: " + ioe, true);
} finally {
Context.exit();
}
}

Loading
Oops, something went wrong.

0 comments on commit 1567f6c

Please sign in to comment.