Skip to content

Commit

Permalink
Added failing test for hippymethodinvoker
Browse files Browse the repository at this point in the history
  • Loading branch information
magott authored and dsyer committed May 4, 2011
1 parent 80889c2 commit 2bfba44
Showing 1 changed file with 106 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,114 @@

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

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 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 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 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 PlainPojo {
public String handle(double value, String input) {
return value+"."+input;
}
public String disorder(String input, double value) {
return value+"."+input;
}
public String duplicate(String input, Object value) {
return value+"."+input;
}
public String missing(String input) {
return input+"."+input;
}
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);
}
}

}

@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 testMissingArgument() throws Exception {
TestMethodAdapter adapter = new TestMethodAdapter();
adapter.setTargetMethod("missing");
adapter.setTargetObject(new PlainPojo());
assertEquals("foo.foo", adapter.getMessage(2, "foo"));
}

@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 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 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);
}

public static class OverloadingPojo {
public Class<?> foo(List<?> arrayList) {
return List.class;
}

public Class<?> foo(Set<?> linkedList) {
return Set.class;
}
}

public static class PlainPojo {
public String handle(double value, String input) {
return value + "." + input;
}

public String disorder(String input, double value) {
return value + "." + input;
}

public String duplicate(String input, Object value) {
return value + "." + input;
}

public String missing(String input) {
return input + "." + input;
}

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);
}
}

}

}

0 comments on commit 2bfba44

Please sign in to comment.