Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
MockTypeListener |
|
| 1.3333333333333333;1.333 | ||||
MockTypeListener$1 |
|
| 1.3333333333333333;1.333 |
1 | package org.apache.onami.test.mock.guice; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.lang.reflect.Field; | |
23 | import java.util.Map; | |
24 | ||
25 | import org.apache.onami.test.annotation.Mock; | |
26 | import org.apache.onami.test.reflection.AnnotationHandler; | |
27 | import org.apache.onami.test.reflection.ClassVisitor; | |
28 | import org.apache.onami.test.reflection.HandleException; | |
29 | ||
30 | import com.google.inject.TypeLiteral; | |
31 | import com.google.inject.spi.TypeEncounter; | |
32 | import com.google.inject.spi.TypeListener; | |
33 | ||
34 | /** | |
35 | * <p> | |
36 | * {@link TypeListener} implementation. | |
37 | * </p> | |
38 | * <p> | |
39 | * Creates a specific {@link MockMembersInjector} for each {@link Mock} annotation found. | |
40 | * </p> | |
41 | * | |
42 | * @see MockMembersInjector | |
43 | * @see Mock | |
44 | */ | |
45 | 11 | public class MockTypeListener |
46 | implements TypeListener | |
47 | { | |
48 | ||
49 | private final Map<Field, Object> mockedObjects; | |
50 | ||
51 | /** | |
52 | * Creates a new {@code MockTypeListener} instance given a map of mocked objects. | |
53 | * | |
54 | * @param mockedObjects a map of mocked objects | |
55 | */ | |
56 | public MockTypeListener( Map<Field, Object> mockedObjects ) | |
57 | 7 | { |
58 | 7 | this.mockedObjects = mockedObjects; |
59 | 7 | } |
60 | ||
61 | /** | |
62 | * {@inheritDoc} | |
63 | */ | |
64 | public <I> void hear( final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter ) | |
65 | { | |
66 | try | |
67 | { | |
68 | 31 | new ClassVisitor() |
69 | .registerHandler( Mock.class, new AnnotationHandler<Mock, Field>() | |
70 | 42 | { |
71 | ||
72 | public void handle( Mock annotation, Field field ) | |
73 | throws HandleException | |
74 | { | |
75 | 11 | typeEncounter.register( new MockMembersInjector<I>( field, mockedObjects ) ); |
76 | 11 | } |
77 | ||
78 | } ) | |
79 | .visit( typeLiteral.getRawType() ); | |
80 | } | |
81 | 0 | catch ( HandleException e ) |
82 | { | |
83 | 0 | typeEncounter.addError( e ); |
84 | 31 | } |
85 | 31 | } |
86 | ||
87 | } |