View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }