1 /* 2 * Copyright (c) 2010, 2013, 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 26 package jdk.nashorn.internal.codegen; 27 28 import java.io.File; 29 import java.io.PrintWriter; 30 import java.io.StringWriter; 31 import jdk.nashorn.internal.objects.Global; 32 import jdk.nashorn.internal.runtime.Context; 33 import jdk.nashorn.internal.runtime.ErrorManager; 34 import jdk.nashorn.internal.runtime.ScriptFunction; 35 import jdk.nashorn.internal.runtime.ScriptObject; 36 import jdk.nashorn.internal.runtime.Source; 37 import jdk.nashorn.internal.runtime.options.Options; 38 import org.testng.Assert; 39 import org.testng.annotations.AfterClass; 40 import org.testng.annotations.BeforeClass; 41 import org.testng.annotations.Test; 42 43 /** 44 * Tests to check Nashorn JS compiler - just compiler and not execution of scripts. 45 */ 46 public class CompilerTest { 47 private static final boolean VERBOSE = Boolean.valueOf(System.getProperty("compilertest.verbose")); 48 private static final boolean TEST262 = Boolean.valueOf(System.getProperty("compilertest.test262")); 49 private static final String TEST_BASIC_DIR = System.getProperty("test.basic.dir"); 50 private static final String TEST_NODE_DIR = System.getProperty("test.node.dir"); 51 private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir"); 52 53 interface TestFilter { 54 public boolean exclude(File file, String content); 55 } 56 57 private void log(String msg) { 58 org.testng.Reporter.log(msg, true); 59 } 60 61 private Context context; 62 private Global global; 63 64 @BeforeClass 65 public void setupTest() { 66 final Options options = new Options("nashorn"); 67 options.set("anon.functions", true); 68 options.set("compile.only", true); 69 options.set("print.ast", true); 70 options.set("print.parse", true); 71 options.set("scripting", true); 72 73 final ErrorManager errors = new ErrorManager() { 74 @Override 75 public void error(final String msg) { 76 log(msg); 77 } 78 }; 79 80 final StringWriter sw = new StringWriter(); 81 final PrintWriter pw = new PrintWriter(sw); 82 this.context = new Context(options, errors, pw, pw, Thread.currentThread().getContextClassLoader()); 83 this.global = context.createGlobal(); 84 } 85 86 @AfterClass 87 public void tearDownTest() { 88 this.context = null; 89 this.global = null; 90 } 91 92 @Test 93 public void compileAllTests() { 94 if (TEST262) { 95 compileTestSet(new File(TEST262_SUITE_DIR), new TestFilter() { 96 @Override 97 public boolean exclude(final File file, final String content) { 98 return content.indexOf("@negative") != -1; 99 } 100 }); 101 } 102 compileTestSet(new File(TEST_BASIC_DIR), null); 103 compileTestSet(new File(TEST_NODE_DIR, "node"), null); 104 compileTestSet(new File(TEST_NODE_DIR, "src"), null); 105 } 106 107 private void compileTestSet(final File testSetDir, final TestFilter filter) { 108 passed = 0; 109 failed = 0; 110 skipped = 0; 111 if (! testSetDir.isDirectory()) { 112 log("WARNING: " + testSetDir + " not found or not a directory"); 113 return; 114 } 115 log(testSetDir.getAbsolutePath()); 116 compileJSDirectory(testSetDir, filter); 117 118 log(testSetDir + " compile done!"); 119 log("compile ok: " + passed); 120 log("compile failed: " + failed); 121 log("compile skipped: " + skipped); 122 if (failed != 0) { 123 Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath()); 124 } 125 } 126 127 // number of scripts that compiled fine 128 private int passed; 129 // number of scripts resulting in compile failure 130 private int failed; 131 // scripts that were skipped - all tests with @negative are 132 // skipped for now. 133 private int skipped; 134 135 private void compileJSDirectory(final File dir, final TestFilter filter) { 136 for (final File f : dir.listFiles()) { 137 if (f.isDirectory()) { 138 compileJSDirectory(f, filter); 139 } else if (f.getName().endsWith(".js")) { 140 compileJSFile(f, filter); 141 } 142 } 143 } 144 145 private void compileJSFile(final File file, final TestFilter filter) { 146 if (VERBOSE) { 147 log("Begin compiling " + file.getAbsolutePath()); 148 } 149 150 final Global oldGlobal = Context.getGlobal(); 151 final boolean globalChanged = (oldGlobal != global); 152 153 try { 154 final char[] buffer = Source.readFully(file); 155 boolean excluded = false; 156 157 if (filter != null) { 158 final String content = new String(buffer); 159 excluded = filter.exclude(file, content); 160 } 161 162 if (excluded) { 163 if (VERBOSE) { 164 log("Skipping " + file.getAbsolutePath()); 165 } 166 skipped++; 167 return; 168 } 169 170 if (globalChanged) { 171 Context.setGlobal(global); 172 } 173 final Source source = new Source(file.getAbsolutePath(), buffer); 174 final ScriptFunction script = context.compileScript(source, global); 175 if (script == null || context.getErrorManager().getNumberOfErrors() > 0) { 176 log("Compile failed: " + file.getAbsolutePath()); 177 failed++; 178 } else { 179 passed++; 180 } 181 } catch (final Throwable t) { 182 log("Compile failed: " + file.getAbsolutePath() + " : " + t); 183 if (VERBOSE) { 184 t.printStackTrace(System.out); 185 } 186 failed++; 187 } finally { 188 if (globalChanged) { 189 Context.setGlobal(oldGlobal); 190 } 191 } 192 193 if (VERBOSE) { 194 log("Done compiling " + file.getAbsolutePath()); 195 } 196 } 197 }