View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.monitor;
14  
15  import java.util.HashMap;
16  import java.util.HashSet;
17  import java.util.Iterator;
18  import java.util.Map;
19  import java.util.Set;
20  
21  import com.eviware.soapui.impl.wsdl.WsdlProject;
22  import com.eviware.soapui.model.mock.MockRunner;
23  import com.eviware.soapui.model.mock.MockService;
24  import com.eviware.soapui.model.project.Project;
25  import com.eviware.soapui.model.support.LoadTestRunListenerAdapter;
26  import com.eviware.soapui.model.support.MockRunListenerAdapter;
27  import com.eviware.soapui.model.support.ProjectListenerAdapter;
28  import com.eviware.soapui.model.support.TestRunListenerAdapter;
29  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
30  import com.eviware.soapui.model.support.WorkspaceListenerAdapter;
31  import com.eviware.soapui.model.testsuite.LoadTest;
32  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
33  import com.eviware.soapui.model.testsuite.LoadTestRunner;
34  import com.eviware.soapui.model.testsuite.TestCase;
35  import com.eviware.soapui.model.testsuite.TestRunContext;
36  import com.eviware.soapui.model.testsuite.TestRunner;
37  import com.eviware.soapui.model.testsuite.TestSuite;
38  import com.eviware.soapui.model.workspace.Workspace;
39  
40  /***
41   * Global class for monitoring ongoing test runs (both functional and loadtests)
42   * 
43   * @author Ole.Matzura
44   */
45  
46  public class TestMonitor
47  {
48  	private Set<TestMonitorListener> listeners = new HashSet<TestMonitorListener>();
49  	private InternalWorkspaceListener workspaceListener = new InternalWorkspaceListener();
50  	private InternalProjectListener projectListener = new InternalProjectListener();
51  	private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
52  	private InternalTestRunListener testRunListener = new InternalTestRunListener();
53  	private InternalMockRunListener mockRunListener = new InternalMockRunListener();
54  	private InternalLoadTestRunListener loadTestRunListener = new InternalLoadTestRunListener();
55  	private Set<TestRunner> runningTestCases = new HashSet<TestRunner>();
56  	private Set<LoadTestRunner> runningLoadTests = new HashSet<LoadTestRunner>();
57  	private Set<MockRunner> runningMockServices = new HashSet<MockRunner>();
58  	private Map<String,TestRunner.Status> runStatusHistory = new HashMap<String, TestRunner.Status>();
59  	
60  	public TestMonitor()
61  	{
62  	}
63  	
64  	public TestRunner.Status getLastRunStatus( TestCase testCase )
65  	{
66  		return runStatusHistory.get( testCase.getId() );
67  	}
68  
69  	protected void notifyLoadTestStarted( LoadTestRunner runner )
70  	{
71  		runningLoadTests.add( runner );
72  
73  		if( listeners.isEmpty() )
74  			return;
75  
76  		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
77  		for( int c = 0; c < l.length; c++ )
78  		{
79  			l[c].loadTestStarted( runner );
80  		}
81  	}
82  
83  	protected void notifyLoadTestFinished( LoadTestRunner runner )
84  	{
85  		runningLoadTests.remove( runner.getLoadTest().getTestCase() );
86  
87  		if( listeners.isEmpty() )
88  			return;
89  
90  		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
91  		for( int c = 0; c < l.length; c++ )
92  		{
93  			l[c].loadTestFinished( runner );
94  		}
95  	}
96  
97  	protected void notifyTestCaseStarted( TestRunner runner )
98  	{
99  		if( listeners.isEmpty() )
100 			return;
101 
102 		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
103 		for( int c = 0; c < l.length; c++ )
104 		{
105 			l[c].testCaseStarted( runner );
106 		}
107 	}
108 
109 	protected void notifyTestCaseFinished( TestRunner runner )
110 	{
111 		if( listeners.isEmpty() )
112 			return;
113 
114 		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
115 		for( int c = 0; c < l.length; c++ )
116 		{
117 			l[c].testCaseFinished( runner );
118 		}
119 	}
120 
121 	protected void notifyMockServiceStarted( MockRunner runner )
122 	{
123 		if( listeners.isEmpty() )
124 			return;
125 
126 		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
127 		for( int c = 0; c < l.length; c++ )
128 		{
129 			l[c].mockServiceStarted( runner );
130 		}
131 	}
132 
133 	protected void notifyMockServiceStopped( MockRunner runner )
134 	{
135 		if( listeners.isEmpty() )
136 			return;
137 
138 		TestMonitorListener[] l = listeners.toArray( new TestMonitorListener[listeners.size()] );
139 		for( int c = 0; c < l.length; c++ )
140 		{
141 			l[c].mockServiceStopped( runner );
142 		}
143 	}
144 
145 	public boolean hasRunningLoadTest( TestCase testCase )
146 	{
147 		Iterator<LoadTestRunner> iterator = runningLoadTests.iterator();
148 		while( iterator.hasNext() )
149 		{
150 			if( iterator.next().getLoadTest().getTestCase() == testCase )
151 				return true;
152 		}
153 
154 		return false;
155 	}
156 
157 	public boolean hasRunningTestCase( TestCase testCase )
158 	{
159 		Iterator<TestRunner> iterator = runningTestCases.iterator();
160 		while( iterator.hasNext() )
161 		{
162 			if( iterator.next().getTestCase() == testCase )
163 				return true;
164 		}
165 
166 		return false;
167 	}
168 
169 	public void addTestMonitorListener( TestMonitorListener listener )
170 	{
171 		listeners.add( listener );
172 	}
173 
174 	public void removeTestMonitorListener( TestMonitorListener listener )
175 	{
176 		listeners.remove( listener );
177 	}
178 
179 	private class InternalWorkspaceListener extends WorkspaceListenerAdapter
180 	{
181 		public void projectRemoved( Project project )
182 		{
183 			unmonitorProject( project );
184 		}
185 
186 		public void projectAdded( Project project )
187 		{
188 			monitorProject( project );
189 		}
190 	}
191 
192 	private class InternalProjectListener extends ProjectListenerAdapter
193 	{
194 		public void testSuiteRemoved( TestSuite testSuite )
195 		{
196 			unmonitorTestSuite( testSuite );
197 		}
198 
199 		public void testSuiteAdded( TestSuite testSuite )
200 		{
201 			monitorTestSuite( testSuite );
202 		}
203 
204 		@Override
205 		public void mockServiceAdded( MockService mockService )
206 		{
207 			monitorMockService( mockService );
208 		}
209 
210 		@Override
211 		public void mockServiceRemoved( MockService mockService )
212 		{
213 			unmonitorMockService( mockService );
214 		}
215 	}
216 
217 	private class InternalTestSuiteListener extends TestSuiteListenerAdapter
218 	{
219 		public void testCaseAdded( TestCase testCase )
220 		{
221 			monitorTestCase( testCase );
222 		}
223 
224 		public void testCaseRemoved( TestCase testCase )
225 		{
226 			unmonitorTestCase( testCase );
227 		}
228 
229 		public void loadTestAdded( LoadTest loadTest )
230 		{
231 			monitorLoadTest( loadTest );
232 		}
233 
234 		public void loadTestRemoved( LoadTest loadTest )
235 		{
236 			unmonitorLoadTest( loadTest );
237 		}
238 	}
239 
240 	private class InternalTestRunListener extends TestRunListenerAdapter
241 	{
242 		public void afterRun( TestRunner testRunner, TestRunContext runContext )
243 		{
244 			runStatusHistory.put( testRunner.getTestCase().getId(), testRunner.getStatus() );
245 			
246 			runningTestCases.remove( testRunner );
247 			notifyTestCaseFinished( testRunner );
248 		}
249 
250 		public void beforeRun( TestRunner testRunner, TestRunContext runContext )
251 		{
252 			runningTestCases.add( testRunner );
253 			notifyTestCaseStarted( testRunner );
254 		}
255 	}
256 
257 	private class InternalMockRunListener extends MockRunListenerAdapter
258 	{
259 		@Override
260 		public void onMockRunnerStart( MockRunner mockRunner )
261 		{
262 			runningMockServices.add( mockRunner );
263 			notifyMockServiceStarted( mockRunner );
264 		}
265 
266 		@Override
267 		public void onMockRunnerStop( MockRunner mockRunner )
268 		{
269 			runningMockServices.remove( mockRunner );
270 			notifyMockServiceStopped( mockRunner );
271 		}
272 	}
273 
274 	private class InternalLoadTestRunListener extends LoadTestRunListenerAdapter
275 	{
276 		public void afterLoadTest( LoadTestRunner testRunner, LoadTestRunContext context )
277 		{
278 			runningLoadTests.remove( testRunner );
279 			notifyLoadTestFinished( testRunner );
280 		}
281 
282 		public void beforeLoadTest( LoadTestRunner testRunner, LoadTestRunContext context )
283 		{
284 			runningLoadTests.add( testRunner );
285 			notifyLoadTestStarted( testRunner );
286 		}
287 	}
288 
289 	public LoadTestRunner[] getRunningLoadTest()
290 	{
291 		return runningLoadTests.toArray( new LoadTestRunner[runningLoadTests.size()] );
292 	}
293 
294 	public boolean hasRunningTest( TestCase testCase )
295 	{
296 		return hasRunningLoadTest( testCase ) || hasRunningTestCase( testCase );
297 	}
298 
299 	public void init( Workspace workspace )
300 	{
301 		for( int c = 0; c < workspace.getProjectCount(); c++ )
302 		{
303 			Project project = workspace.getProjectAt( c );
304 			monitorProject( project );
305 		}
306 
307 		workspace.addWorkspaceListener( workspaceListener );
308 	}
309 
310 	public void monitorProject( Project project )
311 	{
312 		project.addProjectListener( projectListener );
313 
314 		for( int i = 0; i < project.getTestSuiteCount(); i++ )
315 		{
316 			monitorTestSuite( project.getTestSuiteAt( i ) );
317 		}
318 
319 		for( int i = 0; i < project.getMockServiceCount(); i++ )
320 		{
321 			monitorMockService( project.getMockServiceAt( i ) );
322 		}
323 	}
324 
325 	private void monitorMockService( MockService mockService )
326 	{
327 		mockService.addMockRunListener( mockRunListener );
328 	}
329 
330 	private void monitorTestSuite( TestSuite testSuite )
331 	{
332 		testSuite.addTestSuiteListener( testSuiteListener );
333 
334 		for( int j = 0; j < testSuite.getTestCaseCount(); j++ )
335 		{
336 			monitorTestCase( testSuite.getTestCaseAt( j ) );
337 		}
338 	}
339 
340 	private void monitorTestCase( TestCase testCase )
341 	{
342 		testCase.addTestRunListener( testRunListener );
343 
344 		for( int v = 0; v < testCase.getLoadTestCount(); v++ )
345 		{
346 			testCase.getLoadTestAt( v ).addLoadTestRunListener( loadTestRunListener );
347 		}
348 	}
349 
350 	private void monitorLoadTest( LoadTest loadTest )
351 	{
352 		loadTest.addLoadTestRunListener( loadTestRunListener );
353 	}
354 
355 	public void unmonitorProject( Project project )
356 	{
357 		project.removeProjectListener( projectListener );
358 
359 		for( int c = 0; c < project.getTestSuiteCount(); c++ )
360 		{
361 			TestSuite testSuite = project.getTestSuiteAt( c );
362 			unmonitorTestSuite( testSuite );
363 		}
364 
365 		for( int c = 0; c < project.getMockServiceCount(); c++ )
366 		{
367 			unmonitorMockService( project.getMockServiceAt( c ) );
368 		}
369 	}
370 
371 	private void unmonitorMockService( MockService mockService )
372 	{
373 		mockService.removeMockRunListener( mockRunListener );
374 	}
375 
376 	private void unmonitorTestSuite( TestSuite testSuite )
377 	{
378 		testSuite.removeTestSuiteListener( testSuiteListener );
379 
380 		for( int j = 0; j < testSuite.getTestCaseCount(); j++ )
381 		{
382 			TestCase testCase = testSuite.getTestCaseAt( j );
383 			unmonitorTestCase( testCase );
384 		}
385 	}
386 
387 	private void unmonitorTestCase( TestCase testCase )
388 	{
389 		testCase.removeTestRunListener( testRunListener );
390 
391 		for( int c = 0; c < testCase.getLoadTestCount(); c++ )
392 		{
393 			unmonitorLoadTest( testCase.getLoadTestAt( c ) );
394 		}
395 	}
396 
397 	private void unmonitorLoadTest( LoadTest loadTest )
398 	{
399 		loadTest.removeLoadTestRunListener( loadTestRunListener );
400 	}
401 
402 	public boolean hasRunningTests()
403 	{
404 		return runningLoadTests.size() + runningTestCases.size() > 0;
405 	}
406 
407 	public boolean hasRunningMock( MockService mockService )
408 	{
409 		for( MockRunner runner : runningMockServices )
410 			if( runner.getMockService() == mockService )
411 				return true;
412 
413 		return false;
414 	}
415 
416 	public boolean hasRunningTests( WsdlProject project )
417 	{
418 		for( TestRunner testRunner : runningTestCases )
419 		{
420 			if( testRunner.getTestCase().getTestSuite().getProject() == project )
421 				return true;
422 		}
423 
424 		for( LoadTestRunner loadTestRunner : runningLoadTests )
425 		{
426 			if( loadTestRunner.getLoadTest().getTestCase().getTestSuite().getProject() == project )
427 				return true;
428 		}
429 
430 		// for( MockRunner mockRunner : runningMockServices )
431 		// {
432 		// if( mockRunner.getMockService().getProject() == project )
433 		// return true;
434 		// }
435 
436 		return false;
437 	}
438 
439 	public void cancelAllTests( String reason )
440 	{
441 		for( TestRunner testRunner : runningTestCases )
442 		{
443 			testRunner.cancel( reason );
444 		}
445 
446 		for( LoadTestRunner loadTestRunner : runningLoadTests )
447 		{
448 			loadTestRunner.cancel( reason );
449 		}
450 
451 		for( MockRunner mockRunner : runningMockServices )
452 		{
453 			mockRunner.stop();
454 		}
455 	}
456 }