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