1 18 package org.apache.batik.test; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 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 40 public abstract class MemoryLeakTest extends AbstractTest { 41 42 final static int NUM_GC=60; 49 50 final static String ERROR_OBJS_NOT_CLEARED = 51 "MemoryLeakTest.message.error.objs.not.cleared"; 52 53 final static String ERROR_DESCRIPTION = 54 "TestReport.entry.key.error.description"; 55 56 public static String fmt(String key, Object []args) { 57 return Messages.formatMessage(key, args); 58 } 59 60 public MemoryLeakTest() { 61 } 62 63 Map objs = new HashMap (); 64 List entries = new ArrayList (); 65 66 public void registerObject(Object o) { 67 synchronized (objs) { 68 String desc = o.toString(); 69 objs.put(desc, new WeakRef(o, desc)); 70 } 71 } 72 public void registerObjectDesc(Object o, String desc) { 73 synchronized (objs) { 74 objs.put(desc, new WeakRef(o, desc)); 75 } 76 } 77 78 public boolean checkObject(String desc) { 79 String [] strs = new String [1]; 80 strs[0] = desc; 81 return checkObjects(strs); 82 } 83 84 public boolean checkObjects(String [] 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 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 sb = new StringBuffer (); 101 synchronized (objs) { 102 boolean passed = true; 103 for (int j=0; j< descs.length; j++) { 104 String desc = descs[j]; 105 WeakRef wr = (WeakRef)objs.get(desc); 106 if ((wr == null) || (wr.get() == null)) continue; 107 if (!passed) 108 sb.append(","); passed = false; 110 sb.append("'"); 111 sb.append(wr.getDesc()); 112 sb.append("'"); 113 } 114 } 115 116 String objStr = sb.toString(); 117 TestReport.Entry entry = new TestReport.Entry 118 (fmt(ERROR_DESCRIPTION, null), 119 fmt(ERROR_OBJS_NOT_CLEARED, new Object []{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 return false; 128 } 129 130 public boolean checkObjectsList(List descs) { 131 String [] strs = new String [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 iter = objs.values().iterator(); 142 while (iter.hasNext()) { 143 WeakRef wr = (WeakRef)iter.next(); 144 Object o = wr.get(); 145 if (o != null) { 146 passed = false; 147 break; 148 } 149 } 150 if (passed) return true; 151 } 152 } 153 154 StringBuffer sb = new StringBuffer (); 155 synchronized (objs) { 156 boolean passed = true; 157 Iterator iter = objs.values().iterator(); 158 while (iter.hasNext()) { 159 WeakRef wr = (WeakRef)iter.next(); 160 Object 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 objStr = sb.toString(); 173 174 TestReport.Entry entry = new TestReport.Entry 175 (fmt(ERROR_DESCRIPTION, null), 176 fmt(ERROR_OBJS_NOT_CLEARED, new Object []{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 return false; 185 } 186 187 public TestReport runImpl() throws Exception { 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 ; 208 209 public class WeakRef extends CleanerThread.WeakReferenceCleared { 210 String desc; 211 public WeakRef(Object o) { 212 super(o); 213 this.desc = o.toString(); 214 } 215 public WeakRef(Object o, String desc) { 216 super(o); 217 this.desc = desc; 218 } 219 220 public String getDesc() { return desc; } 221 222 public void cleared() { 223 synchronized (objs) { 224 objs.remove(desc); 225 } 226 } 227 228 } 229 230 } 231 | Popular Tags |