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  package com.eviware.soapui.impl.wsdl.panels.teststeps.amf;
13  
14  import com.eviware.soapui.SoapUI;
15  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
16  import com.eviware.soapui.impl.wsdl.teststeps.AMFRequestTestStep;
17  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
18  import com.eviware.soapui.model.testsuite.TestCaseRunner;
19  import com.eviware.soapui.model.testsuite.TestRunListener;
20  import com.eviware.soapui.model.testsuite.TestStep;
21  import com.eviware.soapui.model.testsuite.TestStepResult;
22  import com.eviware.soapui.support.StringUtils;
23  
24  import flex.messaging.io.amf.client.exceptions.ClientStatusException;
25  import flex.messaging.io.amf.client.exceptions.ServerStatusException;
26  
27  public class AMFTestRunListener implements TestRunListener
28  {
29  	private AMFCredentials amfCredentials;
30  
31  	public void afterRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
32  	{
33  		if( amfCredentials != null && runContext.getProperty( AMFSubmit.AMF_CONNECTION ) != null
34  				&& runContext.getProperty( AMFSubmit.AMF_CONNECTION ) instanceof SoapUIAMFConnection )
35  		{
36  			if( amfCredentials.isLoggedIn() )
37  				amfCredentials.logout();
38  		}
39  	}
40  
41  	public void beforeRun( TestCaseRunner testRunner, TestCaseRunContext runContext )
42  	{
43  		if( testRunner.getTestCase() instanceof WsdlTestCase )
44  		{
45  			try
46  			{
47  				WsdlTestCase wsdlTestCase = ( WsdlTestCase )testRunner.getTestCase();
48  
49  				if( wsdlTestCase.getConfig().getAmfAuthorisation() )
50  				{
51  					if( noAMFTestSteps( wsdlTestCase ) )
52  						return;
53  
54  					String endpoint = runContext.expand( wsdlTestCase.getConfig().getAmfEndpoint() );
55  					String username = runContext.expand( wsdlTestCase.getConfig().getAmfLogin() );
56  					String password = runContext.expand( wsdlTestCase.getConfig().getAmfPassword() );
57  
58  					SoapUIAMFConnection amfConnection = null;
59  
60  					if( StringUtils.hasContent( endpoint ) )
61  					{
62  						if( StringUtils.hasContent( username ) )
63  						{
64  							amfCredentials = new AMFCredentials( endpoint, username, password, runContext );
65  							amfConnection = amfCredentials.login();
66  						}
67  						else
68  						{
69  							amfConnection = new SoapUIAMFConnection();
70  							amfConnection.connect( runContext.expand( endpoint ) );
71  						}
72  
73  						runContext.setProperty( AMFSubmit.AMF_CONNECTION, amfConnection );
74  					}
75  				}
76  			}
77  			catch( ClientStatusException e )
78  			{
79  				SoapUI.logError( e );
80  			}
81  			catch( ServerStatusException e )
82  			{
83  				SoapUI.logError( e );
84  			}
85  		}
86  	}
87  
88  	/***
89  	 * check if there is no amf test steps in test case then disable amf
90  	 * authorisation and return true otherwise return false
91  	 * 
92  	 * @param wsdlTestCase
93  	 * @return boolean
94  	 */
95  	private static boolean noAMFTestSteps( WsdlTestCase wsdlTestCase )
96  	{
97  		if( wsdlTestCase.getTestStepsOfType( AMFRequestTestStep.class ).isEmpty() )
98  		{
99  			// wsdlTestCase.getConfig().setAmfAuthorisation( false );
100 			// SoapUI.log( wsdlTestCase.getName()
101 			// +
102 			// " does not contain any AMF Test Step therefore AMF Authorisation is disabled!"
103 			// );
104 			return true;
105 		}
106 		return false;
107 	}
108 
109 	public void beforeStep( TestCaseRunner testRunner, TestCaseRunContext runContext )
110 	{
111 	}
112 
113 	public void beforeStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStep testStep )
114 	{
115 	}
116 
117 	public void afterStep( TestCaseRunner testRunner, TestCaseRunContext runContext, TestStepResult result )
118 	{
119 	}
120 }