KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > utils > TestIdAllocator


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.junit.utils;
16
17 import org.apache.tapestry.junit.TapestryTestCase;
18 import org.apache.tapestry.util.IdAllocator;
19
20 /**
21  * Tests the {@link org.apache.tapestry.util.IdAllocator}class.
22  *
23  * @author Howard Lewis Ship
24  * @since 3.0
25  */

26
27 public class TestIdAllocator extends TapestryTestCase
28 {
29     public void testSimple()
30     {
31         IdAllocator a = new IdAllocator();
32
33         assertEquals("name", a.allocateId("name"));
34
35         for (int i = 0; i < 10; i++)
36             assertEquals("name$" + i, a.allocateId("name"));
37     }
38
39     public void testSimpleNamespace()
40     {
41         IdAllocator a = new IdAllocator("_NS");
42
43         assertEquals("name_NS", a.allocateId("name"));
44
45         for (int i = 0; i < 10; i++)
46             assertEquals("name_NS$" + i, a.allocateId("name"));
47
48         // This is current behavior, but is probably something
49
// that could be improved.
50

51         assertEquals("foo_NS_NS", a.allocateId("foo_NS"));
52         assertEquals("foo_NS_NS$0", a.allocateId("foo_NS"));
53     }
54
55     public void testDegenerate()
56     {
57         IdAllocator a = new IdAllocator();
58
59         assertEquals("d$1", a.allocateId("d$1"));
60
61         assertEquals("d", a.allocateId("d"));
62         assertEquals("d$0", a.allocateId("d"));
63         assertEquals("d$2", a.allocateId("d"));
64
65         assertEquals("d$3", a.allocateId("d"));
66         assertEquals("d$1$0", a.allocateId("d$1"));
67     }
68
69     public void testDegenerateNamespace()
70     {
71         IdAllocator a = new IdAllocator("_NS");
72
73         assertEquals("d$1_NS", a.allocateId("d$1"));
74
75         assertEquals("d_NS", a.allocateId("d"));
76         assertEquals("d_NS$0", a.allocateId("d"));
77         assertEquals("d_NS$1", a.allocateId("d"));
78         assertEquals("d_NS$2", a.allocateId("d"));
79         assertEquals("d_NS$3", a.allocateId("d"));
80
81         assertEquals("d$1_NS$0", a.allocateId("d$1"));
82
83         // This is very degenerate, and maybe something that needs fixing.
84

85         assertEquals("d$1_NS_NS", a.allocateId("d$1_NS"));
86     }
87
88     public void testClear()
89     {
90         IdAllocator a = new IdAllocator();
91
92         assertEquals("foo", a.allocateId("foo"));
93         assertEquals("foo_0", a.allocateId("foo_0"));
94
95         a.clear();
96
97         assertEquals("foo", a.allocateId("foo"));
98         assertEquals("foo_0", a.allocateId("foo_0"));
99     }
100
101 }
Popular Tags