File: TestUtils.java

package info (click to toggle)
htmlunit-core-js 2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 5,804 kB
  • ctags: 10,088
  • sloc: java: 75,059; xml: 889; sh: 14; makefile: 11
file content (79 lines) | stat: -rw-r--r-- 2,608 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.mozilla.javascript.drivers;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Arrays;

import org.mozilla.javascript.ContextFactory;

public class TestUtils {
    private static ContextFactory.GlobalSetter globalSetter;

    public static void GrabContextFactoryGlobalSetter() {
        if (globalSetter == null) {
            globalSetter = ContextFactory.getGlobalSetter();
        }
    }

    public static void setGlobalContextFactory(ContextFactory factory) {
        GrabContextFactoryGlobalSetter();
        globalSetter.setContextFactoryGlobal(factory);
    }

    public static File[] recursiveListFiles(File dir, FileFilter filter) {
        if (!dir.isDirectory())
            throw new IllegalArgumentException(dir + " is not a directory");
        List<File> fileList = new ArrayList<File>();
        recursiveListFilesHelper(dir, filter, fileList);
        return fileList.toArray(new File[fileList.size()]);
    }

    public static void recursiveListFilesHelper(File dir, FileFilter filter,
                                                List<File> fileList)
    {
        for (File f: dir.listFiles()) {
            if (f.isDirectory()) {
                recursiveListFilesHelper(f, filter, fileList);
            } else {
                if (filter.accept(f))
                    fileList.add(f);
            }
        }
    }

    public static void addTestsFromFile(String filename, List<String> list)
            throws IOException {
        addTestsFromStream(new FileInputStream(new File(filename)), list);
    }

    public static void addTestsFromStream(InputStream in, List<String> list)
            throws IOException {
        Properties props = new Properties();
        props.load(in);
        for (Object obj: props.keySet()) {
            list.add(obj.toString());
        }
    }

    public static String[] loadTestsFromResource(String resource, String[] inherited)
            throws IOException {
        List<String> list = inherited == null ?
                new ArrayList<String>() :
                new ArrayList<String>(Arrays.asList(inherited));
        InputStream in = StandardTests.class.getResourceAsStream(resource);
        if (in != null)
            addTestsFromStream(in, list);
        return list.toArray(new String[0]);
    }

    public static boolean matches(String[] patterns, String path) {
        for (int i=0; i<patterns.length; i++) {
            if (path.startsWith(patterns[i])) {
                return true;
            }
        }
        return false;
    }
}