/* Run the Tcl tests under JUnit.
Copyright (c) 2011-2014 The Regents of the University of California.
All rights reserved.
Permission is hereby granted, without written agreement and without
license or royalty fees, to use, copy, modify, and distribute this
software and its documentation for any purpose, provided that the above
copyright notice and the following two paragraphs appear in all copies
of this software.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
ENHANCEMENTS, OR MODIFICATIONS.
PT_COPYRIGHT_VERSION_2
COPYRIGHTENDKEY
*/
package ptolemy.util.test.junit;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;
import org.junit.AfterClass;
import org.junit.Test;
import org.junit.runner.RunWith;
///////////////////////////////////////////////////////////////////
//// TclTests
/**
* Run the Tcl tests under JUnit.
*
*
* This test must be run from the directory that contains the auto/ directory,
* for example:
*
*
* @author Christopher Brooks
* @version $Id: TclTests.java 70402 2014-10-23 00:52:20Z cxh $
* @since Ptolemy II 10.0
* @Pt.ProposedRating Red (cxh)
* @Pt.AcceptedRating Red (cxh)
*/
@RunWith(JUnitParamsRunner.class)
public class TclTests {
/**
* Call the Tcl doneTests command to print out the number of errors.
*
* @exception Throwable
* If the class, constructor or method cannot be found. or if
* the Interp cannot be instantiated.
*/
@AfterClass
public static void doneTests() throws Throwable {
if (_tclFileCount == 0) {
// No .tcl files were found, so testDefs.tcl, which
// defines the Tcl doneTests command has *not* been
// sourced. So, we return.
return;
}
// util/testsuite/testDefs.tcl doneTests tcl command checks
// the value of the reallyExit tcl variable. If reallyExit is
// not present or 1, then ::tycho::TopLevel::exitProgram is
// called. We don't want that because it prints an error
// message, so we set reallyExit to 0.
_setVarMethod.invoke(_interp, new Object[] { "reallyExit",
_tclObjectZero, 1 /*TCL.GLOBAL_ONLY*/});
// Invoke the doneTests Tcl command which prints the number of
// tests.
try {
_evalMethod.invoke(_interp, new Object[] { "doneTests", 0 });
} catch (Throwable throwable) {
if (!_tclExceptionClass.isInstance(throwable.getCause())) {
throw throwable;
} else {
Integer completionCode = (Integer) _getCompletionCodeMethod
.invoke(throwable.getCause(), new Object[] {});
if (completionCode.intValue() == 1 /** TCL.ERROR */
) {
// The completion code was 1, which means that the
// command could not be completed successfully.
// The Tcl errorInfo global variable will have information
// about what went wrong.
Object errorInfoTclObject = _getVarMethod.invoke(_interp,
new Object[] { "errorInfo", null, 1 /*TCL.GLOBAL_ONLY*/
});
throw new Exception(
"Evaluating the Tcl method \"doneTests\" "
+ "resulted in a TclException being thrown.\nThe Tcl "
+ "errorInfo global variable has the value:\n"
+ errorInfoTclObject);
}
}
}
}
/**
* Return a two dimensional array of arrays of strings that name the .tcl files
* to be executed. If there are no .tcl files, return a list with one element that
* has the value of the {@link #THERE_ARE_NO_TCL_TESTS} field.
*
* @return The List of tcl tests.
* @exception IOException If there is a problem accessing the auto/ directory.
*/
public Object[] parametersForRunTclFile() throws IOException {
String[] tclFiles = new File(".").list(new FilenameFilter() {
/**
* Return true if the file name ends with .tcl and is not
* alljtests.tcl or testDefs.tcl
*
* @param directory
* Ignored
* @param name
* The name of the file.
* @return true if the file name ends with .xml or .moml
*/
@Override
public boolean accept(File directory, String name) {
String fileName = name.toLowerCase(Locale.getDefault());
if (fileName.endsWith(".tcl")) {
// alljsimpletests.tcl calls exit,
// which results in JUnit
// producing
// "junit.framework.AssertionFailedError:
// Forked Java VM exited
// abnormally. Please note the
// time in the report does not
// reflect the time until the VM
// exit."
if (!fileName.endsWith("alljsimpletests.tcl")
&& !fileName.endsWith("alljtests.tcl")
&& !fileName.endsWith("testdefs.tcl")) {
return true;
}
}
return false;
}
});
if (tclFiles.length > 0) {
int i = 0;
Object[][] data = new Object[tclFiles.length][1];
for (String tclFile : tclFiles) {
data[i++][0] = new File(tclFile).getCanonicalPath();
}
// Sort the tcl files so that _Configuration.tcl is first
// in ptolemy/actor/gui/test
// File.list() returns files in a different order
// on different platforms. So much for write once, run everywhere.
Arrays.sort(data, new Comparator