View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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 org.apache.log4j.Logger;
23  
24  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
25  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
26  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
27  import com.eviware.soapui.model.testsuite.TestStep;
28  import com.eviware.soapui.support.UISupport;
29  
30  /***
31   * Action for setting the endpoint for all requests in a testcase
32   * 
33   * @author Ole.Matzura
34   */
35  
36  public class SetEndpointAction extends AbstractAction
37  {
38  	private static final String USE_CURRENT = "- use current -";
39  	private final static Logger log = Logger.getLogger(SetEndpointAction.class);
40  	private final WsdlTestCase testCase;
41  
42  	public SetEndpointAction( WsdlTestCase testCase )
43     {
44  		this.testCase = testCase;
45  		putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/set_endpoint.gif"));
46        putValue( Action.SHORT_DESCRIPTION, "Sets the endpoint for all requests in this testcase" );
47     }
48     
49  	public void actionPerformed(ActionEvent e)
50  	{
51  		Set<String> endpointSet = new HashSet<String>();
52  		Set<String> currentEndpointSet = new HashSet<String>();
53  		
54  		endpointSet.add( USE_CURRENT );
55  		
56  		for( int c = 0; c < testCase.getTestStepCount(); c++ )
57     	{
58     		TestStep step = testCase.getTestStepAt( c );
59     		if( step instanceof WsdlTestRequestStep )
60     		{
61     			WsdlTestRequestStep requestStep = (WsdlTestRequestStep) step;
62     			String [] endpoints = requestStep.getTestRequest().getOperation().getInterface().getEndpoints();
63     			for( int i = 0; i < endpoints.length; i++ )
64     			{
65     				endpointSet.add( endpoints[i] );
66     			}
67     			
68     			currentEndpointSet.add( requestStep.getTestRequest().getEndpoint() );
69     		}
70     	}
71  		
72  		String selected = (String) UISupport.prompt(	"Select endpoint to set for all requests", "Set Endpoint", endpointSet.toArray(), 
73  				currentEndpointSet.size() == 1 ? currentEndpointSet.iterator().next() : USE_CURRENT );
74  		
75  		if( selected == null || selected.equals( USE_CURRENT )) return;
76  		
77  		int cnt = 0;
78  		
79  		for( int c = 0; c < testCase.getTestStepCount(); c++ )
80     	{
81     		TestStep step = testCase.getTestStepAt( c );
82     		if( step instanceof WsdlTestRequestStep )
83     		{
84     			WsdlTestRequestStep requestStep = (WsdlTestRequestStep) step;
85     			WsdlTestRequest testRequest = requestStep.getTestRequest();
86     			
87     			if( !testRequest.getEndpoint().equals( selected ))
88     			{
89     				testRequest.setEndpoint( selected );
90     				cnt++;
91     			}
92     		}
93     	}
94  		
95  		UISupport.showInfoMessage( "Changed endpoint to [" + selected + "] for " + cnt + " requests" );
96     }
97  }