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.Lists; 20 import com.google.inject.AbstractModule; 21 import com.google.inject.Binding; 22 import com.google.inject.ConfigurationException; 23 import com.google.inject.Guice; 24 import com.google.inject.Inject; 25 import com.google.inject.Injector; 26 import com.google.inject.Key; 27 import com.google.inject.Module; 28 import com.google.inject.Provider; 29 import com.google.inject.name.Names; 30 31 import junit.framework.TestCase; 32 33 import java.util.List; 34 35 /** 36 * @author jessewilson (at) google.com (Jesse Wilson) 37 */ 38 public class ModuleRewriterTest extends TestCase { 39 40 public void testRewriteBindings() { 41 // create a module the binds String.class and CharSequence.class 42 Module module = new AbstractModule() { 43 protected void configure() { 44 bind(String.class).toInstance("Pizza"); 45 bind(CharSequence.class).toInstance("Wine"); 46 } 47 }; 48 49 // record the elements from that module 50 List<Element> elements = Elements.getElements(module); 51 52 // create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer' 53 List<Element> rewritten = Lists.newArrayList(); 54 for (Element element : elements) { 55 element = element.acceptVisitor(new DefaultElementVisitor<Element>() { 56 @Override public <T> Element visit(Binding<T> binding) { 57 T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor()); 58 if ("Wine".equals(target)) { 59 return null; 60 } 61 else { 62 return binding; 63 } 64 } 65 }); 66 if (element != null) { 67 rewritten.add(element); 68 } 69 } 70 71 // create a module from the original list of elements and the rewriter 72 Module rewrittenModule = Elements.getModule(rewritten); 73 74 // the wine binding is dropped 75 Injector injector = Guice.createInjector(rewrittenModule); 76 try { 77 injector.getInstance(CharSequence.class); 78 fail(); 79 } catch (ConfigurationException expected) { 80 } 81 } 82 83 public void testGetProviderAvailableAtInjectMembersTime() { 84 Module module = new AbstractModule() { 85 public void configure() { 86 final Provider<String> stringProvider = getProvider(String.class); 87 88 bind(String.class).annotatedWith(Names.named("2")).toProvider(new Provider<String>() { 89 private String value; 90 91 @Inject void initialize() { 92 value = stringProvider.get(); 93 } 94 95 public String get() { 96 return value; 97 } 98 }); 99 100 bind(String.class).toInstance("A"); 101 } 102 }; 103 104 // the module works fine normally 105 Injector injector = Guice.createInjector(module); 106 assertEquals("A", injector.getInstance(Key.get(String.class, Names.named("2")))); 107 108 // and it should also work fine if we rewrite it 109 List<Element> elements = Elements.getElements(module); 110 Module replayed = Elements.getModule(elements); 111 Injector replayedInjector = Guice.createInjector(replayed); 112 assertEquals("A", replayedInjector.getInstance(Key.get(String.class, Names.named("2")))); 113 } 114 } 115