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