1 19 package org.netbeans.api.java.source.test.support; 20 21 import java.lang.ref.Reference ; 22 import java.lang.ref.WeakReference ; 23 import java.lang.reflect.Field ; 24 import java.lang.reflect.Method ; 25 import java.util.Collection ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import junit.framework.Test; 30 import junit.framework.TestCase; 31 import junit.framework.TestSuite; 32 import org.netbeans.api.java.source.JavaSource; 33 import org.netbeans.junit.NbTestCase; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.FileUtil; 37 import org.openide.loaders.DataObject; 38 39 43 public class MemoryValidator extends NbTestCase { 44 45 private static final boolean ENABLED = Boolean.getBoolean("org.netbeans.api.java.source.test.support.MemoryValidator.enable"); 46 47 private TestCase delegate; 48 49 50 public MemoryValidator(TestCase delegate) { 51 super(delegate.getName()); 52 53 this.delegate = delegate; 54 } 55 56 public static Test wrap(Test t) { 57 if (t instanceof TestCase) { 58 return wrap((TestCase) t); 59 } 60 if (t instanceof TestSuite) { 61 return wrap((TestSuite) t); 62 } 63 64 throw new IllegalArgumentException ("Unknown type to wrap"); 65 } 66 67 public static TestCase wrap(TestCase t) { 68 return new MemoryValidator(t); 69 } 70 71 public static TestSuite wrap(TestSuite t) { 72 TestSuite result = new TestSuite(); 73 74 for (int cntr = 0; cntr < t.testCount(); cntr++) { 75 result.addTest(wrap(t.testAt(cntr))); 76 } 77 78 return result; 79 } 80 81 protected @Override void runTest() throws Throwable { 82 delegate.runBare(); 83 84 if (ENABLED) { 85 long start = System.currentTimeMillis(); 88 long end = -1; 89 90 try { 91 Collection <FileObject> allFileObjects = null; 92 93 try { 94 Class poolClass = Class.forName("org.openide.loaders.DataObjectPool"); 95 Method getPOOL = poolClass.getDeclaredMethod("getPOOL", new Class [0]); 96 getPOOL.setAccessible(true); 97 Object pool = getPOOL.invoke(null, new Object [0]); 98 Field m = poolClass.getDeclaredField("map"); 99 m.setAccessible(true); 100 101 Map <FileObject, Object > map = (Map ) m.get(pool); 102 103 allFileObjects = new HashSet (map.keySet()); 104 } catch (ThreadDeath t) { 105 throw t; 106 } catch (Throwable t) { 107 ErrorManager.getDefault().notify(t); 108 } 109 110 if (allFileObjects != null) { 111 for (Iterator <FileObject> i = allFileObjects.iterator(); i.hasNext(); ){ 112 FileObject file = i.next(); 113 114 i.remove(); 115 116 String name = FileUtil.getFileDisplayName(file); 117 DataObject d = DataObject.find(file); 118 JavaSource s = JavaSource.forFileObject(d.getPrimaryFile()); 119 120 if (s != null) { 121 Reference rD = new WeakReference (d); 122 Reference sD = new WeakReference (s); 123 124 file = null; 125 d = null; 126 s = null; 127 128 NbTestCase.assertGC(name, rD); 129 NbTestCase.assertGC(name, sD); 130 } 131 } 132 } 133 134 end = System.currentTimeMillis(); 135 } finally { 136 if (end != (-1)) { 137 log(getName() + ": reference check took: " + (end - start)); 138 } else { 139 log(getName() + ": reference check failed"); 140 } 141 } 142 } 143 } 144 145 } 146 | Popular Tags |