aboutsummaryrefslogtreecommitdiff
path: root/core/test/com/google/inject/spi/ToolStageInjectorTest.java
blob: 892911294932f0b2e4c03916c87d006272cae0ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.google.inject.spi;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

import com.google.inject.AbstractModule;
import com.google.inject.Asserts;
import com.google.inject.CreationException;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Stage;
import com.google.inject.spi.Toolable;

public class ToolStageInjectorTest extends TestCase {

  @Override
  protected void setUp() throws Exception {
    Foo.s = null;
    Foo.sm = null;
  }

  public void testToolStageInjectorRestrictions() {
    Injector injector = Guice.createInjector(Stage.TOOL);
    try {
      injector.injectMembers(new Object());
      fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
    } catch (UnsupportedOperationException expected) {
    }

    try {
      injector.getInstance(Injector.class);
      fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
    } catch (UnsupportedOperationException expected) {
    }

    try {
      injector.getInstance(Key.get(Injector.class));
      fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
    } catch (UnsupportedOperationException expected) {
    }

    try {
      injector.getProvider(Injector.class);
      fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
    } catch (UnsupportedOperationException expected) {
    }

    try {
      injector.getProvider(Key.get(Injector.class));
      fail("Non-SPI Injector methods must throw an exception in the TOOL stage.");
    } catch (UnsupportedOperationException expected) {
    }
  }
  
  public void testToolStageDoesntInjectInstances() {
    final Foo foo = new Foo();
    Guice.createInjector(Stage.TOOL, new AbstractModule() {
      @Override
      protected void configure() {
        requestStaticInjection(Foo.class);
        requestInjection(foo);
      }
    });
    assertNull(Foo.s);
    assertNull(Foo.sm);
    assertNull(foo.f);
    assertNull(foo.m);
  }
  
  public void testToolStageDoesntInjectProviders() {
    final Foo foo = new Foo();
    Guice.createInjector(Stage.TOOL, new AbstractModule() {
      @Override
      protected void configure() {
        requestStaticInjection(Foo.class);
        bind(Object.class).toProvider(foo);
      }
    });
    assertNull(Foo.s);
    assertNull(Foo.sm);
    assertNull(foo.f);
    assertNull(foo.m);
  }
  
  public void testToolStageWarnsOfMissingObjectGraph() {
    final Bar bar = new Bar();
    try {
      Guice.createInjector(Stage.TOOL, new AbstractModule() {
        @Override
        protected void configure() {
          requestStaticInjection(Bar.class);
          requestInjection(bar);
        }
      });
      fail("expected exception");
    } catch(CreationException expected) {
      Asserts.assertContains(expected.toString(), "No implementation for java.util.Collection was bound.",
          "No implementation for java.util.Map was bound.",
          "No implementation for java.util.List was bound.",
          "No implementation for java.util.Set was bound.");
    }
  }
  
  public void testToolStageInjectsTooledMethods() {
    final Tooled tooled = new Tooled();
    Guice.createInjector(Stage.TOOL, new AbstractModule() {
      @Override
      protected void configure() {
        requestStaticInjection(Tooled.class);
        bind(Object.class).toProvider(tooled);
      }
    });
    assertNull(Tooled.s);
    assertNotNull(Tooled.sm);
    assertNull(tooled.f);
    assertNotNull(tooled.m);
  }
  
  @SuppressWarnings("unchecked")
  private static class Bar {
    @SuppressWarnings("unused") @Inject private static List list;
    @SuppressWarnings("unused") @Inject private Set set;
    @SuppressWarnings("unused") @Inject void method(Collection c) {}
    @SuppressWarnings("unused") @Inject static void staticMethod(Map map) {}
  }
  
  private static class Foo implements Provider<Object> {
    @Inject private static S s; 
    @Inject private F f;
    private M m;
    @SuppressWarnings("unused") @Inject void method(M m) { this.m = m; }
    private static SM sm;
    @SuppressWarnings("unused") @Inject static void staticMethod(SM sm) { Tooled.sm = sm; }
    
    public Object get() {
      return null;
    }
  }
  
  private static class Tooled implements Provider<Object> {
    @Inject private static S s; 
    @Inject private F f;
    private M m;
    @Toolable @SuppressWarnings("unused") @Inject void method(M m) { this.m = m; }
    private static SM sm;
    @Toolable @SuppressWarnings("unused") @Inject static void staticMethod(SM sm) { Tooled.sm = sm; }
    
    public Object get() {
      return null;
    }
  }
    
  private static class S {}
  private static class F {}
  private static class M {}
  private static class SM {}

}