Package org.jnode.test.shell.harness

Source Code of org.jnode.test.shell.harness.CommandTestRunner

/*
* $Id$
*
* Copyright (C) 2003-2014 JNode.org
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.jnode.test.shell.harness;

import java.io.IOException;

import org.jnode.shell.CommandShell;


/**
* This TestRunner runs a class by calling its 'static void main(String[])' entry
* point.  Note that classes that call System.exit(status) are problematic.
*
* @author crawley@jnode.org
*/
class CommandTestRunner extends TestRunnerBase implements TestRunnable {

    public CommandTestRunner(TestSpecification spec, TestHarness harness) {
        super(spec, harness);
    }

    @Override
    public int run() throws Exception {
        StringBuffer sb = new StringBuffer();
        CommandShell shell = getShell();
        sb.append(shell.escapeWord(spec.getCommand()));
        for (String arg : spec.getArgs()) {
            sb.append(" ").append(shell.escapeWord(arg));
        }
        int rc;
        try {
            rc = shell.runCommand(sb.toString());
        } catch (Throwable ex) {
            Class<? extends Throwable> exception = spec.getException();
            if (exception != null && exception.isInstance(ex)) {
                rc = 0;
            } else if (ex instanceof Error) {
                throw (Error) ex;
            } else {
                throw (Exception) ex;
            }
        }
        flush();
        return check(rc) ? 0 : 1;
    }

    private boolean check(int rc) throws IOException {
        return
            harness.expect(rc, spec.getRc(), "return code") &
                harness.expect(outBucket.toString(), spec.getOutputContent(), "output content") &
                harness.expect(errBucket.toString(), spec.getErrorContent(), "err content") &
                checkFiles();
    }
}
TOP

Related Classes of org.jnode.test.shell.harness.CommandTestRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.