1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl;
14
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import javax.swing.ImageIcon;
23 import javax.swing.JFileChooser;
24 import javax.swing.JOptionPane;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.xmlbeans.XmlException;
29
30 import com.eviware.soapui.SoapUI;
31 import com.eviware.soapui.config.InterfaceConfig;
32 import com.eviware.soapui.config.ProjectConfig;
33 import com.eviware.soapui.config.SoapuiProjectDocumentConfig;
34 import com.eviware.soapui.config.TestSuiteConfig;
35 import com.eviware.soapui.impl.wsdl.actions.project.AddInterfaceActionFromFile;
36 import com.eviware.soapui.impl.wsdl.actions.project.AddInterfaceActionFromURL;
37 import com.eviware.soapui.impl.wsdl.actions.project.AddNewTestSuiteAction;
38 import com.eviware.soapui.impl.wsdl.actions.project.RemoveProjectAction;
39 import com.eviware.soapui.impl.wsdl.actions.project.RenameProjectAction;
40 import com.eviware.soapui.impl.wsdl.support.WsdlImporter;
41 import com.eviware.soapui.model.iface.Interface;
42 import com.eviware.soapui.model.project.Project;
43 import com.eviware.soapui.model.project.ProjectListener;
44 import com.eviware.soapui.model.testsuite.TestSuite;
45 import com.eviware.soapui.model.tree.SoapUITreeNode;
46 import com.eviware.soapui.model.tree.nodes.ProjectTreeNode;
47
48 /***
49 * WSDL project implementation
50 *
51 * @author Ole.Matzura
52 */
53
54 public class WsdlProject extends AbstractWsdlModelItem implements Project
55 {
56 private ProjectConfig projectConfig;
57 private String path;
58 private List<WsdlInterface> interfaces = new ArrayList<WsdlInterface>();
59 private List<WsdlTestSuite> testSuites = new ArrayList<WsdlTestSuite>();
60 private Set<ProjectListener> listeners = new HashSet<ProjectListener>();
61 private SoapuiProjectDocumentConfig projectDocument;
62
63 private final static Log log = LogFactory.getLog( WsdlProject.class );
64 private ImageIcon projectIcon;
65
66 public WsdlProject() throws XmlException, IOException
67 {
68 projectDocument = SoapuiProjectDocumentConfig.Factory.newInstance();
69 projectConfig = projectDocument.addNewSoapuiProject();
70
71 init();
72 }
73
74 public WsdlProject(String path) throws XmlException, IOException
75 {
76 this.path = path;
77 File file = new File(path);
78
79 if( file.exists())
80 {
81 projectDocument = SoapuiProjectDocumentConfig.Factory.parse( file );
82 projectConfig = projectDocument.getSoapuiProject();
83 log.info( "Loaded project from [" + path + "]" );
84
85 InterfaceConfig [] interfaceConfigs = projectConfig.getInterfaceArray();
86 for (int i = 0; i < interfaceConfigs.length; i++)
87 {
88 interfaces.add( new WsdlInterface( this, interfaceConfigs[i] ));
89 }
90
91 TestSuiteConfig [] testSuiteConfigs = projectConfig.getTestSuiteArray();
92 for (int i = 0; i < testSuiteConfigs.length; i++)
93 {
94 testSuites.add( new WsdlTestSuite( this, testSuiteConfigs[i] ));
95 }
96 }
97 else
98 {
99 projectDocument = SoapuiProjectDocumentConfig.Factory.newInstance();
100 projectConfig = projectDocument.addNewSoapuiProject();
101 }
102
103 init();
104 }
105
106 private void init()
107 {
108
109 addAction( new AddInterfaceActionFromURL( this ) );
110 addAction( new AddInterfaceActionFromFile( this ) );
111 addAction( new AddNewTestSuiteAction( this ) );
112 addAction( new RenameProjectAction( this ) );
113 addAction( new RemoveProjectAction( this ) );
114
115 projectIcon = SoapUI.createImageIcon("/project.gif");
116 }
117
118 public Interface getInterfaceAt(int index)
119 {
120 return interfaces.get( index );
121 }
122
123 public int getInterfaceCount()
124 {
125 return interfaces.size();
126 }
127
128 public String getName()
129 {
130 return projectConfig.getName();
131 }
132
133 public String getPath()
134 {
135 return path;
136 }
137
138 public String save() throws IOException
139 {
140 if( path == null )
141 {
142 JFileChooser chooser = new JFileChooser();
143 chooser.setDialogTitle( "Save project " + getName() );
144 if( chooser.showSaveDialog( SoapUI.getInstance().getFrame() ) != JFileChooser.APPROVE_OPTION ) return null;
145
146 path = chooser.getSelectedFile().getAbsolutePath();
147 }
148
149 File projectFile = new File( path );
150 projectDocument.save( projectFile);
151 log.info( "Saved project to [" + projectFile.getAbsolutePath() + "]" );
152 return path;
153 }
154
155 public void setName(String name)
156 {
157 String old = getName();
158 projectConfig.setName( name );
159 notifyPropertyChanged(NAME_PROPERTY, old, name);
160 }
161
162 public Interface [] importWsdl(String url )
163 {
164 try
165 {
166 WsdlInterface[] result = WsdlImporter.getInstance().importWsdl( this, url );
167
168 boolean createRequests = JOptionPane.showConfirmDialog( SoapUI.getInstance().getFrame(),
169 "Create default requests for all operations", "Import WSDL", JOptionPane.YES_NO_OPTION ) == JOptionPane.YES_OPTION;
170
171 if( createRequests )
172 {
173 for (WsdlInterface iface : result)
174 {
175 for( int c = 0; c < iface.getOperationCount(); c++ )
176 {
177 WsdlOperation operation = (WsdlOperation) iface.getOperationAt( c );
178 WsdlRequest request = operation.addNewRequest( "Request 1");
179 try
180 {
181 request.setRequest( operation.createRequest( true ));
182 }
183 catch (Exception e)
184 {
185 e.printStackTrace();
186 }
187 }
188 }
189 }
190
191 return result;
192 }
193 catch (Exception e)
194 {
195 log.error( "Error importing wsdl: " + e.getMessage() );
196 e.printStackTrace();
197 }
198 return null;
199 }
200
201 public ProjectConfig getProjectConfig()
202 {
203 return projectConfig;
204 }
205
206 public WsdlInterface addNewInterface( String name )
207 {
208 WsdlInterface iface = new WsdlInterface( this, projectConfig.addNewInterface());
209 iface.setName( name );
210 interfaces.add( iface );
211 notifyInterfaceAdded( iface );
212
213 return iface;
214 }
215
216 public void addProjectListener(ProjectListener listener)
217 {
218 listeners.add( listener );
219 }
220
221 public void removeProjectListener(ProjectListener listener)
222 {
223 listeners.remove( listener );
224 }
225
226 public void notifyInterfaceAdded( WsdlInterface iface )
227 {
228 ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
229
230 for (int c = 0; c < a.length; c++ )
231 {
232 a[c].interfaceAdded( iface );
233 }
234 }
235
236 public void notifyInterfaceRemoved( WsdlInterface iface )
237 {
238 ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
239
240 for (int c = 0; c < a.length; c++ )
241 {
242 a[c].interfaceRemoved( iface );
243 }
244 }
245
246
247 public void notifyTestSuiteAdded( WsdlTestSuite testSuite )
248 {
249 ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
250
251 for (int c = 0; c < a.length; c++ )
252 {
253 a[c].testSuiteAdded( testSuite );
254 }
255 }
256
257 public void notifyTestSuiteRemoved( WsdlTestSuite testSuite )
258 {
259 ProjectListener[] a = listeners.toArray( new ProjectListener[listeners.size()] );
260
261 for (int c = 0; c < a.length; c++ )
262 {
263 a[c].testSuiteRemoved( testSuite );
264 }
265 }
266
267
268 public void removeInterface(WsdlInterface iface )
269 {
270 int ix = interfaces.indexOf( iface );
271 notifyInterfaceRemoved( iface );
272 interfaces.remove( ix );
273 projectConfig.removeInterface( ix );
274 }
275
276 public void removeTestSuite(WsdlTestSuite testSuite )
277 {
278 int ix = testSuites.indexOf( testSuite );
279 notifyTestSuiteRemoved( testSuite );
280 testSuites.remove( ix );
281 projectConfig.removeTestSuite( ix );
282 }
283
284 public ImageIcon getIcon()
285 {
286 return projectIcon;
287 }
288
289 public int getTestSuiteCount()
290 {
291 return testSuites.size();
292 }
293
294 public TestSuite getTestSuiteAt(int index)
295 {
296 return testSuites.get( index );
297 }
298
299 public WsdlTestSuite addNewTestSuite(String name)
300 {
301 WsdlTestSuite testSuite = new WsdlTestSuite( this, projectConfig.addNewTestSuite());
302 testSuite.setName( name );
303 testSuites.add( testSuite );
304 notifyTestSuiteAdded( testSuite );
305
306 return testSuite;
307 }
308
309 protected SoapUITreeNode createTreeNode()
310 {
311 return new ProjectTreeNode( this );
312 }
313 }