1 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 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 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 137 140 protected void assertAsBoolean(boolean expected, Object value) { 141 boolean answer = InvokerHelper.asBool(value); 142 assertEquals("value: " + value + " asBoolean()", expected, answer); 143 } 144 145 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 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 } 176 | Popular Tags |