KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > common > collections > SoftReferenceUnitTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.test.common.collections;
24
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.ReferenceQueue JavaDoc;
27 import java.lang.ref.SoftReference JavaDoc;
28 import java.math.BigInteger JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Random JavaDoc;
33
34 import junit.framework.TestCase;
35 import org.jboss.util.collection.SoftSet;
36 import org.jboss.util.collection.SoftValueHashMap;
37
38 /**
39  * Tests of the SoftReference based jboss common collection classes
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 44792 $
43  */

44 public class SoftReferenceUnitTest extends TestCase
45 {
46    public SoftReferenceUnitTest(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    /**
52     * Tests of the SoftSet
53     * @throws Exception
54     */

55    public void testSoftValueSet() throws Exception JavaDoc
56    {
57       SoftSet set = new SoftSet();
58       StringBuffer JavaDoc akey = new StringBuffer JavaDoc("Key#");
59       for(int n = 0; n < 1000; n ++)
60       {
61          String JavaDoc key = "Key#"+n;
62          set.add(key);
63          assertTrue("set has key", set.contains(key));
64          akey.setLength(4);
65          akey.append(""+n);
66          String JavaDoc key2 = akey.toString();
67          assertEquals(key, key2);
68          assertEquals(key.hashCode(), key2.hashCode());
69          assertTrue("set has akey", set.contains(key2));
70       }
71       assertEquals("Size == 1000", 1000, set.size());
72       assertEquals("Set.isEmpty is false", false, set.isEmpty());
73       String JavaDoc[] keys = new String JavaDoc[1000];
74       set.toArray(keys);
75       for(int n = 0; n < 1000; n ++)
76       {
77          String JavaDoc key = keys[n];
78          assertTrue("set has key", set.contains(key));
79       }
80
81       HashSet JavaDoc set2 = new HashSet JavaDoc();
82       set2.add("Key#1000");
83       assertFalse("set has not Key#1000", set.contains("Key#1000"));
84       assertTrue("Key#1000 was added", set.addAll(set2));
85       assertEquals("Size == 1001", 1001, set.size());
86       assertTrue("Key#1000 was removed", set.removeAll(set2));
87       assertEquals("Size == 1000", 1000, set.size());
88       set.add("Key#1000");
89       assertTrue("Key#1000 was removed", set.retainAll(set2));
90       assertEquals("Size == 1", 1, set.size());
91       assertTrue("set contains [Key#1000]", set.containsAll(set2));
92       
93       set.clear();
94       assertEquals("Size == 0", 0, set.size());
95       assertEquals("Size is empty", true, set.isEmpty());
96       for(int n = 0; n < 1000; n ++)
97       {
98          String JavaDoc key = keys[n];
99          set.add(key);
100          assertTrue("set has key", set.contains(key));
101       }
102
103       for(int n = 0; n < 1000; n ++)
104       {
105          String JavaDoc key = "Key#"+n;
106          set.remove(key);
107          assertFalse("key was removed", set.contains(key));
108       }
109
110       for(int n = 0; n < 1000; n ++)
111       {
112          String JavaDoc key = "Key#"+n;
113          set.add(key);
114          assertTrue("set has key", set.contains(key));
115       }
116       Iterator JavaDoc iter = set.iterator();
117       while( iter.hasNext() )
118       {
119          Object JavaDoc o = iter.next();
120          assertTrue("o instanceof String", o instanceof String JavaDoc);
121       }
122
123       forceSoftRefCollection();
124       assertEquals("Size == 0 after gc", 0, set.size());
125    }
126
127    /**
128     * Tests of the SoftValueHashMap
129     * @throws Exception
130     */

131    public void testSoftValueHashMap() throws Exception JavaDoc
132    {
133       SoftValueHashMap map = new SoftValueHashMap();
134       for(int n = 0; n < 1000; n ++)
135       {
136          String JavaDoc key = "Key#"+n;
137          String JavaDoc value = "Value#"+n;
138          map.put(key, value);
139       }
140       assertEquals("Size == 1000", 1000, map.size());
141       forceSoftRefCollection();
142       assertEquals("Size == 0 after gc", 0, map.size());
143    }
144
145    private void forceSoftRefCollection()
146    {
147       ReferenceQueue JavaDoc queue = new ReferenceQueue JavaDoc();
148       SoftReference JavaDoc reference = new SoftReference JavaDoc(new Object JavaDoc(), queue);
149
150       ArrayList JavaDoc list = new ArrayList JavaDoc();
151       try
152       {
153          Random JavaDoc rnd = new Random JavaDoc();
154          for(int i = 0; true; i ++)
155          {
156             BigInteger JavaDoc bi = new BigInteger JavaDoc(16384, rnd);
157             list.add(bi);
158             if (i%1000==0)
159             {
160                Reference JavaDoc ref;
161                if ( (ref = queue.poll()) != null)
162                {
163                   System.out.println("Break as the soft reference has been queued: "+ref);
164                   break;
165                }
166             }
167          }
168       }
169       catch (Throwable JavaDoc e)
170       {
171       }
172    }
173 }
174
Popular Tags