1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.data.actions;
14
15 import java.awt.event.ActionEvent;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.text.SimpleDateFormat;
20 import java.util.Date;
21
22 import javax.swing.AbstractAction;
23 import javax.swing.Action;
24
25 import com.eviware.soapui.SoapUI;
26 import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLog;
27 import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLogEntry;
28 import com.eviware.soapui.model.testsuite.TestStep;
29 import com.eviware.soapui.support.UISupport;
30
31 /***
32 * Simple loadtest log exporter, creates a comma-separated file containing a header row
33 * and values for each log entry
34 *
35 * @author Ole.Matzura
36 */
37
38 public class ExportLoadTestLogAction extends AbstractAction
39 {
40 private final LoadTestLog loadTestLog;
41
42 public ExportLoadTestLogAction(LoadTestLog loadTestLog)
43 {
44 this.loadTestLog = loadTestLog;
45 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif"));
46 putValue( Action.SHORT_DESCRIPTION, "Export current loadtest log to a file" );
47 }
48
49 public void actionPerformed(ActionEvent e)
50 {
51 try
52 {
53 if( loadTestLog.getSize() == 0 )
54 {
55 UISupport.showErrorMessage( "No data to export!" );
56 return;
57 }
58
59 File file = UISupport.getFileDialogs().saveAs(this, "Select file for log export");
60 if( file == null )
61 return;
62
63 int cnt = exportToFile(file);
64
65 UISupport.showInfoMessage( "Saved " + cnt + " log entries to file [" + file.getName() + "]" );
66 }
67 catch (IOException e1)
68 {
69 SoapUI.logError( e1 );
70 }
71 }
72
73 public int exportToFile(File file) throws IOException
74 {
75 PrintWriter writer = new PrintWriter(file);
76 writeHeader(writer);
77 int cnt = writeLog(writer);
78 writer.flush();
79 writer.close();
80 return cnt;
81 }
82
83 private int writeLog( PrintWriter writer)
84 {
85 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
86
87 int c = 0;
88 for( ; c < loadTestLog.getSize(); c++ )
89 {
90 LoadTestLogEntry logEntry = (LoadTestLogEntry) loadTestLog.getElementAt( c );
91 writer.write( sdf.format( new Date(logEntry.getTimeStamp()) ));
92 writer.write( ',' );
93 writer.write( logEntry.getType() );
94 writer.write( ',' );
95
96 TestStep targetStep = logEntry.getTargetStep();
97 if( targetStep != null )
98 writer.write( targetStep.getName() );
99
100 writer.write( ",\"" );
101 writer.write( logEntry.getMessage() );
102 writer.write( '"' );
103 writer.println();
104 }
105
106 return c;
107 }
108
109 private void writeHeader(PrintWriter writer)
110 {
111 writer.println( "time,type,step,message");
112 }
113 }