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