001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.tools;
014:
015: import java.io.OutputStreamWriter;
016:
017: import org.apache.commons.cli.CommandLine;
018: import org.apache.commons.cli.CommandLineParser;
019: import org.apache.commons.cli.HelpFormatter;
020: import org.apache.commons.cli.Options;
021: import org.apache.commons.cli.PosixParser;
022: import org.apache.log4j.ConsoleAppender;
023: import org.apache.log4j.Logger;
024: import org.apache.log4j.PatternLayout;
025:
026: import com.eviware.soapui.SoapUI;
027: import com.eviware.soapui.monitor.TestMonitor;
028: import com.eviware.soapui.support.UISupport;
029:
030: public abstract class AbstractSoapUIRunner {
031: private boolean groovyLogInitialized;
032: private String projectFile;
033: protected static Logger log;
034:
035: public AbstractSoapUIRunner(String title) {
036: log = Logger.getLogger(this .getClass());
037:
038: if (title != null)
039: System.out.println(title);
040:
041: SoapUI.initSoapUILog();
042: SoapUI.setTestMonitor(new TestMonitor());
043: SoapUI.loadExtLibs();
044: }
045:
046: public void initGroovyLog() {
047: if (!groovyLogInitialized) {
048: Logger logger = Logger.getLogger("groovy.log");
049:
050: ConsoleAppender appender = new ConsoleAppender();
051: appender.setWriter(new OutputStreamWriter(System.out));
052: appender.setLayout(new PatternLayout(
053: "%d{ABSOLUTE} %-5p [%c{1}] %m%n"));
054: logger.addAppender(appender);
055:
056: groovyLogInitialized = true;
057: }
058: }
059:
060: public void runFromCommandLine(String[] args) {
061: try {
062: initFromCommandLine(args, true);
063: run();
064: System.exit(0);
065: } catch (Throwable e) {
066: log.error(e.toString());
067: SoapUI.logError(e);
068: System.exit(1);
069: }
070: }
071:
072: public boolean initFromCommandLine(String[] args, boolean printHelp)
073: throws Exception {
074: SoapUIOptions options = initCommandLineOptions();
075:
076: CommandLineParser parser = new PosixParser();
077: CommandLine cmd = parser.parse(options, args);
078:
079: if (options.requiresProject()) {
080: args = cmd.getArgs();
081:
082: if (args.length != 1) {
083: if (printHelp) {
084: HelpFormatter formatter = new HelpFormatter();
085: formatter.printHelp(options.getRunnerName()
086: + " [options] <soapui-project-file>",
087: options);
088: }
089:
090: System.err.println("Missing soapUI project file..");
091: return false;
092: }
093:
094: setProjectFile(args[0]);
095: }
096:
097: return processCommandLine(cmd);
098: }
099:
100: public void enableSwingUI() {
101: log.info("Enabling Swing UI");
102: SoapUI.initSoapUILookAndFeel();
103: SoapUI.prepareSwingUI();
104: UISupport.setMainFrame(null);
105: }
106:
107: protected abstract boolean processCommandLine(CommandLine cmd);
108:
109: protected abstract SoapUIOptions initCommandLineOptions();
110:
111: public abstract void run() throws Exception;
112:
113: public String getProjectFile() {
114: return projectFile;
115: }
116:
117: /**
118: * Sets the soapUI project file containing the tests to run
119: *
120: * @param projectFile the soapUI project file containing the tests to run
121: */
122:
123: public void setProjectFile(String projectFile) {
124: log.info("setting projectFile to [" + projectFile + "]");
125: this .projectFile = projectFile;
126: }
127:
128: public static class SoapUIOptions extends Options {
129: private final String runnerName;
130:
131: public SoapUIOptions(String runnerName) {
132: this .runnerName = runnerName;
133: }
134:
135: public String getRunnerName() {
136: return runnerName;
137: }
138:
139: public boolean requiresProject() {
140: return true;
141: }
142: }
143: }
|