View Javadoc

1    /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.impl.wsdl.loadtest.WsdlLoadTest;
25  import com.eviware.soapui.model.support.TestSuiteListenerAdapter;
26  import com.eviware.soapui.model.testsuite.TestStep;
27  
28  /***
29   * Log for LoadTest events
30   * 
31   * @author Ole.Matzura
32   */
33  
34  public class LoadTestLog extends AbstractListModel implements Runnable
35  {
36  	private List<LoadTestLogEntry> entries = Collections.synchronizedList( new ArrayList<LoadTestLogEntry>());
37  	private final WsdlLoadTest loadTest;
38  	private int totalErrorCount;
39  	private Map<String,Integer> errorCounts = new HashMap<String,Integer>();
40  	private Stack<LoadTestLogEntry> entriesStack = new Stack<LoadTestLogEntry>();
41  	private Thread modelThread;
42  	private InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener();;
43  	
44  	public LoadTestLog(WsdlLoadTest loadTest)
45  	{
46  		this.loadTest = loadTest;
47  		loadTest.getTestCase().getTestSuite().addTestSuiteListener( testSuiteListener );
48  	}
49  	
50  	public void release()
51  	{
52  		loadTest.getTestCase().getTestSuite().removeTestSuiteListener( testSuiteListener );
53  	}
54  
55  	public int getSize()
56  	{
57  		return entries.size();
58  	}
59  
60  	public Object getElementAt(int index)
61  	{
62  		return entries.get( index );
63  	}
64  	
65  	public synchronized void addEntry( LoadTestLogEntry entry )
66  	{
67  		entriesStack.push( entry );
68  		
69  		if( modelThread == null )
70  		{
71  			modelThread = new Thread( this );
72  			modelThread.start();
73  		}
74  	}
75  	
76  	public void run()
77  	{
78  		while( true )
79  		{
80  			try
81  			{
82  				while( !entriesStack.isEmpty() )
83  				{
84  					int cnt = 0;
85  					while (cnt < 10 && !entriesStack.isEmpty())
86  					{
87  						LoadTestLogEntry entry = entriesStack.pop();
88  						if (entry != null)
89  						{
90  							entries.add(entry);
91  							if (entry.isError())
92  							{
93  								totalErrorCount++;
94  								TestStep targetStep = entry.getTargetStep();
95  								String stepName = targetStep == null ? "- Total -" : targetStep.getName();
96  								
97  								Integer errorCount = errorCounts.get(stepName);
98  								if (errorCount == null)
99  									errorCount = 1;
100 								else
101 									errorCount = errorCount + 1;
102 
103 								errorCounts.put(stepName, errorCount);
104 							}
105 
106 							cnt++;
107 						}
108 					}
109 					
110 					if (cnt > 0)
111 						fireIntervalAdded(this, entries.size() - cnt, entries.size() - 1);
112 				}				
113 				Thread.sleep( 200 );
114 			}
115 			catch (Exception e)
116 			{
117 				e.printStackTrace();
118 			}			
119 		}
120 	}
121 		
122 	public void clear()
123 	{
124 		entriesStack.clear();
125 		
126 		if( !entries.isEmpty() )
127 		{
128 			int size = entries.size();
129 			entries.clear();
130 			fireIntervalRemoved( this, 0, size-1 );
131 			totalErrorCount = 0;
132 			errorCounts.clear();
133 		}
134 	}
135 
136 	public void clearErrors()
137 	{
138 		int sz = entries.size();
139 		
140 		for( int c = 0; c < entries.size(); c++ )
141 		{
142 			if( entries.get( c ).isError() )
143 			{
144 				entries.remove( c );
145 				c--;
146 			}
147 		}
148 
149 		totalErrorCount = 0;
150 		errorCounts.clear();
151 		
152 		if( sz > entries.size() )
153 		{
154 			fireIntervalRemoved( this, entries.size(), sz );
155 			fireContentsChanged( this, 0, entries.size() );
156 		}
157 	}
158 	
159 	public void clearEntries( TestStep testStep )
160 	{
161 		int sz = entries.size();
162 		
163 		String testStepName = testStep.getName();
164 		for( int c = 0; c < entries.size(); c++ )
165 		{
166 			if( entries.get( c ).getTargetStep() != null && testStepName.equals( entries.get( c ).getTargetStep().getName() ) )
167 			{
168 				entries.remove( c );
169 				c--;
170 			}
171 		}
172 
173 		if( errorCounts.containsKey( testStepName ))
174 		{
175 			totalErrorCount -= errorCounts.get( testStepName ).intValue();
176 			errorCounts.remove( testStepName );
177 		}
178 		
179 		if( sz > entries.size() )
180 		{
181 			fireIntervalRemoved( this, entries.size(), sz );
182 			fireContentsChanged( this, 0, entries.size() );
183 		}
184 	}
185 
186 	public WsdlLoadTest getLoadTest()
187 	{
188 		return loadTest;
189 	}
190 
191 	public int getErrorCount( String stepName )
192 	{
193 		if( stepName == null )
194 			return totalErrorCount;
195 
196 		Integer counts = errorCounts.get( stepName );
197 		return counts == null ? 0 : counts; 
198 	}
199 	
200 	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
201 	{
202 		public void testStepRemoved(TestStep testStep, int index)
203 		{
204 			if( testStep.getTestCase() == loadTest.getTestCase() )
205 			{
206 				clearEntries( testStep );
207 			}
208 		}
209 	}
210 }