/*
* soapUI, copyright (C) 2004-2007 eviware.com
*
* soapUI is free software; you can redistribute it and/or modify it under the
* terms of version 2.1 of the GNU Lesser General Public License as published by
* the Free Software Foundation.
*
* soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details at gnu.org.
*/
package com.eviware.soapui.impl.wsdl.teststeps;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.swing.ImageIcon;
import com.eviware.soapui.config.PropertiesStepConfig;
import com.eviware.soapui.config.PropertyConfig;
import com.eviware.soapui.config.TestStepConfig;
import com.eviware.soapui.config.PropertiesStepConfig.Properties;
import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
import com.eviware.soapui.model.testsuite.TestRunContext;
import com.eviware.soapui.model.testsuite.TestRunner;
import com.eviware.soapui.model.testsuite.TestStep;
import com.eviware.soapui.model.testsuite.TestStepProperty;
import com.eviware.soapui.model.testsuite.TestStepResult;
import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
import com.eviware.soapui.support.UISupport;
import com.eviware.soapui.support.types.StringList;
/**
* TestStep that holds an arbitrary number of custom properties
*
* @author ole.matzura
*/
public class WsdlPropertiesTestStep extends WsdlTestStep
{
private PropertiesStepConfig propertiesStepConfig;
private List<StepProperty> properties = new ArrayList<StepProperty>();
private ImageIcon okIcon;
private ImageIcon failedIcon;
public WsdlPropertiesTestStep(WsdlTestCase testCase, TestStepConfig config, boolean forLoadTest)
{
super(testCase, config, true, forLoadTest);
if( !forLoadTest )
{
okIcon = UISupport.createImageIcon("/properties_step.gif");
failedIcon = UISupport.createImageIcon("/properties_step_failed.gif");
setIcon( okIcon );
}
if( config.getConfig() == null )
{
propertiesStepConfig = (PropertiesStepConfig) config.addNewConfig().changeType( PropertiesStepConfig.type );
propertiesStepConfig.addNewProperties();
propertiesStepConfig.setCreateMissingOnLoad( true );
}
else
{
propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
if( propertiesStepConfig.isSetProperties() )
{
Properties props = propertiesStepConfig.getProperties();
for( int c = 0; c < props.sizeOfPropertyArray(); c++ )
{
StepProperty stepProperty = new StepProperty( props.getPropertyArray( c ));
properties.add( stepProperty );
addProperty( stepProperty );
}
}
else
{
propertiesStepConfig.addNewProperties();
}
if( !propertiesStepConfig.isSetSaveFirst() )
propertiesStepConfig.setSaveFirst( true );
}
}
public TestStepResult run(TestRunner testRunner, TestRunContext testRunContext)
{
WsdlTestStepResult result = new WsdlTestStepResult( this );
if( okIcon != null )
setIcon( okIcon );
result.setStatus( TestStepStatus.OK );
result.startTimer();
if( isSaveFirst() )
saveDuringRun( result );
String source = getSource();
if( source != null && source.trim().length() > 0 )
{
try
{
int cnt = loadProperties(source,isCreateMissingOnLoad());
result.setStatus( TestStepStatus.OK );
result.addMessage( "Loaded " + cnt + " properties from [" + source + "]" );
}
catch (IOException e)
{
result.stopTimer();
result.addMessage( "Failed to load properties from [" + source + "]" );
result.setStatus( TestStepStatus.FAILED );
result.setError( e );
if( failedIcon != null )
setIcon( failedIcon );
}
}
if( !isSaveFirst() )
saveDuringRun( result );
result.stopTimer();
return result;
}
private boolean saveDuringRun( WsdlTestStepResult result )
{
String target = getTarget();
if( target != null && target.trim().length() > 0 )
{
try
{
int cnt = saveProperties(target);
result.setStatus( TestStepStatus.OK );
result.addMessage( "Saved " + cnt + " properties to [" + target + "]" );
}
catch (IOException e)
{
result.stopTimer();
result.addMessage( "Failed to save properties to [" + target + "]" );
result.setStatus( TestStepStatus.FAILED );
result.setError( e );
if( failedIcon != null )
setIcon( failedIcon );
return false;
}
}
return true;
}
private int saveProperties( String target ) throws IOException
{
java.util.Properties props = new java.util.Properties();
int cnt = 0;
for( StepProperty p : properties )
{
String name = p.getName();
props.setProperty( name, p.getValue() );
cnt++;
}
props.store(getPropertiesOutputStream(target), "TestStep [" + getName() + "] properties");
return cnt;
}
private FileOutputStream getPropertiesOutputStream(String target) throws FileNotFoundException
{
String fileProperty = System.getProperty( target );
if( fileProperty != null )
target = fileProperty;
return new FileOutputStream(target);
}
private int loadProperties(String source, boolean createMissing) throws IOException
{
// override methods so propertynames are returned in readorder
java.util.Properties props = new java.util.Properties()
{
public StringList names = new StringList();
@Override
public synchronized Object put( Object key, Object value )
{
names.add( key.toString() );
return super.put( key, value );
}
@Override
public Enumeration<?> propertyNames()
{
return Collections.enumeration( names );
}
};
props.load(getPropertiesInputStream(source));
int cnt = 0;
Enumeration names = props.propertyNames();
while( names.hasMoreElements() )
{
String name = names.nextElement().toString();
TestStepProperty property = getProperty( name );
if( property != null )
{
property.setValue( props.get( name ).toString() );
cnt++;
}
else if( createMissing )
{
addProperty( name ).setValue( props.get( name ).toString() );
cnt++;
}
}
return cnt;
}
private InputStream getPropertiesInputStream(String source) throws IOException
{
String fileProperty = System.getProperty( source );
if( fileProperty != null )
source = fileProperty;
URL url = null;
try
{
url = new URL( source );
}
catch( MalformedURLException e )
{
url = new URL( "file:" + source );
}
return url.openStream();
}
public StepProperty getTestStepPropertyAt( int index )
{
return properties.get( index );
}
public int getStepPropertyCount()
{
return properties.size();
}
public String getSource()
{
return propertiesStepConfig.getSource();
}
public void setSource( String source )
{
propertiesStepConfig.setSource( source );
}
public String getTarget()
{
return propertiesStepConfig.getTarget();
}
public void setTarget( String target )
{
propertiesStepConfig.setTarget( target );
}
public void resetConfigOnMove(TestStepConfig config)
{
super.resetConfigOnMove( config );
propertiesStepConfig = (PropertiesStepConfig) config.getConfig().changeType(PropertiesStepConfig.type);
for( int c = 0; c < propertiesStepConfig.getProperties().sizeOfPropertyArray(); c++ )
{
properties.get( c ).setConfig( propertiesStepConfig.getProperties().getPropertyArray( c ));
}
}
public StepProperty addProperty( String name )
{
PropertyConfig property = propertiesStepConfig.getProperties().addNewProperty();
property.setName( name );
StepProperty stepProperty = new StepProperty( property );
properties.add( stepProperty );
addProperty( stepProperty );
firePropertyAdded( name );
return stepProperty;
}
public void removeProperty( String name )
{
for( int c = 0; c < properties.size(); c++ )
{
if( properties.get( c ).getName().equalsIgnoreCase( name ))
{
removePropertyAt( c );
return;
}
}
}
public void removePropertyAt( int index )
{
String name = properties.get( index ).getName();
properties.remove( index );
deleteProperty( name );
firePropertyRemoved( name );
propertiesStepConfig.getProperties().removeProperty( index );
}
/**
* Internal property class
*
* @author ole
*/
public class StepProperty implements TestStepProperty
{
private PropertyConfig propertyConfig;
public StepProperty(PropertyConfig propertyConfig)
{
this.propertyConfig = propertyConfig;
}
public void setConfig(PropertyConfig propertyConfig)
{
this.propertyConfig = propertyConfig;
}
public String getName()
{
return propertyConfig.getName();
}
public void setName( String name )
{
String oldName = getName();
propertyConfig.setName( name );
propertyRenamed( oldName );
}
public String getDescription()
{
return null;
}
public String getValue()
{
return propertyConfig.getValue();
}
public void setValue(String value)
{
String oldValue = getValue();
propertyConfig.setValue( value );
firePropertyValueChanged( getName(), oldValue, value );
}
public boolean isReadOnly()
{
return false;
}
public TestStep getTestStep()
{
return WsdlPropertiesTestStep.this;
}
}
public int loadProperties( boolean createMissing ) throws IOException
{
return loadProperties( getSource(), createMissing );
}
public int saveProperties() throws IOException
{
return saveProperties( getTarget() );
}
public boolean isCreateMissingOnLoad()
{
return propertiesStepConfig.getCreateMissingOnLoad();
}
public void setCreateMissingOnLoad( boolean b )
{
propertiesStepConfig.setCreateMissingOnLoad( b );
}
public boolean isSaveFirst()
{
return propertiesStepConfig.getSaveFirst();
}
public void setSaveFirst( boolean b )
{
propertiesStepConfig.setSaveFirst( b );
}
}
|