1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.tools;
14
15 import java.io.OutputStreamWriter;
16
17 import org.apache.commons.cli.CommandLine;
18 import org.apache.commons.cli.CommandLineParser;
19 import org.apache.commons.cli.HelpFormatter;
20 import org.apache.commons.cli.Options;
21 import org.apache.commons.cli.PosixParser;
22 import org.apache.log4j.ConsoleAppender;
23 import org.apache.log4j.Logger;
24 import org.apache.log4j.PatternLayout;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.monitor.TestMonitor;
28 import com.eviware.soapui.support.UISupport;
29
30 public abstract class AbstractSoapUIRunner
31 {
32 private boolean groovyLogInitialized;
33 private String projectFile;
34 protected static Logger log;
35
36 public AbstractSoapUIRunner( String title )
37 {
38 log = Logger.getLogger( this.getClass() );
39
40 if( title != null )
41 System.out.println( title );
42
43 SoapUI.initSoapUILog();
44 SoapUI.setTestMonitor( new TestMonitor() );
45 SoapUI.loadExtLibs();
46 }
47
48 public void initGroovyLog()
49 {
50 if( !groovyLogInitialized )
51 {
52 Logger logger = Logger.getLogger( "groovy.log" );
53
54 ConsoleAppender appender = new ConsoleAppender();
55 appender.setWriter( new OutputStreamWriter( System.out ));
56 appender.setLayout( new PatternLayout( "%d{ABSOLUTE} %-5p [%c{1}] %m%n") );
57 logger.addAppender( appender);
58
59 groovyLogInitialized = true;
60 }
61 }
62
63 public void runFromCommandLine( String [] args )
64 {
65 try
66 {
67 initFromCommandLine( args, true );
68 run();
69 System.exit( 0 );
70 }
71 catch( Throwable e )
72 {
73 log.error( e.toString() );
74 SoapUI.logError( e );
75 System.exit( 1 );
76 }
77 }
78
79 public boolean initFromCommandLine( String[] args, boolean printHelp ) throws Exception
80 {
81 SoapUIOptions options = initCommandLineOptions();
82
83 CommandLineParser parser = new PosixParser();
84 CommandLine cmd = parser.parse( options, args);
85
86 if( options.requiresProject() )
87 {
88 args = cmd.getArgs();
89
90 if( args.length != 1 )
91 {
92 if( printHelp )
93 {
94 HelpFormatter formatter = new HelpFormatter();
95 formatter.printHelp( options.getRunnerName() + " [options] <soapui-project-file>", options );
96 }
97
98 System.err.println( "Missing soapUI project file.." );
99 return false;
100 }
101
102 setProjectFile( args[0] );
103 }
104
105 return processCommandLine( cmd );
106 }
107
108 public void enableSwingUI()
109 {
110 log.info( "Enabling Swing UI" );
111 SoapUI.initSoapUILookAndFeel();
112 SoapUI.prepareSwingUI();
113 UISupport.setMainFrame( null );
114 }
115
116 protected abstract boolean processCommandLine( CommandLine cmd );
117
118 protected abstract SoapUIOptions initCommandLineOptions();
119
120 public abstract void run() throws Exception;
121
122 public String getProjectFile()
123 {
124 return projectFile;
125 }
126
127 /***
128 * Sets the soapUI project file containing the tests to run
129 *
130 * @param projectFile the soapUI project file containing the tests to run
131 */
132
133 public void setProjectFile(String projectFile)
134 {
135 log.info( "setting projectFile to [" + projectFile + "]" );
136 this.projectFile = projectFile;
137 }
138
139 public static class SoapUIOptions extends Options
140 {
141 private final String runnerName;
142
143 public SoapUIOptions( String runnerName )
144 {
145 this.runnerName = runnerName;}
146
147 public String getRunnerName()
148 {
149 return runnerName;
150 }
151
152 public boolean requiresProject()
153 {
154 return true;
155 }
156 }
157 }