View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.impl.wsdl.panels.support;
14  
15  import java.awt.Color;
16  
17  import javax.swing.JProgressBar;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
21  import com.eviware.soapui.model.testsuite.LoadTestRunner;
22  import com.eviware.soapui.model.testsuite.TestRunContext;
23  import com.eviware.soapui.model.testsuite.TestRunListener;
24  import com.eviware.soapui.model.testsuite.TestRunner;
25  import com.eviware.soapui.model.testsuite.TestStep;
26  import com.eviware.soapui.model.testsuite.TestStepResult;
27  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
28  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
29  
30  /***
31   * Class that keeps a JProgressBars state in sync with a TestCase
32   * 
33   * @author Ole.Matzura
34   */
35  
36  public class ProgressBarAdapter 
37  {
38  	private final JProgressBar progressBar;
39  	private final WsdlTestCase testCase;
40  	private InternalTestRunListener internalTestRunListener;
41  	private InternalTestMonitorListener internalTestMonitorListener;
42  
43  	public ProgressBarAdapter( JProgressBar progressBar, WsdlTestCase testCase )
44  	{
45  		this.progressBar = progressBar;
46  		this.testCase = testCase;
47  		
48  		setLoadTestingState();
49  		
50  		internalTestRunListener = new InternalTestRunListener();
51  		testCase.addTestRunListener( internalTestRunListener );
52  		internalTestMonitorListener = new InternalTestMonitorListener();
53  		SoapUI.getTestMonitor().addTestMonitorListener( internalTestMonitorListener );
54  	}
55  	
56  	private void setLoadTestingState()
57  	{
58  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testCase ))
59  		{
60  			progressBar.setIndeterminate( true );
61  			progressBar.setString( "loadTesting" );
62  		}
63  		else
64  		{
65  			progressBar.setIndeterminate( false );
66  			progressBar.setString( "" );
67  		}
68  	}
69  	
70  	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
71  	{
72  		public void loadTestStarted(LoadTestRunner loadTestRunner)
73  		{
74  			setLoadTestingState();
75  		}
76  	
77  		public void loadTestFinished(LoadTestRunner loadTestRunner)
78  		{
79  			setLoadTestingState();
80  		}
81  	}
82  	
83  	public class InternalTestRunListener implements TestRunListener
84  	{
85  		public void beforeRun(TestRunner testRunner, TestRunContext runContext) 
86  		{
87  			if( progressBar.isIndeterminate() )
88  				return;
89  			
90  			progressBar.getModel().setMaximum( testRunner.getTestCase().getTestStepCount() );
91  			progressBar.setForeground( Color.GREEN.darker() );
92  		}
93  	
94  		public void beforeStep(TestRunner testRunner, TestRunContext runContext) 
95  		{
96  			if( progressBar.isIndeterminate() )
97  				return;
98  
99  			TestStep testStep = runContext.getCurrentStep();
100 			progressBar.setString( testStep.getName() );
101 			progressBar.setValue( runContext.getCurrentStepIndex() );
102 		}
103 		
104 		public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result)
105 		{
106 			if( progressBar.isIndeterminate() )
107 				return;
108 
109 			if( result.getStatus() == TestStepStatus.FAILED )
110 			{
111 				progressBar.setForeground( Color.RED );
112 			}
113 			else if( !testCase.getFailTestCaseOnErrors())
114 			{
115 				progressBar.setForeground( Color.GREEN.darker() );
116 			}
117 			
118 			progressBar.setValue( runContext.getCurrentStepIndex()+1 );
119 		}
120 	
121 		public void afterRun(TestRunner testRunner, TestRunContext runContext) 
122 		{
123 			if( !testCase.getFailOnError() && !testCase.getFailTestCaseOnErrors())
124 			{
125 				progressBar.setForeground( Color.GREEN.darker() );
126 			}
127 			
128 			if( progressBar.isIndeterminate() )
129 				return;
130 
131 			progressBar.setString( testRunner.getStatus().toString() );
132 		}
133 	}
134 }