001    // Copyright 2004, 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.services.impl;
016    
017    import java.util.ArrayList;
018    import java.util.List;
019    
020    import org.apache.tapestry.BaseComponentTestCase;
021    import org.apache.tapestry.services.ObjectPool;
022    import org.testng.annotations.Test;
023    
024    /**
025     * Tests for {@link org.apache.tapestry.services.impl.ObjectPoolImpl}.
026     * 
027     * @author Howard Lewis Ship
028     * @since 4.0
029     */
030    @Test
031    public class TestObjectPool extends BaseComponentTestCase
032    {
033        public void testStoreAndGet()
034        {
035            String key = "POOLED-KEY";
036            String pooled = "POOLED";
037            ObjectPool p = new ObjectPoolImpl();
038    
039            assertNull(p.get(key));
040    
041            p.store(key, pooled);
042    
043            assertSame(pooled, p.get(key));
044    
045            assertNull(p.get(key));
046        }
047    
048        public void testStoreMany()
049        {
050            ObjectPool p = new ObjectPoolImpl();
051    
052            Object pooled1 = new Object();
053            Object pooled2 = new Object();
054    
055            String key = "POOLED-KEY";
056    
057            p.store(key, pooled1);
058            p.store(key, pooled2);
059    
060            // No guarantee that we'll get them out in the order they were put in.
061    
062            List l = new ArrayList();
063            l.add(pooled1);
064            l.add(pooled2);
065    
066            for (int i = 0; i < 2; i++)
067            {
068                Object pooled = p.get(key);
069    
070                assertTrue(l.remove(pooled));
071            }
072    
073            assertNull(p.get(key));
074        }
075    }