001: // Copyright 2006, 2007 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 java.util.Arrays.asList;
018: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
019: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
020: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newSet;
021:
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.HashMap;
025: import java.util.HashSet;
026: import java.util.List;
027: import java.util.Map;
028: import java.util.Set;
029:
030: import org.testng.Assert;
031: import org.testng.annotations.Test;
032:
033: public class CollectionFactoryTest extends Assert {
034:
035: @Test
036: public void new_map() {
037: Map<String, Class> map = newMap();
038:
039: assertTrue(map instanceof HashMap);
040: }
041:
042: @Test
043: public void copy_map() {
044: Map<String, Class> map = newMap();
045:
046: map.put("this", CollectionFactoryTest.class);
047:
048: Map<String, Class> copy = CollectionFactory.newMap(map);
049:
050: assertEquals(copy, map);
051:
052: map.put("other", Map.class);
053:
054: assertFalse(copy.equals(map));
055: }
056:
057: @Test
058: public void new_set() {
059: Set<String> set = newSet();
060:
061: assertTrue(set instanceof HashSet);
062: }
063:
064: @Test
065: public void copy_set() {
066: List<String> start = asList("fred", "barney");
067:
068: Set<String> set = newSet(start);
069:
070: assertEquals(set.size(), 2);
071: assertTrue(set.contains("fred"));
072: assertTrue(set.contains("barney"));
073: }
074:
075: @Test
076: public void set_from_varargs() {
077: Set<String> set = newSet("fred", "barney");
078:
079: assertEquals(set.size(), 2);
080: assertTrue(set.contains("fred"));
081: assertTrue(set.contains("barney"));
082: }
083:
084: @Test
085: public void new_list() {
086: List<String> list = newList();
087:
088: assertTrue(list instanceof ArrayList);
089: }
090:
091: @Test
092: public void new_list_copy() {
093: List<String> start = Arrays.asList("Fred", "Barney", "Wilma");
094: List<String> copy = newList(start);
095:
096: assertNotSame(copy, start);
097: assertEquals(copy, start);
098: }
099:
100: @Test
101: public void new_list_from_elements() {
102: List<String> list = newList("Fred", "Barney");
103:
104: assertEquals(list.size(), 2);
105: assertEquals(list.get(0), "Fred");
106: assertEquals(list.get(1), "Barney");
107: }
108:
109: private static final int THREAD_COUNT = 20;
110:
111: @Test
112: public void new_threadsafe_list() throws Exception {
113: final List<String> threadNames = CollectionFactory
114: .newThreadSafeList();
115:
116: List<Thread> threads = CollectionFactory.newList();
117:
118: Runnable r = new Runnable() {
119: public void run() {
120: String name = Thread.currentThread().getName();
121: threadNames.add(name);
122: }
123: };
124:
125: for (int i = 0; i < THREAD_COUNT; i++) {
126: Thread t = new Thread(r);
127: threads.add(t);
128: }
129:
130: // Start all the threads at the same time.
131:
132: for (Thread t : threads) {
133: t.start();
134: }
135:
136: // Wait for all threads to complete
137:
138: for (Thread t : threads) {
139: t.join();
140: }
141:
142: // Make sure they all executed. If the list was not thread safe, highly unlikely this
143: // would work.
144:
145: assertEquals(threadNames.size(), THREAD_COUNT);
146: }
147: }
|