1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.log;
14
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Stack;
21
22 import javax.swing.AbstractListModel;
23
24 import com.eviware.soapui.SoapUI;
25 import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
26 import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
27 import com.eviware.soapui.model.testsuite.TestStep;
28
29 /***
30 * Log for LoadTest events
31 *
32 * @author Ole.Matzura
33 */
34
35 public class LoadTestLog extends AbstractListModel implements Runnable
36 {
37 private List<LoadTestLogEntry> entries = Collections.synchronizedList( new ArrayList<LoadTestLogEntry>());
38 private final WsdlLoadTest loadTest;
39 private int totalErrorCount;
40 private Map<String,Integer> errorCounts = new HashMap<String,Integer>();
41 private Stack<LoadTestLogEntry> entriesStack = new Stack<LoadTestLogEntry>();
42 private Thread modelThread;
43 private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();
44
45 public LoadTestLog(WsdlLoadTest loadTest)
46 {
47 this.loadTest = loadTest;
48 loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener );
49 }
50
51 public void release()
52 {
53 loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
54 }
55
56 public int getSize()
57 {
58 return entries.size();
59 }
60
61 public Object getElementAt(int index)
62 {
63 return entries.get( index );
64 }
65
66 public synchronized void addEntry( LoadTestLogEntry entry )
67 {
68 entriesStack.push( entry );
69
70 if( modelThread == null )
71 {
72 modelThread = new Thread( this, loadTest.getName() + " LoadTestLog Updater" );
73 modelThread.start();
74 }
75 }
76
77 public void run()
78 {
79
80 while( true )
81 {
82 try
83 {
84 while( !entriesStack.isEmpty() )
85 {
86 int cnt = 0;
87 while (cnt < 10 && !entriesStack.isEmpty())
88 {
89 LoadTestLogEntry entry = entriesStack.pop();
90 if (entry != null)
91 {
92 entries.add(entry);
93 if (entry.isError())
94 {
95 totalErrorCount++;
96 TestStep targetStep = entry.getTargetStep();
97 String stepName = targetStep == null ? "- Total -" : targetStep.getName();
98
99 Integer errorCount = errorCounts.get(stepName);
100 if (errorCount == null)
101 errorCount = 1;
102 else
103 errorCount = errorCount + 1;
104
105 errorCounts.put(stepName, errorCount);
106 }
107
108 cnt++;
109 }
110 }
111
112 if (cnt > 0)
113 fireIntervalAdded(this, entries.size() - cnt, entries.size() - 1);
114 }
115
116
117 if( !loadTest.isRunning() )
118 break;
119
120 Thread.sleep( 200 );
121 }
122 catch (Exception e)
123 {
124 SoapUI.logError( e );
125 }
126 }
127
128 modelThread = null;
129 }
130
131 public void clear()
132 {
133 entriesStack.clear();
134
135 if( !entries.isEmpty() )
136 {
137 int size = entries.size();
138 entries.clear();
139 fireIntervalRemoved( this, 0, size-1 );
140 totalErrorCount = 0;
141 errorCounts.clear();
142 }
143 }
144
145 public void clearErrors()
146 {
147 int sz = entries.size();
148
149 for( int c = 0; c < entries.size(); c++ )
150 {
151 if( entries.get( c ).isError() )
152 {
153 entries.remove( c );
154 c--;
155 }
156 }
157
158 totalErrorCount = 0;
159 errorCounts.clear();
160
161 if( sz > entries.size() )
162 {
163 fireIntervalRemoved( this, entries.size(), sz );
164 fireContentsChanged( this, 0, entries.size() );
165 }
166 }
167
168 public void clearEntries( TestStep testStep )
169 {
170 int sz = entries.size();
171
172 String testStepName = testStep.getName();
173 for( int c = 0; c < entries.size(); c++ )
174 {
175 if( entries.get( c ).getTargetStep() != null && testStepName.equals( entries.get( c ).getTargetStep().getName() ) )
176 {
177 entries.remove( c );
178 c--;
179 }
180 }
181
182 if( errorCounts.containsKey( testStepName ))
183 {
184 totalErrorCount -= errorCounts.get( testStepName ).intValue();
185 errorCounts.remove( testStepName );
186 }
187
188 if( sz > entries.size() )
189 {
190 fireIntervalRemoved( this, entries.size(), sz );
191 fireContentsChanged( this, 0, entries.size() );
192 }
193 }
194
195 public WsdlLoadTest getLoadTest()
196 {
197 return loadTest;
198 }
199
200 public int getErrorCount( String stepName )
201 {
202 if( stepName == null )
203 return totalErrorCount;
204
205 Integer counts = errorCounts.get( stepName );
206 return counts == null ? 0 : counts;
207 }
208
209 private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
210 {
211 public void testStepRemoved(TestStep testStep, int index)
212 {
213 if( testStep.getTestCase() == loadTest.getTestCase() )
214 {
215 clearEntries( testStep );
216 }
217 }
218 }
219 }