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    package org.apache.tapestry.util;
015    
016    import org.apache.tapestry.util.io.CompressedDataEncoder;
017    import static org.testng.AssertJUnit.assertEquals;
018    import org.testng.annotations.Test;
019    
020    
021    /**
022     * Tests functionality of {@link IdAllocator}.
023     */
024    @Test
025    public class TestIdAllocator
026    {
027        
028        public void test_Simple_Allocation()
029        {
030            IdAllocator ida = new IdAllocator();
031            
032            assertEquals("name", ida.allocateId("name"));
033            assertEquals("name_0", ida.allocateId("name"));
034            assertEquals("name_1", ida.allocateId("name"));
035        }
036        
037        public void test_Namespace_Allocation()
038        {
039            IdAllocator ida = new IdAllocator("_test");
040            
041            assertEquals("name_test",ida.allocateId("name"));
042            assertEquals("name_test_0", ida.allocateId("name"));
043            assertEquals("name_test_1", ida.allocateId("name"));
044        }
045        
046        public void test_Peek_Allocation()
047        {
048            IdAllocator ida = new IdAllocator();
049            
050            assertEquals("name", ida.allocateId("name"));
051            
052            assertEquals("name_0", ida.peekNextId("name"));
053            assertEquals("name_0", ida.allocateId("name"));
054            
055            assertEquals("name_1", ida.peekNextId("name"));
056            assertEquals("name_1", ida.peekNextId("name"));
057        }
058        
059        public void test_Peek_Allocation_With_PriorId()
060        {
061            IdAllocator ida = new IdAllocator();
062            
063            assertEquals("name", ida.allocateId("name"));
064            
065            assertEquals("name_0", ida.peekNextId("name_0"));
066            assertEquals("name_0", ida.allocateId("name"));
067            
068            assertEquals("name_1", ida.peekNextId("name"));
069            assertEquals("name_1", ida.peekNextId("name"));
070            
071            assertEquals("name_1", ida.peekNextId("name_0"));
072        }
073        
074        public void test_Ignore_Case()
075        {
076            IdAllocator ida = new IdAllocator();
077            
078            assertEquals("name", ida.allocateId("name"));
079            assertEquals("name_0", ida.allocateId("Name"));
080        }
081    
082        public void test_To_External_String()
083        {
084            IdAllocator ida = new IdAllocator();
085    
086            ida.allocateId("ext");
087            
088            assertEquals(",ext$0", ida.toExternalString());
089    
090            ida.allocateId("ext");
091            ida.allocateId("ext2");
092            
093            assertEquals(",ext$1,ext2$0", ida.toExternalString());
094        }
095    
096        public void test_Empty_To_External_String()
097        {
098            IdAllocator ida = new IdAllocator();
099    
100            assertEquals("", ida.toExternalString());
101        }
102    
103        public void test_To_External_String_Namespace()
104        {
105            IdAllocator ida = new IdAllocator("NS");
106    
107            ida.allocateId("ext");
108    
109            assertEquals("NS,extNS$0", ida.toExternalString());
110        }
111    
112        public void test_From_External_String()
113        {
114            String seed = "NS,extNS$3,testNS$2,simpleNS$0";
115    
116            assertEquals(seed, IdAllocator.fromExternalString(seed).toExternalString());
117    
118            seed = ",ext$0";
119            assertEquals(seed, IdAllocator.fromExternalString(seed).toExternalString());
120    
121            seed = "";
122            assertEquals(seed, IdAllocator.fromExternalString(seed).toExternalString());
123        }
124    
125        public void test_From_External_String_State()
126        {
127            String seed = "NS,extNS$3,testNS$2,simpleNS$0,ext_0NS$0";
128            IdAllocator ida = IdAllocator.fromExternalString(seed);
129    
130            assertEquals("NS", ida._namespace);
131            assertEquals(9, ida._generatorMap.size());
132            assertEquals(4, ida._uniqueGenerators.size());
133            assertEquals("extNS_3", ida.allocateId("ext"));
134        }
135    
136        public void test_Compressed_External_String()
137        {
138            String seed = "NS,extNS$3,testNS$2,simpleNS$0,ext_0NS$0";
139    
140            String compressed = CompressedDataEncoder.encodeString(seed);
141            assertEquals(seed, CompressedDataEncoder.decodeString(compressed));
142    
143            IdAllocator ida = IdAllocator.fromExternalString(CompressedDataEncoder.decodeString(compressed));
144    
145            assertEquals("NS", ida._namespace);
146            assertEquals(9, ida._generatorMap.size());
147            assertEquals(4, ida._uniqueGenerators.size());
148            assertEquals("extNS_3", ida.allocateId("ext"));
149        }
150    
151        public void test_Clear()
152        {
153            String seed = "NS,extNS$3,testNS$2,simpleNS$0,ext_0NS$0";
154            IdAllocator ida = IdAllocator.fromExternalString(seed);
155    
156            assertEquals("NS", ida._namespace);
157            assertEquals(9, ida._generatorMap.size());
158            assertEquals(4, ida._uniqueGenerators.size());
159    
160            ida.clear();
161    
162            assertEquals(0, ida._generatorMap.size());
163            assertEquals(0, ida._uniqueGenerators.size());
164        }
165    
166        public void test_To_External_String_Underscores()
167        {
168            IdAllocator ida = IdAllocator.fromExternalString(",service$0,page$0,component$0,container$0,session$0,sp$0,NestedIfBorder$0,Shell$0," +
169                                                             "Body$0,TopComponent$0,testId$0,If_0$0,BorderComponent$0,If$2,NestedComponent$0,RenderBody$0,Form$0");
170    
171            assertEquals("If_0_0", ida.allocateId("If_0"));
172        }
173    }