KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > GCTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.util.Collection JavaDoc;
38
39 public class GCTest extends NbTestCase {
40     
41     private Context rootContext;
42     private FileSystem mfs;
43     
44     public GCTest(String JavaDoc name) {
45         super (name);
46     }
47     
48     public static void main(String JavaDoc[] args) {
49         TestRunner.run(new NbTestSuite(GCTest.class));
50     }
51     
52     protected void setUp () throws Exception JavaDoc {
53     }
54     
55     private void init() throws Exception JavaDoc {
56         LocalFileSystem lfs = new LocalFileSystem();
57         lfs.setRootDirectory(getWorkDir());
58         
59         URL JavaDoc 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 JavaDoc {
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         // referenced object cannot be garbage collected
78
WeakReference JavaDoc ref = new WeakReference JavaDoc(cd);
79         System.gc(); System.gc(); System.gc(); System.gc(); System.gc();
80         assertTrue("Object was garbage collected", ref.get() != null);
81
82         // retrieving second time existing and referenced object
83
// must return the same instancce
84
CD cd2 = (CD)ctx.getObject("cd", null);
85         assertTrue("Objects are not the same", cd == cd2 );
86         
87         // referenced context cannot be garbage collected
88
Context ctx2 = rootContext.getSubcontext("gctest2");
89         assertNotNull(ctx2);
90         ref = new WeakReference JavaDoc(ctx2);
91         System.gc(); System.gc(); System.gc(); System.gc(); System.gc();
92         assertTrue("Object was garbage collected", ref.get() != null);
93         
94         // retrieving second time existing and referenced context
95
// must return the same instancce
96
Context ctx3 = rootContext.getSubcontext("gctest2");
97         assertEquals("Objects are not equal", ctx2, ctx3 );
98     }
99     
100     public void testGCRelease() throws Exception JavaDoc {
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         // NON-referenced object must be garbage collected
110
WeakReference JavaDoc ref = new WeakReference JavaDoc(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         // test retrieval
116
CD cd2 = (CD)ctx.getObject("cd", null);
117         assertNotNull(cd2);
118         assertEquals(new CD("John Cage", "Early Piano Works"), cd2);
119         
120         // NON-referenced context must be garbage collected
121
Context ctx2 = rootContext.getSubcontext("gctest2");
122         assertNotNull(ctx2);
123         ref = new WeakReference JavaDoc(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         // test retrieval
129
Context ctx3 = rootContext.getSubcontext("gctest2");
130         assertNotNull(ctx3);
131     }
132
133     public void testGCNewBinding() throws Exception JavaDoc {
134         
135         init();
136         Context ctx = rootContext.getSubcontext("gctest");
137         CD cd = new CD("aaaa", "bbbbb");
138         ctx.putObject("aaa", cd);
139         WeakReference JavaDoc ref = new WeakReference JavaDoc(cd);
140         cd = null;
141         assertGC("Object was not GCed", ref);
142     }
143
144     public void testGCExternallyRemovedBinding() throws Exception JavaDoc {
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 JavaDoc {
159         LocalFileSystem lfs = new LocalFileSystem();
160         lfs.setRootDirectory(getWorkDir());
161         
162         URL JavaDoc 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 JavaDoc coll = ctx.getBindingNames();
170         Collection JavaDoc coll2 = ctx.getAttributeNames(null);
171         Collection JavaDoc coll3 = ctx.getSubcontextNames();
172         
173         WeakReference JavaDoc ref1 = new WeakReference JavaDoc(rootCtx);
174         WeakReference JavaDoc ref2 = new WeakReference JavaDoc(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