001: // Copyright 2004, 2005, 2006 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.ioc.internal.util;
016:
017: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
018:
019: import java.util.List;
020:
021: import org.apache.tapestry.ioc.internal.util.IdAllocator;
022: import org.testng.Assert;
023: import org.testng.annotations.Test;
024:
025: public class IdAllocatorTest extends Assert {
026:
027: @Test
028: public void simple() {
029: IdAllocator a = new IdAllocator();
030: List<String> ids = newList();
031:
032: assertFalse(a.isAllocated("name"));
033:
034: assertEquals(a.allocateId("name"), "name");
035: assertTrue(a.isAllocated("name"));
036:
037: ids.add("name");
038:
039: for (int i = 0; i < 10; i++) {
040:
041: String expected = "name_" + i;
042:
043: assertFalse(a.isAllocated(expected));
044:
045: String nextId = a.allocateId("name");
046:
047: assertTrue(a.isAllocated(expected));
048:
049: assertEquals(nextId, expected);
050:
051: ids.add(nextId);
052: }
053:
054: assertEquals(a.getAllocatedIds(), ids);
055: }
056:
057: @Test
058: public void simple_with_namespace() {
059: IdAllocator a = new IdAllocator("_NS");
060:
061: assertEquals(a.allocateId("name"), "name_NS");
062:
063: for (int i = 0; i < 10; i++)
064: assertEquals(a.allocateId("name"), "name_NS_" + i);
065:
066: // This is current behavior, but is probably something
067: // that could be improved.
068:
069: assertEquals(a.allocateId("foo_NS"), "foo_NS_NS");
070: assertEquals(a.allocateId("foo_NS"), "foo_NS_NS_0");
071: }
072:
073: @Test
074: public void degenerate() {
075: IdAllocator a = new IdAllocator();
076:
077: assertEquals(a.allocateId("d_1"), "d_1");
078:
079: assertEquals(a.allocateId("d"), "d");
080: assertEquals(a.allocateId("d"), "d_0");
081: assertEquals(a.allocateId("d"), "d_2");
082:
083: assertEquals(a.allocateId("d"), "d_3");
084: assertEquals(a.allocateId("d_1"), "d_1_0");
085: }
086:
087: @Test
088: public void degenerate_with_namespace() {
089: IdAllocator a = new IdAllocator("_NS");
090:
091: assertEquals(a.allocateId("d_1"), "d_1_NS");
092:
093: assertEquals(a.allocateId("d"), "d_NS");
094: assertEquals(a.allocateId("d"), "d_NS_0");
095: assertEquals(a.allocateId("d"), "d_NS_1");
096: assertEquals(a.allocateId("d"), "d_NS_2");
097: assertEquals(a.allocateId("d"), "d_NS_3");
098:
099: assertEquals(a.allocateId("d_1"), "d_1_NS_0");
100:
101: // This is very degenerate, and maybe something that needs fixing.
102:
103: assertEquals(a.allocateId("d_1_NS"), "d_1_NS_NS");
104: }
105:
106: @Test
107: public void clear() {
108: IdAllocator a = new IdAllocator();
109:
110: assertEquals(a.allocateId("foo"), "foo");
111: assertEquals(a.allocateId("foo_0"), "foo_0");
112:
113: a.clear();
114:
115: assertEquals(a.allocateId("foo"), "foo");
116: assertEquals(a.allocateId("foo_0"), "foo_0");
117: }
118:
119: @Test
120: public void clone_test() {
121: IdAllocator a = new IdAllocator();
122:
123: assertEquals(a.allocateId("foo"), "foo");
124: assertEquals(a.allocateId("foo_0"), "foo_0");
125: assertEquals(a.allocateId("foo"), "foo_1");
126:
127: IdAllocator b = a.clone();
128:
129: // After making a clone, parallel operations should return the same results.
130: // If anything under the covers was shared, then parallel operations would
131: // interfere with each other.
132:
133: assertEquals(b.allocateId("bar"), a.allocateId("bar"));
134: assertEquals(b.allocateId("foo"), a.allocateId("foo"));
135: assertEquals(b.allocateId("foo_0"), a.allocateId("foo_0"));
136:
137: }
138:
139: }
|