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