1 package com.lexicalscope.fluentreflection;
2
3 import static com.lexicalscope.fluentreflection.ReflectionMatchers.hasArguments;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.equalTo;
6
7 import org.jmock.Expectations;
8 import org.junit.Rule;
9 import org.junit.Test;
10
11 import com.google.inject.TypeLiteral;
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class TestReflectedClassImpl {
30 @Rule public JUnitRuleMockery context = new JUnitRuleMockery();
31
32 private final ReflectedTypeFactory reflectedTypeFactory = context.mock(ReflectedTypeFactory.class);
33 private final ReflectedMembers<ExampleClass> members =
34 context.mock(new TypeLiteral<ReflectedMembers<ExampleClass>>() {
35 });
36
37 private final FluentConstructor<ExampleClass> reflectedConstructor =
38 context.mock(new TypeLiteral<FluentConstructor<ExampleClass>>() {
39 });
40
41 private final FluentObject<ExampleClass> reflectedInstance =
42 context.mock(new TypeLiteral<FluentObject<ExampleClass>>() {
43 });
44
45 class ExampleClass {
46 int method() {
47 return 42;
48 }
49 }
50
51 private final ExampleClass instance = new ExampleClass();
52
53 @Test public void constructFindsConstructorAndBindsReflectedResult() throws Exception {
54 context.checking(new Expectations() {
55 {
56 oneOf(members).constructor(hasArguments());
57 will(returnValue(reflectedConstructor));
58
59 oneOf(reflectedConstructor).callRaw();
60 will(returnValue(instance));
61
62 oneOf(reflectedTypeFactory).reflect(TypeLiteral.get(ExampleClass.class), instance);
63 will(returnValue(reflectedInstance));
64 }
65 });
66
67 final FluentClassImpl<ExampleClass> reflectedTypeImpl =
68 new FluentClassImpl<ExampleClass>(reflectedTypeFactory, TypeLiteral.get(ExampleClass.class), members);
69
70 assertThat(reflectedTypeImpl.construct(), equalTo(reflectedInstance));
71 }
72 }