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.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 ProgressBarTestCaseAdapter 
37  {
38  	private final JProgressBar progressBar;
39  	private final WsdlTestCase testCase;
40  	private InternalTestRunListener internalTestRunListener;
41  	private InternalTestMonitorListener internalTestMonitorListener;
42  
43  	public ProgressBarTestCaseAdapter( 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  	public void release()
57  	{
58  		testCase.removeTestRunListener( internalTestRunListener );
59  		SoapUI.getTestMonitor().removeTestMonitorListener( internalTestMonitorListener );
60  	}
61  	
62  	private void setLoadTestingState()
63  	{
64  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testCase ))
65  		{
66  			progressBar.setIndeterminate( true );
67  			progressBar.setString( "loadTesting" );
68  		}
69  		else
70  		{
71  			progressBar.setIndeterminate( false );
72  			progressBar.setString( "" );
73  		}
74  	}
75  	
76  	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
77  	{
78  		public void loadTestStarted(LoadTestRunner loadTestRunner)
79  		{
80  			setLoadTestingState();
81  		}
82  	
83  		public void loadTestFinished(LoadTestRunner loadTestRunner)
84  		{
85  			setLoadTestingState();
86  		}
87  	}
88  	
89  	public class InternalTestRunListener implements TestRunListener
90  	{
91  		public void beforeRun(TestRunner testRunner, TestRunContext runContext) 
92  		{
93  			if( progressBar.isIndeterminate() )
94  				return;
95  			
96  			progressBar.getModel().setMaximum( testRunner.getTestCase().getTestStepCount() );
97  			progressBar.setForeground( Color.GREEN.darker() );
98  		}
99  	
100 		public void beforeStep(TestRunner testRunner, TestRunContext runContext) 
101 		{
102 			if( progressBar.isIndeterminate() )
103 				return;
104 
105 			TestStep testStep = runContext.getCurrentStep();
106 			progressBar.setString( testStep.getName() );
107 			progressBar.setValue( runContext.getCurrentStepIndex() );
108 		}
109 		
110 		public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result)
111 		{
112 			if( progressBar.isIndeterminate() )
113 				return;
114 
115 			if( result.getStatus() == TestStepStatus.FAILED )
116 			{
117 				progressBar.setForeground( Color.RED );
118 			}
119 			else if( !testCase.getFailTestCaseOnErrors())
120 			{
121 				progressBar.setForeground( Color.GREEN.darker() );
122 			}
123 			
124 			progressBar.setValue( runContext.getCurrentStepIndex()+1 );
125 		}
126 	
127 		public void afterRun(TestRunner testRunner, TestRunContext runContext) 
128 		{
129 			if( !testCase.getFailOnError() && !testCase.getFailTestCaseOnErrors())
130 			{
131 				progressBar.setForeground( Color.GREEN.darker() );
132 			}
133 			
134 			if( progressBar.isIndeterminate() )
135 				return;
136 
137 			if( testRunner.getStatus() == TestRunner.Status.FINISHED )
138 				progressBar.setValue( testRunner.getTestCase().getTestStepCount() );
139 			
140 			progressBar.setString( testRunner.getStatus().toString() );
141 		}
142 	}
143 }