View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  package com.eviware.soapui.integration.impl;
13  
14  import java.awt.Frame;
15  import java.io.File;
16  import java.util.List;
17  
18  import com.eviware.soapui.SoapUI;
19  import com.eviware.soapui.integration.TestCaseEditIntegration;
20  import com.eviware.soapui.model.ModelItem;
21  import com.eviware.soapui.model.project.Project;
22  import com.eviware.soapui.model.testsuite.TestCase;
23  import com.eviware.soapui.model.testsuite.TestSuite;
24  import com.eviware.soapui.model.workspace.Workspace;
25  import com.eviware.soapui.support.SoapUIException;
26  import com.eviware.soapui.support.StringUtils;
27  import com.eviware.soapui.support.UISupport;
28  
29  public class TestCaseEditIntegrationImpl implements TestCaseEditIntegration
30  {
31  
32  	public void editTestCase( String project, String testSuite, String testCase )
33  	{
34  		// TODO Auto-generated method stub
35  	}
36  
37  	public void test()
38  	{
39  		CajoClient.getInstance().setLoadUIPath();
40  	}
41  
42  	public String getSoapUIPath()
43  	{
44  		String os = System.getProperty( "os.name" );
45  		if( os == null )
46  			return null;
47  
48  		String ext;
49  		if( os.indexOf( "Windows" ) >= 0 )
50  			ext = "bat";
51  		else if( os.indexOf( "Mac OS X" ) >= 0 )
52  			ext = "command";
53  		else
54  			ext = "sh";
55  
56  		String pro = "";
57  		Frame mainFrame = UISupport.getMainFrame();
58  		if( mainFrame != null && mainFrame.getTitle().toLowerCase().indexOf( "pro" ) > -1 )
59  		{
60  			pro = "-pro";
61  		}
62  
63  		String path = System.getProperty( "soapui.home" );
64  		if( path == null )
65  			return null;
66  		
67  		File pathFile = new File( path );
68  		path = pathFile.getAbsolutePath();
69  		path += File.separator + "bin" + File.separator + "soapui" + pro + "." + ext;
70  
71  		File f = new File( path );
72  		if( f.exists() )
73  			return f.getAbsolutePath();
74  		else
75  			return null;
76  	}
77  
78  	public void printLog( String log )
79  	{
80  		SoapUI.log( log );
81  	}
82  
83  	public void openProject( String[] parameters )
84  	{
85  		if( parameters != null && parameters.length == 1 )
86  		{
87  			String projectFilePath = parameters[0];
88  
89  			try
90  			{
91  				Workspace workspace = SoapUI.getWorkspace();
92  				Project project = findProject( projectFilePath, workspace );
93  
94  				project = openProject( projectFilePath, workspace, project );
95  
96  				showModelItem( project );
97  				bringToFront();
98  			}
99  			catch( Exception e )
100 			{
101 				SoapUI.logError( e );
102 			}
103 		}
104 	}
105 
106 	public void openTestCase( String[] parameters )
107 	{
108 		if( !isValid( parameters ) )
109 			return;
110 
111 		String projectFilePath = parameters[0];
112 		String testSuiteName = parameters[1];
113 		String testCaseName = parameters[2];
114 
115 		try
116 		{
117 			Workspace workspace = SoapUI.getWorkspace();
118 			Project project = findProject( projectFilePath, workspace );
119 
120 			project = openProject( projectFilePath, workspace, project );
121 
122 			TestSuite testSuite = project.getTestSuiteByName( testSuiteName );
123 			TestCase testCase = testSuite.getTestCaseByName( testCaseName );
124 
125 			showModelItem( testCase );
126 			bringToFront();
127 		}
128 		catch( Exception e )
129 		{
130 			SoapUI.logError( e );
131 		}
132 	}
133 
134 	public void bringToFront()
135 	{
136 		UISupport.getMainFrame().setVisible( true );
137 		UISupport.getMainFrame().setAlwaysOnTop( true );
138 		UISupport.getMainFrame().setAlwaysOnTop( false );
139 	}
140 
141 	private Project openProject( String projectFilePath, Workspace workspace, Project project ) throws SoapUIException
142 	{
143 		if( project != null )
144 		{
145 			if( !project.isOpen() )
146 				project = workspace.openProject( project );
147 		}
148 		else
149 		{
150 			project = workspace.importProject( projectFilePath );
151 		}
152 		if( project == null )
153 		{
154 			throw new SoapUIException( "Cannot open project on path: " + projectFilePath );
155 		}
156 		return project;
157 	}
158 
159 	private void showModelItem( ModelItem modelItem ) throws SoapUIException
160 	{
161 		if( modelItem != null )
162 		{
163 			UISupport.selectAndShow( modelItem );
164 		}
165 	}
166 
167 	private Project findProject( String projectFile, Workspace workspace )
168 	{
169 		Project project = null;
170 		List<? extends Project> projectList = workspace.getProjectList();
171 		for( Project proj : projectList )
172 		{
173 			if( proj.getPath().equalsIgnoreCase( projectFile ) )
174 			{
175 				project = workspace.getProjectByName( proj.getName() );
176 				break;
177 			}
178 		}
179 		return project;
180 	}
181 
182 	private boolean isValid( String[] parameters )
183 	{
184 		if( parameters != null && parameters.length == 3 )
185 		{
186 			for( String parameter : parameters )
187 			{
188 				if( StringUtils.isNullOrEmpty( parameter ) )
189 					return false;
190 			}
191 			return true;
192 		}
193 		else
194 			return false;
195 	}
196 
197 }