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 org.jdesktop.swingx.JXTable;
26
27 import com.eviware.soapui.SoapUI;
28 import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLog;
29 import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLogEntry;
30 import com.eviware.soapui.support.UISupport;
31
32 /***
33 * Simple loadtest log exporter, creates a comma-separated file containing a header row
34 * and values for each log entry
35 *
36 * @author Ole.Matzura
37 */
38
39 public class ExportLoadTestLogAction extends AbstractAction
40 {
41 private final LoadTestLog loadTestLog;
42 private final JXTable logTable;
43
44 public ExportLoadTestLogAction(LoadTestLog loadTestLog, JXTable logTable)
45 {
46 this.loadTestLog = loadTestLog;
47 this.logTable = logTable;
48 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif"));
49 putValue( Action.SHORT_DESCRIPTION, "Export current loadtest log to a file" );
50 }
51
52 public void actionPerformed(ActionEvent e)
53 {
54 try
55 {
56 if( loadTestLog.getSize() == 0 || (logTable != null && logTable.getRowCount() == 0 ))
57 {
58 UISupport.showErrorMessage( "No data to export!" );
59 return;
60 }
61
62 File file = UISupport.getFileDialogs().saveAs(this, "Select file for log export");
63 if( file == null )
64 return;
65
66 int cnt = exportToFile(file);
67
68 UISupport.showInfoMessage( "Saved " + cnt + " log entries to file [" + file.getName() + "]" );
69 }
70 catch (IOException e1)
71 {
72 SoapUI.logError( e1 );
73 }
74 }
75
76 public int exportToFile(File file) throws IOException
77 {
78 PrintWriter writer = new PrintWriter(file);
79 writeHeader(writer);
80 int cnt = writeLog(writer);
81 writer.flush();
82 writer.close();
83 return cnt;
84 }
85
86 private int writeLog( PrintWriter writer)
87 {
88 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
89
90 int cnt = 0;
91 for( int c = 0 ; c < loadTestLog.getSize(); c++ )
92 {
93 if( logTable != null )
94 {
95 int index = logTable.getFilters().convertRowIndexToView( c );
96 if( index == -1 )
97 continue;
98 }
99
100 LoadTestLogEntry logEntry = (LoadTestLogEntry) loadTestLog.getElementAt( c );
101 writer.write( sdf.format( new Date(logEntry.getTimeStamp()) ));
102 writer.write( ',' );
103 writer.write( logEntry.getType() );
104 writer.write( ',' );
105 String targetStepName = logEntry.getTargetStepName();
106 writer.write( targetStepName == null ? "" : targetStepName );
107 writer.write( ",\"" );
108 writer.write( logEntry.getMessage() );
109 writer.write( '"' );
110 writer.println();
111 cnt++;
112 }
113
114 return cnt;
115 }
116
117 private void writeHeader(PrintWriter writer)
118 {
119 writer.println( "time,type,step,message");
120 }
121 }