File: NullInjectedIntoNonNullableTest.java

package info (click to toggle)
guice 5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,692 kB
  • sloc: java: 72,879; xml: 1,431; sh: 58; jsp: 12; makefile: 5
file content (113 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download
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
package com.google.inject.errors;

import static com.google.inject.errors.ErrorMessageTestUtils.assertGuiceErrorEqualsIgnoreLineNumber;
import static org.junit.Assert.assertThrows;
import static org.junit.Assume.assumeTrue;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import com.google.inject.internal.InternalFlags;
import com.google.inject.internal.InternalFlags.IncludeStackTraceOption;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Field;
import javax.inject.Inject;
import javax.inject.Qualifier;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public final class NullInjectedIntoNonNullableTest {

  @Qualifier
  @Retention(RetentionPolicy.RUNTIME)
  @interface Bar {}

  static class Foo {
    @Inject
    Foo(@Bar String string) {}
  }

  static class FromProvidesMethodModule extends AbstractModule {
    @Provides
    @Bar
    String provideString() {
      return null;
    }
  }

  static class FromProviderModule extends AbstractModule {
    @Override
    protected void configure() {
      bind(String.class).annotatedWith(Bar.class).toProvider(() -> null);
    }
  }

  static class IntermediateModule extends AbstractModule {
    public static final String NULL = null;

    private static final Module MODULE =
        new AbstractModule() {
          @Override
          protected void configure() {
            Field field = null;
            try {
              field = IntermediateModule.class.getField("NULL");
            } catch (NoSuchFieldException error) {
              throw new AssertionError("NULL field missing!");
            }
            binder()
                .withSource(field)
                .bind(String.class)
                .annotatedWith(Bar.class)
                .toProvider(() -> NULL);
          }
        };

    @Override
    protected void configure() {
      install(MODULE);
    }
  }

  @Before
  public void ensureStackTraceIsIncluded() {
    assumeTrue(InternalFlags.getIncludeStackTraceOption() != IncludeStackTraceOption.OFF);
  }

  @Test
  public void nullReturnedFromProvidesMethod() {
    Injector injector = Guice.createInjector(new FromProvidesMethodModule());

    ProvisionException exception =
        assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(
        exception.getMessage(), "null_returned_from_provides_method.txt");
  }

  @Test
  public void nullReturnedFromProvider() {
    Injector injector = Guice.createInjector(new FromProviderModule());

    ProvisionException exception =
        assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(
        exception.getMessage(), "null_returned_from_provider.txt");
  }

  @Test
  public void nullReturnedFromProviderWithModuleStack() {
    Injector injector = Guice.createInjector(new IntermediateModule());

    ProvisionException exception =
        assertThrows(ProvisionException.class, () -> injector.getInstance(Foo.class));
    assertGuiceErrorEqualsIgnoreLineNumber(
        exception.getMessage(), "null_returned_from_provider_with_module_stack.txt");
  }
}