1
2
3
4
5
6
7
8
9
10
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
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 protected void showReport(File reportFile, String configFile ) throws Exception
167 {
168 WSIReportPanel panel = new WSIReportPanel( reportFile, configFile, null, true );
169 panel.setPreferredSize( new Dimension( 600, 400 ));
170
171 UISupport.showDesktopPanel(
172 new DefaultDesktopPanel( "WS-I Report", "WS-I Report for Interface [" + getModelItem().getName() + "]", panel ));
173 }
174
175 public static File transformReport( File reportFile ) throws Exception
176 {
177 String dir = SoapUI.getSettings().getString( WSISettings.WSI_LOCATION, null );
178 File xsltFile = new File( dir + File.separatorChar + "common" + File.separatorChar + "xsl" +
179 File.separatorChar + "report.xsl" );
180
181 Source xmlSource = new StreamSource(reportFile);
182 Source xsltSource = new StreamSource(xsltFile);
183
184 TransformerFactory transFact = TransformerFactory.newInstance();
185 Transformer trans = transFact.newTransformer(xsltSource);
186
187 String outputFolder = SoapUI.getSettings().getString( WSISettings.OUTPUT_FOLDER, null );
188 File output = outputFolder == null || outputFolder.trim().length() == 0 ? null : new File( outputFolder );
189
190 File tempFile = File.createTempFile( "wsi-report", ".html", output );
191 trans.transform(xmlSource, new StreamResult( new FileWriter( tempFile )));
192
193 log.info( "WSI Report created at [" + tempFile.getAbsolutePath() + "]" );
194
195 return tempFile;
196 }
197
198 private class WSIProcessToolRunner extends ProcessToolRunner
199 {
200 private File reportFile;
201 private final Interface modelItem;
202
203 public WSIProcessToolRunner(ProcessBuilder builder, File reportFile, Interface modelItem )
204 {
205 super(builder, "WSI Analyzer", modelItem);
206 this.reportFile = reportFile;
207 this.modelItem = modelItem;
208 }
209
210 public String getDescription()
211 {
212 return "Running WSI Analysis tools..";
213 }
214
215 protected void afterRun( int exitCode, RunnerContext context )
216 {
217 if( exitCode == 0 && context.getStatus() == RunnerContext.RunnerStatus.FINISHED )
218 {
219 try
220 {
221 reportFile = transformReport( reportFile );
222 }
223 catch( Exception e1 )
224 {
225 SoapUI.logError( e1 );
226 }
227
228 SwingUtilities.invokeLater( new Runnable()
229 {
230 public void run()
231 {
232 try
233 {
234 showReport( reportFile, configFile );
235 }
236 catch( Exception e )
237 {
238 UISupport.showErrorMessage( e );
239 }
240 }
241 } );
242 }
243
244 closeDialog( modelItem );
245 }
246
247 public boolean showLog()
248 {
249 return modelItem.getSettings().getBoolean( WSISettings.SHOW_LOG );
250 }
251 }
252 }