001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.util;
016    
017    import org.apache.tapestry.BaseComponentTestCase;
018    import org.testng.annotations.Test;
019    
020    /**
021     * Tests for {@link org.apache.tapestry.util.ObjectIdentityMap}
022     * 
023     * @author Howard Lewis Ship
024     * @since 4.0
025     */
026    public class TestObjectIdentityMap extends BaseComponentTestCase
027    {
028        @Test
029        public void testGetNotFound()
030        {
031            ObjectIdentityMap map = new ObjectIdentityMap();
032    
033            map.put("fee", "fie");
034    
035            assertNull(map.get("foe"));
036        }
037        
038        @Test
039        public void testGetFound()
040        {
041            ObjectIdentityMap map = new ObjectIdentityMap();
042    
043            Object value = new Object();
044            Object key = new Object();
045    
046            map.put(key, value);
047    
048            assertSame(value, map.get(key));
049        }
050    
051        @Test
052        public void testGetWhenEmpty()
053        {
054            ObjectIdentityMap map = new ObjectIdentityMap();
055    
056            assertNull(map.get(null));
057        }
058    
059        @Test
060        public void testStoreNull()
061        {
062            ObjectIdentityMap map = new ObjectIdentityMap();
063    
064            Object value = new Object();
065    
066            map.put(null, value);
067    
068            assertSame(value, map.get(null));
069        }
070    
071        @Test
072        public void testStoreMany()
073        {
074            ObjectIdentityMap map = new ObjectIdentityMap();
075    
076            int count = 50;
077    
078            Object[] keys = new Object[count];
079            Object[] values = new Object[count];
080    
081            for (int i = 0; i < count; i++)
082            {
083                keys[i] = new Object();
084                values[i] = new Object();
085    
086                map.put(keys[i], values[i]);
087            }
088    
089            for (int i = 0; i < count; i++)
090            {
091                assertSame(values[i], map.get(keys[i]));
092            }
093        }
094    }