KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > mimelookup > MimeLookupMemoryTest


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.api.editor.mimelookup;
21
22 import org.netbeans.junit.NbTestCase;
23 import org.netbeans.modules.editor.mimelookup.EditorTestLookup;
24 import org.netbeans.modules.editor.mimelookup.TestUtilities;
25 import org.openide.util.Lookup;
26
27 /**
28  *
29  * @author Martin Roskanin, Vita Stejskal
30  */

31 public class MimeLookupMemoryTest extends NbTestCase {
32
33     public MimeLookupMemoryTest(String JavaDoc testName) {
34         super(testName);
35     }
36
37     protected void setUp() throws java.lang.Exception JavaDoc {
38         // Set up the default lookup, repository, etc.
39
EditorTestLookup.setLookup(
40             new String JavaDoc[] {
41                 "Services/org-netbeans-modules-editor-mimelookup-DummyMimeDataProvider.instance"
42             },
43             getWorkDir(),
44             new Object JavaDoc[] {},
45             getClass().getClassLoader()
46         );
47     }
48     
49     public void testSimple1() {
50         MimePath path = MimePath.get("text/x-java");
51         Lookup lookupA = MimeLookup.getLookup(path);
52         Lookup lookupB = MimeLookup.getLookup(path);
53         assertSame("Lookups are not reused", lookupA, lookupB);
54     }
55
56     public void testSimple2() {
57         MimePath path = MimePath.get("text/x-java");
58         int idA = System.identityHashCode(MimeLookup.getLookup(path));
59         
60         TestUtilities.consumeAllMemory();
61         TestUtilities.gc();
62         
63         int idB = System.identityHashCode(MimeLookup.getLookup(path));
64         assertEquals("Lookup instance was lost", idA, idB);
65     }
66
67     public void testSimple3() {
68         int idA = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
69         
70         TestUtilities.consumeAllMemory();
71         TestUtilities.gc();
72         
73         int idB = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
74         assertEquals("Lookup instance was lost", idA, idB);
75     }
76     
77     public void testLookupsRelease() {
78         int idA = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
79         
80         TestUtilities.consumeAllMemory();
81         TestUtilities.gc();
82         
83         int idB = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
84         assertEquals("Lookup instance was lost", idA, idB);
85         
86         // Force the MimePath instance to be dropped from the list of recently used
87
for (int i = 0; i < MimePath.MAX_LRU_SIZE; i++) {
88             MimePath.get("text/x-nonsense-" + i);
89         }
90         
91         TestUtilities.consumeAllMemory();
92         TestUtilities.gc();
93         
94         int idC = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
95         assertTrue("Lookup instance was not released", idA != idC);
96     }
97
98     public void testLookupResultHoldsTheLookup() {
99         MimePath path = MimePath.get("text/x-java");
100         Lookup lookup = MimeLookup.getLookup(path);
101         Lookup.Result lr = lookup.lookupResult(Object JavaDoc.class);
102         
103         int pathIdA = System.identityHashCode(path);
104         int lookupIdA = System.identityHashCode(lookup);
105         
106         path = null;
107         lookup = null;
108         
109         // Force the MimePath instance to be dropped from the list of recently used
110
for (int i = 0; i < MimePath.MAX_LRU_SIZE; i++) {
111             MimePath.get("text/x-nonsense-" + i);
112         }
113         
114         TestUtilities.consumeAllMemory();
115         TestUtilities.gc();
116
117         int pathIdB = System.identityHashCode(MimePath.get("text/x-java"));
118         int lookupIdB = System.identityHashCode(MimeLookup.getLookup(MimePath.get("text/x-java")));
119         
120         assertEquals("MimePath instance was lost", pathIdA, pathIdB);
121         assertEquals("Lookup instance was lost", lookupIdA, lookupIdB);
122     }
123 }
124
Popular Tags