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.monitor;
014:
015: import java.util.HashSet;
016: import java.util.Iterator;
017: import java.util.Set;
018:
019: import com.eviware.soapui.impl.wsdl.WsdlProject;
020: import com.eviware.soapui.model.mock.MockRunner;
021: import com.eviware.soapui.model.mock.MockService;
022: import com.eviware.soapui.model.project.Project;
023: import com.eviware.soapui.model.support.LoadTestRunListenerAdapter;
024: import com.eviware.soapui.model.support.MockRunListenerAdapter;
025: import com.eviware.soapui.model.support.ProjectListenerAdapter;
026: import com.eviware.soapui.model.support.TestRunListenerAdapter;
027: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
028: import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
029: import com.eviware.soapui.model.testsuite.LoadTest;
030: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
031: import com.eviware.soapui.model.testsuite.LoadTestRunner;
032: import com.eviware.soapui.model.testsuite.TestCase;
033: import com.eviware.soapui.model.testsuite.TestRunContext;
034: import com.eviware.soapui.model.testsuite.TestRunner;
035: import com.eviware.soapui.model.testsuite.TestSuite;
036: import com.eviware.soapui.model.workspace.Workspace;
037:
038: /**
039: * Global class for monitoring ongoing test runs (both functional and loadtests)
040: *
041: * @author Ole.Matzura
042: */
043:
044: public class TestMonitor {
045: private Set<TestMonitorListener> listeners = new HashSet<TestMonitorListener>();
046: private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
047: private InternalProjectListener projectListener = new InternalProjectListener();
048: private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
049: private InternalTestRunListener testRunListener = new InternalTestRunListener();
050: private InternalMockRunListener mockRunListener = new InternalMockRunListener();
051: private InternalLoadTestRunListener loadTestRunListener = new InternalLoadTestRunListener();
052: private Set<TestRunner> runningTestCases = new HashSet<TestRunner>();
053: private Set<LoadTestRunner> runningLoadTests = new HashSet<LoadTestRunner>();
054: private Set<MockRunner> runningMockServices = new HashSet<MockRunner>();
055:
056: public TestMonitor() {
057: }
058:
059: protected void notifyLoadTestStarted(LoadTestRunner runner) {
060: runningLoadTests.add(runner);
061:
062: if (listeners.isEmpty())
063: return;
064:
065: TestMonitorListener[] l = listeners
066: .toArray(new TestMonitorListener[listeners.size()]);
067: for (int c = 0; c < l.length; c++) {
068: l[c].loadTestStarted(runner);
069: }
070: }
071:
072: protected void notifyLoadTestFinished(LoadTestRunner runner) {
073: runningLoadTests.remove(runner.getLoadTest().getTestCase());
074:
075: if (listeners.isEmpty())
076: return;
077:
078: TestMonitorListener[] l = listeners
079: .toArray(new TestMonitorListener[listeners.size()]);
080: for (int c = 0; c < l.length; c++) {
081: l[c].loadTestFinished(runner);
082: }
083: }
084:
085: protected void notifyTestCaseStarted(TestRunner runner) {
086: if (listeners.isEmpty())
087: return;
088:
089: TestMonitorListener[] l = listeners
090: .toArray(new TestMonitorListener[listeners.size()]);
091: for (int c = 0; c < l.length; c++) {
092: l[c].testCaseStarted(runner);
093: }
094: }
095:
096: protected void notifyTestCaseFinished(TestRunner runner) {
097: if (listeners.isEmpty())
098: return;
099:
100: TestMonitorListener[] l = listeners
101: .toArray(new TestMonitorListener[listeners.size()]);
102: for (int c = 0; c < l.length; c++) {
103: l[c].testCaseFinished(runner);
104: }
105: }
106:
107: protected void notifyMockServiceStarted(MockRunner runner) {
108: if (listeners.isEmpty())
109: return;
110:
111: TestMonitorListener[] l = listeners
112: .toArray(new TestMonitorListener[listeners.size()]);
113: for (int c = 0; c < l.length; c++) {
114: l[c].mockServiceStarted(runner);
115: }
116: }
117:
118: protected void notifyMockServiceStopped(MockRunner runner) {
119: if (listeners.isEmpty())
120: return;
121:
122: TestMonitorListener[] l = listeners
123: .toArray(new TestMonitorListener[listeners.size()]);
124: for (int c = 0; c < l.length; c++) {
125: l[c].mockServiceStopped(runner);
126: }
127: }
128:
129: public boolean hasRunningLoadTest(TestCase testCase) {
130: Iterator<LoadTestRunner> iterator = runningLoadTests.iterator();
131: while (iterator.hasNext()) {
132: if (iterator.next().getLoadTest().getTestCase() == testCase)
133: return true;
134: }
135:
136: return false;
137: }
138:
139: public boolean hasRunningTestCase(TestCase testCase) {
140: Iterator<TestRunner> iterator = runningTestCases.iterator();
141: while (iterator.hasNext()) {
142: if (iterator.next().getTestCase() == testCase)
143: return true;
144: }
145:
146: return false;
147: }
148:
149: public void addTestMonitorListener(TestMonitorListener listener) {
150: listeners.add(listener);
151: }
152:
153: public void removeTestMonitorListener(TestMonitorListener listener) {
154: listeners.remove(listener);
155: }
156:
157: private class InternalWorkspaceListener extends
158: WorkspaceListenerAdapter {
159: public void projectRemoved(Project project) {
160: unmonitorProject(project);
161: }
162:
163: public void projectAdded(Project project) {
164: monitorProject(project);
165: }
166: }
167:
168: private class InternalProjectListener extends
169: ProjectListenerAdapter {
170: public void testSuiteRemoved(TestSuite testSuite) {
171: unmonitorTestSuite(testSuite);
172: }
173:
174: public void testSuiteAdded(TestSuite testSuite) {
175: monitorTestSuite(testSuite);
176: }
177:
178: @Override
179: public void mockServiceAdded(MockService mockService) {
180: monitorMockService(mockService);
181: }
182:
183: @Override
184: public void mockServiceRemoved(MockService mockService) {
185: unmonitorMockService(mockService);
186: }
187: }
188:
189: private class InternalTestSuiteListener extends
190: TestSuiteListenerAdapter {
191: public void testCaseAdded(TestCase testCase) {
192: monitorTestCase(testCase);
193: }
194:
195: public void testCaseRemoved(TestCase testCase) {
196: unmonitorTestCase(testCase);
197: }
198:
199: public void loadTestAdded(LoadTest loadTest) {
200: monitorLoadTest(loadTest);
201: }
202:
203: public void loadTestRemoved(LoadTest loadTest) {
204: unmonitorLoadTest(loadTest);
205: }
206: }
207:
208: private class InternalTestRunListener extends
209: TestRunListenerAdapter {
210: public void afterRun(TestRunner testRunner,
211: TestRunContext runContext) {
212: runningTestCases.remove(testRunner);
213: notifyTestCaseFinished(testRunner);
214: }
215:
216: public void beforeRun(TestRunner testRunner,
217: TestRunContext runContext) {
218: runningTestCases.add(testRunner);
219: notifyTestCaseStarted(testRunner);
220: }
221: }
222:
223: private class InternalMockRunListener extends
224: MockRunListenerAdapter {
225: @Override
226: public void onMockRunnerStart(MockRunner mockRunner) {
227: runningMockServices.add(mockRunner);
228: notifyMockServiceStarted(mockRunner);
229: }
230:
231: @Override
232: public void onMockRunnerStop(MockRunner mockRunner) {
233: runningMockServices.remove(mockRunner);
234: notifyMockServiceStopped(mockRunner);
235: }
236: }
237:
238: private class InternalLoadTestRunListener extends
239: LoadTestRunListenerAdapter {
240: public void afterLoadTest(LoadTestRunner testRunner,
241: LoadTestRunContext context) {
242: runningLoadTests.remove(testRunner);
243: notifyLoadTestFinished(testRunner);
244: }
245:
246: public void beforeLoadTest(LoadTestRunner testRunner,
247: LoadTestRunContext context) {
248: runningLoadTests.add(testRunner);
249: notifyLoadTestStarted(testRunner);
250: }
251: }
252:
253: public LoadTestRunner[] getRunningLoadTest() {
254: return runningLoadTests
255: .toArray(new LoadTestRunner[runningLoadTests.size()]);
256: }
257:
258: public boolean hasRunningTest(TestCase testCase) {
259: return hasRunningLoadTest(testCase)
260: || hasRunningTestCase(testCase);
261: }
262:
263: public void init(Workspace workspace) {
264: for (int c = 0; c < workspace.getProjectCount(); c++) {
265: Project project = workspace.getProjectAt(c);
266: monitorProject(project);
267: }
268:
269: workspace.addWorkspaceListener(workspaceListener);
270: }
271:
272: public void monitorProject(Project project) {
273: project.addProjectListener(projectListener);
274:
275: for (int i = 0; i < project.getTestSuiteCount(); i++) {
276: monitorTestSuite(project.getTestSuiteAt(i));
277: }
278:
279: for (int i = 0; i < project.getMockServiceCount(); i++) {
280: monitorMockService(project.getMockServiceAt(i));
281: }
282: }
283:
284: private void monitorMockService(MockService mockService) {
285: mockService.addMockRunListener(mockRunListener);
286: }
287:
288: private void monitorTestSuite(TestSuite testSuite) {
289: testSuite.addTestSuiteListener(testSuiteListener);
290:
291: for (int j = 0; j < testSuite.getTestCaseCount(); j++) {
292: monitorTestCase(testSuite.getTestCaseAt(j));
293: }
294: }
295:
296: private void monitorTestCase(TestCase testCase) {
297: testCase.addTestRunListener(testRunListener);
298:
299: for (int v = 0; v < testCase.getLoadTestCount(); v++) {
300: testCase.getLoadTestAt(v).addLoadTestRunListener(
301: loadTestRunListener);
302: }
303: }
304:
305: private void monitorLoadTest(LoadTest loadTest) {
306: loadTest.addLoadTestRunListener(loadTestRunListener);
307: }
308:
309: public void unmonitorProject(Project project) {
310: project.removeProjectListener(projectListener);
311:
312: for (int c = 0; c < project.getTestSuiteCount(); c++) {
313: TestSuite testSuite = project.getTestSuiteAt(c);
314: unmonitorTestSuite(testSuite);
315: }
316:
317: for (int c = 0; c < project.getMockServiceCount(); c++) {
318: unmonitorMockService(project.getMockServiceAt(c));
319: }
320: }
321:
322: private void unmonitorMockService(MockService mockService) {
323: mockService.removeMockRunListener(mockRunListener);
324: }
325:
326: private void unmonitorTestSuite(TestSuite testSuite) {
327: testSuite.removeTestSuiteListener(testSuiteListener);
328:
329: for (int j = 0; j < testSuite.getTestCaseCount(); j++) {
330: TestCase testCase = testSuite.getTestCaseAt(j);
331: unmonitorTestCase(testCase);
332: }
333: }
334:
335: private void unmonitorTestCase(TestCase testCase) {
336: testCase.removeTestRunListener(testRunListener);
337:
338: for (int c = 0; c < testCase.getLoadTestCount(); c++) {
339: unmonitorLoadTest(testCase.getLoadTestAt(c));
340: }
341: }
342:
343: private void unmonitorLoadTest(LoadTest loadTest) {
344: loadTest.removeLoadTestRunListener(loadTestRunListener);
345: }
346:
347: public boolean hasRunningTests() {
348: return runningLoadTests.size() + runningTestCases.size() > 0;
349: }
350:
351: public boolean hasRunningMock(MockService mockService) {
352: for (MockRunner runner : runningMockServices)
353: if (runner.getMockService() == mockService)
354: return true;
355:
356: return false;
357: }
358:
359: public boolean hasRunningTests(WsdlProject project) {
360: for (TestRunner testRunner : runningTestCases) {
361: if (testRunner.getTestCase().getTestSuite().getProject() == project)
362: return true;
363: }
364:
365: for (LoadTestRunner loadTestRunner : runningLoadTests) {
366: if (loadTestRunner.getLoadTest().getTestCase()
367: .getTestSuite().getProject() == project)
368: return true;
369: }
370:
371: // for( MockRunner mockRunner : runningMockServices )
372: // {
373: // if( mockRunner.getMockService().getProject() == project )
374: // return true;
375: // }
376:
377: return false;
378: }
379:
380: public void cancelAllTests(String reason) {
381: for (TestRunner testRunner : runningTestCases) {
382: testRunner.cancel(reason);
383: }
384:
385: for (LoadTestRunner loadTestRunner : runningLoadTests) {
386: loadTestRunner.cancel(reason);
387: }
388:
389: for (MockRunner mockRunner : runningMockServices) {
390: mockRunner.stop();
391: }
392: }
393: }
|