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.panels.support;
014:
015: import java.awt.Color;
016:
017: import javax.swing.JProgressBar;
018:
019: import com.eviware.soapui.SoapUI;
020: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
021: import com.eviware.soapui.model.testsuite.LoadTestRunner;
022: import com.eviware.soapui.model.testsuite.TestRunContext;
023: import com.eviware.soapui.model.testsuite.TestRunListener;
024: import com.eviware.soapui.model.testsuite.TestRunner;
025: import com.eviware.soapui.model.testsuite.TestStep;
026: import com.eviware.soapui.model.testsuite.TestStepResult;
027: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
028: import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
029:
030: /**
031: * Class that keeps a JProgressBars state in sync with a TestCase
032: *
033: * @author Ole.Matzura
034: */
035:
036: public class ProgressBarAdapter {
037: private final JProgressBar progressBar;
038: private final WsdlTestCase testCase;
039: private InternalTestRunListener internalTestRunListener;
040: private InternalTestMonitorListener internalTestMonitorListener;
041:
042: public ProgressBarAdapter(JProgressBar progressBar,
043: WsdlTestCase testCase) {
044: this .progressBar = progressBar;
045: this .testCase = testCase;
046:
047: setLoadTestingState();
048:
049: internalTestRunListener = new InternalTestRunListener();
050: testCase.addTestRunListener(internalTestRunListener);
051: internalTestMonitorListener = new InternalTestMonitorListener();
052: SoapUI.getTestMonitor().addTestMonitorListener(
053: internalTestMonitorListener);
054: }
055:
056: public void release() {
057: testCase.removeTestRunListener(internalTestRunListener);
058: SoapUI.getTestMonitor().removeTestMonitorListener(
059: internalTestMonitorListener);
060: }
061:
062: private void setLoadTestingState() {
063: if (SoapUI.getTestMonitor().hasRunningLoadTest(testCase)) {
064: progressBar.setIndeterminate(true);
065: progressBar.setString("loadTesting");
066: } else {
067: progressBar.setIndeterminate(false);
068: progressBar.setString("");
069: }
070: }
071:
072: private class InternalTestMonitorListener extends
073: TestMonitorListenerAdapter {
074: public void loadTestStarted(LoadTestRunner loadTestRunner) {
075: setLoadTestingState();
076: }
077:
078: public void loadTestFinished(LoadTestRunner loadTestRunner) {
079: setLoadTestingState();
080: }
081: }
082:
083: public class InternalTestRunListener implements TestRunListener {
084: public void beforeRun(TestRunner testRunner,
085: TestRunContext runContext) {
086: if (progressBar.isIndeterminate())
087: return;
088:
089: progressBar.getModel().setMaximum(
090: testRunner.getTestCase().getTestStepCount());
091: progressBar.setForeground(Color.GREEN.darker());
092: }
093:
094: public void beforeStep(TestRunner testRunner,
095: TestRunContext runContext) {
096: if (progressBar.isIndeterminate())
097: return;
098:
099: TestStep testStep = runContext.getCurrentStep();
100: progressBar.setString(testStep.getName());
101: progressBar.setValue(runContext.getCurrentStepIndex());
102: }
103:
104: public void afterStep(TestRunner testRunner,
105: TestRunContext runContext, TestStepResult result) {
106: if (progressBar.isIndeterminate())
107: return;
108:
109: if (result.getStatus() == TestStepStatus.FAILED) {
110: progressBar.setForeground(Color.RED);
111: } else if (!testCase.getFailTestCaseOnErrors()) {
112: progressBar.setForeground(Color.GREEN.darker());
113: }
114:
115: progressBar.setValue(runContext.getCurrentStepIndex() + 1);
116: }
117:
118: public void afterRun(TestRunner testRunner,
119: TestRunContext runContext) {
120: if (!testCase.getFailOnError()
121: && !testCase.getFailTestCaseOnErrors()) {
122: progressBar.setForeground(Color.GREEN.darker());
123: }
124:
125: if (progressBar.isIndeterminate())
126: return;
127:
128: progressBar.setString(testRunner.getStatus().toString());
129: }
130: }
131: }
|