• 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.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 import java.util.List;
31 import junit.framework.TestCase;
32 
33 /** @author jessewilson@google.com (Jesse Wilson) */
34 public class ModuleRewriterTest extends TestCase {
35 
testRewriteBindings()36   public void testRewriteBindings() {
37     // create a module the binds String.class and CharSequence.class
38     Module module =
39         new AbstractModule() {
40           @Override
41           protected void configure() {
42             bind(String.class).toInstance("Pizza");
43             bind(CharSequence.class).toInstance("Wine");
44           }
45         };
46 
47     // record the elements from that module
48     List<Element> elements = Elements.getElements(module);
49 
50     // create a rewriter that rewrites the binding to 'Wine' with a binding to 'Beer'
51     List<Element> rewritten = Lists.newArrayList();
52     for (Element element : elements) {
53       element =
54           element.acceptVisitor(
55               new DefaultElementVisitor<Element>() {
56                 @Override
57                 public <T> Element visit(Binding<T> binding) {
58                   T target = binding.acceptTargetVisitor(Elements.<T>getInstanceVisitor());
59                   if ("Wine".equals(target)) {
60                     return null;
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 
testGetProviderAvailableAtInjectMembersTime()83   public void testGetProviderAvailableAtInjectMembersTime() {
84     Module module =
85         new AbstractModule() {
86           @Override
87           public void configure() {
88             final Provider<String> stringProvider = getProvider(String.class);
89 
90             bind(String.class)
91                 .annotatedWith(Names.named("2"))
92                 .toProvider(
93                     new Provider<String>() {
94                       private String value;
95 
96                       @Inject
97                       void initialize() {
98                         value = stringProvider.get();
99                       }
100 
101                       @Override
102                       public String get() {
103                         return value;
104                       }
105                     });
106 
107             bind(String.class).toInstance("A");
108           }
109         };
110 
111     // the module works fine normally
112     Injector injector = Guice.createInjector(module);
113     assertEquals("A", injector.getInstance(Key.get(String.class, Names.named("2"))));
114 
115     // and it should also work fine if we rewrite it
116     List<Element> elements = Elements.getElements(module);
117     Module replayed = Elements.getModule(elements);
118     Injector replayedInjector = Guice.createInjector(replayed);
119     assertEquals("A", replayedInjector.getInstance(Key.get(String.class, Names.named("2"))));
120   }
121 }
122