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