001/* Run Tcl Tests using JUnit
002
003 Copyright (c) 2010-2011 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028
029package ptolemy.util.test.junit;
030
031import java.io.File;
032
033import tcl.lang.Interp;
034import tcl.lang.TclException;
035
036///////////////////////////////////////////////////////////////////
037//// JUnitTclTestRun
038/**
039 * Run the Tcl tests under JUnit.
040 *
041 * <p>If the fileName JVM property is set, then the file named by that property is
042 * sourced. Otherwise, the testDefs.tcl file is sourced and the doallTests Tcl
043 * proc that is defined in $PTII/util/testsuite/testDefs.tcl is invoked and then
044 * any models in the auto/ directory are invoked.</p>
045 *
046 * <p>To run one test file (NamedObj.tcl):</p>
047 * <pre>
048 * cd $PTII/ptolemy/kernel/util/test
049 * java -DfileName=NamedObj.tcl -classpath ${PTII}:${PTII}/lib/ptjacl.jar:${PTII}/lib/junit-4.8.2.jar:${PTII}/lib/JUnitParams-0.3.0.jar org.junit.runner.JUnitCore ptolemy.util.test.junit.JUnitTclTest
050 * </pre>
051 *
052 * <p>To run all the .tcl files:</p>
053 * <pre>
054 * cd $PTII/ptolemy/kernel/util/test
055 * java -classpath ${PTII}:${PTII}/lib/ptjacl.jar:${PTII}/lib/junit-4.8.2.jar:${PTII}/lib/JUnitParams-0.3.0.jar org.junit.runner.JUnitCore ptolemy.kernel.util.test.JUnitTclTest
056 * </pre>
057 *
058 * <p>A copy of this file appears in each test/ subdirectory so that
059 * it is easy for developers to run tests. The master file is in
060 * $PTII/util/testsuite/JUnitTclTest.java.in. To update all the files,
061 * run updateJUnitTclTest.</p>
062 *
063 * @author Christopher Brooks
064 * @version $Id$
065 * @since Ptolemy II 10.0
066 * @Pt.ProposedRating Green (cxh)
067 * @Pt.AcceptedRating Green (cxh)
068 */
069public class JUnitTclTestRun {
070    /**
071     * Run a test.
072     * <p>
073     * If the fileName JVM property is set, then the file named by that property
074     * is sourced. Otherwise, the testDefs.tcl file is sourced and the
075     * doallTests Tcl proc that is defined in $PTII/util/testsuite/testDefs.tcl
076     * is invoked and then any models in the auto/ directory are invoked.
077     *
078     * @exception TclException
079     *                If thrown while evaluating the Tcl test code.
080     */
081    @org.junit.Test
082    public void run() throws TclException {
083        String fileName = System.getProperty("fileName");
084        Interp interp = new Interp();
085        if (fileName != null) {
086            interp.evalFile(fileName);
087        } else {
088            if (!new File("testDefs.tcl").exists()) {
089                // We might be running from a different directory
090                String directory = getClass().getPackage().getName()
091                        .replace('.', '/');
092                if (new File(directory + "/testDefs.tcl").exists()) {
093                    System.out.println(directory + "/testDefs.tcl exists");
094                    interp.eval("cd " + directory);
095                }
096            }
097            interp.evalFile("testDefs.tcl");
098            interp.eval("doAllTests");
099        }
100    }
101}