1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.onami.factoryannotation;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
24
25 import org.apache.onami.factoryannotation.annotations.SecondLayer;
26 import org.apache.onami.factoryannotation.annotations.User;
27 import org.apache.onami.factoryannotation.impl.SecondLayerEntity;
28 import org.apache.onami.factoryannotation.impl.SecondLayerIdentityProvider;
29 import org.apache.onami.factoryannotation.impl.SecondLayerIdentityProvider.InjectableSecondLayerEntity;
30 import org.apache.onami.factoryannotation.impl.UserEntity;
31 import org.apache.onami.factoryannotation.impl.UserValueInjector;
32 import org.junit.Test;
33
34 import com.google.inject.Guice;
35 import com.google.inject.Injector;
36
37 public class DeepInjectionTestCase
38 {
39
40 @Test
41 public void testDeepInjection()
42 {
43 final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
44 {
45 @Override
46 protected void configure()
47 {
48 bindType( SecondLayerEntity.class ).annotatedWith( SecondLayer.class ).toAnnotationFactory( new SecondLayerIdentityProvider() );
49
50 bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new UserValueInjector() );
51 }
52 } );
53
54 final Layer injectable = new Layer();
55 injector.injectMembers( injectable );
56
57 assertEquals( "User1", injectable.userEntity.getName() );
58 assertEquals( 1, injectable.userEntity.getId() );
59
60 assertEquals( "SecondLayerEntity1", injectable.layerEntity1.getValue() );
61 assertEquals( 1, injectable.layerEntity1.getId() );
62 assertEquals( "SecondLayerEntity1", injectable.layerEntity1.getValue() );
63 assertEquals( 1, injectable.layerEntity1.getId() );
64
65 assertTrue( injectable.layerEntity2 instanceof InjectableSecondLayerEntity );
66
67 InjectableSecondLayerEntity entity = (InjectableSecondLayerEntity) injectable.layerEntity2;
68
69 assertNotNull( entity.getUserEntity() );
70
71 assertEquals( "SecondLayerEntity3_User1", injectable.layerEntity2.getValue() );
72 assertEquals( 3, injectable.layerEntity2.getId() );
73
74 assertEquals( "User1", entity.getUserEntity().getName() );
75 assertEquals( 1, entity.getUserEntity().getId() );
76 }
77
78 public static class Layer
79 {
80 @User( byId = 1 )
81 private UserEntity userEntity;
82
83 @SecondLayer( byId = 1 )
84 private SecondLayerEntity layerEntity1;
85
86 @SecondLayer( byId = 3 )
87 private SecondLayerEntity layerEntity2;
88 }
89
90 }