View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.testcase;
14  
15  import java.lang.ref.SoftReference;
16  import java.util.Collections;
17  import java.util.LinkedList;
18  import java.util.List;
19  
20  import javax.swing.AbstractListModel;
21  
22  import com.eviware.soapui.model.testsuite.TestStepResult;
23  
24  /***
25   * ListModel for the TestCaseLog
26   * 
27   * @author ole.matzura
28   */
29  
30  public class TestCaseLogModel extends AbstractListModel
31  {
32  	private List<Object> items = Collections.synchronizedList( new LinkedList<Object>() );
33  	private List<SoftReference<TestStepResult>> results = Collections
34  			.synchronizedList( new LinkedList<SoftReference<TestStepResult>>() );
35  	private int stepCount;
36  	private int maxSize = 0;
37  
38  	public synchronized void addText( String msg )
39  	{
40  		items.add( msg );
41  		results.add( null );
42  		fireIntervalAdded( this, items.size() - 1, items.size() - 1 );
43  
44  		enforceMaxSize();
45  	}
46  
47  	private synchronized void enforceMaxSize()
48  	{
49  		while( items.size() > maxSize )
50  		{
51  			items.remove( 0 );
52  			results.remove( 0 );
53  			fireIntervalRemoved( this, 0, 0 );
54  		}
55  	}
56  
57  	public synchronized void addTestStepResult( TestStepResult result )
58  	{
59  		stepCount++ ;
60  
61  		int size = items.size();
62  		items.add( "Step " + stepCount + " [" + result.getTestStep().getName() + "] " + result.getStatus() + ": took "
63  				+ result.getTimeTaken() + " ms" );
64  		SoftReference<TestStepResult> ref = new SoftReference<TestStepResult>( result );
65  		results.add( ref );
66  		for( String msg : result.getMessages() )
67  		{
68  			items.add( " -> " + msg );
69  			results.add( ref );
70  		}
71  
72  		fireIntervalAdded( this, size, items.size() - 1 );
73  		enforceMaxSize();
74  	}
75  
76  	public synchronized void clear()
77  	{
78  		int sz = items.size();
79  		items.clear();
80  		results.clear();
81  		stepCount = 0;
82  		fireIntervalRemoved( this, 0, sz );
83  	}
84  
85  	public int getMaxSize()
86  	{
87  		return maxSize;
88  	}
89  
90  	public void setMaxSize( int maxSize )
91  	{
92  		this.maxSize = maxSize;
93  		enforceMaxSize();
94  	}
95  
96  	public int getSize()
97  	{
98  		return items.size();
99  	}
100 
101 	public synchronized Object getElementAt( int arg0 )
102 	{
103 		try
104 		{
105 			return items.get( arg0 );
106 		}
107 		catch( Throwable e )
108 		{
109 			return null;
110 		}
111 	}
112 
113 	public synchronized TestStepResult getResultAt( int index )
114 	{
115 		if( index >= results.size() )
116 			return null;
117 
118 		SoftReference<TestStepResult> result = results.get( index );
119 		return result == null ? null : result.get();
120 	}
121 
122 	public void setStepIndex( int i )
123 	{
124 		stepCount = i;
125 	}
126 }