001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1/GPL 2.0
003: *
004: * The contents of this file are subject to the Mozilla Public License Version
005: * 1.1 (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the
012: * License.
013: *
014: * The Original Code is Rhino code, released May 6, 1999.
015: *
016: * The Initial Developer of the Original Code is
017: * Netscape Communications Corporation.
018: * Portions created by the Initial Developer are Copyright (C) 1997-1999
019: * the Initial Developer. All Rights Reserved.
020: *
021: * Contributor(s):
022: * Attila Szegedi
023: * David P. Caldwell <inonit@inonit.com>
024: *
025: * Alternatively, the contents of this file may be used under the terms of
026: * the GNU General Public License Version 2 or later (the "GPL"), in which
027: * case the provisions of the GPL are applicable instead of those above. If
028: * you wish to allow use of your version of this file only under the terms of
029: * the GPL and not to allow others to use your version of this file under the
030: * MPL, indicate your decision by deleting the provisions above and replacing
031: * them with the notice and other provisions required by the GPL. If you do
032: * not delete the provisions above, a recipient may use your version of this
033: * file under either the MPL or the GPL.
034: *
035: * ***** END LICENSE BLOCK ***** */
036:
037: package org.mozilla.javascript.drivers;
038:
039: import java.io.File;
040: import java.io.FileNotFoundException;
041: import java.io.IOException;
042: import java.io.InputStream;
043: import java.net.URL;
044: import java.util.Arrays;
045: import java.util.Properties;
046:
047: import junit.framework.Assert;
048: import junit.framework.TestCase;
049: import junit.framework.TestSuite;
050:
051: import org.mozilla.javascript.tools.shell.ShellContextFactory;
052:
053: /**
054: * Executes the tests in the js/tests directory, much like jsDriver.pl does.
055: * Excludes tests found in the js/tests/rhino-n.tests file.
056: * @author Attila Szegedi
057: * @version $Id: StandardTests.java,v 1.6.2.2 2008/02/11 16:57:16 nboyd%atg.com Exp $
058: */
059: public class StandardTests extends TestSuite {
060: public static TestSuite suite() throws Exception {
061: TestSuite suite = new TestSuite("Standard JavaScript tests");
062:
063: File testDir = null;
064: if (System.getProperty("mozilla.js.tests") != null) {
065: testDir = new File(System.getProperty("mozilla.js.tests"));
066: } else {
067: URL url = StandardTests.class.getResource(".");
068: String path = url.getFile();
069: int jsIndex = path.lastIndexOf("/js");
070: if (jsIndex == -1) {
071: throw new IllegalStateException(
072: "You aren't running the tests from within the standard mozilla/js directory structure");
073: }
074: path = path.substring(0, jsIndex + 3).replace('/',
075: File.separatorChar);
076: testDir = new File(path, "tests");
077: }
078: if (!testDir.isDirectory()) {
079: throw new FileNotFoundException(testDir
080: + " is not a directory");
081: }
082: Properties excludes = new Properties();
083: loadExcludes(excludes, "/base.skip");
084: Properties opt1Excludes = new Properties();
085: loadExcludes(opt1Excludes, "/opt1.skip");
086: opt1Excludes.putAll(excludes);
087: for (int i = -1; i < 2; ++i) {
088: TestSuite optimizationLevelSuite = new TestSuite(
089: "Optimization level " + i);
090: addSuites(optimizationLevelSuite, testDir,
091: i == -1 ? excludes : opt1Excludes, i);
092: suite.addTest(optimizationLevelSuite);
093: }
094: return suite;
095: }
096:
097: private static void loadExcludes(Properties excludes,
098: String excludeFileName) throws IOException {
099: InputStream in = StandardTests.class
100: .getResourceAsStream(excludeFileName);
101: try {
102: excludes.load(in);
103: } finally {
104: in.close();
105: }
106: }
107:
108: private static void addSuites(TestSuite topLevel, File testDir,
109: Properties excludes, int optimizationLevel) {
110: File[] subdirs = testDir.listFiles(ShellTest.DIRECTORY_FILTER);
111: Arrays.sort(subdirs);
112: for (int i = 0; i < subdirs.length; i++) {
113: File subdir = subdirs[i];
114: String name = subdir.getName();
115: TestSuite testSuite = new TestSuite(name);
116: addCategories(testSuite, subdir, name + "/", excludes,
117: optimizationLevel);
118: topLevel.addTest(testSuite);
119: }
120: }
121:
122: private static void addCategories(TestSuite suite, File suiteDir,
123: String prefix, Properties excludes, int optimizationLevel) {
124: File[] subdirs = suiteDir.listFiles(ShellTest.DIRECTORY_FILTER);
125: Arrays.sort(subdirs);
126: for (int i = 0; i < subdirs.length; i++) {
127: File subdir = subdirs[i];
128: String name = subdir.getName();
129: TestSuite testCategory = new TestSuite(name);
130: addTests(testCategory, subdir, prefix + name + "/",
131: excludes, optimizationLevel);
132: suite.addTest(testCategory);
133: }
134: }
135:
136: private static void addTests(TestSuite suite, File suiteDir,
137: String prefix, Properties excludes, int optimizationLevel) {
138: File[] jsFiles = suiteDir.listFiles(ShellTest.TEST_FILTER);
139: Arrays.sort(jsFiles);
140: for (int i = 0; i < jsFiles.length; i++) {
141: File jsFile = jsFiles[i];
142: String name = jsFile.getName();
143: if (!excludes.containsKey(prefix + name)) {
144: suite
145: .addTest(new JsTestCase(jsFile,
146: optimizationLevel));
147: }
148: }
149: }
150:
151: private static class JunitStatus extends ShellTest.Status {
152: final void running(File jsFile) {
153: // do nothing
154: }
155:
156: final void failed(String s) {
157: Assert.fail(s);
158: }
159:
160: final void exitCodesWere(int expected, int actual) {
161: Assert.assertEquals("Unexpected exit code", expected,
162: actual);
163: }
164:
165: final void outputWas(String s) {
166: System.out.print(s);
167: }
168:
169: final void threw(Throwable t) {
170: Assert.fail(ShellTest.getStackTrace(t));
171: }
172:
173: final void timedOut() {
174: failed("Timed out.");
175: }
176: }
177:
178: private static final class JsTestCase extends TestCase {
179: private final File jsFile;
180: private final int optimizationLevel;
181:
182: JsTestCase(File jsFile, int optimizationLevel) {
183: super (jsFile.getName()
184: + (optimizationLevel == 1 ? "-compiled"
185: : "-interpreted"));
186: this .jsFile = jsFile;
187: this .optimizationLevel = optimizationLevel;
188: }
189:
190: public int countTestCases() {
191: return 1;
192: }
193:
194: private static class ShellTestParameters extends
195: ShellTest.Parameters {
196: int getTimeoutMilliseconds() {
197: if (System.getProperty("mozilla.js.tests.timeout") != null) {
198: return Integer.parseInt(System
199: .getProperty("mozilla.js.tests.timeout"));
200: }
201: return 60000;
202: }
203: }
204:
205: public void runBare() throws Exception {
206: final ShellContextFactory shellContextFactory = new ShellContextFactory();
207: shellContextFactory.setOptimizationLevel(optimizationLevel);
208: ShellTest.run(shellContextFactory, jsFile,
209: new ShellTestParameters(), new JunitStatus());
210: }
211: }
212: }
|