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.impl.wsdl.mock;
014:
015: import java.util.ArrayList;
016: import java.util.Arrays;
017: import java.util.HashSet;
018: import java.util.List;
019: import java.util.Set;
020:
021: import javax.servlet.http.HttpServletRequest;
022: import javax.servlet.http.HttpServletResponse;
023: import javax.swing.ImageIcon;
024:
025: import com.eviware.soapui.config.MockOperationConfig;
026: import com.eviware.soapui.config.MockServiceConfig;
027: import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
028: import com.eviware.soapui.impl.wsdl.WsdlInterface;
029: import com.eviware.soapui.impl.wsdl.WsdlOperation;
030: import com.eviware.soapui.impl.wsdl.WsdlProject;
031: import com.eviware.soapui.impl.wsdl.support.ModelItemIconAnimator;
032: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
033: import com.eviware.soapui.model.iface.Operation;
034: import com.eviware.soapui.model.mock.MockResult;
035: import com.eviware.soapui.model.mock.MockRunListener;
036: import com.eviware.soapui.model.mock.MockRunner;
037: import com.eviware.soapui.model.mock.MockService;
038: import com.eviware.soapui.model.mock.MockServiceListener;
039: import com.eviware.soapui.model.project.Project;
040:
041: /**
042: * A MockService for simulation WsdlInterfaces and their operations
043: *
044: * @author ole.matzura
045: */
046:
047: public class WsdlMockService extends
048: AbstractWsdlModelItem<MockServiceConfig> implements MockService {
049: private static final String REQUIRE_SOAP_VERSION = WsdlMockService.class
050: .getName()
051: + "@require-soap-version";
052: private List<WsdlMockOperation> mockOperations = new ArrayList<WsdlMockOperation>();
053: private Set<MockRunListener> mockRunListeners = new HashSet<MockRunListener>();
054: private Set<MockServiceListener> mockServiceListeners = new HashSet<MockServiceListener>();
055: private MockServiceIconAnimator iconAnimator;
056: private WsdlMockRunner mockRunner;
057:
058: public WsdlMockService(Project project, MockServiceConfig config) {
059: super (config, project, "/mockService.gif");
060:
061: List<MockOperationConfig> testStepConfigs = config
062: .getMockOperationList();
063: for (MockOperationConfig tsc : testStepConfigs) {
064: WsdlMockOperation testStep = new WsdlMockOperation(this ,
065: tsc);
066: mockOperations.add(testStep);
067: }
068:
069: if (!config.isSetPort() || config.getPort() < 1)
070: config.setPort(8080);
071:
072: if (!config.isSetPath())
073: config.setPath("/");
074:
075: iconAnimator = new MockServiceIconAnimator();
076: addMockRunListener(iconAnimator);
077: }
078:
079: public void addMockRunListener(MockRunListener listener) {
080: mockRunListeners.add(listener);
081: }
082:
083: public String getPath() {
084: return getConfig().getPath();
085: }
086:
087: public WsdlMockOperation getMockOperationAt(int index) {
088: return mockOperations.get(index);
089: }
090:
091: public WsdlMockOperation getMockOperationByName(String name) {
092: return (WsdlMockOperation) getWsdlModelItemByName(
093: mockOperations, name);
094: }
095:
096: public int getMockOperationCount() {
097: return mockOperations.size();
098: }
099:
100: public WsdlProject getProject() {
101: return (WsdlProject) getParent();
102: }
103:
104: public int getPort() {
105: return getConfig().getPort();
106: }
107:
108: public void removeMockRunListener(MockRunListener listener) {
109: mockRunListeners.remove(listener);
110: }
111:
112: public WsdlMockRunner start(WsdlTestRunContext context)
113: throws Exception {
114: String path = getPath();
115: if (path == null || path.trim().length() == 0
116: || path.trim().charAt(0) != '/')
117: throw new Exception("Invalid path; must start with '/'");
118:
119: mockRunner = new WsdlMockRunner(this , context);
120: return mockRunner;
121: }
122:
123: public WsdlMockRunner getMockRunner() {
124: return mockRunner;
125: }
126:
127: public WsdlMockOperation getMockOperation(Operation operation) {
128: for (int c = 0; c < getMockOperationCount(); c++) {
129: WsdlMockOperation mockOperation = mockOperations.get(c);
130: if (mockOperation.getOperation() == operation)
131: return mockOperation;
132: }
133:
134: return null;
135: }
136:
137: public WsdlMockOperation addNewMockOperation(WsdlOperation operation) {
138: if (getMockOperation(operation) != null)
139: return null;
140:
141: MockOperationConfig config = getConfig().addNewMockOperation();
142: config.setName(operation.getName());
143: WsdlMockOperation mockOperation = new WsdlMockOperation(this ,
144: config, operation);
145:
146: mockOperations.add(mockOperation);
147: fireMockOperationAdded(mockOperation);
148:
149: return mockOperation;
150: }
151:
152: public void setPort(int port) {
153: String oldEndpoint = getLocalEndpoint();
154:
155: int oldPort = getPort();
156: if (port != oldPort) {
157: getConfig().setPort(port);
158: notifyPropertyChanged(PORT_PROPERTY, oldPort, port);
159:
160: for (WsdlInterface iface : getMockedInterfaces()) {
161: if (Arrays.asList(iface.getEndpoints()).contains(
162: oldEndpoint))
163: iface.changeEndpoint(oldEndpoint,
164: getLocalEndpoint());
165: }
166: }
167: }
168:
169: public WsdlInterface[] getMockedInterfaces() {
170: Set<WsdlInterface> result = new HashSet<WsdlInterface>();
171:
172: for (WsdlMockOperation mockOperation : mockOperations)
173: result.add(mockOperation.getOperation().getInterface());
174:
175: return result.toArray(new WsdlInterface[result.size()]);
176: }
177:
178: public void release() {
179: super .release();
180:
181: for (WsdlMockOperation operation : mockOperations)
182: operation.release();
183: }
184:
185: public void setPath(String path) {
186: String oldEndpoint = getLocalEndpoint();
187:
188: String oldPath = getPath();
189: if (!path.equals(oldPath)) {
190: getConfig().setPath(path);
191: notifyPropertyChanged(PATH_PROPERTY, oldPath, path);
192:
193: for (WsdlInterface iface : getMockedInterfaces()) {
194: if (Arrays.asList(iface.getEndpoints()).contains(
195: oldEndpoint))
196: iface.changeEndpoint(oldEndpoint,
197: getLocalEndpoint());
198: }
199: }
200: }
201:
202: public MockRunListener[] getMockRunListeners() {
203: return mockRunListeners
204: .toArray(new MockRunListener[mockRunListeners.size()]);
205: }
206:
207: public void removeMockOperation(WsdlMockOperation mockOperation) {
208: int ix = mockOperations.indexOf(mockOperation);
209: if (ix == -1)
210: throw new RuntimeException(
211: "Unkonws MockOperation specified to removeMockOperation");
212:
213: mockOperations.remove(ix);
214: fireMockOperationRemoved(mockOperation);
215: mockOperation.release();
216: getConfig().removeMockOperation(ix);
217: }
218:
219: public void addMockServiceListener(MockServiceListener listener) {
220: mockServiceListeners.add(listener);
221: }
222:
223: public void removeMockServiceListener(MockServiceListener listener) {
224: mockServiceListeners.remove(listener);
225: }
226:
227: protected void fireMockOperationAdded(
228: WsdlMockOperation mockOperation) {
229: MockServiceListener[] listeners = mockServiceListeners
230: .toArray(new MockServiceListener[mockServiceListeners
231: .size()]);
232: for (MockServiceListener listener : listeners) {
233: listener.mockOperationAdded(mockOperation);
234: }
235: }
236:
237: protected void fireMockOperationRemoved(
238: WsdlMockOperation mockOperation) {
239: MockServiceListener[] listeners = mockServiceListeners
240: .toArray(new MockServiceListener[mockServiceListeners
241: .size()]);
242: for (MockServiceListener listener : listeners) {
243: listener.mockOperationRemoved(mockOperation);
244: }
245: }
246:
247: protected void fireMockResponseAdded(WsdlMockResponse mockResponse) {
248: MockServiceListener[] listeners = mockServiceListeners
249: .toArray(new MockServiceListener[mockServiceListeners
250: .size()]);
251: for (MockServiceListener listener : listeners) {
252: listener.mockResponseAdded(mockResponse);
253: }
254: }
255:
256: protected void fireMockResponseRemoved(WsdlMockResponse mockResponse) {
257: MockServiceListener[] listeners = mockServiceListeners
258: .toArray(new MockServiceListener[mockServiceListeners
259: .size()]);
260: for (MockServiceListener listener : listeners) {
261: listener.mockResponseRemoved(mockResponse);
262: }
263: }
264:
265: @Override
266: public ImageIcon getIcon() {
267: return iconAnimator.getIcon();
268: }
269:
270: private class MockServiceIconAnimator extends ModelItemIconAnimator
271: implements MockRunListener {
272: public MockServiceIconAnimator() {
273: super (WsdlMockService.this , "/mockService.gif",
274: new String[] { "/mockService_1.gif",
275: "/mockService_2.gif", "/mockService_3.gif",
276: "/mockService_4.gif" });
277: }
278:
279: public void onMockRequest(MockRunner runner,
280: HttpServletRequest request, HttpServletResponse response) {
281: }
282:
283: public void onMockResult(MockResult result) {
284: }
285:
286: public void onMockRunnerStart(MockRunner mockRunner) {
287: start();
288: }
289:
290: public void onMockRunnerStop(MockRunner mockRunner) {
291: stop();
292: WsdlMockService.this .mockRunner = null;
293: }
294: }
295:
296: public String getLocalEndpoint() {
297: return "http://127.0.0.1:" + getPort() + getPath();
298: }
299:
300: public boolean isRequireSoapVersion() {
301: return getSettings().getBoolean(REQUIRE_SOAP_VERSION);
302: }
303:
304: public void setRequireSoapVersion(boolean requireSoapVersion) {
305: getSettings().setBoolean(REQUIRE_SOAP_VERSION,
306: requireSoapVersion);
307: }
308:
309: public WsdlMockRunner start() throws Exception {
310: return start(null);
311: }
312:
313: public boolean hasMockOperation(Operation operation) {
314: return getMockOperation(operation) != null;
315: }
316:
317: @Override
318: public void onSave() {
319: for (WsdlMockOperation mockOperation : mockOperations)
320: mockOperation.onSave();
321: }
322: }
|