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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
package com.google.inject;
import static com.google.inject.Asserts.assertContains;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.common.base.Optional;
import com.google.inject.multibindings.OptionalBinder;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import junit.framework.TestCase;
/** @author jessewilson@google.com (Jesse Wilson) */
public class NullableInjectionPointTest extends TestCase {
public void testInjectNullIntoNotNullableConstructor() {
try {
createInjector().getInstance(FooConstructor.class);
fail("Injecting null should fail with an error");
} catch (ProvisionException expected) {
assertContains(
expected.getMessage(),
"null returned by binding at NullableInjectionPointTest$1.configure",
"the 1st parameter foo of NullableInjectionPointTest$FooConstructor.<init>(",
"is not @Nullable");
}
}
public void testInjectNullIntoNotNullableMethod() {
try {
createInjector().getInstance(FooMethod.class);
fail("Injecting null should fail with an error");
} catch (ProvisionException expected) {
assertContains(
expected.getMessage(),
"null returned by binding at NullableInjectionPointTest$1.configure",
"the 1st parameter foo of NullableInjectionPointTest$FooMethod.setFoo(",
"is not @Nullable");
}
}
public void testInjectNullIntoNotNullableField() {
try {
createInjector().getInstance(FooField.class);
fail("Injecting null should fail with an error");
} catch (ProvisionException expected) {
assertContains(
expected.getMessage(),
"null returned by binding at NullableInjectionPointTest$1.configure(",
" but NullableInjectionPointTest$FooField.foo",
" is not @Nullable");
}
}
/** Provider.getInstance() is allowed to return null via direct calls to getInstance(). */
public void testGetInstanceOfNull() {
assertNull(createInjector().getInstance(Foo.class));
}
public void testInjectNullIntoNullableConstructor() {
NullableFooConstructor nfc = createInjector().getInstance(NullableFooConstructor.class);
assertNull(nfc.foo);
}
public void testInjectNullIntoNullableMethod() {
NullableFooMethod nfm = createInjector().getInstance(NullableFooMethod.class);
assertNull(nfm.foo);
}
public void testInjectNullIntoNullableField() {
NullableFooField nff = createInjector().getInstance(NullableFooField.class);
assertNull(nff.foo);
}
public void testInjectNullIntoCustomNullableConstructor() {
CustomNullableFooConstructor nfc =
createInjector().getInstance(CustomNullableFooConstructor.class);
assertNull(nfc.foo);
}
public void testInjectNullIntoCustomNullableMethod() {
CustomNullableFooMethod nfm = createInjector().getInstance(CustomNullableFooMethod.class);
assertNull(nfm.foo);
}
public void testInjectNullIntoCustomNullableField() {
CustomNullableFooField nff = createInjector().getInstance(CustomNullableFooField.class);
assertNull(nff.foo);
}
public void testInjectNullIntoTypeUseNullableConstructor() {
TypeUseNullableFooConstructor nff =
createInjector().getInstance(TypeUseNullableFooConstructor.class);
assertNull(nff.foo);
}
public void testInjectNullIntoTypeUseNullableMethod() {
TypeUseNullableFooMethod nfm = createInjector().getInstance(TypeUseNullableFooMethod.class);
assertNull(nfm.foo);
}
public void testInjectNullIntoTypeUseNullableField() {
TypeUseNullableFooField nff = createInjector().getInstance(TypeUseNullableFooField.class);
assertNull(nff.foo);
}
private Injector createInjector() {
return Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toProvider(Providers.<Foo>of(null));
}
});
}
/** We haven't decided on what the desired behaviour of this test should be... */
public void testBindNullToInstance() {
try {
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toInstance(null);
}
});
fail();
} catch (CreationException expected) {
assertContains(
expected.getMessage(),
"Binding to null instances is not allowed.",
"at NullableInjectionPointTest$2.configure(");
}
}
public void testBindNullToProvider() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toProvider(Providers.<Foo>of(null));
}
});
assertNull(injector.getInstance(NullableFooField.class).foo);
assertNull(injector.getInstance(CustomNullableFooField.class).foo);
try {
injector.getInstance(FooField.class);
fail("Expected ProvisionException");
} catch (ProvisionException expected) {
assertContains(expected.getMessage(), "null returned by binding at");
}
}
public void testBindScopedNull() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toProvider(Providers.<Foo>of(null)).in(Scopes.SINGLETON);
}
});
assertNull(injector.getInstance(NullableFooField.class).foo);
assertNull(injector.getInstance(CustomNullableFooField.class).foo);
try {
injector.getInstance(FooField.class);
fail("Expected ProvisionException");
} catch (ProvisionException expected) {
assertContains(expected.getMessage(), "null returned by binding at");
}
}
public void testBindNullAsEagerSingleton() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toProvider(Providers.<Foo>of(null)).asEagerSingleton();
}
});
assertNull(injector.getInstance(NullableFooField.class).foo);
assertNull(injector.getInstance(CustomNullableFooField.class).foo);
try {
injector.getInstance(FooField.class);
fail();
} catch (ProvisionException expected) {
assertContains(
expected.getMessage(), "null returned by binding at NullableInjectionPointTest");
}
}
/**
* Tests for a regression where dependency objects were not updated properly and OptionalBinder
* was rejecting nulls from its dependencies.
*/
public void testBindNullAndLinkFromOptionalBinder() {
Injector injector =
Guice.createInjector(
new AbstractModule() {
@Override
protected void configure() {
bind(Foo.class).toProvider(Providers.<Foo>of(null));
OptionalBinder.newOptionalBinder(binder(), Foo.class);
}
@Provides
@Named("throughProvidesMethod")
Foo provideFoo(Optional<Foo> foo) {
return foo.orNull();
}
});
assertNull(injector.getInstance(Key.get(Foo.class, Names.named("throughProvidesMethod"))));
}
static class Foo {}
static class FooConstructor {
@Inject
FooConstructor(Foo foo) {}
}
static class FooField {
@Inject Foo foo;
}
static class FooMethod {
@Inject
void setFoo(Foo foo) {}
}
static class NullableFooConstructor {
Foo foo;
@Inject
NullableFooConstructor(@Nullable Foo foo) {
this.foo = foo;
}
}
static class NullableFooField {
@Inject @Nullable Foo foo;
}
static class NullableFooMethod {
Foo foo;
@Inject
void setFoo(@Nullable Foo foo) {
this.foo = foo;
}
}
static class CustomNullableFooConstructor {
Foo foo;
@Inject
CustomNullableFooConstructor(@Namespace.Nullable Foo foo) {
this.foo = foo;
}
}
static class CustomNullableFooField {
@Inject @Namespace.Nullable Foo foo;
}
static class CustomNullableFooMethod {
Foo foo;
@Inject
void setFoo(@Namespace.Nullable Foo foo) {
this.foo = foo;
}
}
private static class TypeUse {
@Retention(RUNTIME)
@Target(TYPE_USE)
private @interface Nullable {}
}
static class TypeUseNullableFooConstructor {
Foo foo;
@Inject
TypeUseNullableFooConstructor(@TypeUse.Nullable Foo foo) {
this.foo = foo;
}
}
static class TypeUseNullableFooField {
@Inject @TypeUse.Nullable Foo foo;
}
static class TypeUseNullableFooMethod {
Foo foo;
@Inject
void setFoo(@TypeUse.Nullable Foo foo) {
this.foo = foo;
}
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
@interface Nullable {}
static interface Namespace {
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.FIELD})
@interface Nullable {}
}
}
|