1 19 20 package org.netbeans.core.output2; 21 22 import java.lang.ref.WeakReference ; 23 import junit.framework.TestCase; 24 25 29 public class PairMapTest extends TestCase { 30 31 public PairMapTest(String testName) { 32 super(testName); 33 } 34 35 private PairMap map; 36 protected void setUp() throws Exception { 37 map = new PairMap(); 38 } 39 40 43 public void testSize() { 44 System.out.println("testSize"); 45 46 for (int i=0; i < 10; i++) { 47 String name = "Item " + i; 48 map.add (name, new NbIO(name)); 49 } 50 51 assertTrue ("Map.size returns " + map.size() + " but should return 10", 52 map.size() == 10); 53 54 } 55 56 59 public void testIsEmpty() { 60 assertTrue ("Map should be initially empty", map.isEmpty()); 61 62 String [] s = new String [10]; 63 for (int i=0; i < s.length; i++) { 64 s[i] = "Item " + i; 65 map.add (s[i], new NbIO(s[i])); 66 } 67 68 assertFalse("Map should not be empty, it should contain 10 items", 69 map.isEmpty()); 70 71 for (int i=0; i < s.length; i++) { 72 s[i] = "Item " + i; 73 map.remove(s[i]); 74 } 75 76 assertTrue ("After removing the 10 items added, map should be empty", 77 map.isEmpty()); 78 79 map.setWeak(true); 80 for (int i=0; i < 5; i++) { 81 System.gc(); 82 } 83 assertTrue ("After gc'ing, map should be empty", map.isEmpty()); 84 } 85 86 89 public void testSetWeak() { 90 System.out.println("testSetWeak"); 91 92 String [] s = new String [10]; 93 for (int i=0; i < s.length; i++) { 94 s[i] = "Item " + i; 95 map.add (s[i], new NbIO(s[i])); 96 } 97 98 Object [] vals = map.vals; 99 for (int i=0; i < vals.length; i++) { 100 assertTrue ("Should be null or NbIO, not " + vals[i].getClass() + " at " + i, 101 vals[i] == null || vals[i] instanceof NbIO); 102 } 103 104 map.setWeak(true); 105 for (int i=0; i < vals.length; i++) { 106 assertTrue ("Should be null or NbIO, not " + vals[i].getClass() + " at " + i, 107 vals[i] == null || vals[i] instanceof WeakReference ); 108 } 109 110 map.setWeak(false); 111 for (int i=0; i < vals.length; i++) { 112 assertTrue ("Should be null or NbIO, not " + vals[i].getClass() + " at " + i, 113 vals[i] == null || vals[i] instanceof NbIO); 114 } 115 116 map.clear(); 117 NbIO[] ios = new NbIO[s.length]; 118 for (int i=0; i < s.length; i++) { 119 NbIO io = new NbIO(s[i]); 120 if (i % 2 == 0) { 121 ios[i] = io; 122 } 123 s[i] = "Item " + i; 124 map.add (s[i], io); 125 } 126 127 map.setWeak(true); 128 for (int i=0; i < 5; i++) { 129 System.gc(); 130 } 131 int size = map.size(); 132 assertTrue ("Five object should have been garbage collected", size == 5); 133 ios = null; 134 for (int i=0; i < 5; i++) { 135 System.gc(); 136 } 137 138 size = map.size(); 139 assertTrue ("All object should have been garbage collected, but size is " + size, size == 0); 140 141 } 142 143 } 144 | Popular Tags |