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.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 = 500;
36  	private boolean discardRemoved;
37  
38  	public 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 void enforceMaxSize()
48  	{
49  		while( items.size() > maxSize )
50  		{
51  			items.remove( 0 );
52  			SoftReference<TestStepResult> result = results.remove( 0 );
53  			if( discardRemoved && result != null && result.get() != null && !result.get().isDiscarded())
54  				result.get().discard();
55  			
56  			fireIntervalAdded(this, 0, 0 );
57  		}
58  	}
59  
60  	public void addTestStepResult(TestStepResult result)
61  	{
62  		stepCount++;
63  
64  		int size = items.size();
65  		items.add("Step " + stepCount + " [" + result.getTestStep().getName() + "] " + result.getStatus() + ": took " + 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 boolean isDiscardRemoved()
99  	{
100 		return discardRemoved;
101 	}
102 
103 	public void setDiscardRemoved( boolean discardRemoved )
104 	{
105 		this.discardRemoved = discardRemoved;
106 	}
107 
108 	public int getSize()
109 	{
110 		return items.size();
111 	}
112 
113 	public Object getElementAt(int arg0)
114 	{
115 		try
116 		{
117 			return items.get(arg0);
118 		}
119 		catch (Throwable e)
120 		{
121 			return null;
122 		}
123 	}
124 
125 	public TestStepResult getResultAt(int index)
126 	{
127 		if( index >= results.size())
128 			return null;
129 		
130 		SoftReference<TestStepResult> result = results.get(index);
131 		return result == null ? null : result.get();
132 	}
133 
134 	public void setStepIndex( int i )
135 	{
136 		stepCount = i;
137 	}
138 }