View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.synchronizedList( new LinkedList<SoftReference<TestStepResult>>());
34  	private int stepCount;
35  	private int maxSize = 0;
36  
37  	public synchronized void addText(String msg)
38  	{
39  		items.add(msg);
40  		results.add(null);
41  		fireIntervalAdded(this, items.size() - 1, items.size()-1 );
42  		
43  		enforceMaxSize();
44  	}
45  
46  	private synchronized void enforceMaxSize()
47  	{
48  		while( items.size() > maxSize )
49  		{
50  			items.remove( 0 );
51  			results.remove( 0 );
52  			fireIntervalRemoved(this, 0, 0 );
53  		}
54  	}
55  
56  	public synchronized void addTestStepResult(TestStepResult result)
57  	{
58  		stepCount++;
59  
60  		int size = items.size();
61  		items.add("Step " + stepCount + " [" + result.getTestStep().getName() + "] " + result.getStatus() + ": took " + result.getTimeTaken() + " ms");
62  		SoftReference<TestStepResult> ref = new SoftReference<TestStepResult>(result);
63  		results.add( ref);
64  		for (String msg : result.getMessages())
65  		{ 
66  			items.add(" -> " + msg);
67  			results.add(ref);
68  		}
69  
70  		fireIntervalAdded(this, size, items.size()-1 );
71  		enforceMaxSize();
72  	}
73  
74  	public synchronized void clear()
75  	{
76  		int sz = items.size();
77  		items.clear();
78  		results.clear();
79  		stepCount = 0;
80  		fireIntervalRemoved(this, 0, sz);
81  	}
82  
83  	public int getMaxSize()
84  	{
85  		return maxSize;
86  	}
87  
88  	public void setMaxSize( int maxSize )
89  	{
90  		this.maxSize = maxSize;
91  		enforceMaxSize();
92  	}
93  
94  	public int getSize()
95  	{
96  		return items.size();
97  	}
98  
99  	public synchronized Object getElementAt(int arg0)
100 	{
101 		try
102 		{
103 			return items.get(arg0);
104 		}
105 		catch (Throwable e)
106 		{
107 			return null;
108 		}
109 	}
110 
111 	public synchronized TestStepResult getResultAt(int index)
112 	{
113 		if( index >= results.size())
114 			return null;
115 		
116 		SoftReference<TestStepResult> result = results.get(index);
117 		return result == null ? null : result.get();
118 	}
119 
120 	public void setStepIndex( int i )
121 	{
122 		stepCount = i;
123 	}
124 }