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.mock;
    14
    15  import java.util.Collection;
    16  import java.util.Map;
    17  import java.util.Set;
    18
    19  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
    20  import com.eviware.soapui.model.mock.MockRunContext;
    21  import com.eviware.soapui.model.testsuite.TestCase;
    22  import com.eviware.soapui.model.testsuite.TestRunContext;
    23  import com.eviware.soapui.model.testsuite.TestRunner;
    24  import com.eviware.soapui.model.testsuite.TestStep;
    25  import com.eviware.soapui.model.testsuite.TestStepProperty;
    26  import com.eviware.soapui.support.types.StringToObjectMap;
    27  import com.eviware.soapui.support.types.StringToStringMap;
    28
    29  /***
    30   * MockRunContext available during dispatching of a WsdlMockRequest
    31   *
    32   * @author ole.matzura
    33   */
    34
    35  public class WsdlMockRunContext implements MockRunContext, Map<String,Object>, TestRunContext
    36  {
    37  	private StringToObjectMap properties;
    38  	private final WsdlMockService mockService;
    39  	private final WsdlTestRunContext context;
    40
    41  	public WsdlMockRunContext( WsdlMockService mockService, WsdlTestRunContext context )
    42  	{
    43  		this.mockService = mockService;
    44  		this.context = context;
    45
    46  		properties = context == null ? new StringToObjectMap() : context.getProperties();
    47  	}
    48
    49  	public WsdlMockService getMockService()
    50  	{
    51  		return mockService;
    52  	}
    53
    54  	public Object getProperty( String name )
    55  	{
    56  		return properties.get( name );
    57  	}
    58
    59  	public boolean hasProperty( String name )
    60  	{
    61  		return properties.containsKey( name );
    62  	}
    63
    64  	public Object removeProperty( String name )
    65  	{
    66  		return properties.remove( name );
    67  	}
    68
    69  	public void setProperty( String name, Object value )
    70  	{
    71  		if( context != null )
    72  		{
    73  			int ix = name.indexOf( PROPERTY_SEPARATOR );
    74  			if( ix > 0 )
    75  			{
    76  				String teststepname = name.substring(0, ix);
    77  				TestStep refTestStep = context.getTestCase().getTestStepByName( teststepname );
    78  				if( refTestStep != null )
    79  				{
    80  					TestStepProperty property = refTestStep.getProperty( name.substring(ix+1));
    81  					if( property != null && !property.isReadOnly() )
    82  					{
    83  						property.setValue( value.toString() );
    84  						return;
    85  					}
    86  				}
    87  			}
    88  		}
    89
    90  		properties.put( name, value );
    91  	}
    92
    93  	public StringToStringMap toStringToStringMap()
    94  	{
    95  		StringToStringMap result = new StringToStringMap();
    96
    97  		for( String key : properties.keySet() )
    98  		{
    99  			Object value = properties.get( key );
    100 			if( value != null )
    101 				result.put( key, value.toString() );
    102 		}
    103
    104 		return result;
    105 	}
    106
    107 	public void clear()
    108 	{
    109 		properties.clear();
    110 	}
    111
    112 	public Object clone()
    113 	{
    114 		return properties.clone();
    115 	}
    116
    117 	public boolean containsKey( Object arg0 )
    118 	{
    119 		return properties.containsKey( arg0 );
    120 	}
    121
    122 	public boolean containsValue( Object arg0 )
    123 	{
    124 		return properties.containsValue( arg0 );
    125 	}
    126
    127 	public Set<Entry<String, Object>> entrySet()
    128 	{
    129 		return properties.entrySet();
    130 	}
    131
    132 	public boolean equals( Object arg0 )
    133 	{
    134 		return properties.equals( arg0 );
    135 	}
    136
    137 	public Object get( Object arg0 )
    138 	{
    139 		return properties.get( arg0 );
    140 	}
    141
    142 	public int hashCode()
    143 	{
    144 		return properties.hashCode();
    145 	}
    146
    147 	public boolean isEmpty()
    148 	{
    149 		return properties.isEmpty();
    150 	}
    151
    152 	public Set<String> keySet()
    153 	{
    154 		return properties.keySet();
    155 	}
    156
    157 	public Object put( String arg0, Object arg1 )
    158 	{
    159 		return properties.put( arg0, arg1 );
    160 	}
    161
    162 	public void putAll( Map<? extends String, ? extends Object> arg0 )
    163 	{
    164 		properties.putAll( arg0 );
    165 	}
    166
    167 	public Object remove( Object arg0 )
    168 	{
    169 		return properties.remove( arg0 );
    170 	}
    171
    172 	public int size()
    173 	{
    174 		return properties.size();
    175 	}
    176
    177 	public String toString()
    178 	{
    179 		return properties.toString();
    180 	}
    181
    182 	public Collection<Object> values()
    183 	{
    184 		return properties.values();
    185 	}
    186
    187 	public TestStep getCurrentStep()
    188 	{
    189 		return context == null ? null : context.getCurrentStep();
    190 	}
    191
    192 	public int getCurrentStepIndex()
    193 	{
    194 		return context == null ? -1 : context.getCurrentStepIndex();
    195 	}
    196
    197 	public Object getProperty( String testStep, String propertyName )
    198 	{
    199 		return context == null ? null : context.getProperty( testStep, propertyName );
    200 	}
    201
    202 	public TestRunner getTestRunner()
    203 	{
    204 		return context == null ? null : context.getTestRunner();
    205 	}
    206
    207 	public TestCase getTestCase()
    208 	{
    209 		// TODO Auto-generated method stub
    210 		return null;
    211 	}
    212 }