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.File;
016: import java.text.SimpleDateFormat;
017: import java.util.Date;
018:
019: import javax.servlet.http.HttpServletRequest;
020: import javax.servlet.http.HttpServletResponse;
021:
022: import org.apache.commons.cli.CommandLine;
023:
024: import com.eviware.soapui.SoapUI;
025: import com.eviware.soapui.impl.wsdl.WsdlProject;
026: import com.eviware.soapui.impl.wsdl.mock.WsdlMockService;
027: import com.eviware.soapui.model.mock.MockResult;
028: import com.eviware.soapui.model.mock.MockRunListener;
029: import com.eviware.soapui.model.mock.MockRunner;
030: import com.eviware.soapui.model.mock.MockService;
031:
032: /**
033: * Standalone tool-runner used from maven-plugin, can also be used from command-line (see xdocs) or
034: * directly from other classes.
035: * <p>
036: * For standalone usage, set the project file (with setProjectFile) and other desired properties before
037: * calling run</p>
038: *
039: * @author Ole.Matzura
040: */
041:
042: public class SoapUIMockServiceRunner extends AbstractSoapUIRunner {
043: private String mockService;
044: private String port;
045: private String path;
046:
047: public static String TITLE = "soapUI " + SoapUI.SOAPUI_VERSION
048: + " MockService Runner";
049:
050: /**
051: * Runs the specified MockService in the specified soapUI project file, see soapUI xdocs for details.
052: *
053: * @param args
054: * @throws Exception
055: */
056:
057: @SuppressWarnings("static-access")
058: public static void main(String[] args) throws Exception {
059: new SoapUIMockServiceRunner().runFromCommandLine(args);
060: }
061:
062: public void setMockService(String mockService) {
063: this .mockService = mockService;
064: }
065:
066: public void setPath(String path) {
067: this .path = path;
068: }
069:
070: public void setPort(String port) {
071: this .port = port;
072: }
073:
074: public SoapUIMockServiceRunner() {
075: super (TITLE);
076: }
077:
078: public SoapUIMockServiceRunner(String title) {
079: super (title);
080: }
081:
082: public void run() throws Exception {
083: String projectFile = getProjectFile();
084:
085: if (!new File(projectFile).exists())
086: throw new Exception("soapUI project file [" + projectFile
087: + "] not found");
088:
089: WsdlProject project = new WsdlProject(projectFile, null);
090: log.info("Running MockService [" + mockService
091: + "] in project [" + project.getName() + "]");
092: log.info("Press any key to terminate");
093:
094: long startTime = System.nanoTime();
095:
096: for (int c = 0; c < project.getMockServiceCount(); c++) {
097: MockService ms = project.getMockServiceAt(c);
098: if (ms.getName().equals(mockService))
099: runMockService((WsdlMockService) ms);
100: }
101:
102: long timeTaken = (System.nanoTime() - startTime) / 1000000;
103: log.info("time taken: " + timeTaken + "ms");
104: }
105:
106: /**
107: * Runs the configured tool for the specified interface.. needs to be refactored to use
108: * some kind of registry/factory pattern for tools
109: *
110: * @param iface
111: */
112:
113: public void runMockService(WsdlMockService mockService) {
114: try {
115: if (path != null)
116: mockService.setPath(path);
117:
118: if (port != null)
119: mockService.setPort(Integer.parseInt(port));
120:
121: mockService.addMockRunListener(new LogListener());
122: MockRunner runner = mockService.start();
123:
124: System.in.read();
125: runner.stop();
126: } catch (Exception e) {
127: SoapUI.logError(e);
128: }
129: }
130:
131: public class LogListener implements MockRunListener {
132: private SimpleDateFormat dateFormat = new SimpleDateFormat(
133: "yyyy-MM-dd HH:mm:ss.SSS");
134: private int responseCount;
135:
136: public void onMockRunnerStart(MockRunner mockRunner) {
137: log.info("MockService started on port "
138: + mockRunner.getMockService().getPort()
139: + " at path ["
140: + mockRunner.getMockService().getPath() + "]");
141: }
142:
143: public void onMockRunnerStop(MockRunner mockRunner) {
144: log.info("MockService stopped, handled " + responseCount
145: + " requests");
146: }
147:
148: public void onMockResult(MockResult result) {
149: responseCount++;
150: log.info("Handled request "
151: + responseCount
152: + "; ["
153: + result.getMockResponse().getMockOperation()
154: .getName()
155: + "] with ["
156: + result.getMockResponse().getName()
157: + "] in ["
158: + result.getTimeTaken()
159: + "ms] at ["
160: + dateFormat
161: .format(new Date(result.getTimestamp()))
162: + "]");
163: }
164:
165: public void onMockRequest(MockRunner runner,
166: HttpServletRequest request, HttpServletResponse response) {
167: }
168: }
169:
170: @Override
171: protected SoapUIOptions initCommandLineOptions() {
172: SoapUIOptions options = new SoapUIOptions("mockservicerunner");
173: options.addOption("m", true, "Sets the MockService");
174: options
175: .addOption("p", true,
176: "Sets the local port to listen on");
177: options.addOption("a", true, "Sets the url path to listen on");
178: options.addOption("s", false,
179: "Sets the soapui-settings.xml file to use");
180:
181: return options;
182: }
183:
184: @Override
185: protected boolean processCommandLine(CommandLine cmd) {
186: setMockService(cmd.getOptionValue("m"));
187:
188: if (cmd.hasOption("a"))
189: setPath(cmd.getOptionValue("a"));
190:
191: if (cmd.hasOption("p"))
192: setPort(cmd.getOptionValue("p"));
193:
194: if (cmd.hasOption("s"))
195: SoapUI.initSettings(cmd.getOptionValue("s"));
196:
197: return true;
198: }
199: }
|