View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.support.TestRunListenerAdapter;
22  import com.eviware.soapui.model.testsuite.LoadTestRunner;
23  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
24  import com.eviware.soapui.model.testsuite.TestCaseRunner;
25  import com.eviware.soapui.model.testsuite.TestStep;
26  import com.eviware.soapui.model.testsuite.TestStepResult;
27  import com.eviware.soapui.model.testsuite.TestRunner.Status;
28  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
29  import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
30  
31  /***
32   * Class that keeps a JProgressBars state in sync with a TestCase
33   * 
34   * @author Ole.Matzura
35   */
36  
37  public class ProgressBarTestCaseAdapter
38  {
39  	private final JProgressBar progressBar;
40  	private final WsdlTestCase testCase;
41  	private InternalTestRunListener internalTestRunListener;
42  	private InternalTestMonitorListener internalTestMonitorListener;
43  
44  	public ProgressBarTestCaseAdapter( JProgressBar progressBar, WsdlTestCase testCase )
45  	{
46  		this.progressBar = progressBar;
47  		this.testCase = testCase;
48  
49  		setLoadTestingState();
50  
51  		internalTestRunListener = new InternalTestRunListener();
52  		testCase.addTestRunListener( internalTestRunListener );
53  		internalTestMonitorListener = new InternalTestMonitorListener();
54  		SoapUI.getTestMonitor().addTestMonitorListener( internalTestMonitorListener );
55  	}
56  
57  	public void release()
58  	{
59  		testCase.removeTestRunListener( internalTestRunListener );
60  		SoapUI.getTestMonitor().removeTestMonitorListener( internalTestMonitorListener );
61  	}
62  
63  	private void setLoadTestingState()
64  	{
65  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testCase ) )
66  		{
67  			progressBar.setIndeterminate( true );
68  			progressBar.setString( "loadTesting" );
69  		}
70  		else
71  		{
72  			progressBar.setIndeterminate( false );
73  			progressBar.setString( "" );
74  		}
75  	}
76  
77  	private class InternalTestMonitorListener extends TestMonitorListenerAdapter
78  	{
79  		public void loadTestStarted( LoadTestRunner loadTestRunner )
80  		{
81  			setLoadTestingState();
82  		}
83  
84  		public void loadTestFinished( LoadTestRunner loadTestRunner )
85  		{
86  			setLoadTestingState();
87  		}
88  	}
89  
90  	public class InternalTestRunListener extends TestRunListenerAdapter
91  	{
92  		public void beforeRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
93  		{
94  			if( progressBar.isIndeterminate() )
95  				return;
96  
97  			progressBar.getModel().setMaximum( testRunner.getTestCase().getTestStepCount() );
98  			progressBar.setForeground( Color.GREEN.darker() );
99  		}
100 
101 		public void beforeStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStep testStep )
102 		{
103 			if( progressBar.isIndeterminate() )
104 				return;
105 
106 			if( testStep != null )
107 			{
108 				progressBar.setString( testStep.getName() );
109 				progressBar.setValue( runContext.getCurrentStepIndex() );
110 			}
111 		}
112 
113 		public void afterStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStepResult result )
114 		{
115 			if( progressBar.isIndeterminate() )
116 				return;
117 
118 			if( result.getStatus() == TestStepStatus.FAILED )
119 			{
120 				progressBar.setForeground( Color.RED );
121 			}
122 			else if( !testCase.getFailTestCaseOnErrors() )
123 			{
124 				progressBar.setForeground( Color.GREEN.darker() );
125 			}
126 
127 			progressBar.setValue( runContext.getCurrentStepIndex() + 1 );
128 		}
129 
130 		public void afterRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
131 		{
132 			if( testRunner.getStatus() == Status.FAILED )
133 			{
134 				progressBar.setForeground( Color.RED );
135 			}
136 			else if( testRunner.getStatus() == Status.FINISHED )
137 			{
138 				progressBar.setForeground( Color.GREEN.darker() );
139 			}
140 
141 			if( progressBar.isIndeterminate() )
142 				return;
143 
144 			if( testRunner.getStatus() == TestCaseRunner.Status.FINISHED )
145 				progressBar.setValue( testRunner.getTestCase().getTestStepCount() );
146 
147 			progressBar.setString( testRunner.getStatus().toString() );
148 		}
149 	}
150 }