View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.propertyexpansion;
14  
15  import org.apache.commons.beanutils.PropertyUtils;
16  
17  import com.eviware.soapui.model.testsuite.TestProperty;
18  
19  public class MutablePropertyExpansionImpl extends PropertyExpansionImpl implements MutablePropertyExpansion
20  {
21  	private final Object container;
22  	private final String propertyName;
23  	private String stringRep;
24  
25  	public MutablePropertyExpansionImpl( TestProperty tp, String xpath, Object container, String propertyName )
26  	{
27  		super( tp, xpath );
28  		this.container = container;
29  		this.propertyName = propertyName;
30  
31  		stringRep = toString();
32  	}
33  
34  	public void setProperty( TestProperty property )
35  	{
36  		super.setProperty( property );
37  	}
38  
39  	public void setXPath( String xpath )
40  	{
41  		super.setXPath( xpath );
42  	}
43  
44  	public void update() throws Exception
45  	{
46  		String rep = toString();
47  
48  		// not changed
49  		if( stringRep.equals( rep ) )
50  			return;
51  
52  		Object obj = PropertyUtils.getProperty( container, propertyName );
53  		if( obj == null )
54  			throw new Exception( "property value is null" );
55  
56  		String str = obj.toString();
57  		int ix = str.indexOf( stringRep );
58  		if( ix == -1 )
59  			throw new Exception( "property expansion [" + stringRep + "] not found for update" );
60  
61  		while( ix != -1 )
62  		{
63  			str = str.substring( 0, ix ) + rep + str.substring( ix + stringRep.length() );
64  			ix = str.indexOf( stringRep, ix + rep.length() );
65  		}
66  
67  		PropertyUtils.setProperty( container, propertyName, str );
68  
69  		stringRep = rep;
70  	}
71  }