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
59
60 /***
61 * Test the Invoker class
62 *
63 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
64 * @version $Revision: 1.12 $
65 */
66 public class InvokerTest extends GroovyTestCase {
67
68 protected Invoker invoker = new Invoker();
69
70 public void testAsCollectionWithArray() {
71 Object[] array = { "A", "B", "C" };
72 assertAsCollection(array, 3);
73 }
74
75 public void testAsCollectionWithMap() {
76 Map map = new HashMap();
77 map.put("A", "abc");
78 map.put("B", "def");
79 map.put("C", "xyz");
80 assertAsCollection(map, 3);
81 }
82
83 public void testAsCollectionWithList() {
84 List list = new ArrayList();
85 list.add("A");
86 list.add("B");
87 list.add("C");
88 assertAsCollection(list, 3);
89 }
90
91 public void testInvokerException() throws Throwable {
92 try {
93 throw new GroovyRuntimeException("message", new NullPointerException());
94 }
95 catch (GroovyRuntimeException e) {
96
97 assertEquals("message", e.getMessage());
98 assertTrue(e.getCause() instanceof NullPointerException);
99 }
100 }
101
102 public void testAsBoolean() {
103 assertAsBoolean(true, Boolean.TRUE);
104 assertAsBoolean(true, "true");
105 assertAsBoolean(true, "TRUE");
106 assertAsBoolean(true, "false");
107 assertAsBoolean(false, Boolean.FALSE);
108 assertAsBoolean(false, (String) null);
109 assertAsBoolean(true, new Integer(1234));
110 assertAsBoolean(false, new Integer(0));
111 }
112
113 public void testLessThan() {
114 assertTrue(InvokerHelper.compareLessThan(new Integer(1), new Integer(2)));
115 assertTrue(InvokerHelper.compareLessThanEqual(new Integer(2), new Integer(2)));
116 }
117
118 public void testGreaterThan() {
119 assertTrue(InvokerHelper.compareGreaterThan(new Integer(3), new Integer(2)));
120 assertTrue(InvokerHelper.compareGreaterThanEqual(new Integer(2), new Integer(2)));
121 }
122
123 public void testCompareTo() {
124 assertTrue(InvokerHelper.compareEqual("x", new Integer('x')));
125 }
126
127
128
129
130 /***
131 * Asserts the asBoolean method returns the given flag
132 */
133 protected void assertAsBoolean(boolean expected, Object value) {
134 boolean answer = InvokerHelper.asBool(value);
135 assertEquals("value: " + value + " asBoolean()", expected, answer);
136 }
137
138 /***
139 * Asserts that the given object can be converted into a collection and iterator
140 * of the given size
141 */
142 protected void assertAsCollection(Object collectionObject, int count) {
143 Collection collection = invoker.asCollection(collectionObject);
144 assertTrue("Collection is not null", collection != null);
145 assertEquals("Collection size", count, collection.size());
146
147 assertIterator("collections iterator", collection.iterator(), count);
148 assertIterator("invoker.asIterator", invoker.asIterator(collectionObject), count);
149 assertIterator("invoker.asIterator(invoker.asCollection)", invoker.asIterator(collection), count);
150 assertIterator("invoker.asIterator(invoker.asIterator)", invoker.asIterator(invoker.asIterator(collectionObject)), count);
151 }
152
153 /***
154 * Asserts that the iterator is valid and of the right size
155 */
156 protected void assertIterator(String message, Iterator iterator, int count) {
157 for (int i = 0; i < count; i++) {
158 assertTrue(message + ": should have item: " + i, iterator.hasNext());
159 assertTrue(message + ": item: " + i + " should not be null", iterator.next() != null);
160 }
161
162 assertFalse(
163 message + ": should not have item after iterating through: " + count + " items",
164 iterator.hasNext());
165 }
166
167
168 }