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.WsdlTestSuite;
21  import com.eviware.soapui.model.testsuite.LoadTestRunner;
22  import com.eviware.soapui.model.testsuite.TestCase;
23  import com.eviware.soapui.model.testsuite.TestCaseRunner;
24  import com.eviware.soapui.model.testsuite.TestRunner;
25  import com.eviware.soapui.model.testsuite.TestSuiteRunContext;
26  import com.eviware.soapui.model.testsuite.TestSuiteRunListener;
27  import com.eviware.soapui.model.testsuite.TestSuiteRunner;
28  import com.eviware.soapui.model.testsuite.TestRunner.Status;
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 ProgressBarTestSuiteAdapter
38  {
39  	private final JProgressBar progressBar;
40  	private final WsdlTestSuite testSuite;
41  	private InternalTestSuiteRunListener internalTestRunListener;
42  	private InternalTestMonitorListener internalTestMonitorListener;
43  
44  	public ProgressBarTestSuiteAdapter( JProgressBar progressBar, WsdlTestSuite testSuite )
45  	{
46  		this.progressBar = progressBar;
47  		this.testSuite = testSuite;
48  
49  		setLoadTestingState();
50  
51  		internalTestRunListener = new InternalTestSuiteRunListener();
52  		testSuite.addTestSuiteRunListener( internalTestRunListener );
53  		internalTestMonitorListener = new InternalTestMonitorListener();
54  		SoapUI.getTestMonitor().addTestMonitorListener( internalTestMonitorListener );
55  	}
56  
57  	public void release()
58  	{
59  		testSuite.removeTestSuiteRunListener( internalTestRunListener );
60  		SoapUI.getTestMonitor().removeTestMonitorListener( internalTestMonitorListener );
61  	}
62  
63  	private void setLoadTestingState()
64  	{
65  		if( SoapUI.getTestMonitor().hasRunningLoadTest( testSuite ) )
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 InternalTestSuiteRunListener implements TestSuiteRunListener
91  	{
92  		public void beforeRun( TestSuiteRunner testRunner, TestSuiteRunContext runContext )
93  		{
94  			if( progressBar.isIndeterminate() )
95  				return;
96  
97  			progressBar.getModel().setMaximum( testRunner.getTestSuite().getTestCaseCount() );
98  			progressBar.setForeground( Color.GREEN.darker() );
99  		}
100 
101 		public void beforeTestCase( TestSuiteRunner testRunner, TestSuiteRunContext runContext, TestCase testCase )
102 		{
103 			if( progressBar.isIndeterminate() )
104 				return;
105 
106 			progressBar.setString( testCase.getName() );
107 			progressBar.setValue( testRunner.getResults().size() );
108 		}
109 
110 		public void afterTestCase( TestSuiteRunner testRunner, TestSuiteRunContext runContext, TestCaseRunner result )
111 		{
112 			if( progressBar.isIndeterminate() )
113 				return;
114 
115 			if( result.getStatus() == TestRunner.Status.FAILED )
116 			{
117 				progressBar.setForeground( Color.RED );
118 			}
119 			else if( !testSuite.isFailOnErrors() )
120 			{
121 				progressBar.setForeground( Color.GREEN.darker() );
122 			}
123 
124 			progressBar.setValue( testRunner.getResults().size() + 1 );
125 		}
126 
127 		public void afterRun( TestSuiteRunner testRunner, TestSuiteRunContext runContext )
128 		{
129 			if( testRunner.getStatus() == Status.FAILED )
130 			{
131 				progressBar.setForeground( Color.RED );
132 			}
133 			else if( testRunner.getStatus() == Status.FINISHED )
134 			{
135 				progressBar.setForeground( Color.GREEN.darker() );
136 			}
137 
138 			if( progressBar.isIndeterminate() )
139 				return;
140 
141 			if( testRunner.getStatus() == TestCaseRunner.Status.FINISHED )
142 				progressBar.setValue( testRunner.getTestSuite().getTestCaseCount() );
143 
144 			progressBar.setString( testRunner.getStatus().toString() );
145 		}
146 	}
147 }