View Javadoc

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