View Javadoc

1   package org.apache.onami.autobind.scanner.asm.tests.autobind.duplicate;
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 static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import org.apache.onami.autobind.annotations.Bind;
27  import org.apache.onami.autobind.annotations.To;
28  import org.apache.onami.autobind.annotations.To.Type;
29  import org.apache.onami.autobind.configuration.StartupModule;
30  import org.apache.onami.autobind.scanner.PackageFilter;
31  import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
32  import org.junit.Test;
33  
34  import com.google.inject.ConfigurationException;
35  import com.google.inject.Guice;
36  import com.google.inject.Injector;
37  
38  public class DuplicateAutobindTests
39  {
40  
41      @Test
42      public void createDynamicModule()
43      {
44          Injector injector =
45              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
46                                                          PackageFilter.create( DuplicateAutobindTests.class ) ) );
47          assertNotNull( injector );
48      }
49  
50      @Test
51      public void testWithWrongPackage()
52      {
53          Injector injector =
54              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( "java" ) ) );
55          assertNotNull( injector );
56  
57          try
58          {
59              SecondTestInterface testInstance = injector.getInstance( SecondTestInterface.class );
60              fail( "The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
61                  + ( testInstance == null ) );
62          }
63          catch ( ConfigurationException e )
64          {
65              // ok
66          }
67      }
68  
69      @Test
70      public void createTestInterface()
71      {
72          Injector injector =
73              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
74                                                          PackageFilter.create( DuplicateAutobindTests.class ) ) );
75          assertNotNull( injector );
76  
77          try
78          {
79              TestInterface testInstance = injector.getInstance( TestInterface.class );
80              fail( "Instance implements TestInterface, but was not bound to. Instance may be null? "
81                  + ( testInstance == null ) );
82          }
83          catch ( ConfigurationException e )
84          {
85              // ok
86          }
87      }
88  
89      @Test
90      public void createSecondTestInterface()
91      {
92          Injector injector =
93              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
94                                                          PackageFilter.create( DuplicateAutobindTests.class ) ) );
95          assertNotNull( injector );
96  
97          SecondTestInterface sameInstance = injector.getInstance( SecondTestInterface.class );
98          assertNotNull( sameInstance );
99          assertTrue( sameInstance.fireEvent().equals( TestInterfaceImplementation.EVENT ) );
100         assertTrue( sameInstance instanceof TestInterface );
101     }
102 
103     public static interface TestInterface
104     {
105         String sayHello();
106     }
107 
108     public static interface SecondTestInterface
109     {
110         String fireEvent();
111     }
112 
113     @Bind( to = @To( value = Type.CUSTOM, customs = { SecondTestInterface.class } ) )
114     public static class TestInterfaceImplementation
115         implements TestInterface, SecondTestInterface
116     {
117         public static final String TEST = "test";
118 
119         public static final String EVENT = "event";
120 
121         @Override
122         public String sayHello()
123         {
124             return TEST;
125         }
126 
127         @Override
128         public String fireEvent()
129         {
130             return EVENT;
131         }
132     }
133 
134     @Bind( to = @To( value = Type.CUSTOM, customs = { SecondTestInterface.class } ) )
135     public static class SecondInterfaceImplementation
136         implements TestInterface, SecondTestInterface
137     {
138         public static final String TEST = "test";
139 
140         public static final String EVENT = "event";
141 
142         @Override
143         public String sayHello()
144         {
145             return TEST;
146         }
147 
148         @Override
149         public String fireEvent()
150         {
151             return EVENT;
152         }
153     }
154 
155 }