View Javadoc

1   /*
2    *  jDTAUS Core RI Client Container
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19   *
20   */
21  package org.jdtaus.core.container.ri.client.test;
22  
23  import java.util.ConcurrentModificationException;
24  import java.util.Iterator;
25  import java.util.Map;
26  import junit.framework.Assert;
27  import junit.framework.TestCase;
28  import org.jdtaus.core.container.ri.client.WeakIdentityHashMap;
29  
30  /**
31   * Tests the {@code WeakIdentityHashMap} implementation.
32   *
33   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
34   * @version $JDTAUS: WeakIdentityHashMapTest.java 8641 2012-09-27 06:45:17Z schulte $
35   */
36  public class WeakIdentityHashMapTest extends TestCase
37  {
38      //--Tests-------------------------------------------------------------------
39  
40      public void testObject() throws Exception
41      {
42          final Map m1 = new WeakIdentityHashMap();
43          final Map m2 = new WeakIdentityHashMap();
44  
45          Assert.assertEquals( m1, m2 );
46          Assert.assertEquals( m1.hashCode(), m2.hashCode() );
47  
48          Assert.assertEquals( m1.entrySet(), m2.entrySet() );
49          Assert.assertEquals( m1.entrySet().hashCode(),
50              m2.entrySet().hashCode() );
51  
52          Assert.assertEquals( m1.keySet(), m2.keySet() );
53          Assert.assertEquals( m1.keySet().hashCode(), m2.keySet().hashCode() );
54  
55          System.out.println( m1 );
56          System.out.println( m1.keySet() );
57          System.out.println( m1.entrySet() );
58          System.out.println( m1.values() );
59  
60          System.out.println( m2 );
61          System.out.println( m2.keySet() );
62          System.out.println( m2.entrySet() );
63          System.out.println( m2.values() );
64  
65          final Object o = new Object(); // Must not get garbage collected.
66          m1.put( o, "TEST" );
67  
68          Assert.assertFalse( m1.equals( m2 ) );
69          Assert.assertFalse( m1.hashCode() == m2.hashCode() );
70      }
71  
72      public void testPutGetRemove() throws Exception
73      {
74          final Object o1 = new Object(); // Must not get garbage collected.
75          final Object o2 = new Object(); // Must not get garbage collected.
76          final Map m1 = new WeakIdentityHashMap();
77  
78          Assert.assertTrue( m1.isEmpty() );
79          Assert.assertEquals( m1.size(), 0 );
80          Assert.assertFalse( m1.containsKey( o1 ) );
81          Assert.assertFalse( m1.containsKey( o2 ) );
82          Assert.assertFalse( m1.containsValue( "TEST1" ) );
83          Assert.assertFalse( m1.containsValue( "TEST2" ) );
84  
85          System.out.println( m1 );
86  
87          m1.put( o1, "TEST1" );
88          m1.put( o2, "TEST2" );
89  
90          Assert.assertFalse( m1.isEmpty() );
91          Assert.assertEquals( 2, m1.size() );
92          Assert.assertTrue( m1.containsKey( o1 ) );
93          Assert.assertTrue( m1.containsKey( o2 ) );
94          Assert.assertTrue( m1.containsValue( "TEST1" ) );
95          Assert.assertTrue( m1.containsValue( "TEST2" ) );
96  
97          System.out.println( m1 );
98  
99          m1.clear();
100 
101         Assert.assertTrue( m1.isEmpty() );
102         Assert.assertEquals( m1.size(), 0 );
103         Assert.assertFalse( m1.containsKey( o1 ) );
104         Assert.assertFalse( m1.containsKey( o2 ) );
105         Assert.assertFalse( m1.containsValue( "TEST1" ) );
106         Assert.assertFalse( m1.containsValue( "TEST2" ) );
107 
108         System.out.println( m1 );
109     }
110 
111     public void testConcurrentModificationException() throws Exception
112     {
113         final Object o1 = new Object(); // Must not get garbage collected.
114         final Object o2 = new Object(); // Must not get garbage collected.
115         final Map m1 = new WeakIdentityHashMap();
116 
117         m1.put( o1, "TEST1" );
118         m1.put( o2, "TEST2" );
119 
120         final Iterator keyIterator = m1.keySet().iterator();
121         final Iterator valueIterator = m1.values().iterator();
122         final Iterator entryIterator = m1.entrySet().iterator();
123 
124         m1.clear();
125 
126         try
127         {
128             keyIterator.hasNext();
129             throw new AssertionError();
130         }
131         catch ( ConcurrentModificationException e )
132         {
133             Assert.assertNotNull( e.getMessage() );
134             System.out.println( e.toString() );
135         }
136 
137         try
138         {
139             valueIterator.hasNext();
140             throw new AssertionError();
141         }
142         catch ( ConcurrentModificationException e )
143         {
144             Assert.assertNotNull( e.getMessage() );
145             System.out.println( e.toString() );
146         }
147 
148         try
149         {
150             entryIterator.hasNext();
151             throw new AssertionError();
152         }
153         catch ( ConcurrentModificationException e )
154         {
155             Assert.assertNotNull( e.getMessage() );
156             System.out.println( e.toString() );
157         }
158     }
159 
160     public void testRemoveThroughIterator() throws Exception
161     {
162         final Object o1 = new Object(); // Must not get garbage collected.
163         final Object o2 = new Object(); // Must not get garbage collected.
164         final Map m1 = new WeakIdentityHashMap();
165 
166         m1.put( o1, "TEST1" );
167         m1.put( o2, "TEST2" );
168 
169         Assert.assertFalse( m1.isEmpty() );
170         Assert.assertEquals( m1.size(), 2 );
171         Assert.assertTrue( m1.containsKey( o1 ) );
172         Assert.assertTrue( m1.containsKey( o2 ) );
173         Assert.assertTrue( m1.containsValue( "TEST1" ) );
174         Assert.assertTrue( m1.containsValue( "TEST2" ) );
175 
176         final Iterator keyIterator = m1.keySet().iterator();
177         keyIterator.next();
178         keyIterator.remove();
179         keyIterator.next();
180         keyIterator.remove();
181 
182         Assert.assertFalse( keyIterator.hasNext() );
183         Assert.assertTrue( m1.isEmpty() );
184         Assert.assertEquals( m1.size(), 0 );
185         Assert.assertFalse( m1.containsKey( o1 ) );
186         Assert.assertFalse( m1.containsKey( o2 ) );
187         Assert.assertFalse( m1.containsValue( "TEST1" ) );
188         Assert.assertFalse( m1.containsValue( "TEST2" ) );
189 
190         m1.put( o1, "TEST1" );
191         m1.put( o2, "TEST2" );
192 
193         final Iterator valueIterator = m1.keySet().iterator();
194         valueIterator.next();
195         valueIterator.remove();
196         valueIterator.next();
197         valueIterator.remove();
198 
199         Assert.assertFalse( valueIterator.hasNext() );
200         Assert.assertTrue( m1.isEmpty() );
201         Assert.assertEquals( m1.size(), 0 );
202         Assert.assertFalse( m1.containsKey( o1 ) );
203         Assert.assertFalse( m1.containsKey( o2 ) );
204         Assert.assertFalse( m1.containsValue( "TEST1" ) );
205         Assert.assertFalse( m1.containsValue( "TEST2" ) );
206 
207         m1.put( o1, "TEST1" );
208         m1.put( o2, "TEST2" );
209 
210         final Iterator entryIterator = m1.keySet().iterator();
211         entryIterator.next();
212         entryIterator.remove();
213         entryIterator.next();
214         entryIterator.remove();
215 
216         Assert.assertFalse( entryIterator.hasNext() );
217         Assert.assertTrue( m1.isEmpty() );
218         Assert.assertEquals( m1.size(), 0 );
219         Assert.assertFalse( m1.containsKey( o1 ) );
220         Assert.assertFalse( m1.containsKey( o2 ) );
221         Assert.assertFalse( m1.containsValue( "TEST1" ) );
222         Assert.assertFalse( m1.containsValue( "TEST2" ) );
223     }
224 
225     //-------------------------------------------------------------------Tests--
226 }