View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.teststeps;
    14
    15  import java.io.PrintWriter;
    16  import java.util.ArrayList;
    17  import java.util.Arrays;
    18  import java.util.List;
    19
    20  import javax.swing.ImageIcon;
    21
    22  import com.eviware.soapui.config.TestStepConfig;
    23  import com.eviware.soapui.config.TransferValuesStepConfig;
    24  import com.eviware.soapui.config.ValueTransferConfig;
    25  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
    26  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowTransferValuesResultsAction;
    27  import com.eviware.soapui.model.testsuite.TestRunContext;
    28  import com.eviware.soapui.model.testsuite.TestRunner;
    29  import com.eviware.soapui.model.testsuite.TestStepResult;
    30  import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
    31  import com.eviware.soapui.support.UISupport;
    32
    33  /***
    34   * WsdlTestStep for transferring values from a WsdlTestRequest response to a
    35   * WsdlTestRequest request using XPath expressions
    36   *
    37   * @author Ole.Matzura
    38   */
    39
    40  public class TransferResponseValuesTestStep extends WsdlTestStep
    41  {
    42  	private TransferValuesStepConfig transferStepConfig;
    43  	private boolean canceled;
    44  	private List<PropertyTransfer> transfers = new ArrayList<PropertyTransfer>();
    45  	private ImageIcon failedIcon;
    46  	private ImageIcon okIcon;
    47
    48  	public TransferResponseValuesTestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
    49  	{
    50  		super(testCase, config, true, forLoadTest);
    51
    52  		if( !forLoadTest )
    53  		{
    54  	      okIcon = UISupport.createImageIcon("/value_transfer.gif");
    55  	      failedIcon = UISupport.createImageIcon( "/value_transfer_failed.gif" );
    56
    57  			setIcon( okIcon);
    58
    59  //			addAction( ActionSupport.SEPARATOR_ACTION );
    60  //	      addAction( new CloneTestStepAction( this, "PropertyTransfer" ) );
    61  //	      addAction( ActionSupport.SEPARATOR_ACTION );
    62  //	      addAction( new ShowOnlineHelpAction( HelpUrls.PROPERTYTRANSFER_HELP_URL ));
    63  		}
    64
    65        if( testCase == null )
    66        	postInit( config );
    67  	}
    68
    69  	public void postInit( TestStepConfig config )
    70  	{
    71  		if( config.getConfig() != null )
    72        {
    73  			transferStepConfig = (TransferValuesStepConfig) config.getConfig().changeType(TransferValuesStepConfig.type);
    74  			for( int c = 0; c < transferStepConfig.sizeOfTransfersArray(); c++ )
    75  			{
    76  				transfers.add( new PropertyTransfer( this, transferStepConfig.getTransfersArray( c )));
    77  			}
    78        }
    79        else
    80        {
    81           transferStepConfig = (TransferValuesStepConfig) config.addNewConfig().changeType( TransferValuesStepConfig.type );
    82        }
    83  	}
    84
    85  	public TransferValuesStepConfig getTransferConfig()
    86  	{
    87  		return transferStepConfig;
    88  	}
    89
    90  	public void resetConfigOnMove(TestStepConfig config)
    91  	{
    92  		super.resetConfigOnMove(config);
    93
    94  		transferStepConfig = (TransferValuesStepConfig) config.getConfig().changeType(TransferValuesStepConfig.type);
    95  		for( int c = 0; c < transferStepConfig.sizeOfTransfersArray(); c++ )
    96  		{
    97  			transfers.get( c ).setConfigOnMove( transferStepConfig.getTransfersArray( c ));
    98  		}
    99  	}
    100
    101 	public TestStepResult run( TestRunner runner, TestRunContext context )
    102 	{
    103 		ValueTransferResult result = new ValueTransferResult();
    104 		result.addAction( new ShowTransferValuesResultsAction( result ), true );
    105
    106 		canceled = false;
    107
    108 		long startTime = System.currentTimeMillis();
    109
    110 		for( int c = 0; c < transfers.size(); c++ )
    111 		{
    112 			PropertyTransfer valueTransfer = transfers.get( c );
    113
    114 			try
    115 			{
    116 				if( canceled )
    117 				{
    118 					result.setStatus( TestStepStatus.CANCELED );
    119 					result.setTimeTaken( System.currentTimeMillis()-startTime );
    120 					return result;
    121 				}
    122
    123 				String [] values = valueTransfer.transferProperties( context );
    124 				if( values != null && values.length > 0 )
    125 				{
    126 					String name = valueTransfer.getName();
    127 					result.addMessage( "Performed transfer [" + name + "]" );
    128 					result.addTransferResult( valueTransfer, values );
    129 				}
    130 			}
    131 			catch (PropertyTransferException e)
    132 			{
    133 				result.addMessage( "Error performing transfer [" + valueTransfer.getName() + "]" );
    134 				result.addTransferResult( valueTransfer, new String[] {e.toString()} );
    135
    136 				if( transfers.get( c ).getFailOnError() )
    137 				{
    138 					result.setError( e );
    139 					result.setStatus( TestStepStatus.FAILED );
    140 					result.setTimeTaken( System.currentTimeMillis()-startTime );
    141
    142 					if( failedIcon != null )
    143 						setIcon( failedIcon );
    144
    145 					return result;
    146 				}
    147 			}
    148 		}
    149
    150 		if( okIcon != null )
    151 			setIcon( okIcon );
    152
    153 		result.setStatus( TestStepStatus.OK );
    154 		result.setTimeTaken( System.currentTimeMillis()-startTime );
    155
    156 		return result;
    157 	}
    158
    159 	public boolean cancel()
    160 	{
    161 		canceled = true;
    162 		return canceled;
    163 	}
    164
    165 	public int getTransferCount()
    166 	{
    167 		return transferStepConfig.sizeOfTransfersArray();
    168 	}
    169
    170 	public PropertyTransfer getTransferAt( int index )
    171 	{
    172 		return transfers.get( index );
    173 	}
    174
    175 	public PropertyTransfer addTransfer( String name )
    176 	{
    177 		PropertyTransfer transfer = new PropertyTransfer( this, transferStepConfig.addNewTransfers());
    178 		transfer.setName( name );
    179 		transfer.setFailOnError( true );
    180 		transfers.add( transfer );
    181 		return transfer;
    182 	}
    183
    184 	public void removeTransferAt( int index )
    185 	{
    186 		transfers.remove( index ).release();
    187 		transferStepConfig.removeTransfers( index );
    188 	}
    189
    190 	public TestStepResult createFailedResult(String message)
    191 	{
    192 		ValueTransferResult result = new ValueTransferResult();
    193 		result.setStatus( TestStepStatus.FAILED );
    194 		result.addMessage( message );
    195
    196 		return result;
    197 	}
    198
    199 	public void release()
    200 	{
    201 		super.release();
    202
    203 		for( PropertyTransfer transfer : transfers )
    204 		{
    205 			transfer.release();
    206 		}
    207 	}
    208
    209 	public class ValueTransferResult extends WsdlTestStepResult
    210 	{
    211 		private List<ValueTransferConfig> transfers = new ArrayList<ValueTransferConfig>();
    212 		private List<String[]> values = new ArrayList<String[]>();
    213
    214 		public ValueTransferResult()
    215 		{
    216 			super( TransferResponseValuesTestStep.this );
    217 		}
    218
    219 		public void addTransferResult( PropertyTransfer transfer, String [] values )
    220 		{
    221 			// save a copy, so we dont mirror changes
    222 			transfers.add( ( ValueTransferConfig ) transfer.getConfig().copy()  );
    223 			this.values.add( values );
    224 		}
    225
    226 		public int getTransferCount()
    227 		{
    228 			return transfers == null ? 0 : transfers.size();
    229 		}
    230
    231 		public ValueTransferConfig getTransferAt( int index )
    232 		{
    233 			return transfers == null ? null : transfers.get( index );
    234 		}
    235
    236 		public String [] getTransferredValuesAt( int index )
    237 		{
    238 			return values == null ? null : values.get( index );
    239 		}
    240
    241 		public void discard()
    242 		{
    243 			super.discard();
    244
    245 			transfers = null;
    246 			values = null;
    247 		}
    248
    249 		public void writeTo(PrintWriter writer)
    250 		{
    251 			super.writeTo( writer );
    252
    253 			if( !isDiscarded() )
    254 			{
    255 				writer.println( "----------------------------------------------------" );
    256 				for( int c = 0; c < transfers.size(); c++ )
    257 				{
    258 					ValueTransferConfig transfer = transfers.get( c );
    259 					writer.println( transfer.getName() + " transferred [" + Arrays.toString(values.get( c )) + "] from [" +
    260 							transfer.getSourceStep() + "." + transfer.getSourceType() + "] to [" +
    261 							transfer.getTargetStep() + "." + transfer.getTargetType() + "]" );
    262 					if( transfer.getSourcePath() != null )
    263 					{
    264 						writer.println( "------------ source path -------------" );
    265 						writer.println( transfer.getSourcePath() );
    266 					}
    267 					if( transfer.getTargetPath() != null )
    268 					{
    269 						writer.println( "------------ target path -------------" );
    270 						writer.println( transfer.getTargetPath() );
    271 					}
    272 				}
    273 			}
    274 		}
    275 	}
    276
    277 	public PropertyTransfer getTransferByName(String name)
    278 	{
    279 		for( int c = 0; c < getTransferCount(); c++ )
    280 		{
    281 			PropertyTransfer transfer = getTransferAt( c );
    282 			if( transfer.getName().equals( name ))
    283 				return transfer;
    284 		}
    285
    286 		return null;
    287 	}
    288 }