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