• 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 com.google.common.collect.ImmutableSet;
19 import com.google.inject.AbstractModule;
20 import com.google.inject.Binder;
21 import com.google.inject.Guice;
22 import com.google.inject.Injector;
23 import com.google.inject.Key;
24 import com.google.inject.Module;
25 import com.google.inject.Provides;
26 import com.google.inject.TypeLiteral;
27 import com.google.inject.multibindings.Multibinder;
28 import com.google.inject.util.Providers;
29 import dagger.multibindings.IntoSet;
30 import java.lang.annotation.Retention;
31 import java.lang.annotation.RetentionPolicy;
32 import java.util.Set;
33 import javax.inject.Qualifier;
34 import junit.framework.TestCase;
35 
36 /**
37  * Tests for {@link DaggerAdapter}.
38  *
39  * @author cgruber@google.com (Christian Gruber)
40  */
41 
42 public class DaggerAdapterTest extends TestCase {
43   @dagger.Module
44   static class SimpleDaggerModule {
45     @dagger.Provides
anInteger()46     Integer anInteger() {
47       return 1;
48     }
49   }
50 
testSimpleModule()51   public void testSimpleModule() {
52     Injector i = Guice.createInjector(DaggerAdapter.from(new SimpleDaggerModule()));
53     assertEquals((Integer) 1, i.getInstance(Integer.class));
54   }
55 
56   static class SimpleGuiceModule extends AbstractModule {
57     @Provides
aString(Integer i)58     String aString(Integer i) {
59       return i.toString();
60     }
61   }
62 
testInteractionWithGuiceModules()63   public void testInteractionWithGuiceModules() {
64     Injector i =
65         Guice.createInjector(new SimpleGuiceModule(), DaggerAdapter.from(new SimpleDaggerModule()));
66     assertEquals("1", i.getInstance(String.class));
67   }
68 
69   @dagger.Module
70   static class SetBindingDaggerModule1 {
71     @dagger.Provides
72     @IntoSet
anInteger()73     Integer anInteger() {
74       return 5;
75     }
76   }
77 
78   @dagger.Module
79   static class SetBindingDaggerModule2 {
80     @dagger.Provides
81     @IntoSet
anInteger()82     Integer anInteger() {
83       return 3;
84     }
85   }
86 
testSetBindings()87   public void testSetBindings() {
88     Injector i =
89         Guice.createInjector(
90             DaggerAdapter.from(new SetBindingDaggerModule1(), new SetBindingDaggerModule2()));
91     assertEquals(ImmutableSet.of(3, 5), i.getInstance(new Key<Set<Integer>>() {}));
92   }
93 
94   @Qualifier
95   @Retention(RetentionPolicy.RUNTIME)
96   public @interface AnnotationOnSet {}
97 
98   @dagger.Module
99   static class SetBindingWithAnnotationDaggerModule {
100     @dagger.Provides
101     @IntoSet
102     @AnnotationOnSet
anInteger()103     Integer anInteger() {
104       return 4;
105     }
106   }
107 
testSetBindingsWithAnnotation()108   public void testSetBindingsWithAnnotation() {
109     Injector i =
110         Guice.createInjector(DaggerAdapter.from(new SetBindingWithAnnotationDaggerModule()));
111     assertEquals(
112         ImmutableSet.of(4),
113         i.getInstance(Key.get(new TypeLiteral<Set<Integer>>() {}, AnnotationOnSet.class)));
114   }
115 
116   static class MultibindingGuiceModule implements Module {
117     @Override
configure(Binder binder)118     public void configure(Binder binder) {
119       Multibinder<Integer> mb = Multibinder.newSetBinder(binder, Integer.class);
120       mb.addBinding().toInstance(13);
121       mb.addBinding().toProvider(Providers.of(8)); // mix'n'match.
122     }
123   }
124 
testSetBindingsWithGuiceModule()125   public void testSetBindingsWithGuiceModule() {
126     Injector i =
127         Guice.createInjector(
128             new MultibindingGuiceModule(),
129             DaggerAdapter.from(new SetBindingDaggerModule1(), new SetBindingDaggerModule2()));
130     assertEquals(ImmutableSet.of(13, 3, 5, 8), i.getInstance(new Key<Set<Integer>>() {}));
131   }
132 }
133