View Javadoc

1   package org.apache.onami.autobind.scanner.asm.tests.autobind.multiple;
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 java.util.ArrayList;
27  import java.util.List;
28  import java.util.Set;
29  
30  import javax.inject.Inject;
31  
32  import org.apache.onami.autobind.annotations.Bind;
33  import org.apache.onami.autobind.configuration.StartupModule;
34  import org.apache.onami.autobind.scanner.PackageFilter;
35  import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
36  import org.junit.Test;
37  
38  import com.google.inject.ConfigurationException;
39  import com.google.inject.Guice;
40  import com.google.inject.Injector;
41  
42  public class MultibindTests
43  {
44  
45      @Test
46      public void createDynamicModule()
47      {
48          Injector injector =
49              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
50                                                          PackageFilter.create( MultibindTests.class ) ) );
51          assertNotNull( injector );
52      }
53  
54      @Test
55      public void testWithWrongPackage1()
56      {
57          Injector injector =
58              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( "java" ) ) );
59          assertNotNull( injector );
60  
61          try
62          {
63              FirstContainer container = injector.getInstance( FirstContainer.class );
64              fail( "The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
65                  + ( container == null ) );
66          }
67          catch ( ConfigurationException e )
68          {
69              // ok
70          }
71      }
72  
73      @Test
74      public void testWithWrongPackage2()
75      {
76          Injector injector =
77              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( "java" ) ) );
78          assertNotNull( injector );
79  
80          try
81          {
82              SecondContainer container = injector.getInstance( SecondContainer.class );
83              fail( "The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
84                  + ( container == null ) );
85          }
86          catch ( ConfigurationException e )
87          {
88              // ok
89          }
90      }
91  
92      @Test
93      public void createFirstContainer()
94      {
95          Injector injector =
96              Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
97                                                          PackageFilter.create( MultibindTests.class ) ) );
98          assertNotNull( injector );
99  
100         FirstContainer container = injector.getInstance( FirstContainer.class );
101         assertNotNull( container );
102         assertTrue( container.size() == 2 );
103         for ( FirstInterface obj : container.get() )
104         {
105             assertTrue( obj instanceof FirstInterface );
106             assertTrue( obj instanceof SecondInterface );
107             assertTrue( obj instanceof FirstImplementation || obj instanceof SecondImplementation );
108         }
109     }
110 
111     @Test
112     public void createSecondTestInterface()
113     {
114         Injector injector =
115             Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
116                                                         PackageFilter.create( MultibindTests.class ) ) );
117         assertNotNull( injector );
118 
119         SecondContainer container = injector.getInstance( SecondContainer.class );
120         assertNotNull( container );
121         assertTrue( container.size() == 2 );
122         for ( SecondInterface obj : container.get() )
123         {
124             assertTrue( obj instanceof FirstInterface );
125             assertTrue( obj instanceof SecondInterface );
126             assertTrue( obj instanceof FirstImplementation || obj instanceof SecondImplementation );
127         }
128     }
129 
130     @Test
131     public void createAllInterfaces()
132     {
133         Injector injector =
134             Guice.createInjector( StartupModule.create( ASMClasspathScanner.class,
135                                                         PackageFilter.create( MultibindTests.class ) ) );
136         assertNotNull( injector );
137 
138         FirstContainer firstContainer = injector.getInstance( FirstContainer.class );
139         assertNotNull( firstContainer );
140         assertTrue( firstContainer.size() == 2 );
141         for ( FirstInterface obj : firstContainer.get() )
142         {
143             assertTrue( obj instanceof FirstInterface );
144             assertTrue( obj instanceof SecondInterface );
145             assertTrue( obj instanceof FirstImplementation || obj instanceof SecondImplementation );
146         }
147 
148         SecondContainer secondContainer = injector.getInstance( SecondContainer.class );
149         assertNotNull( secondContainer );
150         assertTrue( secondContainer.size() == 2 );
151         for ( SecondInterface obj : secondContainer.get() )
152         {
153             assertTrue( obj instanceof FirstInterface );
154             assertTrue( obj instanceof SecondInterface );
155             assertTrue( obj instanceof FirstImplementation || obj instanceof SecondImplementation );
156         }
157     }
158 
159     public static interface FirstInterface
160     {
161         String sayHello();
162     }
163 
164     public static interface SecondInterface
165     {
166         String fireEvent();
167     }
168 
169     public static class FirstContainer
170     {
171         private List<FirstInterface> implementations;
172 
173         @Inject
174         public FirstContainer( Set<FirstInterface> implementations )
175         {
176             super();
177             this.implementations = new ArrayList<FirstInterface>( implementations );
178         }
179 
180         public int size()
181         {
182             return implementations.size();
183         }
184 
185         public List<FirstInterface> get()
186         {
187             return implementations;
188         }
189     }
190 
191     public static class SecondContainer
192     {
193         private List<SecondInterface> implementations;
194 
195         @Inject
196         public SecondContainer( Set<SecondInterface> implementations )
197         {
198             super();
199             this.implementations = new ArrayList<SecondInterface>( implementations );
200         }
201 
202         public int size()
203         {
204             return implementations.size();
205         }
206 
207         public List<SecondInterface> get()
208         {
209             return implementations;
210         }
211     }
212 
213     @Bind( multiple = true )
214     public static class FirstImplementation
215         implements FirstInterface, SecondInterface
216     {
217         public static final String TEST = "test1";
218 
219         public static final String EVENT = "event1";
220 
221         @Override
222         public String sayHello()
223         {
224             return TEST;
225         }
226 
227         @Override
228         public String fireEvent()
229         {
230             return EVENT;
231         }
232     }
233 
234     @Bind( multiple = true )
235     public static class SecondImplementation
236         implements FirstInterface, SecondInterface
237     {
238         public static final String TEST = "test2";
239 
240         public static final String EVENT = "event2";
241 
242         @Override
243         public String sayHello()
244         {
245             return TEST;
246         }
247 
248         @Override
249         public String fireEvent()
250         {
251             return EVENT;
252         }
253     }
254 
255 }