1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 package org.codehaus.groovy.runtime;
48
49 import groovy.lang.GroovyRuntimeException;
50 import groovy.util.GroovyTestCase;
51
52 import java.util.ArrayList;
53 import java.util.Collection;
54 import java.util.HashMap;
55 import java.util.Iterator;
56 import java.util.List;
57 import java.util.Map;
58 import java.util.Collections;
59 import java.util.Arrays;
60
61
62 /***
63 * Test the Invoker class
64 *
65 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
66 * @version $Revision: 1.13 $
67 */
68 public class InvokerTest extends GroovyTestCase {
69
70 protected Invoker invoker = new Invoker();
71
72 public void testAsCollectionWithArray() {
73 Object[] array = { "A", "B", "C" };
74 assertAsCollection(array, 3);
75 }
76
77 public void testAsCollectionWithMap() {
78 Map map = new HashMap();
79 map.put("A", "abc");
80 map.put("B", "def");
81 map.put("C", "xyz");
82 assertAsCollection(map, 3);
83 }
84
85 public void testAsCollectionWithList() {
86 List list = new ArrayList();
87 list.add("A");
88 list.add("B");
89 list.add("C");
90 assertAsCollection(list, 3);
91 }
92
93 public void testInvokerException() throws Throwable {
94 try {
95 throw new GroovyRuntimeException("message", new NullPointerException());
96 }
97 catch (GroovyRuntimeException e) {
98
99 assertEquals("message", e.getMessage());
100 assertTrue(e.getCause() instanceof NullPointerException);
101 }
102 }
103
104 public void testAsBoolean() {
105 assertAsBoolean(true, Boolean.TRUE);
106 assertAsBoolean(true, "true");
107 assertAsBoolean(true, "TRUE");
108 assertAsBoolean(true, "false");
109 assertAsBoolean(false, Boolean.FALSE);
110 assertAsBoolean(false, (String) null);
111 assertAsBoolean(true, new Integer(1234));
112 assertAsBoolean(false, new Integer(0));
113 assertAsBoolean(true, new Float(0.3f));
114 assertAsBoolean(true, new Double(3.0f));
115 assertAsBoolean(false, new Float(0.0f));
116 assertAsBoolean(false, Collections.EMPTY_LIST);
117 assertAsBoolean(true, Arrays.asList(new Integer[] { new Integer(1) }));
118 }
119
120 public void testLessThan() {
121 assertTrue(InvokerHelper.compareLessThan(new Integer(1), new Integer(2)));
122 assertTrue(InvokerHelper.compareLessThanEqual(new Integer(2), new Integer(2)));
123 }
124
125 public void testGreaterThan() {
126 assertTrue(InvokerHelper.compareGreaterThan(new Integer(3), new Integer(2)));
127 assertTrue(InvokerHelper.compareGreaterThanEqual(new Integer(2), new Integer(2)));
128 }
129
130 public void testCompareTo() {
131 assertTrue(InvokerHelper.compareEqual("x", new Integer('x')));
132 }
133
134
135
136
137 /***
138 * Asserts the asBoolean method returns the given flag
139 */
140 protected void assertAsBoolean(boolean expected, Object value) {
141 boolean answer = InvokerHelper.asBool(value);
142 assertEquals("value: " + value + " asBoolean()", expected, answer);
143 }
144
145 /***
146 * Asserts that the given object can be converted into a collection and iterator
147 * of the given size
148 */
149 protected void assertAsCollection(Object collectionObject, int count) {
150 Collection collection = invoker.asCollection(collectionObject);
151 assertTrue("Collection is not null", collection != null);
152 assertEquals("Collection size", count, collection.size());
153
154 assertIterator("collections iterator", collection.iterator(), count);
155 assertIterator("invoker.asIterator", invoker.asIterator(collectionObject), count);
156 assertIterator("invoker.asIterator(invoker.asCollection)", invoker.asIterator(collection), count);
157 assertIterator("invoker.asIterator(invoker.asIterator)", invoker.asIterator(invoker.asIterator(collectionObject)), count);
158 }
159
160 /***
161 * Asserts that the iterator is valid and of the right size
162 */
163 protected void assertIterator(String message, Iterator iterator, int count) {
164 for (int i = 0; i < count; i++) {
165 assertTrue(message + ": should have item: " + i, iterator.hasNext());
166 assertTrue(message + ": item: " + i + " should not be null", iterator.next() != null);
167 }
168
169 assertFalse(
170 message + ": should not have item after iterating through: " + count + " items",
171 iterator.hasNext());
172 }
173
174
175 }