< prev index next >

test/src/jdk/nashorn/internal/test/framework/TestFinder.java

Print this page




   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.test.framework;
  27 
  28 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG;
  29 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_COMPARE;
  30 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_COMPILE_FAIL;
  31 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_RUN_FAIL;
  32 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_FORK;
  33 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_IGNORE_STD_ERROR;
  34 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_RUN;
  35 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_FAILED_LIST_FILE;
  36 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ENABLE_STRICT_MODE;
  37 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDES_FILE;
  38 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_DIR;
  39 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_LIST;
  40 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_FRAMEWORK;
  41 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_INCLUDES;
  42 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_LIST;
  43 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ROOTS;
  44 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_UNCHECKED_DIR;
  45 import java.io.BufferedReader;
  46 import java.io.File;
  47 import java.io.FileReader;
  48 import java.io.IOException;
  49 import java.nio.ByteOrder;
  50 import java.nio.file.FileSystem;
  51 import java.nio.file.FileSystems;
  52 import java.nio.file.FileVisitOption;
  53 import java.nio.file.FileVisitResult;
  54 import java.nio.file.Files;
  55 import java.nio.file.Path;
  56 import java.nio.file.SimpleFileVisitor;
  57 import java.nio.file.attribute.BasicFileAttributes;
  58 import java.util.ArrayList;
  59 import java.util.Arrays;
  60 import java.util.Collections;
  61 import java.util.EnumSet;
  62 import java.util.HashMap;
  63 import java.util.HashSet;

  64 import java.util.List;
  65 import java.util.Map;
  66 import java.util.Scanner;
  67 import java.util.Set;
  68 import javax.xml.xpath.XPath;
  69 import javax.xml.xpath.XPathConstants;
  70 import javax.xml.xpath.XPathExpressionException;
  71 import javax.xml.xpath.XPathFactory;

  72 import org.w3c.dom.NodeList;
  73 import org.xml.sax.InputSource;
  74 
  75 /**
  76  * Utility class to find/parse script test files and to create 'test' instances.
  77  * Actual 'test' object type is decided by clients of this class.
  78  */
  79 @SuppressWarnings("javadoc")
  80 public final class TestFinder {
  81     private TestFinder() {}


  82 
  83     interface TestFactory<T> {

  84         // 'test' instance type is decided by the client.

  85         T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments);

  86         // place to log messages from TestFinder

  87         void log(String mg);
  88     }
  89 
  90 
  91     // finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found
  92     static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception {
  93         final String framework = System.getProperty(TEST_JS_FRAMEWORK);
  94         final String testList = System.getProperty(TEST_JS_LIST);
  95         final String failedTestFileName = System.getProperty(TEST_FAILED_LIST_FILE);
  96         if(failedTestFileName != null) {
  97             final File failedTestFile = new File(failedTestFileName);
  98             if(failedTestFile.exists() && failedTestFile.length() > 0L) {
  99                 try(final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) {
 100                     for(;;) {
 101                         final String testFileName = r.readLine();
 102                         if(testFileName == null) {
 103                             break;
 104                         }
 105                         handleOneTest(framework, new File(testFileName).toPath(), tests, orphans, testFactory);
 106                     }
 107                 }
 108                 return;
 109             }
 110         }
 111         if (testList == null || testList.length() == 0) {
 112             // Run the tests under the test roots dir, selected by the
 113             // TEST_JS_INCLUDES patterns
 114             final String testRootsString = System.getProperty(TEST_JS_ROOTS, "test/script");
 115             if (testRootsString == null || testRootsString.length() == 0) {
 116                 throw new Exception("Error: " + TEST_JS_ROOTS + " must be set");
 117             }
 118             final String testRoots[] = testRootsString.split(" ");
 119             final FileSystem fileSystem = FileSystems.getDefault();
 120             final Set<String> testExcludeSet = getExcludeSet();
 121             final Path[] excludePaths = getExcludeDirs();
 122             for (final String root : testRoots) {


 134 
 135     private static boolean inExcludePath(final Path file, final Path[] excludePaths) {
 136         if (excludePaths == null) {
 137             return false;
 138         }
 139 
 140         for (final Path excludePath : excludePaths) {
 141             if (file.startsWith(excludePath)) {
 142                 return true;
 143             }
 144         }
 145         return false;
 146     }
 147 
 148     private static <T> void findTests(final String framework, final Path dir, final List<T> tests, final Set<String> orphanFiles, final Path[] excludePaths, final Set<String> excludedTests, final TestFactory<T> factory) throws Exception {
 149         final String pattern = System.getProperty(TEST_JS_INCLUDES);
 150         final String extension = pattern == null ? "js" : pattern;
 151         final Exception[] exceptions = new Exception[1];
 152         final List<String> excludedActualTests = new ArrayList<>();
 153 
 154         if (! dir.toFile().isDirectory()) {
 155             factory.log("WARNING: " + dir + " not found or not a directory");
 156         }
 157 
 158         Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
 159             @Override
 160             public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
 161                 final String fileName = file.getName(file.getNameCount() - 1).toString();
 162                 if (fileName.endsWith(extension)) {
 163                     final String namex = file.toString().replace('\\', '/');
 164                     if (!inExcludePath(file, excludePaths) && !excludedTests.contains(file.getFileName().toString())) {
 165                         try {
 166                             handleOneTest(framework, file, tests, orphanFiles, factory);
 167                         } catch (final Exception ex) {
 168                             exceptions[0] = ex;
 169                             return FileVisitResult.TERMINATE;
 170                         }
 171                     } else {
 172                         excludedActualTests.add(namex);
 173                     }
 174                 }


 202 
 203         assert name.lastIndexOf(".js") > 0 : "not a JavaScript: " + name;
 204 
 205         // defaults: testFile is a test and should be run
 206         boolean isTest = isUnchecked(testFile);
 207         boolean isNotTest = false;
 208         boolean shouldRun = true;
 209         boolean compileFailure = false;
 210         boolean runFailure = false;
 211         boolean checkCompilerMsg = false;
 212         boolean noCompare = false;
 213         boolean ignoreStdError = false;
 214         boolean fork = false;
 215 
 216         final List<String> engineOptions = new ArrayList<>();
 217         final List<String> scriptArguments = new ArrayList<>();
 218         boolean inComment = false;
 219 
 220         boolean explicitOptimistic = false;
 221 
 222         try (Scanner scanner = new Scanner(testFile)) {

 223             while (scanner.hasNext()) {
 224                 // TODO: Scan for /ref=file qualifiers, etc, to determine run
 225                 // behavior
 226                 String token = scanner.next();
 227                 if (token.startsWith("/*")) {
 228                     inComment = true;
 229                 } else if (token.endsWith(("*/"))) {
 230                     inComment = false;
 231                 } else if (!inComment) {
 232                     continue;
 233                 }
 234 
 235                 // remove whitespace and trailing semicolons, if any
 236                 // (trailing semicolons are found in some sputnik tests)
 237                 token = token.trim();
 238                 final int semicolon = token.indexOf(';');
 239                 if (semicolon > 0) {
 240                     token = token.substring(0, semicolon);
 241                 }
 242                 switch (token) {


 307                     fork = true;
 308                     break;
 309                 default:
 310                     break;
 311                 }
 312 
 313                 // negative tests are expected to fail at runtime only
 314                 // for those tests that are expected to fail at compile time,
 315                 // add @test/compile-error
 316                 if (token.equals("@negative") || token.equals("@strict_mode_negative")) {
 317                     shouldRun = true;
 318                     runFailure = true;
 319                 }
 320 
 321                 if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) {
 322                     if (!strictModeEnabled()) {
 323                         return;
 324                     }
 325                 }
 326             }
 327         } catch (final Exception ignored) {
 328             return;
 329         }
 330 
 331         if (isTest) {
 332             final Map<String, String> testOptions = new HashMap<>();
 333             if (compileFailure) {
 334                 testOptions.put(OPTIONS_EXPECT_COMPILE_FAIL, "true");
 335             }
 336             if (shouldRun) {
 337                 testOptions.put(OPTIONS_RUN, "true");
 338             }
 339             if (runFailure) {
 340                 testOptions.put(OPTIONS_EXPECT_RUN_FAIL, "true");
 341             }
 342             if (checkCompilerMsg) {
 343                 testOptions.put(OPTIONS_CHECK_COMPILE_MSG, "true");
 344             }
 345             if (!noCompare) {
 346                 testOptions.put(OPTIONS_COMPARE, "true");
 347             }
 348             if (ignoreStdError) {
 349                 testOptions.put(OPTIONS_IGNORE_STD_ERROR, "true");


 352                 testOptions.put(OPTIONS_FORK, "true");
 353             }
 354 
 355             //if there are explicit optimistic type settings, use those - do not override
 356             //the test might only work with optimistic types on or off.
 357             if (!explicitOptimistic) {
 358                 addExplicitOptimisticTypes(engineOptions);
 359             }
 360 
 361             tests.add(factory.createTest(framework, testFile.toFile(), engineOptions, testOptions, scriptArguments));
 362         } else if (!isNotTest) {
 363             orphans.add(name);
 364         }
 365     }
 366 
 367     //the reverse of the default setting for optimistic types, if enabled, false, otherwise true
 368     //thus, true for 8u40, false for 9
 369     private static final boolean OPTIMISTIC_OVERRIDE = false;
 370 
 371     /**
 372      * Check if there is an optimistic override, that disables the default
 373      * false optimistic types and sets them to true, for testing purposes
 374      *
 375      * @return true if optimistic type override has been set by test suite
 376      */
 377     public static boolean hasOptimisticOverride() {
 378         return Boolean.valueOf(OPTIMISTIC_OVERRIDE).toString().equals(System.getProperty("optimistic.override"));
 379     }
 380 
 381     /**
 382      * Add an optimistic-types=true option to an argument list if this
 383      * is set to override the default false. Add an optimistic-types=true
 384      * options to an argument list if this is set to override the default
 385      * true
 386      *
 387      * @args new argument list array
 388      */
 389     public static String[] addExplicitOptimisticTypes(final String[] args) {
 390         if (hasOptimisticOverride()) {
 391             final List<String> newList = new ArrayList<>(Arrays.asList(args));
 392             newList.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE));
 393             return newList.toArray(new String[0]);
 394         }
 395         return args;
 396     }
 397 
 398     /**
 399      * Add an optimistic-types=true option to an argument list if this
 400      * is set to override the default false
 401      *
 402      * @args argument list
 403      */
 404     public static void addExplicitOptimisticTypes(final List<String> args) {
 405         if (hasOptimisticOverride()) {
 406             args.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE));
 407         }
 408     }
 409 
 410     private static boolean strictModeEnabled() {
 411         return Boolean.getBoolean(TEST_JS_ENABLE_STRICT_MODE);
 412     }
 413 
 414     private static Set<String> getExcludeSet() throws XPathExpressionException {
 415         final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
 416 
 417         String[] testExcludeArray = {};
 418         if (testExcludeList != null) {
 419             testExcludeArray = testExcludeList.split(" ");
 420         }
 421         final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
 422         for (final String test : testExcludeArray) {
 423             testExcludeSet.add(test);
 424         }
 425 
 426         final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
 427         if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
 428             try {
 429                 loadExcludesFile(testExcludesFile, testExcludeSet);
 430             } catch (final XPathExpressionException e) {
 431                 System.err.println("Error: unable to load test excludes from " + testExcludesFile);
 432                 e.printStackTrace();
 433                 throw e;
 434             }
 435         }
 436         return testExcludeSet;
 437     }
 438 
 439     private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException {
 440         final XPath xpath = XPathFactory.newInstance().newXPath();
 441         final NodeList testIds = (NodeList)xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET);
 442         for (int i = testIds.getLength() - 1; i >= 0; i--) {
 443             testExcludeSet.add(testIds.item(i).getNodeValue());
 444         }
 445     }
 446 
 447     private static Path[] getExcludeDirs() {
 448         final String excludeDirs[] = System.getProperty(TEST_JS_EXCLUDE_DIR, "test/script/currently-failing").split(" ");
 449         final Path[] excludePaths = new Path[excludeDirs.length];
 450         final FileSystem fileSystem = FileSystems.getDefault();
 451         int i = 0;
 452         for (final String excludeDir : excludeDirs) {
 453             excludePaths[i++] = fileSystem.getPath(excludeDir);
 454         }
 455         return excludePaths;
 456     }
 457 }


   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.internal.test.framework;
  26 
  27 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_CHECK_COMPILE_MSG;
  28 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_COMPARE;
  29 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_COMPILE_FAIL;
  30 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_EXPECT_RUN_FAIL;
  31 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_FORK;
  32 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_IGNORE_STD_ERROR;
  33 import static jdk.nashorn.internal.test.framework.TestConfig.OPTIONS_RUN;
  34 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_FAILED_LIST_FILE;
  35 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ENABLE_STRICT_MODE;
  36 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDES_FILE;
  37 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_DIR;
  38 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_EXCLUDE_LIST;
  39 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_FRAMEWORK;
  40 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_INCLUDES;
  41 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_LIST;
  42 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_ROOTS;
  43 import static jdk.nashorn.internal.test.framework.TestConfig.TEST_JS_UNCHECKED_DIR;
  44 import java.io.BufferedReader;
  45 import java.io.File;
  46 import java.io.FileReader;
  47 import java.io.IOException;
  48 import java.nio.ByteOrder;
  49 import java.nio.file.FileSystem;
  50 import java.nio.file.FileSystems;
  51 import java.nio.file.FileVisitOption;
  52 import java.nio.file.FileVisitResult;
  53 import java.nio.file.Files;
  54 import java.nio.file.Path;
  55 import java.nio.file.SimpleFileVisitor;
  56 import java.nio.file.attribute.BasicFileAttributes;
  57 import java.util.ArrayList;
  58 import java.util.Arrays;
  59 import java.util.Collections;
  60 import java.util.EnumSet;
  61 import java.util.HashMap;
  62 import java.util.HashSet;
  63 import java.util.Iterator;
  64 import java.util.List;
  65 import java.util.Map;

  66 import java.util.Set;
  67 import javax.xml.xpath.XPath;
  68 import javax.xml.xpath.XPathConstants;
  69 import javax.xml.xpath.XPathExpressionException;
  70 import javax.xml.xpath.XPathFactory;
  71 import jdk.nashorn.internal.runtime.ScriptingFunctions;
  72 import org.w3c.dom.NodeList;
  73 import org.xml.sax.InputSource;
  74 
  75 /**
  76  * Utility class to find/parse script test files and to create 'test' instances.
  77  * Actual 'test' object type is decided by clients of this class.
  78  */
  79 @SuppressWarnings("javadoc")
  80 public final class TestFinder {
  81 
  82     private TestFinder() {
  83     }
  84 
  85     interface TestFactory<T> {
  86 
  87         // 'test' instance type is decided by the client.
  88 
  89         T createTest(final String framework, final File testFile, final List<String> engineOptions, final Map<String, String> testOptions, final List<String> arguments);
  90 
  91         // place to log messages from TestFinder
  92 
  93         void log(String mg);
  94     }
  95 

  96     // finds all tests from configuration and calls TestFactory to create 'test' instance for each script test found
  97     static <T> void findAllTests(final List<T> tests, final Set<String> orphans, final TestFactory<T> testFactory) throws Exception {
  98         final String framework = System.getProperty(TEST_JS_FRAMEWORK);
  99         final String testList = System.getProperty(TEST_JS_LIST);
 100         final String failedTestFileName = System.getProperty(TEST_FAILED_LIST_FILE);
 101         if (failedTestFileName != null) {
 102             final File failedTestFile = new File(failedTestFileName);
 103             if (failedTestFile.exists() && failedTestFile.length() > 0L) {
 104                 try (final BufferedReader r = new BufferedReader(new FileReader(failedTestFile))) {
 105                     for (;;) {
 106                         final String testFileName = r.readLine();
 107                         if (testFileName == null) {
 108                             break;
 109                         }
 110                         handleOneTest(framework, new File(testFileName).toPath(), tests, orphans, testFactory);
 111                     }
 112                 }
 113                 return;
 114             }
 115         }
 116         if (testList == null || testList.length() == 0) {
 117             // Run the tests under the test roots dir, selected by the
 118             // TEST_JS_INCLUDES patterns
 119             final String testRootsString = System.getProperty(TEST_JS_ROOTS, "test/script");
 120             if (testRootsString == null || testRootsString.length() == 0) {
 121                 throw new Exception("Error: " + TEST_JS_ROOTS + " must be set");
 122             }
 123             final String testRoots[] = testRootsString.split(" ");
 124             final FileSystem fileSystem = FileSystems.getDefault();
 125             final Set<String> testExcludeSet = getExcludeSet();
 126             final Path[] excludePaths = getExcludeDirs();
 127             for (final String root : testRoots) {


 139 
 140     private static boolean inExcludePath(final Path file, final Path[] excludePaths) {
 141         if (excludePaths == null) {
 142             return false;
 143         }
 144 
 145         for (final Path excludePath : excludePaths) {
 146             if (file.startsWith(excludePath)) {
 147                 return true;
 148             }
 149         }
 150         return false;
 151     }
 152 
 153     private static <T> void findTests(final String framework, final Path dir, final List<T> tests, final Set<String> orphanFiles, final Path[] excludePaths, final Set<String> excludedTests, final TestFactory<T> factory) throws Exception {
 154         final String pattern = System.getProperty(TEST_JS_INCLUDES);
 155         final String extension = pattern == null ? "js" : pattern;
 156         final Exception[] exceptions = new Exception[1];
 157         final List<String> excludedActualTests = new ArrayList<>();
 158 
 159         if (!dir.toFile().isDirectory()) {
 160             factory.log("WARNING: " + dir + " not found or not a directory");
 161         }
 162 
 163         Files.walkFileTree(dir, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
 164             @Override
 165             public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
 166                 final String fileName = file.getName(file.getNameCount() - 1).toString();
 167                 if (fileName.endsWith(extension)) {
 168                     final String namex = file.toString().replace('\\', '/');
 169                     if (!inExcludePath(file, excludePaths) && !excludedTests.contains(file.getFileName().toString())) {
 170                         try {
 171                             handleOneTest(framework, file, tests, orphanFiles, factory);
 172                         } catch (final Exception ex) {
 173                             exceptions[0] = ex;
 174                             return FileVisitResult.TERMINATE;
 175                         }
 176                     } else {
 177                         excludedActualTests.add(namex);
 178                     }
 179                 }


 207 
 208         assert name.lastIndexOf(".js") > 0 : "not a JavaScript: " + name;
 209 
 210         // defaults: testFile is a test and should be run
 211         boolean isTest = isUnchecked(testFile);
 212         boolean isNotTest = false;
 213         boolean shouldRun = true;
 214         boolean compileFailure = false;
 215         boolean runFailure = false;
 216         boolean checkCompilerMsg = false;
 217         boolean noCompare = false;
 218         boolean ignoreStdError = false;
 219         boolean fork = false;
 220 
 221         final List<String> engineOptions = new ArrayList<>();
 222         final List<String> scriptArguments = new ArrayList<>();
 223         boolean inComment = false;
 224 
 225         boolean explicitOptimistic = false;
 226 
 227         String allContent = new String(Files.readAllBytes(testFile));
 228         Iterator<String> scanner = ScriptingFunctions.tokenizeCommandLine(allContent).iterator();
 229         while (scanner.hasNext()) {
 230             // TODO: Scan for /ref=file qualifiers, etc, to determine run
 231             // behavior
 232             String token = scanner.next();
 233             if (token.startsWith("/*")) {
 234                 inComment = true;
 235             } else if (token.endsWith(("*/"))) {
 236                 inComment = false;
 237             } else if (!inComment) {
 238                 continue;
 239             }
 240 
 241             // remove whitespace and trailing semicolons, if any
 242             // (trailing semicolons are found in some sputnik tests)
 243             token = token.trim();
 244             final int semicolon = token.indexOf(';');
 245             if (semicolon > 0) {
 246                 token = token.substring(0, semicolon);
 247             }
 248             switch (token) {


 313                     fork = true;
 314                     break;
 315                 default:
 316                     break;
 317             }
 318 
 319             // negative tests are expected to fail at runtime only
 320             // for those tests that are expected to fail at compile time,
 321             // add @test/compile-error
 322             if (token.equals("@negative") || token.equals("@strict_mode_negative")) {
 323                 shouldRun = true;
 324                 runFailure = true;
 325             }
 326 
 327             if (token.equals("@strict_mode") || token.equals("@strict_mode_negative") || token.equals("@onlyStrict") || token.equals("@noStrict")) {
 328                 if (!strictModeEnabled()) {
 329                     return;
 330                 }
 331             }
 332         }



 333 
 334         if (isTest) {
 335             final Map<String, String> testOptions = new HashMap<>();
 336             if (compileFailure) {
 337                 testOptions.put(OPTIONS_EXPECT_COMPILE_FAIL, "true");
 338             }
 339             if (shouldRun) {
 340                 testOptions.put(OPTIONS_RUN, "true");
 341             }
 342             if (runFailure) {
 343                 testOptions.put(OPTIONS_EXPECT_RUN_FAIL, "true");
 344             }
 345             if (checkCompilerMsg) {
 346                 testOptions.put(OPTIONS_CHECK_COMPILE_MSG, "true");
 347             }
 348             if (!noCompare) {
 349                 testOptions.put(OPTIONS_COMPARE, "true");
 350             }
 351             if (ignoreStdError) {
 352                 testOptions.put(OPTIONS_IGNORE_STD_ERROR, "true");


 355                 testOptions.put(OPTIONS_FORK, "true");
 356             }
 357 
 358             //if there are explicit optimistic type settings, use those - do not override
 359             //the test might only work with optimistic types on or off.
 360             if (!explicitOptimistic) {
 361                 addExplicitOptimisticTypes(engineOptions);
 362             }
 363 
 364             tests.add(factory.createTest(framework, testFile.toFile(), engineOptions, testOptions, scriptArguments));
 365         } else if (!isNotTest) {
 366             orphans.add(name);
 367         }
 368     }
 369 
 370     //the reverse of the default setting for optimistic types, if enabled, false, otherwise true
 371     //thus, true for 8u40, false for 9
 372     private static final boolean OPTIMISTIC_OVERRIDE = false;
 373 
 374     /**
 375      * Check if there is an optimistic override, that disables the default false
 376      * optimistic types and sets them to true, for testing purposes
 377      *
 378      * @return true if optimistic type override has been set by test suite
 379      */
 380     public static boolean hasOptimisticOverride() {
 381         return Boolean.valueOf(OPTIMISTIC_OVERRIDE).toString().equals(System.getProperty("optimistic.override"));
 382     }
 383 
 384     /**
 385      * Add an optimistic-types=true option to an argument list if this is set to
 386      * override the default false. Add an optimistic-types=true options to an
 387      * argument list if this is set to override the default true

 388      *
 389      * @args new argument list array
 390      */
 391     public static String[] addExplicitOptimisticTypes(final String[] args) {
 392         if (hasOptimisticOverride()) {
 393             final List<String> newList = new ArrayList<>(Arrays.asList(args));
 394             newList.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE));
 395             return newList.toArray(new String[0]);
 396         }
 397         return args;
 398     }
 399 
 400     /**
 401      * Add an optimistic-types=true option to an argument list if this is set to
 402      * override the default false
 403      *
 404      * @args argument list
 405      */
 406     public static void addExplicitOptimisticTypes(final List<String> args) {
 407         if (hasOptimisticOverride()) {
 408             args.add("--optimistic-types=" + Boolean.valueOf(OPTIMISTIC_OVERRIDE));
 409         }
 410     }
 411 
 412     private static boolean strictModeEnabled() {
 413         return Boolean.getBoolean(TEST_JS_ENABLE_STRICT_MODE);
 414     }
 415 
 416     private static Set<String> getExcludeSet() throws XPathExpressionException {
 417         final String testExcludeList = System.getProperty(TEST_JS_EXCLUDE_LIST);
 418 
 419         String[] testExcludeArray = {};
 420         if (testExcludeList != null) {
 421             testExcludeArray = testExcludeList.split(" ");
 422         }
 423         final Set<String> testExcludeSet = new HashSet<>(testExcludeArray.length);
 424         for (final String test : testExcludeArray) {
 425             testExcludeSet.add(test);
 426         }
 427 
 428         final String testExcludesFile = System.getProperty(TEST_JS_EXCLUDES_FILE);
 429         if (testExcludesFile != null && !testExcludesFile.isEmpty()) {
 430             try {
 431                 loadExcludesFile(testExcludesFile, testExcludeSet);
 432             } catch (final XPathExpressionException e) {
 433                 System.err.println("Error: unable to load test excludes from " + testExcludesFile);
 434                 e.printStackTrace();
 435                 throw e;
 436             }
 437         }
 438         return testExcludeSet;
 439     }
 440 
 441     private static void loadExcludesFile(final String testExcludesFile, final Set<String> testExcludeSet) throws XPathExpressionException {
 442         final XPath xpath = XPathFactory.newInstance().newXPath();
 443         final NodeList testIds = (NodeList) xpath.evaluate("/excludeList/test/@id", new InputSource(testExcludesFile), XPathConstants.NODESET);
 444         for (int i = testIds.getLength() - 1; i >= 0; i--) {
 445             testExcludeSet.add(testIds.item(i).getNodeValue());
 446         }
 447     }
 448 
 449     private static Path[] getExcludeDirs() {
 450         final String excludeDirs[] = System.getProperty(TEST_JS_EXCLUDE_DIR, "test/script/currently-failing").split(" ");
 451         final Path[] excludePaths = new Path[excludeDirs.length];
 452         final FileSystem fileSystem = FileSystems.getDefault();
 453         int i = 0;
 454         for (final String excludeDir : excludeDirs) {
 455             excludePaths[i++] = fileSystem.getPath(excludeDir);
 456         }
 457         return excludePaths;
 458     }
 459 }
< prev index next >