001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.loadtest.data.actions;
014:
015: import java.awt.event.ActionEvent;
016: import java.io.File;
017: import java.io.IOException;
018: import java.io.PrintWriter;
019: import java.text.SimpleDateFormat;
020: import java.util.Date;
021:
022: import javax.swing.AbstractAction;
023: import javax.swing.Action;
024:
025: import com.eviware.soapui.SoapUI;
026: import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLog;
027: import com.eviware.soapui.impl.wsdl.loadtest.log.LoadTestLogEntry;
028: import com.eviware.soapui.model.testsuite.TestStep;
029: import com.eviware.soapui.support.UISupport;
030:
031: /**
032: * Simple loadtest log exporter, creates a comma-separated file containing a header row
033: * and values for each log entry
034: *
035: * @author Ole.Matzura
036: */
037:
038: public class ExportLoadTestLogAction extends AbstractAction {
039: private final LoadTestLog loadTestLog;
040:
041: public ExportLoadTestLogAction(LoadTestLog loadTestLog) {
042: this .loadTestLog = loadTestLog;
043: putValue(Action.SMALL_ICON, UISupport
044: .createImageIcon("/export.gif"));
045: putValue(Action.SHORT_DESCRIPTION,
046: "Export current loadtest log to a file");
047: }
048:
049: public void actionPerformed(ActionEvent e) {
050: try {
051: if (loadTestLog.getSize() == 0) {
052: UISupport.showErrorMessage("No data to export!");
053: return;
054: }
055:
056: File file = UISupport.getFileDialogs().saveAs(this ,
057: "Select file for log export");
058: if (file == null)
059: return;
060:
061: int cnt = exportToFile(file);
062:
063: UISupport.showInfoMessage("Saved " + cnt
064: + " log entries to file [" + file.getName() + "]");
065: } catch (IOException e1) {
066: SoapUI.logError(e1);
067: }
068: }
069:
070: public int exportToFile(File file) throws IOException {
071: PrintWriter writer = new PrintWriter(file);
072: writeHeader(writer);
073: int cnt = writeLog(writer);
074: writer.flush();
075: writer.close();
076: return cnt;
077: }
078:
079: private int writeLog(PrintWriter writer) {
080: SimpleDateFormat sdf = new SimpleDateFormat(
081: "yyyy-MM-dd HH:mm:ss");
082:
083: int c = 0;
084: for (; c < loadTestLog.getSize(); c++) {
085: LoadTestLogEntry logEntry = (LoadTestLogEntry) loadTestLog
086: .getElementAt(c);
087: writer.write(sdf.format(new Date(logEntry.getTimeStamp())));
088: writer.write(',');
089: writer.write(logEntry.getType());
090: writer.write(',');
091:
092: TestStep targetStep = logEntry.getTargetStep();
093: if (targetStep != null)
094: writer.write(targetStep.getName());
095:
096: writer.write(",\"");
097: writer.write(logEntry.getMessage());
098: writer.write('"');
099: writer.println();
100: }
101:
102: return c;
103: }
104:
105: private void writeHeader(PrintWriter writer) {
106: writer.println("time,type,step,message");
107: }
108: }
|