use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testRecursiveBinding.
public void testRecursiveBinding() {
try {
Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
bind(Runnable.class).to(Runnable.class);
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Binding points to itself.", "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testArrayTypeCanonicalization.
/**
* Although {@code String[].class} isn't equal to {@code new
* GenericArrayTypeImpl(String.class)}, Guice should treat these two types
* interchangeably.
*/
public void testArrayTypeCanonicalization() {
final String[] strings = new String[] { "A" };
final Integer[] integers = new Integer[] { 1 };
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(strings);
bind(new TypeLiteral<Integer[]>() {
}).toInstance(integers);
}
});
assertSame(integers, injector.getInstance(Key.get(new TypeLiteral<Integer[]>() {
})));
assertSame(integers, injector.getInstance(new Key<Integer[]>() {
}));
assertSame(integers, injector.getInstance(Integer[].class));
assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
})));
assertSame(strings, injector.getInstance(new Key<String[]>() {
}));
assertSame(strings, injector.getInstance(String[].class));
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(new String[] { "A" });
bind(new TypeLiteral<String[]>() {
}).toInstance(new String[] { "B" });
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) A binding to java.lang.String[] was already configured at " + getClass().getName(), "at " + getClass().getName(), getDeclaringSourcePart(getClass()));
assertContains(expected.getMessage(), "1 error");
}
// passes because duplicates are ignored
injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String[].class).toInstance(strings);
bind(new TypeLiteral<String[]>() {
}).toInstance(strings);
}
});
assertSame(strings, injector.getInstance(Key.get(new TypeLiteral<String[]>() {
})));
assertSame(strings, injector.getInstance(new Key<String[]>() {
}));
assertSame(strings, injector.getInstance(String[].class));
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testUserReportedErrorsAreAlsoLogged.
public void testUserReportedErrorsAreAlsoLogged() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
addError(new Message("Whoops!", new IllegalArgumentException()));
}
});
fail();
} catch (CreationException expected) {
}
LogRecord logRecord = Iterables.getOnlyElement(this.logRecords);
assertContains(logRecord.getMessage(), "An exception was caught and reported. Message: java.lang.IllegalArgumentException");
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testBindingToProvider.
public void testBindingToProvider() {
try {
Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(new TypeLiteral<Provider<String>>() {
}).toInstance(Providers.of("A"));
}
});
fail();
} catch (CreationException expected) {
assertContains(expected.getMessage(), "1) Binding to Provider is not allowed.", "at " + BinderTest.class.getName(), getDeclaringSourcePart(getClass()));
}
}
use of com.google.inject.CreationException in project roboguice by roboguice.
the class BinderTest method testToStringOnBinderApi.
public void testToStringOnBinderApi() {
try {
Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
assertEquals("Binder", binder().toString());
assertEquals("Provider<java.lang.Integer>", getProvider(Integer.class).toString());
assertEquals("Provider<java.util.List<java.lang.String>>", getProvider(Key.get(new TypeLiteral<List<String>>() {
})).toString());
assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).toString());
assertEquals("BindingBuilder<java.lang.Integer>", bind(Integer.class).annotatedWith(Names.named("a")).toString());
assertEquals("ConstantBindingBuilder", bindConstant().toString());
assertEquals("ConstantBindingBuilder", bindConstant().annotatedWith(Names.named("b")).toString());
assertEquals("AnnotatedElementBuilder", binder().newPrivateBinder().expose(Integer.class).toString());
}
});
fail();
} catch (CreationException ignored) {
}
}
Aggregations