1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package jdk.nashorn.api.tree.test; 26 27 import java.io.File; 28 import java.io.IOException; 29 import java.nio.charset.Charset; 30 import java.nio.charset.StandardCharsets; 31 import java.nio.file.Files; 32 import jdk.nashorn.api.tree.Parser; 33 import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1; 34 import jdk.nashorn.api.tree.Tree; 35 import org.testng.Assert; 36 import org.testng.annotations.Test; 37 38 /** 39 * Test for nashorn Parser API (jdk.nashorn.api.tree.*) 40 */ 41 public class ParseAPITest { 42 43 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("parserapitest.verbose")); 44 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("parserapitest.test262")); 45 46 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 47 private static final String TEST_MAPTESTS_DIR = System.getProperty("test.maptests.dir"); 48 private static final String TEST_SANDBOX_DIR = System.getProperty("test.sandbox.dir"); 49 private static final String TEST_TRUSTED_DIR = System.getProperty("test.trusted.dir"); 50 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 51 52 interface TestFilter { 53 public boolean exclude(File file, String content); 54 } 55 56 private void log(String msg) { 57 org.testng.Reporter.log(msg, true); 58 } 59 60 private static final String[] options = new String[] { 61 "-scripting", "--const-as-var" 62 }; 63 64 @Test 65 public void parseAllTests() { 66 if (TEST262) { 67 parseTestSet(TEST262_SUITE_DIR, new TestFilter() { 68 @Override 69 public boolean exclude(final File file, final String content) { 70 return content.contains("@negative"); 71 } 72 }); 73 } 74 parseTestSet(TEST_BASIC_DIR, new TestFilter() { 75 @Override 76 public boolean exclude(final File file, final String content) { 77 return file.getParentFile().getName().equals("es6"); 78 } 79 }); 80 parseTestSet(TEST_MAPTESTS_DIR, null); 81 parseTestSet(TEST_SANDBOX_DIR, null); 82 parseTestSet(TEST_TRUSTED_DIR, null); 83 } 84 85 private void parseTestSet(final String testSet, final TestFilter filter) { 86 passed = 0; 87 failed = 0; 88 skipped = 0; 89 90 final File testSetDir = new File(testSet); 91 if (! testSetDir.isDirectory()) { 92 log("WARNING: " + testSetDir + " not found or not a directory"); 93 return; 94 } 95 log(testSetDir.getAbsolutePath()); 96 parseJSDirectory(testSetDir, filter); 97 98 log(testSet + " parse API done!"); 99 log("parse API ok: " + passed); 100 log("parse API failed: " + failed); 101 log("parse API skipped: " + skipped); 102 if (failed != 0) { 103 Assert.fail(failed + " tests failed to parse in " + testSetDir.getAbsolutePath()); 104 } 105 } 106 107 // number of scripts that parsed fine 108 private int passed; 109 // number of scripts resulting in parse failure 110 private int failed; 111 // scripts that were skipped - all tests with @negative are 112 // skipped for now. 113 private int skipped; 114 115 private void parseJSDirectory(final File dir, final TestFilter filter) { 116 for (final File f : dir.listFiles()) { 117 if (f.isDirectory()) { 118 parseJSDirectory(f, filter); 119 } else if (f.getName().endsWith(".js")) { 120 parseJSFile(f, filter); 121 } 122 } 123 } 124 125 private void parseJSFile(final File file, final TestFilter filter) { 126 if (VERBOSE) { 127 log("Begin parsing " + file.getAbsolutePath()); 128 } 129 130 try { 131 final char[] buffer = readFully(file); 132 final String content = new String(buffer); 133 boolean excluded = false; 134 if (filter != null) { 135 excluded = filter.exclude(file, content); 136 } 137 138 if (excluded) { 139 if (VERBOSE) { 140 log("Skipping " + file.getAbsolutePath()); 141 } 142 skipped++; 143 return; 144 } 145 146 final Parser parser = Parser.create(options); 147 final Tree tree = parser.parse(file.getAbsolutePath(), content, null); 148 tree.accept(new SimpleTreeVisitorES5_1<Void, Void>(), null); 149 passed++; 150 } catch (final Throwable exp) { 151 log("Parse API failed: " + file.getAbsolutePath() + " : " + exp); 152 //if (VERBOSE) { 153 exp.printStackTrace(System.out); 154 //} 155 failed++; 156 } 157 158 if (VERBOSE) { 159 log("Done parsing via parser API " + file.getAbsolutePath()); 160 } 161 } 162 163 private static char[] byteToCharArray(final byte[] bytes) { 164 Charset cs = StandardCharsets.UTF_8; 165 int start = 0; 166 // BOM detection. 167 if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) { 168 start = 2; 169 cs = StandardCharsets.UTF_16BE; 170 } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) { 171 start = 2; 172 cs = StandardCharsets.UTF_16LE; 173 } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) { 174 start = 3; 175 cs = StandardCharsets.UTF_8; 176 } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) { 177 start = 4; 178 cs = Charset.forName("UTF-32LE"); 179 } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) { 180 start = 4; 181 cs = Charset.forName("UTF-32BE"); 182 } 183 184 return new String(bytes, start, bytes.length - start, cs).toCharArray(); 185 } 186 187 private static char[] readFully(final File file) throws IOException { 188 final byte[] buf = Files.readAllBytes(file.toPath()); 189 return byteToCharArray(buf); 190 } 191 }