• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2015 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.inject.daggeradapter;
17 
18 import static dagger.Provides.Type.SET;
19 
20 import com.google.common.collect.ImmutableSet;
21 import com.google.inject.AbstractModule;
22 import com.google.inject.Binder;
23 import com.google.inject.Guice;
24 import com.google.inject.Injector;
25 import com.google.inject.Key;
26 import com.google.inject.Module;
27 import com.google.inject.Provides;
28 import com.google.inject.multibindings.Multibinder;
29 import com.google.inject.util.Providers;
30 
31 import junit.framework.TestCase;
32 
33 import java.util.Set;
34 
35 /**
36  * Tests for {@link DaggerAdapter}.
37  *
38  * @author cgruber@google.com (Christian Gruber)
39  */
40 
41 public class DaggerAdapterTest extends TestCase {
42   @dagger.Module static class SimpleDaggerModule {
anInteger()43     @dagger.Provides Integer anInteger() {
44       return 1;
45     }
46   }
47 
testSimpleModule()48   public void testSimpleModule() {
49     Injector i = Guice.createInjector(DaggerAdapter.from(new SimpleDaggerModule()));
50     assertEquals((Integer) 1, i.getInstance(Integer.class));
51   }
52 
53   static class SimpleGuiceModule extends AbstractModule {
aString(Integer i)54     @Provides String aString(Integer i) {
55       return i.toString();
56     }
configure()57     @Override protected void configure() {}
58   }
59 
testInteractionWithGuiceModules()60   public void testInteractionWithGuiceModules() {
61      Injector i = Guice.createInjector(
62          new SimpleGuiceModule(),
63          DaggerAdapter.from(new SimpleDaggerModule()));
64      assertEquals("1", i.getInstance(String.class));
65   }
66 
67   @dagger.Module static class SetBindingDaggerModule1 {
anInteger()68     @dagger.Provides(type=SET) Integer anInteger() {
69       return 5;
70     }
71   }
72 
73   @dagger.Module static class SetBindingDaggerModule2 {
anInteger()74     @dagger.Provides(type=SET) Integer anInteger() {
75       return 3;
76     }
77   }
78 
testSetBindings()79   public void testSetBindings() {
80     Injector i = Guice.createInjector(
81         DaggerAdapter.from(new SetBindingDaggerModule1(), new SetBindingDaggerModule2()));
82     assertEquals(ImmutableSet.of(3, 5), i.getInstance(new Key<Set<Integer>>() {}));
83   }
84 
85   static class MultibindingGuiceModule implements Module {
configure(Binder binder)86     @Override public void configure(Binder binder) {
87       Multibinder<Integer> mb = Multibinder.newSetBinder(binder, Integer.class);
88       mb.addBinding().toInstance(13);
89       mb.addBinding().toProvider(Providers.of(8)); // mix'n'match.
90     }
91   }
92 
testSetBindingsWithGuiceModule()93   public void testSetBindingsWithGuiceModule() {
94     Injector i = Guice.createInjector(
95         new MultibindingGuiceModule(),
96         DaggerAdapter.from(new SetBindingDaggerModule1(), new SetBindingDaggerModule2()));
97     assertEquals(ImmutableSet.of(13, 3, 5, 8), i.getInstance(new Key<Set<Integer>>() {}));
98   }
99 }
100