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