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