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