KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > docscan > HeapTest


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.modules.tasklist.docscan;
21
22 import org.netbeans.junit.NbTestCase;
23 import org.netbeans.junit.MemoryFilter;
24 import org.openide.util.actions.SystemAction;
25 import org.openide.filesystems.LocalFileSystem;
26 import org.openide.filesystems.Repository;
27 import org.openide.filesystems.FileSystem;
28
29 import javax.swing.*;
30 import java.net.URL JavaDoc;
31 import java.io.File JavaDoc;
32 import java.lang.reflect.InvocationTargetException JavaDoc;
33 import java.util.Collections JavaDoc;
34
35 /**
36  * Set of tests running GUI and hunting for memoty leaks.
37  * <P>
38  * Depends On: http://xml.netbeans.org/issues/show_bug.cgi?id=40633
39  *
40  * @author Petr Kuzel
41  */

42 public final class HeapTest extends NbTestCase {
43
44     public HeapTest(String JavaDoc s) {
45         super(s);
46     }
47
48
49     /** NB filesytem initialized during setUp. */
50     private FileSystem dataFS;
51     private Filter filter = new Filter();
52
53     protected void setUp () throws Exception JavaDoc {
54         URL JavaDoc url = this.getClass().getResource("data");
55         String JavaDoc resString = NbTestCase.convertNBFSURL(url);
56         LocalFileSystem fs = new LocalFileSystem();
57         fs.setRootDirectory(new File JavaDoc(resString));
58         Repository.getDefault().addFileSystem(fs);
59         dataFS = fs;
60     }
61
62     public void testBug40565() {
63
64         if (SourceTasksAction.class.getClassLoader() != getClass().getClassLoader()) {
65             throw new IllegalStateException JavaDoc("Test must be laoded by tested module classloader");
66         }
67
68         try {
69             SwingUtilities.invokeAndWait( new Runnable JavaDoc() {
70                 public void run() {
71                     // it requires GUI mode
72
SourceTasksAction action = (SourceTasksAction) SystemAction.get(SourceTasksAction.class);
73                     action.performAction();
74
75                 }
76             });
77         } catch (InterruptedException JavaDoc e) {
78
79         } catch (InvocationTargetException JavaDoc e) {
80
81         }
82
83         SourceTasksView view = (SourceTasksView) SourceTasksView.getCurrent();
84
85
86
87         view.selectedFolder = dataFS.findResource("leaks");
88         view.getAllFiles().doClick(1);
89
90         // wait till all threads done (otherwise we get randam numbers)
91
try {
92             Thread.sleep(20000);
93         } catch (InterruptedException JavaDoc e) {
94         }
95         // XXX we still sometimes get random numbers
96

97         view.getRefresh().doClick(1);
98         try {
99             Thread.sleep(2000);
100         } catch (InterruptedException JavaDoc e) {
101         }
102
103         // max estimates sbased on 20040301 build
104
int TL_SIZE = 6650336;
105         //int TABLE_SIZE = 6604320 + 20000; // the table size is very random (I've seen 7308880 (with all submodules), too)
106
// XXX why submodule spresence affects it >500kB
107
int TABLE_SIZE = 7308880 + 40000;
108         int TABLE_LEAK = 400; // tolerated leak
109

110 // int initial_tl_size = assertSize("Tasklist actual size too big", Collections.singleton(view.discloseModel()), TL_SIZE, filter);
111
int initial_table_size = assertSize("Table actual size too big", Collections.singleton(view.discloseTable()), TABLE_SIZE, filter);
112         int initial_node_size = assertSize("Node actual size too big", Collections.singleton(view.discloseNode()), Integer.MAX_VALUE, filter);
113         int initial_view_size = assertSize("View actual size too big", Collections.singleton(view), Integer.MAX_VALUE, filter);
114
115         for (int i = 0 ; i <5; i++) {
116             view.getRefresh().doClick(1);
117             try {
118                 Thread.sleep(2000);
119             } catch (InterruptedException JavaDoc e) {
120             }
121
122 // assertSize("Tasklist after refresh #" + i, Collections.singleton(view.discloseModel()), initial_tl_size, filter); // 0B leak
123
int current_table_size = assertSize("Table after refresh #" + i, Collections.singleton(view.discloseTable()), initial_table_size + TABLE_LEAK, filter); // 20-400B leak
124
// int current_node_size = assertSize("Node after refresh #" + i, Collections.singleton(view.discloseNode()), initial_node_size, filter); // 968b leak
125
//
126
// int knownLeak = (current_table_size - initial_table_size) + (current_node_size - initial_node_size);
127
// assertSize("View after refresh #" + i, Collections.singleton(view), initial_view_size + knownLeak, filter); // 2Kb leak
128
}
129         assertSize("View after refresh test", Collections.singleton(view), initial_view_size, filter); // 2Kb leak
130

131     }
132
133     private class Filter implements MemoryFilter {
134         public boolean reject (Object JavaDoc obj) {
135             return obj instanceof java.lang.ref.Reference JavaDoc;
136         }
137     }
138 }
139
Popular Tags