KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > PairMapTest


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.core.output2;
21
22 import java.lang.ref.WeakReference JavaDoc;
23 import junit.framework.TestCase;
24
25 /**
26  *
27  * @author tim
28  */

29 public class PairMapTest extends TestCase {
30
31     public PairMapTest(String JavaDoc testName) {
32         super(testName);
33     }
34
35     private PairMap map;
36     protected void setUp() throws Exception JavaDoc {
37         map = new PairMap();
38     }
39
40     /**
41      * Test of size method, of class org.netbeans.core.output2.PairMap.
42      */

43     public void testSize() {
44         System.out.println("testSize");
45
46         for (int i=0; i < 10; i++) {
47             String JavaDoc 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     /**
57      * Test of isEmpty method, of class org.netbeans.core.output2.PairMap.
58      */

59     public void testIsEmpty() {
60         assertTrue ("Map should be initially empty", map.isEmpty());
61         
62         String JavaDoc[] s = new String JavaDoc[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     /**
87      * Test of setWeak method, of class org.netbeans.core.output2.PairMap.
88      */

89     public void testSetWeak() {
90         System.out.println("testSetWeak");
91         
92         String JavaDoc[] s = new String JavaDoc[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 JavaDoc[] 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 JavaDoc);
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