Debian Codegraph @

rhino/testsrc/org/mozilla/javascript/drivers/TestUtils.java

raw symbols SCIP
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 package org.mozilla.javascript.drivers;
6
7 import java.io.*;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.Arrays;
12
13 import org.mozilla.javascript.ContextFactory;
14
15 public class TestUtils {
16 private static ContextFactory.GlobalSetter globalSetter;
17
18 public static void grabContextFactoryGlobalSetter() {
19 if (globalSetter == null) {
20 globalSetter = ContextFactory.getGlobalSetter();
21 }
22 }
23
24 public static void setGlobalContextFactory(ContextFactory factory) {
25 grabContextFactoryGlobalSetter();
26 globalSetter.setContextFactoryGlobal(factory);
27 }
28
29 public static File[] recursiveListFiles(File dir, FileFilter filter) {
30 if (!dir.isDirectory())
31 throw new IllegalArgumentException(dir + " is not a directory");
32 List<File> fileList = new ArrayList<File>();
33 recursiveListFilesHelper(dir, filter, fileList);
34 return fileList.toArray(new File[fileList.size()]);
35 }
36
37 public static void recursiveListFilesHelper(File dir, FileFilter filter,
38 List<File> fileList)
39 {
40 for (File f: dir.listFiles()) {
41 if (f.isDirectory()) {
42 recursiveListFilesHelper(f, filter, fileList);
43 } else {
44 if (filter.accept(f))
45 fileList.add(f);
46 }
47 }
48 }
49
50 public static void addTestsFromFile(String filename, List<String> list)
51 throws IOException {
52 addTestsFromStream(new FileInputStream(new File(filename)), list);
53 }
54
55 public static void addTestsFromStream(InputStream in, List<String> list)
56 throws IOException {
57 Properties props = new Properties();
58 props.load(in);
59 for (Object obj: props.keySet()) {
60 list.add(obj.toString());
61 }
62 }
63
64 public static String[] loadTestsFromResource(String resource, String[] inherited)
65 throws IOException {
66 List<String> list = inherited == null ?
67 new ArrayList<String>() :
68 new ArrayList<String>(Arrays.asList(inherited));
69 InputStream in = JsTestsBase.class.getResourceAsStream(resource);
70 if (in != null)
71 addTestsFromStream(in, list);
72 return list.toArray(new String[0]);
73 }
74
75 public static boolean matches(String[] patterns, String path) {
76 for (int i=0; i<patterns.length; i++) {
77 if (path.startsWith(patterns[i])) {
78 return true;
79 }
80 }
81 return false;
82 }
83
84 public static final FileFilter JS_FILE_FILTER = new FileFilter() {
85 @Override
86 public boolean accept(File pathname) {
87 return pathname.getAbsolutePath().endsWith(".js");
88 }
89 };
90
91 }