001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.cmd;
021    
022    import java.io.File;
023    
024    import org.dom4j.Document;
025    import org.mactor.framework.MactorException;
026    import org.mactor.framework.MockRunner;
027    import org.mactor.framework.TestRunner;
028    import org.mactor.framework.data.DataProviderFactory;
029    import org.mactor.framework.data.DataTable;
030    import org.mactor.framework.spec.MockBatterySpec;
031    import org.mactor.framework.spec.ProjectContext;
032    import org.mactor.framework.spec.TestRunSpec;
033    import org.mactor.framework.spec.TestSpec;
034    
035    public class MActorCmd {
036            public static void main(String[] args) {
037                    if (args.length != 1) {
038                            printUsage();
039                            System.exit(1);
040                    }
041                    File f = new File(args[0]);
042                    if (!f.exists()) {
043                            printUsage();
044                            System.exit(1);
045                    }
046                    String name = f.getName();
047                    try {
048                            ProjectContext.getGlobalInstance().setProjectDir(f.getParentFile());
049                            Document doc = ProjectContext.getGlobalInstance().readFromFile(name, false);
050                            if (doc.getRootElement().getName().equals("test")) {
051                                    runTest(doc, name);
052                            } else if (doc.getRootElement().getName().equals("test_run")) {
053                                    runTestRun(doc, name);
054                            } else if (doc.getRootElement().getName().equals("mock_battery")) {
055                                    runMockBattery(doc, name);
056                            } else {
057                                    System.out.println("'" + name + "' in project '" + ProjectContext.getGlobalInstance().getProjectName() + "' is not an executable test");
058                                    printUsage();
059                                    System.exit(1);
060                            }
061                    } catch (MactorException me) {
062                            System.out.println("Execution of '" + name + "' in project '" + ProjectContext.getGlobalInstance().getProjectName() + "' failed with reason:" + me.getMessage());
063                            printUsage();
064                            System.exit(1);
065                    }
066            }
067            private static void runTest(Document doc, String name) throws MactorException {
068                    TestSpec spec = TestSpec.loadFromDocument(doc, name);
069                    DataTable dt = new DataTable();
070                    dt.addColumn("dummy_");
071                    dt.addRow(new String[] { "" });
072                    TestRunner tr = new TestRunner(1, spec, dt, new SimpleLogFeedbackHandler());
073                    tr.start();
074                    if (tr.waitForCompletion() == 0)
075                            System.exit(0);
076                    else
077                            System.exit(1);
078            }
079            private static void runTestRun(Document doc, String name) throws MactorException {
080                    TestRunSpec spec = TestRunSpec.loadFromDocument(doc, name);
081                    TestRunner tr = new TestRunner(spec.getThreadCount(), TestSpec.loadFromFile(spec.getTest()), DataProviderFactory.getDataProvider(spec.getDataSource()).loadData(),
082                                    new SimpleLogFeedbackHandler());
083                    tr.start();
084                    if (tr.waitForCompletion() == 0)
085                            System.exit(0);
086                    else
087                            System.exit(1);
088            }
089            private static void runMockBattery(Document doc, String name) throws MactorException {
090                    MockBatterySpec spec = MockBatterySpec.loadFromDocument(doc, name);
091                    MockRunner mr = new MockRunner(spec.getThreadCount(), spec.getTestSpecs(), new SimpleLogFeedbackHandler());
092                    mr.start();
093                    mr.waitForCompletion();
094            }
095            private static void printUsage() {
096                    System.out.println("Usage: runtest <path to the test file to execute (the file must be a test a test_run or mock_battery)>");
097            }
098    }