View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.testcase.actions;
14  
15  import java.awt.event.ActionEvent;
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  import javax.swing.AbstractAction;
20  import javax.swing.Action;
21  
22  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
23  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
24  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
25  import com.eviware.soapui.model.testsuite.TestStep;
26  import com.eviware.soapui.support.UISupport;
27  
28  /***
29   * Action for setting the endpoint for all requests in a testcase
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class SetEndpointAction extends AbstractAction
35  {
36  	private static final String USE_CURRENT = "- use current -";
37  	private final WsdlTestCase testCase;
38  
39  	public SetEndpointAction( WsdlTestCase testCase )
40  	{
41  		this.testCase = testCase;
42  		putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/set_endpoint.gif" ) );
43  		putValue( Action.SHORT_DESCRIPTION, "Sets the endpoint for all requests in this testcase" );
44  	}
45  
46  	public void actionPerformed( ActionEvent e )
47  	{
48  		Set<String> endpointSet = new HashSet<String>();
49  		Set<String> currentEndpointSet = new HashSet<String>();
50  
51  		endpointSet.add( USE_CURRENT );
52  
53  		for( int c = 0; c < testCase.getTestStepCount(); c++ )
54  		{
55  			TestStep step = testCase.getTestStepAt( c );
56  			if( step instanceof WsdlTestRequestStep )
57  			{
58  				WsdlTestRequestStep requestStep = ( WsdlTestRequestStep )step;
59  				String[] endpoints = requestStep.getTestRequest().getOperation().getInterface().getEndpoints();
60  				for( int i = 0; i < endpoints.length; i++ )
61  				{
62  					endpointSet.add( endpoints[i] );
63  				}
64  
65  				currentEndpointSet.add( requestStep.getTestRequest().getEndpoint() );
66  			}
67  		}
68  
69  		String selected = ( String )UISupport.prompt( "Select endpoint to set for all requests", "Set Endpoint",
70  				endpointSet.toArray(), currentEndpointSet.size() == 1 ? currentEndpointSet.iterator().next() : USE_CURRENT );
71  
72  		if( selected == null || selected.equals( USE_CURRENT ) )
73  			return;
74  
75  		int cnt = 0;
76  
77  		for( int c = 0; c < testCase.getTestStepCount(); c++ )
78  		{
79  			TestStep step = testCase.getTestStepAt( c );
80  			if( step instanceof WsdlTestRequestStep )
81  			{
82  				WsdlTestRequestStep requestStep = ( WsdlTestRequestStep )step;
83  				WsdlTestRequest testRequest = requestStep.getTestRequest();
84  
85  				if( testRequest.getEndpoint() == null || !testRequest.getEndpoint().equals( selected ) )
86  				{
87  					testRequest.setEndpoint( selected );
88  					cnt++ ;
89  				}
90  			}
91  		}
92  
93  		UISupport.showInfoMessage( "Changed endpoint to [" + selected + "] for " + cnt + " requests" );
94  	}
95  }