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.teststeps;
014:
015: import java.awt.event.ActionEvent;
016: import java.io.PrintWriter;
017: import java.util.ArrayList;
018: import java.util.Date;
019: import java.util.List;
020:
021: import javax.swing.AbstractAction;
022: import javax.swing.Action;
023:
024: import com.eviware.soapui.model.testsuite.TestStep;
025: import com.eviware.soapui.model.testsuite.TestStepResult;
026: import com.eviware.soapui.support.UISupport;
027: import com.eviware.soapui.support.action.swing.ActionList;
028: import com.eviware.soapui.support.action.swing.DefaultActionList;
029:
030: /**
031: * Default implementation of TestStepResult interface
032: *
033: * @author Ole.Matzura
034: */
035:
036: public class WsdlTestStepResult implements TestStepResult {
037: private static final String[] EMPTY_MESSAGES = new String[0];
038: private final WsdlTestStep testStep;
039: private List<String> messages = new ArrayList<String>();
040: private Throwable error;
041: private TestStepStatus status = TestStepStatus.UNKNOWN;
042: private long timeTaken;
043: private long timeStamp;
044: private long size;
045: private DefaultActionList actionList;
046: private long startTime;
047: private boolean discarded;
048:
049: private static DefaultActionList discardedActionList = new DefaultActionList(
050: null);
051:
052: static {
053: discardedActionList.setDefaultAction(new AbstractAction() {
054: public void actionPerformed(ActionEvent arg0) {
055: UISupport.showErrorMessage("Result has been discarded");
056: }
057: });
058: }
059:
060: public WsdlTestStepResult(WsdlTestStep testStep) {
061: this .testStep = testStep;
062: timeStamp = System.currentTimeMillis();
063: }
064:
065: public TestStepStatus getStatus() {
066: return status;
067: }
068:
069: public void setStatus(TestStepStatus status) {
070: this .status = status;
071: }
072:
073: public TestStep getTestStep() {
074: return testStep;
075: }
076:
077: public ActionList getActions() {
078: if (isDiscarded())
079: return discardedActionList;
080:
081: if (actionList == null) {
082: actionList = new DefaultActionList(testStep.getName());
083: actionList.setDefaultAction(new AbstractAction() {
084:
085: public void actionPerformed(ActionEvent e) {
086: if (getMessages().length > 0) {
087: StringBuffer buf = new StringBuffer();
088: if (getError() != null)
089: buf.append(getError().toString()).append(
090: "\r\n");
091:
092: for (String s : getMessages())
093: buf.append(s).append("\r\n");
094:
095: UISupport.showExtendedInfo("TestStep Result",
096: "Step [" + testStep.getName()
097: + "] ran with status ["
098: + getStatus() + "]", buf
099: .toString(), null);
100: } else if (getError() != null) {
101: UISupport.showExtendedInfo("TestStep Result",
102: "Step [" + testStep.getName()
103: + "] ran with status ["
104: + getStatus() + "]", getError()
105: .toString(), null);
106: } else {
107: UISupport.showInfoMessage("Step ["
108: + testStep.getName()
109: + "] ran with status [" + getStatus()
110: + "]", "TestStep Result");
111: }
112: }
113: });
114: }
115:
116: return actionList;
117: }
118:
119: public void addAction(Action action, boolean isDefault) {
120: if (isDiscarded())
121: return;
122:
123: if (actionList == null) {
124: actionList = new DefaultActionList(testStep.getName());
125: }
126:
127: actionList.addAction(action);
128: if (isDefault)
129: actionList.setDefaultAction(action);
130: }
131:
132: public Throwable getError() {
133: return error;
134: }
135:
136: public void setError(Throwable error) {
137: this .error = error;
138: }
139:
140: public String[] getMessages() {
141: return messages == null ? EMPTY_MESSAGES : messages
142: .toArray(new String[messages.size()]);
143: }
144:
145: public void addMessage(String message) {
146: if (messages != null)
147: messages.add(message);
148: }
149:
150: public long getTimeTaken() {
151: return timeTaken;
152: }
153:
154: public void setTimeTaken(long timeTaken) {
155: this .timeTaken = timeTaken;
156: }
157:
158: public long getTimeStamp() {
159: return timeStamp;
160: }
161:
162: public void setTimeStamp(long timeStamp) {
163: this .timeStamp = timeStamp;
164: }
165:
166: public void setSize(long size) {
167: this .size = size;
168: }
169:
170: public long getSize() {
171: return size;
172: }
173:
174: public void writeTo(PrintWriter writer) {
175: writer.println("Status: " + getStatus());
176: writer.println("Time Taken: " + getTimeTaken());
177: writer.println("Size: " + getSize());
178: writer.println("Timestamp: "
179: + new Date(getTimeStamp()).toString());
180: writer.println("TestStep: " + getTestStep().getName());
181: if (error != null)
182: writer.println("Error:" + error.toString());
183:
184: if (messages != null)
185: for (String message : messages)
186: if (message != null)
187: writer.println(message);
188:
189: if (isDiscarded())
190: writer.println("Result has been Discarded!");
191: }
192:
193: public void startTimer() {
194: startTime = System.nanoTime();
195: }
196:
197: public void stopTimer() {
198: timeTaken = ((System.nanoTime() - startTime) / 1000000);
199: }
200:
201: public void discard() {
202: discarded = true;
203:
204: messages = null;
205: error = null;
206: actionList = null;
207: }
208:
209: public boolean isDiscarded() {
210: return discarded;
211: }
212: }
|