View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.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 implements TestRunListener
91  	{
92  		public void beforeRun(TestRunner testRunner, TestRunContext 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(TestRunner testRunner, TestRunContext runContext) 
102 		{
103 			if( progressBar.isIndeterminate() )
104 				return;
105 
106 			TestStep testStep = runContext.getCurrentStep();
107 			progressBar.setString( testStep.getName() );
108 			progressBar.setValue( runContext.getCurrentStepIndex() );
109 		}
110 		
111 		public void afterStep(TestRunner testRunner, TestRunContext runContext, TestStepResult result)
112 		{
113 			if( progressBar.isIndeterminate() )
114 				return;
115 
116 			if( result.getStatus() == TestStepStatus.FAILED )
117 			{
118 				progressBar.setForeground( Color.RED );
119 			}
120 			else if( !testCase.getFailTestCaseOnErrors())
121 			{
122 				progressBar.setForeground( Color.GREEN.darker() );
123 			}
124 			
125 			progressBar.setValue( runContext.getCurrentStepIndex()+1 );
126 		}
127 	
128 		public void afterRun(TestRunner testRunner, TestRunContext runContext) 
129 		{
130 			if( testRunner.getStatus() == Status.FAILED )
131 			{
132 				progressBar.setForeground( Color.RED );
133 			}
134 			else if( testRunner.getStatus() == Status.FINISHED )
135 			{
136 				progressBar.setForeground( Color.GREEN.darker() );
137 			}
138 			
139 			if( progressBar.isIndeterminate() )
140 				return;
141 
142 			if( testRunner.getStatus() == TestRunner.Status.FINISHED )
143 				progressBar.setValue( testRunner.getTestCase().getTestStepCount() );
144 			
145 			progressBar.setString( testRunner.getStatus().toString() );
146 		}
147 	}
148 }