1 46 47 package org.codehaus.groovy.runtime; 48 49 import groovy.lang.GString; 50 import groovy.util.GroovyTestCase; 51 52 58 public class InvokeConstructorTest extends GroovyTestCase { 59 60 protected Invoker invoker = new Invoker(); 61 62 public void testInvokeConstructorNoParams() throws Throwable { 63 assertConstructor(new DummyBean(), new Object [0]); 64 } 65 66 public void testInvokeConstructorOneParam() throws Throwable { 67 assertConstructor(new DummyBean("Bob"), "Bob"); 68 } 69 70 public void testInvokeConstructorOneParamWhichIsNull() throws Throwable { 71 assertConstructor(new DummyBean("Bob", new Integer (1707)), new Object [] { "Bob", new Integer (1707)}); 72 } 73 74 public void testConstructorWithGStringCoercion() throws Throwable { 75 GString gstring = new GString(new Object [] { new Integer (123)}) { 76 public String [] getStrings() { 77 return new String [] { "" }; 78 } 79 }; 80 81 Object expected = new Long (gstring.toString()); 82 83 assertConstructor(expected, new Object [] { gstring }); 84 } 85 86 protected void assertConstructor(Object expected, Object arguments) throws Throwable { 87 Object value = invoke(expected.getClass().getName(), arguments); 88 89 assertEquals("Invoking overloaded method for arguments: " + InvokerHelper.toString(arguments), expected, value); 90 } 91 92 protected Object invoke(String type, Object args) throws Throwable { 93 try { 94 return invoker.invokeConstructor(type, args); 95 } 96 catch (InvokerInvocationException e) { 97 throw e.getCause(); 98 } 99 } 100 } 101 | Popular Tags |