View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.model.support;
14  
15  import org.apache.commons.beanutils.PropertyUtils;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
19  import com.eviware.soapui.model.ModelItem;
20  
21  /***
22   * TestStepProperty implementation that maps to a standard javabean property
23   * 
24   * @author Ole.Matzura
25   */
26  
27  public class TestStepBeanProperty extends DefaultTestStepProperty
28  {
29  	public TestStepBeanProperty(String name, boolean isReadOnly, Object targetObject, String targetName, WsdlTestStep testStep )
30  	{
31  		super(name, isReadOnly, new BeanPropertyHandler( targetObject, targetName ), testStep );
32  	}
33  	
34  	/***
35  	 * PropertyHandler for setting/getting bean properties
36  	 * 
37  	 * @author Ole.Matzura
38  	 */
39  	
40  	public static class BeanPropertyHandler implements PropertyHandler
41  	{
42  		private final Object target;
43  		private final String targetName;
44  		
45  		public BeanPropertyHandler(Object targetObject, String targetName)
46  		{
47  			this.target = targetObject;
48  			this.targetName = targetName;
49  		}
50  
51  		public String getValue(DefaultTestStepProperty prop)
52  		{
53  			try
54  			{
55  				Object property = PropertyUtils.getProperty(target, targetName );
56  				return property == null ? null : property.toString();
57  			}
58  			catch (Exception e)
59  			{
60  				if( target instanceof ModelItem )
61  				{
62  					SoapUI.logError( new Exception( "Error getting property [" + targetName + "] from modelItem [" + 
63  							((ModelItem)target).getName() + "]", e) );
64  				}
65  				else
66  				{
67  					SoapUI.logError( new Exception( "Error getting property [" + targetName + "] from bean [" + target + "]", e) );
68  				}
69  				
70  				return null;
71  			}			
72  		}
73  
74  		public void setValue(DefaultTestStepProperty property,String value)
75  		{
76  			try
77  			{
78  				PropertyUtils.setProperty( target, targetName, value );
79  			}
80  			catch (Exception e)
81  			{
82  				SoapUI.logError( e );
83  			}
84  		}
85  	}
86  
87  }