KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > test > MemoryLeakTest


1 /*
2
3    Copyright 2003-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.test;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 import org.apache.batik.test.AbstractTest;
27 import org.apache.batik.test.DefaultTestReport;
28 import org.apache.batik.test.TestReport;
29
30 import org.apache.batik.util.CleanerThread;
31
32 /**
33  * One line Class Desc
34  *
35  * Complete Class Desc
36  *
37  * @author <a HREF="mailto:deweese@apache.org">l449433</a>
38  * @version $Id: MemoryLeakTest.java,v 1.9 2005/03/27 08:58:37 cam Exp $
39  */

40 public abstract class MemoryLeakTest extends AbstractTest {
41
42     // I know that 120 seems _really_ high but it turns out
43
// That the "GraphicsNodeTree" was not being cleared when I
44
// tested with as high as 60. So I would leave it at 120
45
// (why so large I don't know) - it will bail if the all
46
// the objects of interest are collected sooner so the runtime
47
// is really only a concern for failures.
48
final static int NUM_GC=60;
49
50     final static String JavaDoc ERROR_OBJS_NOT_CLEARED =
51         "MemoryLeakTest.message.error.objs.not.cleared";
52
53     final static String JavaDoc ERROR_DESCRIPTION =
54         "TestReport.entry.key.error.description";
55         
56     public static String JavaDoc fmt(String JavaDoc key, Object JavaDoc []args) {
57         return Messages.formatMessage(key, args);
58     }
59
60     public MemoryLeakTest() {
61     }
62     
63     Map JavaDoc objs = new HashMap JavaDoc();
64     List JavaDoc entries = new ArrayList JavaDoc();
65
66     public void registerObject(Object JavaDoc o) {
67         synchronized (objs) {
68             String JavaDoc desc = o.toString();
69             objs.put(desc, new WeakRef(o, desc));
70         }
71     }
72     public void registerObjectDesc(Object JavaDoc o, String JavaDoc desc) {
73         synchronized (objs) {
74             objs.put(desc, new WeakRef(o, desc));
75         }
76     }
77     
78     public boolean checkObject(String JavaDoc desc) {
79         String JavaDoc [] strs = new String JavaDoc[1];
80         strs[0] = desc;
81         return checkObjects(strs);
82     }
83
84     public boolean checkObjects(String JavaDoc [] descs) {
85         for (int i=0; i<NUM_GC; i++) {
86             System.gc();
87             boolean passed = true;
88             for (int j=0; j< descs.length; j++) {
89                 String JavaDoc desc = descs[j];
90                 WeakRef wr = (WeakRef)objs.get(desc);
91                 if ((wr != null) && (wr.get() != null)) {
92                     passed = false;
93                     break;
94                 }
95             }
96             if (passed) return true;
97             Thread.yield();
98         }
99
100         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
101         synchronized (objs) {
102             boolean passed = true;
103             for (int j=0; j< descs.length; j++) {
104                 String JavaDoc desc = descs[j];
105                 WeakRef wr = (WeakRef)objs.get(desc);
106                 if ((wr == null) || (wr.get() == null)) continue;
107                 if (!passed)
108                     sb.append(","); // Already put one obj out
109
passed = false;
110                 sb.append("'");
111                 sb.append(wr.getDesc());
112                 sb.append("'");
113             }
114         }
115         
116         String JavaDoc objStr = sb.toString();
117         TestReport.Entry entry = new TestReport.Entry
118             (fmt(ERROR_DESCRIPTION, null),
119              fmt(ERROR_OBJS_NOT_CLEARED, new Object JavaDoc[]{objStr}));
120         entries.add(entry);
121
122         if (objStr.length() > 40)
123             objStr = objStr.substring(0,40) + "..." ;
124         System.err.println(">>>>> Objects not cleared: " + objStr);
125         // System.err.println("Waiting for heap dump...");
126
// try { Thread.sleep(60000); } catch (InterruptedException ie) { }
127
return false;
128     }
129
130     public boolean checkObjectsList(List JavaDoc descs) {
131         String JavaDoc [] strs = new String JavaDoc[descs.size()];
132         descs.toArray(strs);
133         return checkObjects(strs);
134     }
135
136     public boolean checkAllObjects() {
137         for (int i=0; i<NUM_GC; i++) {
138             System.gc();
139             synchronized (objs) {
140                 boolean passed = true;
141                 Iterator JavaDoc iter = objs.values().iterator();
142                 while (iter.hasNext()) {
143                     WeakRef wr = (WeakRef)iter.next();
144                     Object JavaDoc o = wr.get();
145                     if (o != null) {
146                         passed = false;
147                         break;
148                     }
149                 }
150                 if (passed) return true;
151             }
152         }
153         
154         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
155         synchronized (objs) {
156             boolean passed = true;
157             Iterator JavaDoc iter = objs.values().iterator();
158             while (iter.hasNext()) {
159                 WeakRef wr = (WeakRef)iter.next();
160                 Object JavaDoc o = wr.get();
161                 if (o == null) continue;
162                 if (!passed)
163                     sb.append(",");
164                 passed = false;
165                 sb.append("'");
166                 sb.append(wr.getDesc());
167                 sb.append("'");
168             }
169             if (passed) return true;
170         }
171         
172         String JavaDoc objStr = sb.toString();
173
174         TestReport.Entry entry = new TestReport.Entry
175             (fmt(ERROR_DESCRIPTION, null),
176              fmt(ERROR_OBJS_NOT_CLEARED, new Object JavaDoc[]{objStr}));
177         entries.add(entry);
178
179         if (objStr.length() > 40)
180             objStr = objStr.substring(0,40) + "..." ;
181         System.err.println(">>>>> Objects not cleared: " + objStr);
182         // System.err.println("Waiting for heap dump...");
183
// try { Thread.sleep(60000); } catch (InterruptedException ie) { }
184
return false;
185     }
186
187     public TestReport runImpl() throws Exception JavaDoc {
188         TestReport ret = doSomething();
189         if ((ret != null) && !ret.hasPassed())
190             return ret;
191
192         checkAllObjects();
193
194         DefaultTestReport report = new DefaultTestReport(this);
195         if (entries.size() == 0) {
196             report.setPassed(true);
197             return report;
198         }
199         report.setErrorCode(ERROR_OBJS_NOT_CLEARED);
200         report.setDescription
201             ((TestReport.Entry[])entries.toArray
202              (new TestReport.Entry[entries.size()]));
203         report.setPassed(false);
204         return report;
205     }
206
207     public abstract TestReport doSomething() throws Exception JavaDoc;
208
209     public class WeakRef extends CleanerThread.WeakReferenceCleared {
210         String JavaDoc desc;
211         public WeakRef(Object JavaDoc o) {
212             super(o);
213             this.desc = o.toString();
214         }
215         public WeakRef(Object JavaDoc o, String JavaDoc desc) {
216             super(o);
217             this.desc = desc;
218         }
219
220         public String JavaDoc getDesc() { return desc; }
221
222         public void cleared() {
223             synchronized (objs) {
224                 objs.remove(desc);
225             }
226         }
227         
228     }
229     
230 }
231
Popular Tags