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   * Copyright 2011 Tim Wood
15   *
16   * Licensed under the Apache License, Version 2.0 (the "License");
17   * you may not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * http://www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an "AS IS" BASIS,
24   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License. 
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  }