1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.support;
14
15 import java.io.File;
16 import java.sql.Connection;
17 import java.sql.Driver;
18 import java.sql.DriverManager;
19 import java.sql.DriverPropertyInfo;
20 import java.sql.SQLException;
21 import java.util.HashSet;
22 import java.util.Properties;
23 import java.util.Set;
24
25 import org.apache.xmlbeans.XmlException;
26 import org.apache.xmlbeans.XmlObject;
27 import org.w3c.dom.Node;
28
29 import com.eviware.soapui.SoapUI;
30 import com.eviware.soapui.model.project.Project;
31 import com.eviware.soapui.model.propertyexpansion.PropertyExpander;
32 import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
33 import com.eviware.soapui.model.support.ModelSupport;
34 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
35 import com.eviware.soapui.model.testsuite.TestStep;
36
37 public class GroovyUtils
38 {
39 protected final PropertyExpansionContext context;
40
41 public GroovyUtils( PropertyExpansionContext context )
42 {
43 this.context = context;
44 }
45
46 public final String getProjectPath()
47 {
48 Project project = ModelSupport.getModelItemProject( context.getModelItem() );
49
50 String path = project.getPath();
51 int ix = path.lastIndexOf( File.separatorChar );
52 return ix == -1 ? "" : path.substring( 0, ix );
53 }
54
55 public final XmlHolder getXmlHolder( String xmlPropertyOrString ) throws Exception
56 {
57 try
58 {
59 return new XmlHolder( XmlObject.Factory.parse( xmlPropertyOrString ) );
60 }
61 catch( Exception e )
62 {
63 return new XmlHolder( context, xmlPropertyOrString );
64 }
65 }
66
67 public final String expand( String property )
68 {
69 return PropertyExpander.expandProperties( context, property );
70 }
71
72 public final void setPropertyValue( String testStep, String property, String value ) throws Exception
73 {
74 if( !( context instanceof TestCaseRunContext ) )
75 return;
76
77 TestStep step = ( ( TestCaseRunContext )context ).getTestCase().getTestStepByName( testStep );
78 if( step != null )
79 {
80 step.setPropertyValue( property, value );
81 }
82 else
83 {
84 throw new Exception( "Missing TestStep [" + testStep + "] in TestCase" );
85 }
86 }
87
88 public final String getXml( Node node ) throws XmlException
89 {
90 return XmlObject.Factory.parse( node ).xmlText();
91 }
92
93 private static Set<String> registeredDrivers = new HashSet<String>();
94
95 public static void registerJdbcDriver( String name )
96 {
97 if( registeredDrivers.contains( name ) )
98 return;
99
100 try
101 {
102 Driver d = ( Driver )Class.forName( name, true, SoapUI.getSoapUICore().getExtensionClassLoader() )
103 .newInstance();
104 DriverManager.registerDriver( new DriverShim( d ) );
105 registeredDrivers.add( name );
106 }
107 catch( Exception e )
108 {
109 e.printStackTrace();
110 }
111 }
112
113 static class DriverShim implements Driver
114 {
115 private Driver driver;
116
117 DriverShim( Driver d )
118 {
119 this.driver = d;
120 }
121
122 public boolean acceptsURL( String u ) throws SQLException
123 {
124 return this.driver.acceptsURL( u );
125 }
126
127 public Connection connect( String u, Properties p ) throws SQLException
128 {
129 return this.driver.connect( u, p );
130 }
131
132 public int getMajorVersion()
133 {
134 return this.driver.getMajorVersion();
135 }
136
137 public int getMinorVersion()
138 {
139 return this.driver.getMinorVersion();
140 }
141
142 public DriverPropertyInfo[] getPropertyInfo( String u, Properties p ) throws SQLException
143 {
144 return this.driver.getPropertyInfo( u, p );
145 }
146
147 public boolean jdbcCompliant()
148 {
149 return this.driver.jdbcCompliant();
150 }
151 }
152 }