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;
014:
015: import java.util.ArrayList;
016: import java.util.Date;
017: import java.util.List;
018:
019: import javax.swing.table.AbstractTableModel;
020:
021: import org.apache.log4j.Logger;
022:
023: import com.eviware.soapui.model.support.LoadTestRunListenerAdapter;
024: import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
025: import com.eviware.soapui.model.testsuite.LoadTest;
026: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
027: import com.eviware.soapui.model.testsuite.LoadTestRunner;
028: import com.eviware.soapui.model.testsuite.TestRunContext;
029: import com.eviware.soapui.model.testsuite.TestRunner;
030: import com.eviware.soapui.model.testsuite.TestStep;
031: import com.eviware.soapui.model.testsuite.TestStepResult;
032:
033: /**
034: * Collector of samples from a loadtest, exposed as TableModel
035: *
036: * @author Ole.Matzura
037: */
038:
039: public class LoadTestSamples extends AbstractTableModel {
040: private final LoadTest loadTest;
041: private List<List<LoadTestStepSample[]>> samples = new ArrayList<List<LoadTestStepSample[]>>();
042: private List<Long> timestamps = new ArrayList<Long>();
043: private InternalLoadTestRunListener loadTestRunListener = new InternalLoadTestRunListener();
044: private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
045: private final static Logger log = Logger
046: .getLogger(LoadTestSamples.class);
047:
048: public LoadTestSamples(LoadTest loadTest) {
049: this .loadTest = loadTest;
050:
051: loadTest.addLoadTestRunListener(loadTestRunListener);
052: loadTest.getTestCase().getTestSuite().addTestSuiteListener(
053: testSuiteListener);
054: }
055:
056: public int getRowCount() {
057: return samples.size();
058: }
059:
060: public void release() {
061: loadTest.removeLoadTestRunListener(loadTestRunListener);
062: loadTest.getTestCase().getTestSuite().removeTestSuiteListener(
063: testSuiteListener);
064: }
065:
066: public int getColumnCount() {
067: return loadTest.getTestCase().getTestStepCount() + 1;
068: }
069:
070: public Object getValueAt(int rowIndex, int columnIndex) {
071: return columnIndex == 0 ? new Date(timestamps.get(rowIndex))
072: : samples.get(rowIndex).get(columnIndex - 1);
073: }
074:
075: public Class<?> getColumnClass(int columnIndex) {
076: return columnIndex == 0 ? Date.class
077: : LoadTestStepSample[].class;
078: }
079:
080: public String getColumnName(int columnIndex) {
081: return columnIndex == 0 ? "Timestamp" : loadTest.getTestCase()
082: .getTestStepAt(columnIndex - 1).getName();
083: }
084:
085: public synchronized void clear() {
086: int size = samples.size();
087: samples.clear();
088: timestamps.clear();
089: fireTableRowsDeleted(0, size - 1);
090: }
091:
092: private final class InternalTestSuiteListener extends
093: TestSuiteListenerAdapter {
094: public void loadTestRemoved(LoadTest loadTest) {
095: if (loadTest.equals(LoadTestSamples.this .loadTest)) {
096: loadTest.removeLoadTestRunListener(loadTestRunListener);
097: }
098: }
099:
100: public void testStepAdded(TestStep testStep, int index) {
101: if (testStep.getTestCase() == loadTest.getTestCase()) {
102: for (List<LoadTestStepSample[]> values : samples) {
103: values.add(index, new LoadTestStepSample[0]);
104: }
105: }
106: }
107:
108: public void testStepMoved(TestStep testStep, int fromIndex,
109: int offset) {
110: if (testStep.getTestCase() == loadTest.getTestCase()) {
111: for (List<LoadTestStepSample[]> values : samples) {
112: LoadTestStepSample[] s = values.remove(fromIndex);
113: values.add(offset, s);
114: }
115: }
116: }
117:
118: public void testStepRemoved(TestStep testStep, int index) {
119: if (testStep.getTestCase() == loadTest.getTestCase()) {
120: for (List<LoadTestStepSample[]> values : samples) {
121: values.remove(index);
122: }
123: }
124: }
125: }
126:
127: private class InternalLoadTestRunListener extends
128: LoadTestRunListenerAdapter {
129: public void afterTestCase(LoadTestRunner loadTestRunner,
130: LoadTestRunContext context, TestRunner testRunner,
131: TestRunContext runContext) {
132: long timestamp = System.currentTimeMillis();
133: List<LoadTestStepSample[]> s = new ArrayList<LoadTestStepSample[]>();
134: List<TestStepResult> testResults = testRunner.getResults();
135:
136: for (int c = 0; c < loadTest.getTestCase()
137: .getTestStepCount(); c++) {
138: TestStep testStep = loadTest.getTestCase()
139: .getTestStepAt(c);
140: List<LoadTestStepSample> results = new ArrayList<LoadTestStepSample>();
141:
142: for (int i = 0; i < testResults.size(); i++) {
143: TestStepResult stepResult = testResults.get(i);
144: if (stepResult == null) {
145: log.warn("Result [" + c
146: + "] is null in TestCase ["
147: + testRunner.getTestCase().getName()
148: + "]");
149: continue;
150: }
151:
152: if (stepResult.getTestStep().equals(testStep))
153: results.add(new LoadTestStepSample(stepResult));
154: }
155:
156: s.add(results.toArray(new LoadTestStepSample[results
157: .size()]));
158: }
159:
160: synchronized (this ) {
161: samples.add(s);
162: timestamps.add(timestamp);
163: fireTableRowsInserted(samples.size() - 1, samples
164: .size() - 1);
165: }
166: }
167: }
168: }
|