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  
23  import org.apache.onami.factoryannotation.annotations.User;
24  import org.apache.onami.factoryannotation.impl.InjectedUserValueInjector;
25  import org.apache.onami.factoryannotation.impl.UserEntity;
26  import org.junit.Test;
27  
28  import com.google.inject.AbstractModule;
29  import com.google.inject.Guice;
30  import com.google.inject.Injector;
31  import com.google.inject.name.Names;
32  
33  public class InjectedValueProviderTestCase
34  {
35  
36      @Test
37      public void testInjectedValueProvider()
38      {
39          final Injector injector = Guice.createInjector( new FactoryAnnotationModule()
40          {
41              @Override
42              protected void configure()
43              {
44                  bindType( UserEntity.class ).annotatedWith( User.class ).toAnnotationFactory( new InjectedUserValueInjector() );
45              }
46          }, new AbstractModule()
47          {
48              @Override
49              protected void configure()
50              {
51                  bind( UserEntity.class ).annotatedWith( Names.named( "test1" ) ).toInstance( UserEntity.build( "User1",
52                                                                                                                 1 ) );
53  
54                  bind( UserEntity.class ).annotatedWith( Names.named( "test2" ) ).toInstance( UserEntity.build( "User2",
55                                                                                                                 2 ) );
56              }
57          } );
58  
59          final UserInjectable injectable = new UserInjectable();
60          injector.injectMembers( injectable );
61  
62          assertEquals( "User1", injectable.user1.getName() );
63          assertEquals( 1, injectable.user1.getId() );
64          assertEquals( "User2", injectable.user2.getName() );
65          assertEquals( 2, injectable.user2.getId() );
66      }
67  
68      public class UserInjectable
69      {
70          @User( byId = 1 )
71          private UserEntity user1;
72  
73          @User( byId = 2 )
74          private UserEntity user2;
75      }
76  
77  }