1 package com.lexicalscope.fluentreflection.endtoend; 2 3 import static com.lexicalscope.fluentreflection.FluentReflection.type; 4 import static com.lexicalscope.fluentreflection.ReflectionMatchers.hasName; 5 import static org.hamcrest.MatcherAssert.assertThat; 6 import static org.hamcrest.Matchers.equalTo; 7 8 import org.junit.Before; 9 import org.junit.Test; 10 11 public class TestReflectedClassStaticMethods { 12 public static class ClassWithStaticMethods { 13 private static boolean called; 14 private static boolean objectCalled; 15 private static boolean stringCalled; 16 private static boolean stringAndIntegerCalled; 17 18 public static void simpleStaticMethod() { 19 called = true; 20 } 21 22 public static int staticMethodWithReturnValue() { 23 return 42; 24 } 25 26 public static void staticMethodWithOneArgument(final String string) { 27 stringCalled = true; 28 } 29 30 public static void staticMethodWithTwoArguments(final String string, final Integer integer) { 31 stringAndIntegerCalled = true; 32 } 33 34 public static void staticMethodWithOneArgument(final Object object) { 35 objectCalled = true; 36 } 37 } 38 39 @Before 40 public void resetStaticFixture() { 41 ClassWithStaticMethods.called = false; 42 ClassWithStaticMethods.objectCalled = false; 43 ClassWithStaticMethods.stringCalled = false; 44 ClassWithStaticMethods.stringAndIntegerCalled = false; 45 } 46 47 @Test 48 public void callSimpleStaticMethod() { 49 type(ClassWithStaticMethods.class).staticMethod(hasName("simpleStaticMethod")).callRaw(); 50 51 assertThat( 52 ClassWithStaticMethods.called, 53 equalTo(true)); 54 } 55 56 @Test 57 public void callStaticMethodWithReturnValue() { 58 final Integer result = 59 (Integer) type(ClassWithStaticMethods.class) 60 .staticMethod(hasName("staticMethodWithReturnValue")) 61 .callRaw(); 62 63 assertThat(result, equalTo(42)); 64 } 65 66 @Test 67 public void callStaticMethodWithOneArgumentIfMultipleMatches() { 68 type(ClassWithStaticMethods.class).staticMethod(hasName("staticMethodWithOneArgument")).callRaw("string"); 69 assertThat(ClassWithStaticMethods.stringCalled || ClassWithStaticMethods.objectCalled, 70 equalTo(true)); 71 } 72 73 @Test 74 public void callstaticMethodWithTwoArguments() { 75 type(ClassWithStaticMethods.class).staticMethod(hasName("staticMethodWithTwoArguments")).callRaw("string", 42); 76 assertThat(ClassWithStaticMethods.stringAndIntegerCalled, 77 equalTo(true)); 78 } 79 80 @Test 81 public void methodWithTwoArgumentsHasCorrectArgumentCount() { 82 assertThat( 83 type(ClassWithStaticMethods.class).method(hasName("staticMethodWithTwoArguments")).argCount(), 84 equalTo(2)); 85 } 86 }