View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.impl.wsdl.actions.iface.tools.wsi;
14  
15  import java.awt.Dimension;
16  import java.io.File;
17  import java.io.FileWriter;
18  import java.io.IOException;
19  
20  import javax.swing.SwingUtilities;
21  import javax.xml.transform.Source;
22  import javax.xml.transform.Transformer;
23  import javax.xml.transform.TransformerFactory;
24  import javax.xml.transform.stream.StreamResult;
25  import javax.xml.transform.stream.StreamSource;
26  
27  import org.apache.log4j.Logger;
28  import org.wsI.testing.x2003.x03.common.AddStyleSheet;
29  import org.wsI.testing.x2004.x07.analyzerConfig.AssertionResults;
30  import org.wsI.testing.x2004.x07.analyzerConfig.Configuration;
31  import org.wsI.testing.x2004.x07.analyzerConfig.ConfigurationDocument;
32  import org.wsI.testing.x2004.x07.analyzerConfig.ReportFile;
33  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlElementReference;
34  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlElementType;
35  import org.wsI.testing.x2004.x07.analyzerConfig.WsdlReference;
36  
37  import com.eviware.soapui.SoapUI;
38  import com.eviware.soapui.actions.SoapUIPreferencesAction;
39  import com.eviware.soapui.impl.wsdl.WsdlInterface;
40  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.AbstractToolsAction;
41  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ArgumentBuilder;
42  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ProcessToolRunner;
43  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.RunnerContext;
44  import com.eviware.soapui.impl.wsdl.actions.iface.tools.support.ToolHost;
45  import com.eviware.soapui.model.iface.Interface;
46  import com.eviware.soapui.model.settings.Settings;
47  import com.eviware.soapui.settings.WSISettings;
48  import com.eviware.soapui.support.Tools;
49  import com.eviware.soapui.support.UISupport;
50  import com.eviware.soapui.support.types.StringToStringMap;
51  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
52  
53  /***
54   * Invokes WS-I Analyzer Tool
55   * 
56   * @author Ole.Matzura
57   */
58  
59  public class WSIAnalyzeAction extends AbstractToolsAction<Interface>
60  {
61  	public final static String SOAPUI_ACTION_ID = "WSIAnalyzeAction";
62  	public final static Logger log = Logger.getLogger( WSIAnalyzeAction.class );
63  	
64  	private String configFile;
65  	
66     public WSIAnalyzeAction()
67     {
68        super( "Check WSI Compliance", "Validate this WSDL for WSI Basic Profile compliance");
69     }
70  
71  	protected void generate(StringToStringMap values, ToolHost toolHost, Interface modelItem) throws Exception
72  	{
73  		String wsiDir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, System.getenv("WSI_HOME") );
74  		if( Tools.isEmpty( wsiDir ) )
75  		{
76  			UISupport.showErrorMessage( "WSI Test Tools directory must be set in global preferences" );
77  			
78  			if( UISupport.getMainFrame() != null )
79  			{
80  				if( SoapUIPreferencesAction.getInstance().show( SoapUIPreferencesAction.WS_I_SETTINGS ))
81  				{
82  					wsiDir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
83  				}
84  			}
85  		}
86  		
87  		if( Tools.isEmpty( wsiDir ) )
88  			return;
89  		
90  		ProcessBuilder builder = new ProcessBuilder();
91  		
92  		File reportFile = File.createTempFile( "wsi-report", ".xml" );
93  		
94  		ArgumentBuilder args = buildArgs( reportFile, modelItem );
95  		builder.command(args.getArgs());
96  		File wsiToolDir = new File(wsiDir + File.separatorChar + "cs" + File.separatorChar + "bin" );
97  		if( !wsiToolDir.exists() )
98  			wsiToolDir = new File(wsiDir + File.separatorChar + "java" + File.separatorChar + "bin" );
99  		
100 		builder.directory(wsiToolDir);
101 		
102 		toolHost.run( new WSIProcessToolRunner( builder, reportFile, modelItem ));
103 	}
104 
105 	private ArgumentBuilder buildArgs(File reportFile, Interface modelItem) throws IOException
106 	{
107 		Settings settings = modelItem.getSettings();
108 		
109 		ConfigurationDocument configDoc = createConfigFile(reportFile, settings, (WsdlInterface) modelItem);
110 		configFile = configDoc.toString();
111 		
112 		File file = File.createTempFile( "wsi-analyzer-config", ".xml" );
113 		
114 		configDoc.save( file );
115 		
116 		ArgumentBuilder builder = new ArgumentBuilder( new StringToStringMap() );
117 		builder.startScript( "Analyzer", "", ".sh" );
118 			
119 		builder.addArgs( "-config", file.getAbsolutePath() );
120 		
121 		// add this to command-line due to bug in wsi-tools (?)
122 		if( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ))
123 			builder.addArgs( "-assertionDescription", "true" );
124 		
125 		return builder;
126 	}
127 
128 	private ConfigurationDocument createConfigFile(File reportFile, Settings settings, WsdlInterface iface)
129 	{
130 		ConfigurationDocument configDoc = ConfigurationDocument.Factory.newInstance();
131 		Configuration config = configDoc.addNewConfiguration();
132 		
133 		config.setVerbose( settings.getBoolean( WSISettings.VERBOSE ) );
134 		AssertionResults results = config.addNewAssertionResults();
135 		results.setType( 
136 				AssertionResults.Type.Enum.forString( 
137 						settings.getString( WSISettings.RESULTS_TYPE, AssertionResults.Type.ONLY_FAILED.toString() )));
138 		
139 		results.setMessageEntry( settings.getBoolean( WSISettings.MESSAGE_ENTRY ) );
140 		results.setFailureMessage( settings.getBoolean( WSISettings.FAILURE_MESSAGE ) );
141 		results.setAssertionDescription( settings.getBoolean( WSISettings.ASSERTION_DESCRIPTION ) );
142 
143 		ReportFile report = config.addNewReportFile();
144 		report.setLocation( reportFile.getAbsolutePath() );
145 		report.setReplace( true );
146 		AddStyleSheet stylesheet = report.addNewAddStyleSheet();
147 		stylesheet.setHref( "./../common/Profiles/SSBP10_BP11_TAD.xml" );
148 		stylesheet.setType( "text/xsl" );
149 		stylesheet.setAlternate( false );
150 		
151 		config.setTestAssertionsFile( "../../common/profiles/SSBP10_BP11_TAD.xml" );
152 		
153 		WsdlReference wsdlRef = config.addNewWsdlReference();
154 		
155 		StringToStringMap values = new StringToStringMap();
156 		values.put( WSDL, iface.getDefinition() );
157 		values.put( CACHED_WSDL, Boolean.toString( iface.isCached() ));
158 		
159 		wsdlRef.setWsdlURI( getWsdlUrl( values, iface ) );
160 		WsdlElementReference wsdlElement = wsdlRef.addNewWsdlElement();
161 		wsdlElement.setType( WsdlElementType.BINDING );
162 		wsdlElement.setStringValue( iface.getBindingName().getLocalPart() );
163 		wsdlElement.setNamespace( iface.getBindingName().getNamespaceURI() );
164 		return configDoc;
165 	}
166 	
167 	protected void showReport(File reportFile, String configFile ) throws Exception
168 	{
169 		WSIReportPanel panel = new WSIReportPanel( reportFile, configFile, null, true );
170 		panel.setPreferredSize( new Dimension( 600, 400 ));
171 		
172 		UISupport.showDesktopPanel( 
173 				new DefaultDesktopPanel( "WS-I Report", "WS-I Report for Interface [" + getModelItem().getName() + "]", panel ));
174 	}
175 
176 	public static File transformReport( File reportFile ) throws Exception
177 	{
178 		String dir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
179       File xsltFile = new File( dir + File.separatorChar + "common" + File.separatorChar + "xsl" + 
180       		File.separatorChar + "report.xsl" );
181 
182       Source xmlSource = new StreamSource(reportFile);
183       Source xsltSource = new StreamSource(xsltFile);
184 
185 		TransformerFactory transFact = TransformerFactory.newInstance();
186 		Transformer trans = transFact.newTransformer(xsltSource);
187 
188 		String outputFolder = SoapUI.getSettings().getString( WSISettings.OUTPUT_FOLDER, null );
189 		File output = outputFolder == null || outputFolder.trim().length() == 0 ? null : new File( outputFolder );
190 		
191 		File tempFile = File.createTempFile( "wsi-report", ".html", output );
192 		trans.transform(xmlSource, new StreamResult( new FileWriter( tempFile )));
193 		
194 		log.info( "WSI Report created at [" + tempFile.getAbsolutePath() + "]" );
195 		
196 		return tempFile;
197 	}
198 	
199 	private class WSIProcessToolRunner extends ProcessToolRunner
200 	{
201 		private File reportFile;
202 		private final Interface modelItem;
203 
204 		public WSIProcessToolRunner(ProcessBuilder builder, File reportFile, Interface modelItem )
205 		{
206 			super(builder, "WSI Analyzer", modelItem);
207 			this.reportFile = reportFile;
208 			this.modelItem = modelItem;
209 		}
210 		
211 		public String getDescription()
212 		{
213 			return "Running WSI Analysis tools..";
214 		}
215 
216 		protected void afterRun( int exitCode, RunnerContext context )
217 		{
218 			if( exitCode == 0 && context.getStatus() == RunnerContext.RunnerStatus.FINISHED )
219 			{
220 				try
221 				{
222 					reportFile = transformReport( reportFile );
223 				}
224 				catch( Exception e1 )
225 				{
226 					SoapUI.logError( e1 );
227 				}
228 				
229 				SwingUtilities.invokeLater( new Runnable() 
230 				{
231 					public void run()
232 					{
233 						try
234 						{
235 							showReport( reportFile, configFile );
236 						}
237 	      			catch( Exception e )
238 	      			{
239 	      				UISupport.showErrorMessage( e );
240 	      			}
241 					}
242 				} );
243 			}
244 			
245 			closeDialog( modelItem );
246 		}
247 		
248 		public boolean showLog()
249 		{
250 			return modelItem.getSettings().getBoolean( WSISettings.SHOW_LOG );
251 		}
252 	}
253 }