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 		return run( runner, context, null);
    104 	}
    105
    106 	public TestStepResult run( TestRunner runner, TestRunContext context, PropertyTransfer transfer )
    107 	{
    108 		ValueTransferResult result = new ValueTransferResult();
    109 		result.addAction( new ShowTransferValuesResultsAction( result ), true );
    110
    111 		canceled = false;
    112
    113 		long startTime = System.currentTimeMillis();
    114
    115 		for( int c = 0; c < transfers.size(); c++ )
    116 		{
    117 			PropertyTransfer valueTransfer = transfers.get( c );
    118 			if( transfer != null && transfer != valueTransfer )
    119 				continue;
    120
    121 			try
    122 			{
    123 				if( canceled )
    124 				{
    125 					result.setStatus( TestStepStatus.CANCELED );
    126 					result.setTimeTaken( System.currentTimeMillis()-startTime );
    127 					return result;
    128 				}
    129
    130 				String [] values = valueTransfer.transferProperties( context );
    131 				if( values != null && values.length > 0 )
    132 				{
    133 					String name = valueTransfer.getName();
    134 					result.addMessage( "Performed transfer [" + name + "]" );
    135 					result.addTransferResult( valueTransfer, values );
    136 				}
    137 			}
    138 			catch (PropertyTransferException e)
    139 			{
    140 				result.addMessage( "Error performing transfer [" + valueTransfer.getName() + "]" );
    141 				result.addTransferResult( valueTransfer, new String[] {e.getMessage()} );
    142
    143 				if( transfers.get( c ).getFailOnError() )
    144 				{
    145 					result.setError( e );
    146 					result.setStatus( TestStepStatus.FAILED );
    147 					result.setTimeTaken( System.currentTimeMillis()-startTime );
    148
    149 					if( failedIcon != null )
    150 						setIcon( failedIcon );
    151
    152 					return result;
    153 				}
    154 			}
    155 		}
    156
    157 		if( okIcon != null )
    158 			setIcon( okIcon );
    159
    160 		result.setStatus( TestStepStatus.OK );
    161 		result.setTimeTaken( System.currentTimeMillis()-startTime );
    162
    163 		return result;
    164 	}
    165
    166 	public boolean cancel()
    167 	{
    168 		canceled = true;
    169 		return canceled;
    170 	}
    171
    172 	public int getTransferCount()
    173 	{
    174 		return transferStepConfig.sizeOfTransfersArray();
    175 	}
    176
    177 	public PropertyTransfer getTransferAt( int index )
    178 	{
    179 		return transfers.get( index );
    180 	}
    181
    182 	public PropertyTransfer addTransfer( String name )
    183 	{
    184 		PropertyTransfer transfer = new PropertyTransfer( this, transferStepConfig.addNewTransfers());
    185 		transfer.setName( name );
    186 		transfer.setFailOnError( true );
    187 		transfers.add( transfer );
    188 		return transfer;
    189 	}
    190
    191 	public void removeTransferAt( int index )
    192 	{
    193 		transfers.remove( index ).release();
    194 		transferStepConfig.removeTransfers( index );
    195 	}
    196
    197 	public TestStepResult createFailedResult(String message)
    198 	{
    199 		ValueTransferResult result = new ValueTransferResult();
    200 		result.setStatus( TestStepStatus.FAILED );
    201 		result.addMessage( message );
    202
    203 		return result;
    204 	}
    205
    206 	public void release()
    207 	{
    208 		super.release();
    209
    210 		for( PropertyTransfer transfer : transfers )
    211 		{
    212 			transfer.release();
    213 		}
    214 	}
    215
    216 	public class ValueTransferResult extends WsdlTestStepResult
    217 	{
    218 		private List<ValueTransferConfig> transfers = new ArrayList<ValueTransferConfig>();
    219 		private List<String[]> values = new ArrayList<String[]>();
    220
    221 		public ValueTransferResult()
    222 		{
    223 			super( TransferResponseValuesTestStep.this );
    224 		}
    225
    226 		public void addTransferResult( PropertyTransfer transfer, String [] values )
    227 		{
    228 			// save a copy, so we dont mirror changes
    229 			transfers.add( ( ValueTransferConfig ) transfer.getConfig().copy()  );
    230 			this.values.add( values );
    231 		}
    232
    233 		public int getTransferCount()
    234 		{
    235 			return transfers == null ? 0 : transfers.size();
    236 		}
    237
    238 		public ValueTransferConfig getTransferAt( int index )
    239 		{
    240 			return transfers == null ? null : transfers.get( index );
    241 		}
    242
    243 		public String [] getTransferredValuesAt( int index )
    244 		{
    245 			return values == null ? null : values.get( index );
    246 		}
    247
    248 		public void discard()
    249 		{
    250 			super.discard();
    251
    252 			transfers = null;
    253 			values = null;
    254 		}
    255
    256 		public void writeTo(PrintWriter writer)
    257 		{
    258 			super.writeTo( writer );
    259
    260 			if( !isDiscarded() )
    261 			{
    262 				writer.println( "----------------------------------------------------" );
    263 				for( int c = 0; c < transfers.size(); c++ )
    264 				{
    265 					ValueTransferConfig transfer = transfers.get( c );
    266 					writer.println( transfer.getName() + " transferred [" + Arrays.toString(values.get( c )) + "] from [" +
    267 							transfer.getSourceStep() + "." + transfer.getSourceType() + "] to [" +
    268 							transfer.getTargetStep() + "." + transfer.getTargetType() + "]" );
    269 					if( transfer.getSourcePath() != null )
    270 					{
    271 						writer.println( "------------ source path -------------" );
    272 						writer.println( transfer.getSourcePath() );
    273 					}
    274 					if( transfer.getTargetPath() != null )
    275 					{
    276 						writer.println( "------------ target path -------------" );
    277 						writer.println( transfer.getTargetPath() );
    278 					}
    279 				}
    280 			}
    281 		}
    282 	}
    283
    284 	public PropertyTransfer getTransferByName(String name)
    285 	{
    286 		for( int c = 0; c < getTransferCount(); c++ )
    287 		{
    288 			PropertyTransfer transfer = getTransferAt( c );
    289 			if( transfer.getName().equals( name ))
    290 				return transfer;
    291 		}
    292
    293 		return null;
    294 	}
    295 }