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 org.apache.tapestry.ioc.internal.util.CollectionFactory.newMap;
018: import static org.apache.tapestry.ioc.internal.util.InternalUtils.toList;
019:
020: import java.lang.reflect.Method;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.Comparator;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028:
029: import org.apache.tapestry.ioc.Locatable;
030: import org.apache.tapestry.ioc.Location;
031: import org.apache.tapestry.ioc.test.IOCTestCase;
032: import org.testng.annotations.DataProvider;
033: import org.testng.annotations.Test;
034:
035: public class InternalUtilsTest extends IOCTestCase {
036: @Test
037: public void method_as_string_no_args() throws Exception {
038:
039: Method m = Object.class.getMethod("toString");
040:
041: assertEquals(InternalUtils.asString(m),
042: "java.lang.Object.toString()");
043: }
044:
045: @Test
046: public void method_as_string_with_args() throws Exception {
047: Method m = Collections.class.getMethod("sort", List.class,
048: Comparator.class);
049:
050: assertEquals(InternalUtils.asString(m),
051: "java.util.Collections.sort(List, Comparator)");
052: }
053:
054: @Test
055: public void method_as_string_primitive_arg() throws Exception {
056: Method m = Object.class.getMethod("wait", long.class);
057:
058: assertEquals(InternalUtils.asString(m),
059: "java.lang.Object.wait(long)");
060: }
061:
062: @Test
063: public void method_as_string_primitive_array_arg() throws Exception {
064: Method m = Arrays.class.getMethod("sort", int[].class);
065:
066: assertEquals(InternalUtils.asString(m),
067: "java.util.Arrays.sort(int[])");
068: }
069:
070: @Test
071: public void method_as_string_array_arg() throws Exception {
072: Method m = Arrays.class.getMethod("sort", Object[].class);
073:
074: assertEquals(InternalUtils.asString(m),
075: "java.util.Arrays.sort(Object[])");
076: }
077:
078: @Test
079: public void array_size_when_null() {
080: assertEquals(InternalUtils.size(null), 0);
081: }
082:
083: @Test
084: public void array_size_when_non_null() {
085: Object[] array = { 1, 2, 3 };
086:
087: assertEquals(InternalUtils.size(array), 3);
088: }
089:
090: @Test(dataProvider="memberPrefixData")
091: public void strip_member_prefix(String input, String expected) {
092: assertEquals(InternalUtils.stripMemberPrefix(input), expected);
093: }
094:
095: @DataProvider(name="memberPrefixData")
096: public Object[][] memberPrefixData() {
097: return new Object[][] { { "simple", "simple" },
098: { "_name", "name" }, { "$name", "name" },
099: { "$_$__$__$_$___$_$_$_$$name$", "name$" } };
100: }
101:
102: @Test
103: public void enumeration_to_list() {
104: List<String> input = Arrays.asList("wilma", "fred", "barney");
105: Enumeration e = Collections.enumeration(input);
106:
107: List<String> output = toList(e);
108:
109: assertEquals(output, Arrays.asList("barney", "fred", "wilma"));
110: }
111:
112: @Test
113: public void join_empty_list() {
114: List<String> empty = CollectionFactory.newList();
115:
116: assertEquals(InternalUtils.join(empty), "");
117: }
118:
119: @Test
120: public void join_single() {
121: List<String> single = Arrays.asList("barney");
122:
123: assertEquals(InternalUtils.join(single), "barney");
124: }
125:
126: @Test
127: public void join_multiple() {
128: List<String> many = Arrays.asList("fred", "barney", "wilma");
129: assertEquals(InternalUtils.join(many), "fred, barney, wilma");
130: }
131:
132: @Test
133: public void join_sorted() {
134: List<String> unsorted = Arrays.asList("betty", "fred",
135: "barney", "wilma");
136: List<String> copy = CollectionFactory.newList(unsorted);
137:
138: assertEquals(InternalUtils.joinSorted(copy),
139: "barney, betty, fred, wilma");
140:
141: // Make sure that joinSorted() doesn't change the input list
142:
143: assertEquals(copy, unsorted);
144: }
145:
146: @Test(dataProvider="capitalize_inputs")
147: public void capitalize(String input, String expected) {
148: assertEquals(InternalUtils.capitalize(input), expected);
149: }
150:
151: @DataProvider(name="capitalize_inputs")
152: public Object[][] capitalize_inputs() {
153: return new Object[][] { { "hello", "Hello" },
154: { "Goodbye", "Goodbye" }, { "", "" }, { "a", "A" },
155: { "A", "A" } };
156: }
157:
158: @Test
159: public void location_of_not_found() {
160: assertNull(InternalUtils.locationOf(null));
161: assertNull(InternalUtils.locationOf("La! La!"));
162: }
163:
164: @Test
165: public void location_of_location() {
166: Location l = mockLocation();
167:
168: replay();
169:
170: assertSame(l, InternalUtils.locationOf(l));
171:
172: verify();
173: }
174:
175: @Test
176: public void location_of_locatable() {
177: Location l = mockLocation();
178: Locatable locatable = newMock(Locatable.class);
179:
180: expect(locatable.getLocation()).andReturn(l);
181:
182: replay();
183:
184: assertSame(l, InternalUtils.locationOf(locatable));
185:
186: verify();
187: }
188:
189: @Test
190: public void sorted_keys_from_null_map() {
191: List<String> list = InternalUtils.sortedKeys(null);
192:
193: assertTrue(list.isEmpty());
194: }
195:
196: @Test
197: public void sorted_keys_from_map() {
198: Map<String, String> map = newMap();
199:
200: map.put("fred", "flintstone");
201: map.put("barney", "rubble");
202:
203: assertEquals(InternalUtils.sortedKeys(map), Arrays.asList(
204: "barney", "fred"));
205: }
206:
207: @Test
208: public void get_from_null_map() {
209: assertNull(InternalUtils.get(null, null));
210: }
211:
212: @Test
213: public void get_from_map() {
214: Map<String, String> map = newMap();
215:
216: map.put("fred", "flintstone");
217:
218: assertEquals("flintstone", InternalUtils.get(map, "fred"));
219: }
220:
221: @Test
222: public void reverse_iterator() {
223: List<String> list = Arrays.asList("a", "b", "c");
224:
225: Iterator<String> i = InternalUtils.reverseIterator(list);
226:
227: assertTrue(i.hasNext());
228: assertEquals(i.next(), "c");
229:
230: assertTrue(i.hasNext());
231: assertEquals(i.next(), "b");
232:
233: assertTrue(i.hasNext());
234: assertEquals(i.next(), "a");
235:
236: assertFalse(i.hasNext());
237: }
238:
239: @Test
240: public void reverse_iterator_does_not_support_remove() {
241: List<String> list = Arrays.asList("a", "b", "c");
242:
243: Iterator<String> i = InternalUtils.reverseIterator(list);
244:
245: try {
246: i.remove();
247: unreachable();
248: } catch (UnsupportedOperationException ex) {
249:
250: }
251: }
252:
253: @Test
254: public void last_term() {
255: String input = "SimpleInput";
256:
257: assertSame(InternalUtils.lastTerm(input), input);
258:
259: assertEquals(InternalUtils.lastTerm("fie.fie.foe.fum"), "fum");
260: }
261: }
|