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