| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package org.mozilla.javascript.tests; |
| 6 |
|
| 7 |
import java.io.File; |
| 8 |
import java.io.FileFilter; |
| 9 |
import java.io.FileNotFoundException; |
| 10 |
import java.io.FileReader; |
| 11 |
import java.io.IOException; |
| 12 |
import java.io.PrintStream; |
| 13 |
import java.net.URL; |
| 14 |
import java.util.ArrayList; |
| 15 |
import java.util.Arrays; |
| 16 |
import java.util.Collection; |
| 17 |
import java.util.HashSet; |
| 18 |
import java.util.List; |
| 19 |
|
| 20 |
import junit.framework.Assert; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
import org.junit.runner.RunWith; |
| 24 |
import org.junit.runners.Parameterized; |
| 25 |
import org.junit.runners.Parameterized.Parameters; |
| 26 |
import org.mozilla.javascript.drivers.JsTestsBase; |
| 27 |
import org.mozilla.javascript.drivers.ShellTest; |
| 28 |
import org.mozilla.javascript.drivers.TestUtils; |
| 29 |
import org.mozilla.javascript.tools.shell.ShellContextFactory; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
@RunWith(Parameterized.class) |
| 49 |
public class MozillaSuiteTest { |
| 50 |
private final File jsFile; |
| 51 |
private final int optimizationLevel; |
| 52 |
|
| 53 |
static final int[] OPT_LEVELS = { -1, 0, 9 }; |
| 54 |
|
| 55 |
public MozillaSuiteTest(File jsFile, int optimizationLevel) { |
| 56 |
this.jsFile = jsFile; |
| 57 |
this.optimizationLevel = optimizationLevel; |
| 58 |
} |
| 59 |
|
| 60 |
public static File getTestDir() throws IOException { |
| 61 |
File testDir = null; |
| 62 |
if (System.getProperty("mozilla.js.tests") != null) { |
| 63 |
testDir = new File(System.getProperty("mozilla.js.tests")); |
| 64 |
} else { |
| 65 |
URL url = JsTestsBase.class.getResource("."); |
| 66 |
String path = url.getFile(); |
| 67 |
int jsIndex = path.lastIndexOf("/js"); |
| 68 |
if (jsIndex == -1) { |
| 69 |
throw new IllegalStateException("You aren't running the tests "+ |
| 70 |
"from within the standard mozilla/js directory structure"); |
| 71 |
} |
| 72 |
path = path.substring(0, jsIndex + 3).replace('/', File.separatorChar); |
| 73 |
path = path.replace("%20", " "); |
| 74 |
testDir = new File(path, "tests"); |
| 75 |
} |
| 76 |
if (!testDir.isDirectory()) { |
| 77 |
throw new FileNotFoundException(testDir + " is not a directory"); |
| 78 |
} |
| 79 |
return testDir; |
| 80 |
} |
| 81 |
|
| 82 |
public static String getTestFilename(int optimizationLevel) { |
| 83 |
return "opt" + optimizationLevel + ".tests"; |
| 84 |
} |
| 85 |
|
| 86 |
public static File[] getTestFiles(int optimizationLevel) throws IOException { |
| 87 |
File testDir = getTestDir(); |
| 88 |
String[] tests = TestUtils.loadTestsFromResource( |
| 89 |
"/" + getTestFilename(optimizationLevel), null); |
| 90 |
Arrays.sort(tests); |
| 91 |
File[] files = new File[tests.length]; |
| 92 |
for (int i=0; i < files.length; i++) { |
| 93 |
files[i] = new File(testDir, tests[i]); |
| 94 |
} |
| 95 |
return files; |
| 96 |
} |
| 97 |
|
| 98 |
public static String loadFile(File f) throws IOException { |
| 99 |
int length = (int) f.length(); |
| 100 |
char[] buf = new char[length]; |
| 101 |
new FileReader(f).read(buf, 0, length); |
| 102 |
return new String(buf); |
| 103 |
} |
| 104 |
|
| 105 |
@Parameters(name = "{index}, js={0}, opt={1}") |
| 106 |
public static Collection<Object[]> mozillaSuiteValues() throws IOException { |
| 107 |
List<Object[]> result = new ArrayList<Object[]>(); |
| 108 |
int[] optLevels = OPT_LEVELS; |
| 109 |
for (int i=0; i < optLevels.length; i++) { |
| 110 |
File[] tests = getTestFiles(optLevels[i]); |
| 111 |
for (File f : tests) { |
| 112 |
result.add(new Object[] { f, optLevels[i] }); |
| 113 |
} |
| 114 |
} |
| 115 |
return result; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public static Collection<Object[]> singleDoctest() throws IOException { |
| 121 |
final String SINGLE_TEST_FILE = "..."; |
| 122 |
final int[] SINGLE_TEST_OPTIMIZATION_LEVEL = OPT_LEVELS; |
| 123 |
|
| 124 |
List<Object[]> result = new ArrayList<Object[]>(); |
| 125 |
for (int optLevel : SINGLE_TEST_OPTIMIZATION_LEVEL) { |
| 126 |
File f = new File(getTestDir(), SINGLE_TEST_FILE); |
| 127 |
result.add(new Object[] { f, optLevel }); |
| 128 |
} |
| 129 |
return result; |
| 130 |
} |
| 131 |
|
| 132 |
private static class ShellTestParameters extends ShellTest.Parameters { |
| 133 |
@Override |
| 134 |
public int getTimeoutMilliseconds() { |
| 135 |
if (System.getProperty("mozilla.js.tests.timeout") != null) { |
| 136 |
return Integer.parseInt(System.getProperty( |
| 137 |
"mozilla.js.tests.timeout")); |
| 138 |
} |
| 139 |
return 10000; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private static class JunitStatus extends ShellTest.Status { |
| 144 |
File file; |
| 145 |
|
| 146 |
@Override |
| 147 |
public final void running(File jsFile) { |
| 148 |
|
| 149 |
file = jsFile; |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
public final void failed(String s) { |
| 154 |
|
| 155 |
|
| 156 |
String msg = "In \"" + file + "\":" + |
| 157 |
System.getProperty("line.separator") + s; |
| 158 |
System.out.println(msg); |
| 159 |
Assert.fail(msg); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public final void exitCodesWere(int expected, int actual) { |
| 164 |
Assert.assertEquals("Unexpected exit code", expected, actual); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public final void outputWas(String s) { |
| 169 |
|
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
@Override |
| 174 |
public final void threw(Throwable t) { |
| 175 |
Assert.fail(ShellTest.getStackTrace(t)); |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public final void timedOut() { |
| 180 |
failed("Timed out."); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
@Test |
| 185 |
public void runMozillaTest() throws Exception { |
| 186 |
|
| 187 |
final ShellContextFactory shellContextFactory = |
| 188 |
new ShellContextFactory(); |
| 189 |
shellContextFactory.setOptimizationLevel(optimizationLevel); |
| 190 |
ShellTestParameters params = new ShellTestParameters(); |
| 191 |
JunitStatus status = new JunitStatus(); |
| 192 |
ShellTest.run(shellContextFactory, jsFile, params, status); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public static void main(String[] args) throws IOException { |
| 201 |
PrintStream out = new PrintStream("fix-tests-files.sh"); |
| 202 |
try { |
| 203 |
for (int i=0; i < OPT_LEVELS.length; i++) { |
| 204 |
int optLevel = OPT_LEVELS[i]; |
| 205 |
File testDir = getTestDir(); |
| 206 |
File[] allTests = |
| 207 |
TestUtils.recursiveListFiles(testDir, |
| 208 |
new FileFilter() { |
| 209 |
public boolean accept(File pathname) |
| 210 |
{ |
| 211 |
return ShellTest.DIRECTORY_FILTER.accept(pathname) || |
| 212 |
ShellTest.TEST_FILTER.accept(pathname); |
| 213 |
} |
| 214 |
}); |
| 215 |
HashSet<File> diff = new HashSet<File>(Arrays.asList(allTests)); |
| 216 |
File testFiles[] = getTestFiles(optLevel); |
| 217 |
diff.removeAll(Arrays.asList(testFiles)); |
| 218 |
ArrayList<String> skippedPassed = new ArrayList<String>(); |
| 219 |
int absolutePathLength = testDir.getAbsolutePath().length() + 1; |
| 220 |
for (File testFile: diff) { |
| 221 |
try { |
| 222 |
(new MozillaSuiteTest(testFile, optLevel)).runMozillaTest(); |
| 223 |
|
| 224 |
String canonicalized = |
| 225 |
testFile.getAbsolutePath().substring(absolutePathLength); |
| 226 |
canonicalized = canonicalized.replace('\\', '/'); |
| 227 |
skippedPassed.add(canonicalized); |
| 228 |
} catch (Throwable t) { |
| 229 |
|
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if (skippedPassed.size() > 0) { |
| 236 |
out.println("cat >> " + getTestFilename(optLevel) + " <<EOF"); |
| 237 |
String[] sorted = skippedPassed.toArray(new String[0]); |
| 238 |
Arrays.sort(sorted); |
| 239 |
for (int j=0; j < sorted.length; j++) { |
| 240 |
out.println(sorted[j]); |
| 241 |
} |
| 242 |
out.println("EOF"); |
| 243 |
} |
| 244 |
} |
| 245 |
System.out.println("Done."); |
| 246 |
} finally { |
| 247 |
out.close(); |
| 248 |
} |
| 249 |
} |
| 250 |
} |