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.junit.utils;
016    
017    import org.apache.tapestry.junit.TapestryTestCase;
018    import org.apache.tapestry.util.IdAllocator;
019    import org.testng.annotations.Test;
020    
021    /**
022     * Tests the {@link org.apache.tapestry.util.IdAllocator}class.
023     * 
024     * @author Howard Lewis Ship
025     * @since 3.0
026     */
027    @Test
028    public class TestIdAllocator extends TapestryTestCase
029    {
030        public void testSimple()
031        {
032            IdAllocator a = new IdAllocator();
033    
034            assertEquals("name", a.allocateId("name"));
035    
036            for (int i = 0; i < 10; i++)
037                assertEquals("name_" + i, a.allocateId("name"));
038        }
039    
040        public void testSimpleNamespace()
041        {
042            IdAllocator a = new IdAllocator("_NS");
043    
044            assertEquals("name_NS", a.allocateId("name"));
045    
046            for (int i = 0; i < 10; i++)
047                assertEquals("name_NS_" + i, a.allocateId("name"));
048    
049            // This is current behavior, but is probably something
050            // that could be improved.
051    
052            assertEquals("foo_NS_NS", a.allocateId("foo_NS"));
053            assertEquals("foo_NS_NS_0", a.allocateId("foo_NS"));
054        }
055    
056        public void testDegenerate()
057        {
058            IdAllocator a = new IdAllocator();
059    
060            assertEquals("d_1", a.allocateId("d_1"));
061    
062            assertEquals("d", a.allocateId("d"));
063            assertEquals("d_0", a.allocateId("d"));
064            assertEquals("d_2", a.allocateId("d"));
065    
066            assertEquals("d_3", a.allocateId("d"));
067            assertEquals("d_1_0", a.allocateId("d_1"));
068        }
069    
070        public void testDegenerateNamespace()
071        {
072            IdAllocator a = new IdAllocator("_NS");
073    
074            assertEquals("d_1_NS", a.allocateId("d_1"));
075    
076            assertEquals("d_NS", a.allocateId("d"));
077            assertEquals("d_NS_0", a.allocateId("d"));
078            assertEquals("d_NS_1", a.allocateId("d"));
079            assertEquals("d_NS_2", a.allocateId("d"));
080            assertEquals("d_NS_3", a.allocateId("d"));
081    
082            assertEquals("d_1_NS_0", a.allocateId("d_1"));
083    
084            // This is very degenerate, and maybe something that needs fixing.
085    
086            assertEquals("d_1_NS_NS", a.allocateId("d_1_NS"));
087        }
088    
089        public void testClear()
090        {
091            IdAllocator a = new IdAllocator();
092    
093            assertEquals("foo", a.allocateId("foo"));
094            assertEquals("foo_0", a.allocateId("foo_0"));
095    
096            a.clear();
097    
098            assertEquals("foo", a.allocateId("foo"));
099            assertEquals("foo_0", a.allocateId("foo_0"));
100        }
101    
102    }