1 46 47 package org.codehaus.groovy.runtime; 48 49 import groovy.lang.Closure; 50 import groovy.lang.MetaClass; 51 import groovy.util.GroovyTestCase; 52 53 import java.util.ArrayList ; 54 import java.util.Arrays ; 55 import java.util.List ; 56 57 63 public class InvokeGroovyMethodTest extends GroovyTestCase { 64 65 protected Invoker invoker = new Invoker(); 66 private StringBuffer buffer; 67 68 71 public void testInvokeMethodNoParams() throws Throwable { 72 buffer = new StringBuffer (); 73 74 List list = new ArrayList (); 75 list.add("abc"); 76 list.add("def"); 77 78 invoker.invokeMethod(list, "each", new Closure(this) { 79 protected Object doCall(Object arguments) { 80 buffer.append(arguments.toString()); 81 return null; 82 } 83 84 public MetaClass getMetaClass() { 85 return null; 86 } 87 88 public void setMetaClass(MetaClass metaClass) { 89 } 90 }); 91 92 assertEquals("buffer", "abcdef", buffer.toString()); 93 } 94 95 public void testMatchesWithObject() throws Throwable { 96 assertMatches(new Integer (1), new Integer (1), true); 97 assertMatches(new Integer (1), new Integer (2), false); 98 } 99 100 public void testMatchesWithClass() throws Throwable { 101 assertMatches(new Integer (1), Integer .class, true); 102 assertMatches(new Integer (1), Number .class, true); 103 assertMatches(new Integer (1), Double .class, false); 104 } 105 106 public void testMatchesWithList() throws Throwable { 107 assertMatches(new Integer (1), Arrays.asList(new Object [] { new Integer (2), new Integer (1)}), true); 108 assertMatches(new Integer (1), Arrays.asList(new Object [] { new Integer (2), new Integer (3)}), false); 109 } 110 111 protected void assertMatches(Object switchValue, Object caseValue, boolean expected) { 114 assertEquals( 115 "Switch on: " + switchValue + " Case: " + caseValue, 116 expected, 117 InvokerHelper.isCase(switchValue, caseValue)); 118 } 119 120 } 121 | Popular Tags |