View Javadoc

    1    /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    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.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  		// always run at least once
    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  								String stepName = entry.getTargetStepName();
    97
    98  								Integer errorCount = errorCounts.get(stepName);
    99  								if (errorCount == null)
    100 									errorCount = 1;
    101 								else
    102 									errorCount = errorCount + 1;
    103
    104 								errorCounts.put(stepName, errorCount);
    105 							}
    106
    107 							cnt++;
    108 						}
    109 					}
    110
    111 					if (cnt > 0)
    112 						fireIntervalAdded(this, entries.size() - cnt, entries.size() - 1);
    113 				}
    114
    115 				// break if load test is not running
    116 				if( !loadTest.isRunning() )
    117 					break;
    118
    119 				Thread.sleep( 200 );
    120 			}
    121 			catch (Exception e)
    122 			{
    123 				SoapUI.logError( e );
    124 			}
    125 		}
    126
    127 		modelThread = null;
    128 	}
    129
    130 	public void clear()
    131 	{
    132 		entriesStack.clear();
    133
    134 		if( !entries.isEmpty() )
    135 		{
    136 			int size = entries.size();
    137 			entries.clear();
    138 			fireIntervalRemoved( this, 0, size-1 );
    139 			totalErrorCount = 0;
    140 			errorCounts.clear();
    141 		}
    142 	}
    143
    144 	public void clearErrors()
    145 	{
    146 		int sz = entries.size();
    147
    148 		for( int c = 0; c < entries.size(); c++ )
    149 		{
    150 			if( entries.get( c ).isError() )
    151 			{
    152 				entries.remove( c );
    153 				c--;
    154 			}
    155 		}
    156
    157 		totalErrorCount = 0;
    158 		errorCounts.clear();
    159
    160 		if( sz > entries.size() )
    161 		{
    162 			fireIntervalRemoved( this, entries.size(), sz );
    163 			fireContentsChanged( this, 0, entries.size() );
    164 		}
    165 	}
    166
    167 	public void clearEntries( TestStep testStep )
    168 	{
    169 		int sz = entries.size();
    170
    171 		String testStepName = testStep.getName();
    172 		for( int c = 0; c < entries.size(); c++ )
    173 		{
    174 			if( testStepName.equals( entries.get( c ).getTargetStepName() ) )
    175 			{
    176 				entries.remove( c );
    177 				c--;
    178 			}
    179 		}
    180
    181 		if( errorCounts.containsKey( testStepName ))
    182 		{
    183 			totalErrorCount -= errorCounts.get( testStepName ).intValue();
    184 			errorCounts.remove( testStepName );
    185 		}
    186
    187 		if( sz > entries.size() )
    188 		{
    189 			fireIntervalRemoved( this, entries.size(), sz );
    190 			fireContentsChanged( this, 0, entries.size() );
    191 		}
    192 	}
    193
    194 	public WsdlLoadTest getLoadTest()
    195 	{
    196 		return loadTest;
    197 	}
    198
    199 	public int getErrorCount( String stepName )
    200 	{
    201 		if( stepName == null )
    202 			return totalErrorCount;
    203
    204 		Integer counts = errorCounts.get( stepName );
    205 		return counts == null ? 0 : counts;
    206 	}
    207
    208 	private final class InternalTestSuiteListener extends TestSuiteListenerAdapter
    209 	{
    210 		public void testStepRemoved(TestStep testStep, int index)
    211 		{
    212 			if( testStep.getTestCase() == loadTest.getTestCase() )
    213 			{
    214 				clearEntries( testStep );
    215 			}
    216 		}
    217 	}
    218 }