KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > source > test > support > MemoryValidator


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 package org.netbeans.api.java.source.test.support;
20
21 import java.lang.ref.Reference JavaDoc;
22 import java.lang.ref.WeakReference JavaDoc;
23 import java.lang.reflect.Field JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
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 /**
40  *
41  * @author Jan Lahoda
42  */

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     /** Creates a new instance of MemoryValidator */
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 JavaDoc("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 JavaDoc void runTest() throws Throwable JavaDoc {
82         delegate.runBare();
83         
84         if (ENABLED) {
85             //if the tests passes, check if all the DataObjects created during the test are reclaimable.
86
//the same for all corresponding JavaSources.
87
long start = System.currentTimeMillis();
88             long end = -1;
89             
90             try {
91                 Collection JavaDoc<FileObject> allFileObjects = null;
92                 
93                 try {
94                     Class JavaDoc poolClass = Class.forName("org.openide.loaders.DataObjectPool");
95                     Method JavaDoc getPOOL = poolClass.getDeclaredMethod("getPOOL", new Class JavaDoc[0]);
96                     getPOOL.setAccessible(true);
97                     Object JavaDoc pool = getPOOL.invoke(null, new Object JavaDoc[0]);
98                     Field JavaDoc m = poolClass.getDeclaredField("map");
99                     m.setAccessible(true);
100                     
101                     Map JavaDoc<FileObject, Object JavaDoc> map = (Map JavaDoc) m.get(pool);
102                     
103                     allFileObjects = new HashSet JavaDoc(map.keySet());
104                 } catch (ThreadDeath JavaDoc t) {
105                     throw t;
106                 } catch (Throwable JavaDoc t) {
107                     ErrorManager.getDefault().notify(t);
108                 }
109                 
110                 if (allFileObjects != null) {
111                     for (Iterator JavaDoc<FileObject> i = allFileObjects.iterator(); i.hasNext(); ){
112                         FileObject file = i.next();
113                         
114                         i.remove();
115                         
116                         String JavaDoc name = FileUtil.getFileDisplayName(file);
117                         DataObject d = DataObject.find(file);
118                         JavaSource s = JavaSource.forFileObject(d.getPrimaryFile());
119                         
120                         if (s != null) {
121                             Reference JavaDoc rD = new WeakReference JavaDoc(d);
122                             Reference JavaDoc sD = new WeakReference JavaDoc(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