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.support;
14  
15  import java.io.File;
16  
17  import org.apache.xmlbeans.XmlException;
18  import org.apache.xmlbeans.XmlObject;
19  import org.w3c.dom.Node;
20  
21  import com.eviware.soapui.model.project.Project;
22  import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
23  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
24  import com.eviware.soapui.model.support.ModelSupport;
25  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
26  import com.eviware.soapui.model.testsuite.TestStep;
27  
28  public class GroovyUtils
29  {
30  	protected final PropertyExpansionContext context;
31  
32  	public GroovyUtils( PropertyExpansionContext context )
33  	{
34  		this.context = context;
35  	}
36  
37  	public final String getProjectPath()
38  	{
39  		Project project = ModelSupport.getModelItemProject( context.getModelItem() );
40  
41  		String path = project.getPath();
42  		int ix = path.lastIndexOf( File.separatorChar );
43  		return ix == -1 ? "" : path.substring( 0, ix );
44  	}
45  
46  	public final XmlHolder getXmlHolder( String xmlPropertyOrString ) throws Exception
47  	{
48  		try
49  		{
50  			return new XmlHolder( XmlObject.Factory.parse( xmlPropertyOrString ) );
51  		}
52  		catch( Exception e )
53  		{
54  			return new XmlHolder( context, xmlPropertyOrString );
55  		}
56  	}
57  
58  	public final String expand( String property )
59  	{
60  		return PropertyExpander.expandProperties( context, property );
61  	}
62  
63  	public final void setPropertyValue( String testStep, String property, String value ) throws Exception
64  	{
65  		if( !( context instanceof TestCaseRunContext ) )
66  			return;
67  
68  		TestStep step = ( ( TestCaseRunContext )context ).getTestCase().getTestStepByName( testStep );
69  		if( step != null )
70  		{
71  			step.setPropertyValue( property, value );
72  		}
73  		else
74  		{
75  			throw new Exception( "Missing TestStep [" + testStep + "] in TestCase" );
76  		}
77  	}
78  
79  	public final String getXml( Node node ) throws XmlException
80  	{
81  		return XmlObject.Factory.parse( node ).xmlText();
82  	}
83  }