1 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 31 public class MimeLookupMemoryTest extends NbTestCase { 32 33 public MimeLookupMemoryTest(String testName) { 34 super(testName); 35 } 36 37 protected void setUp() throws java.lang.Exception { 38 EditorTestLookup.setLookup( 40 new String [] { 41 "Services/org-netbeans-modules-editor-mimelookup-DummyMimeDataProvider.instance" 42 }, 43 getWorkDir(), 44 new Object [] {}, 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 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 .class); 102 103 int pathIdA = System.identityHashCode(path); 104 int lookupIdA = System.identityHashCode(lookup); 105 106 path = null; 107 lookup = null; 108 109 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 |