nashorn/test/src/jdk/nashorn/api/tree/ParseAPITest.java
changeset 29784 b9220497eb43
parent 29783 db33e568f107
parent 29770 6415d011ad02
child 29785 da950f343762
equal deleted inserted replaced
29783:db33e568f107 29784:b9220497eb43
     1 /*
       
     2  * Copyright (c) 2015, 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 package jdk.nashorn.api.tree;
       
    26 
       
    27 import java.io.File;
       
    28 import java.io.IOException;
       
    29 import java.nio.charset.Charset;
       
    30 import java.nio.charset.StandardCharsets;
       
    31 import java.nio.file.Files;
       
    32 import org.testng.Assert;
       
    33 import org.testng.annotations.Test;
       
    34 
       
    35 /**
       
    36  * Test for nashorn Parser API (jdk.nashorn.api.tree.*)
       
    37  */
       
    38 public class ParseAPITest {
       
    39 
       
    40     private static final boolean VERBOSE   = Boolean.valueOf(System.getProperty("parserapitest.verbose"));
       
    41     private static final boolean TEST262   = Boolean.valueOf(System.getProperty("parserapitest.test262"));
       
    42 
       
    43     private static final String TEST_BASIC_DIR     = System.getProperty("test.basic.dir");
       
    44     private static final String TEST_MAPTESTS_DIR  = System.getProperty("test.maptests.dir");
       
    45     private static final String TEST_SANDBOX_DIR   = System.getProperty("test.sandbox.dir");
       
    46     private static final String TEST_TRUSTED_DIR   = System.getProperty("test.trusted.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 static final String[] options = new String[] {
       
    58         "-scripting", "--const-as-var"
       
    59     };
       
    60 
       
    61     @Test
       
    62     public void parseAllTests() {
       
    63         if (TEST262) {
       
    64             parseTestSet(TEST262_SUITE_DIR, new TestFilter() {
       
    65                 @Override
       
    66                 public boolean exclude(final File file, final String content) {
       
    67                     return content.indexOf("@negative") != -1;
       
    68                 }
       
    69             });
       
    70         }
       
    71         parseTestSet(TEST_BASIC_DIR, new TestFilter() {
       
    72             @Override
       
    73             public boolean exclude(final File file, final String content) {
       
    74                 return file.getParentFile().getName().equals("es6");
       
    75             }
       
    76         });
       
    77         parseTestSet(TEST_MAPTESTS_DIR, null);
       
    78         parseTestSet(TEST_SANDBOX_DIR, null);
       
    79         parseTestSet(TEST_TRUSTED_DIR, null);
       
    80     }
       
    81 
       
    82     private void parseTestSet(final String testSet, final TestFilter filter) {
       
    83         passed  = 0;
       
    84         failed  = 0;
       
    85         skipped = 0;
       
    86 
       
    87         final File testSetDir = new File(testSet);
       
    88         if (! testSetDir.isDirectory()) {
       
    89             log("WARNING: " + testSetDir + " not found or not a directory");
       
    90             return;
       
    91         }
       
    92         log(testSetDir.getAbsolutePath());
       
    93         parseJSDirectory(testSetDir, filter);
       
    94 
       
    95         log(testSet + " parse API done!");
       
    96         log("parse API ok: " + passed);
       
    97         log("parse API failed: " + failed);
       
    98         log("parse API skipped: " + skipped);
       
    99         if (failed != 0) {
       
   100             Assert.fail(failed + " tests failed to parse in " + testSetDir.getAbsolutePath());
       
   101         }
       
   102     }
       
   103 
       
   104     // number of scripts that parsed fine
       
   105     private int passed;
       
   106     // number of scripts resulting in parse failure
       
   107     private int failed;
       
   108     // scripts that were skipped - all tests with @negative are
       
   109     // skipped for now.
       
   110     private int skipped;
       
   111 
       
   112     private void parseJSDirectory(final File dir, final TestFilter filter) {
       
   113         for (final File f : dir.listFiles()) {
       
   114             if (f.isDirectory()) {
       
   115                 parseJSDirectory(f, filter);
       
   116             } else if (f.getName().endsWith(".js")) {
       
   117                 parseJSFile(f, filter);
       
   118             }
       
   119         }
       
   120     }
       
   121 
       
   122     private void parseJSFile(final File file, final TestFilter filter) {
       
   123         if (VERBOSE) {
       
   124             log("Begin parsing " + file.getAbsolutePath());
       
   125         }
       
   126 
       
   127         try {
       
   128             final char[] buffer = readFully(file);
       
   129             final String content = new String(buffer);
       
   130             boolean excluded = false;
       
   131             if (filter != null) {
       
   132                 excluded = filter.exclude(file, content);
       
   133             }
       
   134 
       
   135             if (excluded) {
       
   136                 if (VERBOSE) {
       
   137                     log("Skipping " + file.getAbsolutePath());
       
   138                 }
       
   139                 skipped++;
       
   140                 return;
       
   141             }
       
   142 
       
   143             final Parser parser = Parser.create(options);
       
   144             final Tree tree = parser.parse(file.getAbsolutePath(), content, null);
       
   145             tree.accept(new SimpleTreeVisitorES5_1<Void, Void>(), null);
       
   146             passed++;
       
   147         } catch (final Throwable exp) {
       
   148             log("Parse API failed: " + file.getAbsolutePath() + " : " + exp);
       
   149             //if (VERBOSE) {
       
   150                 exp.printStackTrace(System.out);
       
   151             //}
       
   152             failed++;
       
   153         }
       
   154 
       
   155         if (VERBOSE) {
       
   156             log("Done parsing via parser API " + file.getAbsolutePath());
       
   157         }
       
   158     }
       
   159 
       
   160     private static char[] byteToCharArray(final byte[] bytes) {
       
   161         Charset cs = StandardCharsets.UTF_8;
       
   162         int start = 0;
       
   163         // BOM detection.
       
   164         if (bytes.length > 1 && bytes[0] == (byte) 0xFE && bytes[1] == (byte) 0xFF) {
       
   165             start = 2;
       
   166             cs = StandardCharsets.UTF_16BE;
       
   167         } else if (bytes.length > 1 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE) {
       
   168             start = 2;
       
   169             cs = StandardCharsets.UTF_16LE;
       
   170         } else if (bytes.length > 2 && bytes[0] == (byte) 0xEF && bytes[1] == (byte) 0xBB && bytes[2] == (byte) 0xBF) {
       
   171             start = 3;
       
   172             cs = StandardCharsets.UTF_8;
       
   173         } else if (bytes.length > 3 && bytes[0] == (byte) 0xFF && bytes[1] == (byte) 0xFE && bytes[2] == 0 && bytes[3] == 0) {
       
   174             start = 4;
       
   175             cs = Charset.forName("UTF-32LE");
       
   176         } else if (bytes.length > 3 && bytes[0] == 0 && bytes[1] == 0 && bytes[2] == (byte) 0xFE && bytes[3] == (byte) 0xFF) {
       
   177             start = 4;
       
   178             cs = Charset.forName("UTF-32BE");
       
   179         }
       
   180 
       
   181         return new String(bytes, start, bytes.length - start, cs).toCharArray();
       
   182     }
       
   183 
       
   184     private static char[] readFully(final File file) throws IOException {
       
   185         final byte[] buf = Files.readAllBytes(file.toPath());
       
   186         return byteToCharArray(buf);
       
   187     }
       
   188 }