1 package com.lexicalscope.fluentreflection.endtoend; 2 3 import static com.lexicalscope.fluentreflection.FluentReflection.type; 4 import static org.hamcrest.MatcherAssert.assertThat; 5 import static org.hamcrest.Matchers.equalTo; 6 7 import org.junit.Test; 8 9 public class TestRawConstructionByReflection { 10 public static class ClassWithDefaultConstructor { 11 private final boolean called; 12 13 public ClassWithDefaultConstructor() { 14 called = true; 15 } 16 } 17 18 public static class ClassWithTwoConstructors { 19 private boolean stringCalled; 20 private boolean stringAndIntegerCalled; 21 22 public ClassWithTwoConstructors(final String string) { 23 stringCalled = true; 24 } 25 26 public ClassWithTwoConstructors(final String string, final Integer integer) { 27 stringAndIntegerCalled = true; 28 } 29 } 30 31 public static class ClassWithAmbigiousConstructors { 32 private final boolean called; 33 34 public ClassWithAmbigiousConstructors(final Object object) { 35 called = true; 36 } 37 38 public ClassWithAmbigiousConstructors(final String string) { 39 called = true; 40 } 41 } 42 43 @Test 44 public void constructUsingDefaultConstructor() { 45 assertThat(type(ClassWithDefaultConstructor.class).constructRaw().called, equalTo(true)); 46 } 47 48 @Test 49 public void constructUsingOneArgumentConstructor() { 50 assertThat(type(ClassWithTwoConstructors.class).constructRaw("string").stringCalled, equalTo(true)); 51 } 52 53 @Test 54 public void constructUsingOneArgumentWithNullValueConstructor() { 55 assertThat(type(ClassWithTwoConstructors.class).constructRaw((Object) null).stringCalled, equalTo(true)); 56 } 57 58 @Test 59 public void constructUsingTwoArgumentConstructor() { 60 assertThat(type(ClassWithTwoConstructors.class).constructRaw("string", 13).stringAndIntegerCalled, equalTo(true)); 61 } 62 63 @Test 64 public void constructUsingTwoArgumentWithOneNullValueConstructor() { 65 assertThat(type(ClassWithTwoConstructors.class).constructRaw("string", null).stringAndIntegerCalled, equalTo(true)); 66 } 67 68 @Test 69 public void constructUsingTwoArgumentWithTwoNullValueConstructor() { 70 assertThat(type(ClassWithTwoConstructors.class).constructRaw(null, null).stringAndIntegerCalled, equalTo(true)); 71 } 72 73 @Test 74 public void constructUsingAnyConstructorIfMultipleMatchers() { 75 assertThat(type(ClassWithAmbigiousConstructors.class).constructRaw("string").called, equalTo(true)); 76 } 77 }