1 19 20 package org.netbeans.core.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.api.registry.Context; 24 import org.netbeans.api.registry.fs.FileSystemContextFactory; 25 import org.netbeans.core.registry.cdconvertor.CD; 26 import org.netbeans.junit.NbTestCase; 27 import org.netbeans.junit.NbTestSuite; 28 import org.netbeans.spi.registry.BasicContext; 29 import org.netbeans.spi.registry.SpiUtils; 30 import org.openide.filesystems.FileObject; 31 import org.openide.filesystems.FileSystem; 32 import org.openide.filesystems.LocalFileSystem; 33 import org.openide.filesystems.XMLFileSystem; 34 35 import java.lang.ref.WeakReference ; 36 import java.net.URL ; 37 import java.util.Collection ; 38 39 public class GCTest extends NbTestCase { 40 41 private Context rootContext; 42 private FileSystem mfs; 43 44 public GCTest(String name) { 45 super (name); 46 } 47 48 public static void main(String [] args) { 49 TestRunner.run(new NbTestSuite(GCTest.class)); 50 } 51 52 protected void setUp () throws Exception { 53 } 54 55 private void init() throws Exception { 56 LocalFileSystem lfs = new LocalFileSystem(); 57 lfs.setRootDirectory(getWorkDir()); 58 59 URL u1 = getClass().getResource("data/layer_gctest.xml"); 60 61 FileSystem xfs1 = new XMLFileSystem( u1 ); 62 mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } ); 63 64 BasicContext rootCtx = FileSystemContextFactory.createContext(mfs.getRoot()); 65 rootContext = SpiUtils.createContext(rootCtx); 66 } 67 68 public void testGCHold() throws Exception { 69 70 init(); 71 Context ctx = rootContext.getSubcontext("gctest"); 72 CD cd = (CD)ctx.getObject("cd", null); 73 assertNotNull(cd); 74 assertEquals(new CD("John Cage", "Early Piano Works"), cd); 75 76 77 WeakReference ref = new WeakReference (cd); 79 System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); 80 assertTrue("Object was garbage collected", ref.get() != null); 81 82 CD cd2 = (CD)ctx.getObject("cd", null); 85 assertTrue("Objects are not the same", cd == cd2 ); 86 87 Context ctx2 = rootContext.getSubcontext("gctest2"); 89 assertNotNull(ctx2); 90 ref = new WeakReference (ctx2); 91 System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); 92 assertTrue("Object was garbage collected", ref.get() != null); 93 94 Context ctx3 = rootContext.getSubcontext("gctest2"); 97 assertEquals("Objects are not equal", ctx2, ctx3 ); 98 } 99 100 public void testGCRelease() throws Exception { 101 102 init(); 103 Context ctx = rootContext.getSubcontext("gctest"); 104 CD cd = (CD)ctx.getObject("cd", null); 105 assertNotNull(cd); 106 assertEquals(new CD("John Cage", "Early Piano Works"), cd); 107 108 109 WeakReference ref = new WeakReference (cd); 111 cd = null; 112 System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); 113 assertTrue("Object was not garbage collected", ref.get() == null); 114 115 CD cd2 = (CD)ctx.getObject("cd", null); 117 assertNotNull(cd2); 118 assertEquals(new CD("John Cage", "Early Piano Works"), cd2); 119 120 Context ctx2 = rootContext.getSubcontext("gctest2"); 122 assertNotNull(ctx2); 123 ref = new WeakReference (ctx2); 124 ctx2 = null; 125 System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); 126 assertTrue("Object was not garbage collected", ref.get() == null); 127 128 Context ctx3 = rootContext.getSubcontext("gctest2"); 130 assertNotNull(ctx3); 131 } 132 133 public void testGCNewBinding() throws Exception { 134 135 init(); 136 Context ctx = rootContext.getSubcontext("gctest"); 137 CD cd = new CD("aaaa", "bbbbb"); 138 ctx.putObject("aaa", cd); 139 WeakReference ref = new WeakReference (cd); 140 cd = null; 141 assertGC("Object was not GCed", ref); 142 } 143 144 public void testGCExternallyRemovedBinding() throws Exception { 145 146 init(); 147 Context ctx = rootContext.getSubcontext("gctest"); 148 CD cd = new CD("aaaa", "bbbbb"); 149 ctx.putObject("aaa", cd); 150 151 FileObject fo = mfs.findResource("gctest/aaa.xml"); 152 fo.delete(); 153 154 CD cd2 = (CD)ctx.getObject("aaa", null); 155 assertTrue("Object was not deleted or removed from cache", cd2 == null); 156 } 157 158 public void testGCofRootCtx() throws Exception { 159 LocalFileSystem lfs = new LocalFileSystem(); 160 lfs.setRootDirectory(getWorkDir()); 161 162 URL u1 = getClass().getResource("data/layer_gctest.xml"); 163 164 FileSystem xfs1 = new XMLFileSystem( u1 ); 165 FileSystem mfs = new TestMFS( new FileSystem[] { lfs, xfs1 } ); 166 167 BasicContext rootCtx = FileSystemContextFactory.createContext(mfs.getRoot()); 168 Context ctx = SpiUtils.createContext(rootCtx); 169 Collection coll = ctx.getBindingNames(); 170 Collection coll2 = ctx.getAttributeNames(null); 171 Collection coll3 = ctx.getSubcontextNames(); 172 173 WeakReference ref1 = new WeakReference (rootCtx); 174 WeakReference ref2 = new WeakReference (ctx); 175 176 rootCtx = null; 177 ctx = null; 178 179 System.gc(); System.gc(); System.gc(); System.gc(); System.gc(); 180 assertTrue("Object was not garbage collected", ref1.get() == null); 181 assertTrue("Object was not garbage collected", ref2.get() == null); 182 183 } 184 185 } 186 | Popular Tags |