• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.google.inject.spi;
18 
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.collect.Iterables;
21 import com.google.inject.AbstractModule;
22 import com.google.inject.Guice;
23 import com.google.inject.Inject;
24 import com.google.inject.Injector;
25 import com.google.inject.Key;
26 import com.google.inject.Provider;
27 import java.util.Set;
28 import junit.framework.TestCase;
29 
30 /** @author jessewilson@google.com (Jesse Wilson) */
31 public class HasDependenciesTest extends TestCase {
32 
33   /** When an instance implements HasDependencies, the injected dependencies aren't used. */
testInstanceWithDependencies()34   public void testInstanceWithDependencies() {
35     Injector injector =
36         Guice.createInjector(
37             new AbstractModule() {
38               @Override
39               protected void configure() {
40                 bind(A.class).toInstance(new AWithDependencies());
41               }
42             });
43 
44     InstanceBinding<?> binding = (InstanceBinding<?>) injector.getBinding(A.class);
45     assertEquals(
46         ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Integer.class))),
47         binding.getDependencies());
48   }
49 
testInstanceWithoutDependencies()50   public void testInstanceWithoutDependencies() {
51     Injector injector =
52         Guice.createInjector(
53             new AbstractModule() {
54               @Override
55               protected void configure() {
56                 bind(A.class).toInstance(new A());
57               }
58             });
59 
60     InstanceBinding<?> binding = (InstanceBinding<?>) injector.getBinding(A.class);
61     Dependency<?> onlyDependency = Iterables.getOnlyElement(binding.getDependencies());
62     assertEquals(Key.get(String.class), onlyDependency.getKey());
63   }
64 
testProvider()65   public void testProvider() {
66     Injector injector =
67         Guice.createInjector(
68             new AbstractModule() {
69               @Override
70               protected void configure() {
71                 bind(A.class).toProvider(new ProviderOfA());
72               }
73             });
74 
75     ProviderInstanceBinding<?> binding = (ProviderInstanceBinding<?>) injector.getBinding(A.class);
76     Dependency<?> onlyDependency = Iterables.getOnlyElement(binding.getDependencies());
77     assertEquals(Key.get(String.class), onlyDependency.getKey());
78   }
79 
80   static class A {
81     @Inject
injectUnusedDependencies(String unused)82     void injectUnusedDependencies(String unused) {}
83   }
84 
85   static class ProviderOfA implements Provider<A> {
86     @Inject
injectUnusedDependencies(String unused)87     void injectUnusedDependencies(String unused) {}
88 
89     @Override
get()90     public A get() {
91       throw new UnsupportedOperationException();
92     }
93   }
94 
95   static class AWithDependencies extends A implements HasDependencies {
96     @Override
getDependencies()97     public Set<Dependency<?>> getDependencies() {
98       return ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Integer.class)));
99     }
100   }
101 
102   static class ProviderOfAWithDependencies extends ProviderOfA
103       implements ProviderWithDependencies<A> {
104     @Override
getDependencies()105     public Set<Dependency<?>> getDependencies() {
106       return ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(Integer.class)));
107     }
108   }
109 }
110