BATCH-1742: cover a corner case of overloaded methods in HippyMethodInvoker

This commit is contained in:
Dave Syer
2011-05-04 09:19:39 +01:00
parent 2bfba444dc
commit 5b81bf0361
2 changed files with 116 additions and 86 deletions

View File

@@ -51,6 +51,7 @@ public class HippyMethodInvoker extends MethodInvoker {
Class<?>[] paramTypes = candidate.getParameterTypes();
Object[] candidateArguments = new Object[paramTypes.length];
int assignedParameterCount = 0;
boolean assigned = paramTypes.length==0;
for (int j = 0; j < arguments.length; j++) {
for (int k = 0; k < paramTypes.length; k++) {
// Pick the first assignable of the right type that
@@ -58,11 +59,12 @@ public class HippyMethodInvoker extends MethodInvoker {
if (ClassUtils.isAssignableValue(paramTypes[k], arguments[j]) && candidateArguments[k] == null) {
candidateArguments[k] = arguments[j];
assignedParameterCount++;
assigned = true;
break;
}
}
}
if (paramTypes.length <= argCount) {
if (assigned && paramTypes.length <= argCount) {
int typeDiffWeight = getTypeDifferenceWeight(paramTypes, candidateArguments);
if (typeDiffWeight < minTypeDiffWeight) {
minTypeDiffWeight = typeDiffWeight;

View File

@@ -10,106 +10,134 @@ import org.junit.Test;
public class HippyMethodInvokerTests {
@Test
public void testVanillaMethodInvoker() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("handle");
adapter.setTargetObject(new PlainPojo());
assertEquals("2.0.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testVanillaMethodInvoker() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("handle");
adapter.setTargetObject(new PlainPojo());
assertEquals("2.0.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testEmptyParameters() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("empty");
adapter.setTargetObject(new PlainPojo());
assertEquals(".", adapter.getMessage(2, "foo"));
}
@Test
public void testEmptyParameters() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("empty");
adapter.setTargetObject(new PlainPojo());
assertEquals(".", adapter.getMessage(2, "foo"));
}
@Test
public void testMissingArgument() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("missing");
adapter.setTargetObject(new PlainPojo());
assertEquals("foo.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testEmptyParametersEmptyArgs() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("empty");
adapter.setTargetObject(new PlainPojo());
assertEquals(".", adapter.getMessage());
}
@Test
public void testWrongOrder() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("disorder");
adapter.setTargetObject(new PlainPojo());
assertEquals("2.0.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testMissingArgument() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("missing");
adapter.setTargetObject(new PlainPojo());
assertEquals("foo.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testTwoArgsOfSameTypeWithInexactMatch() throws Exception {
HippyMethodInvoker invoker = new HippyMethodInvoker();
invoker.setTargetMethod("duplicate");
invoker.setTargetObject(new PlainPojo());
invoker.setArguments(new Object[] { "2", "foo" });
invoker.prepare();
assertEquals("foo.2", invoker.invoke());
}
@Test
public void testWrongOrder() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("disorder");
adapter.setTargetObject(new PlainPojo());
assertEquals("2.0.foo", adapter.getMessage(2, "foo"));
}
@Test
public void testOverloadedMethodUsingInputWithoutExactMatch() throws Exception {
HippyMethodInvoker invoker = new HippyMethodInvoker();
invoker.setTargetMethod("foo");
invoker.setTargetObject(new OverloadingPojo());
invoker.setArguments(new Object[] { new TreeSet<Object>() });
invoker.prepare();
assertEquals(invoker.invoke(), Set.class);
}
@Test
public void testTwoArgsOfSameTypeWithInexactMatch() throws Exception {
HippyMethodInvoker invoker = new HippyMethodInvoker();
invoker.setTargetMethod("duplicate");
invoker.setTargetObject(new PlainPojo());
invoker.setArguments(new Object[] { "2", "foo" });
invoker.prepare();
assertEquals("foo.2", invoker.invoke());
}
public static class OverloadingPojo {
public Class<?> foo(List<?> arrayList) {
return List.class;
}
@Test
public void testOverloadedMethodUsingInputWithoutExactMatch() throws Exception {
public Class<?> foo(Set<?> linkedList) {
return Set.class;
}
}
HippyMethodInvoker invoker = new HippyMethodInvoker();
invoker.setTargetMethod("foo");
@SuppressWarnings("unused")
class OverloadingPojo {
public Class<?> foo(List<?> arg) {
return List.class;
}
public Class<?> foo(Set<?> arg) {
return Set.class;
}
}
public static class PlainPojo {
public String handle(double value, String input) {
return value + "." + input;
}
TreeSet<Object> arg = new TreeSet<Object>();
OverloadingPojo target = new OverloadingPojo();
assertEquals(target.foo(arg), Set.class);
public String disorder(String input, double value) {
return value + "." + input;
}
invoker.setTargetObject(target);
invoker.setArguments(new Object[] { arg });
invoker.prepare();
assertEquals(invoker.invoke(), Set.class);
public String duplicate(String input, Object value) {
return value + "." + input;
}
}
public String missing(String input) {
return input + "." + input;
}
public static class PlainPojo {
public String handle(double value, String input) {
return value + "." + input;
}
public String empty() {
return ".";
}
}
public String disorder(String input, double value) {
return value + "." + input;
}
public static interface Service {
String getMessage(double value, String input);
}
public String duplicate(String input, Object value) {
return value + "." + input;
}
public static class TestMethodAdapter extends AbstractMethodInvokingDelegator<String> implements Service {
public String missing(String input) {
return input + "." + input;
}
public String getMessage(double value, String input) {
try {
return invokeDelegateMethodWithArguments(new Object[] { value, input });
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public String empty() {
return ".";
}
}
}
public static interface Service {
String getMessage(double value, String input);
}
public static class TestMethodAdapter extends AbstractMethodInvokingDelegator<String> implements Service {
public String getMessage(double value, String input) {
try {
return invokeDelegateMethodWithArguments(new Object[] { value, input });
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
public String getMessage() {
try {
return invokeDelegateMethodWithArguments(new Object[0]);
}
catch (RuntimeException e) {
throw e;
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
}
}