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.Test;
  39 
  40 /**
  41  * Tests to check Nashorn JS compiler - just compiler and not execution of scripts.
  42  */
  43 public class CompilerTest {
  44     private static final boolean VERBOSE  = Boolean.valueOf(System.getProperty("compilertest.verbose"));
  45     private static final boolean TEST262  = Boolean.valueOf(System.getProperty("compilertest.test262"));


  46     private static final String TEST_BASIC_DIR  = System.getProperty("test.basic.dir");
  47     private static final String TEST262_SUITE_DIR = System.getProperty("test262.suite.dir");
  48 
  49     interface TestFilter {
  50         public boolean exclude(File file, String content);
  51     }
  52 
  53     private void log(String msg) {
  54         org.testng.Reporter.log(msg, true);
  55     }
  56 
  57     private Context context;
  58     private ScriptObject global;
  59 
  60     public CompilerTest() {
  61         final Options options = new Options("nashorn");
  62         options.set("anon.functions", true);
  63         options.set("compile.only", true);
  64         options.set("print.ast", true);
  65         options.set("print.parse", true);
  66         options.set("scripting", true);
  67 
  68         final ErrorManager errors = new ErrorManager() {
  69             @Override
  70             public void error(final String msg) {
  71                 log(msg);
  72             }
  73         };
  74 
  75         final StringWriter sw = new StringWriter();
  76         final PrintWriter pw = new PrintWriter(sw);
  77         this.context = new Context(options, errors, pw, pw);
  78         this.global = context.createGlobal();
  79     }
  80 
  81     @Test
  82     public void compileAllTests() {
  83         if (TEST262) {
  84             compileTestSet(TEST262_SUITE_DIR, new TestFilter() {
  85                 @Override
  86                 public boolean exclude(final File file, final String content) {
  87                     return content.indexOf("@negative") != -1;
  88                 }
  89             });
  90         }

  91         compileTestSet(TEST_BASIC_DIR, null);
  92     }
  93 
  94     private void compileTestSet(final String testSet, final TestFilter filter) {
  95         passed = 0;
  96         failed = 0;
  97         skipped = 0;
  98         final File testSetDir = new File(testSet);
  99         if (! testSetDir.isDirectory()) {
 100             log("WARNING: " + testSetDir + " not found or not a directory");
 101             return;
 102         }
 103         log(testSetDir.getAbsolutePath());
 104         compileJSDirectory(testSetDir, filter);
 105 
 106         log(testSet + " compile done!");
 107         log("compile ok: " + passed);
 108         log("compile failed: " + failed);
 109         log("compile skipped: " + skipped);
 110         if (failed != 0) {
 111             Assert.fail(failed + " tests failed to compile in " + testSetDir.getAbsolutePath());
 112         }
 113     }
 114 
 115     // number of scripts that compiled fine
 116     private int passed;
 117     // number of scripts resulting in compile failure
 118     private int failed;
 119     // scripts that were skipped - all tests with @negative are
 120     // skipped for now.
 121     private int skipped;
 122 
 123     private void compileJSDirectory(final File dir, final TestFilter filter) {
 124         for (final File f : dir.listFiles()) {
 125             if (f.isDirectory()) {
 126                 compileJSDirectory(f, filter);
 127             } else if (f.getName().endsWith(".js")) {
 128                 compileJSFile(f, filter);
 129             }
 130         }
 131     }
 132 
 133     private void compileJSFile(final File file, final TestFilter filter) {
 134         if (VERBOSE) {
 135             log("Begin compiling " + file.getAbsolutePath());
 136         }
 137 
 138         final ScriptObject oldGlobal = Context.getGlobal();
 139         final boolean globalChanged = (oldGlobal != global);
 140 
 141         try {
 142             final char[] buffer = Source.readFully(file);
 143             boolean excluded = false;
 144 
 145             if (filter != null) {
 146                 final String content = new String(buffer);
 147                 excluded = filter.exclude(file, content);
 148             }
 149 
 150             if (excluded) {
 151                 if (VERBOSE) {
 152                     log("Skipping " + file.getAbsolutePath());
 153                 }
 154                 skipped++;
 155                 return;
 156             }
 157 
 158             if (globalChanged) {
 159                 Context.setGlobal(global);
 160             }
 161             final Source source = new Source(file.getAbsolutePath(), buffer);
 162             final ScriptFunction script = context.compileScript(source, global, context._strict);
 163             if (script == null || context.getErrors().getNumberOfErrors() > 0) {
 164                 log("Compile failed: " + file.getAbsolutePath());
 165                 failed++;
 166             } else {
 167                 passed++;
 168             }
 169         } catch (final Throwable t) {
 170             log("Compile failed: " + file.getAbsolutePath() + " : " + t);
 171             if (VERBOSE) {
 172                 t.printStackTrace(System.out);
 173             }
 174             failed++;
 175         } finally {
 176             if (globalChanged) {
 177                 Context.setGlobal(oldGlobal);
 178             }
 179         }
 180 
 181         if (VERBOSE) {
 182             log("Done compiling " + file.getAbsolutePath());
 183         }
 184     }
 185 }
--- EOF ---