Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jukito.provbug;
- import javax.inject.Inject;
- import javax.inject.Provider;
- import com.google.inject.AbstractModule;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.Provides;
- import org.jukito.JukitoModule;
- import org.jukito.JukitoRunner;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import static org.junit.Assert.assertEquals;
- /**
- * Uncomment exactly one of the lines marked A, B, or C and run as a test
- * or as a main program.
- * <p>
- * When A is uncommented, the test passes and the main program runs with
- * no problem, because we aren't binding the Provider<Integer> in a
- * @Provides method.
- * <p>
- * When B is uncommented, the test passes and the main program runs with
- * no problem, because String is injected with Integer rather than a
- * Provider<Integer>.
- * <p>
- * When C is uncommented, the test gets an error but the main program
- * still runs with no problem. This is the issue 34 bug: Guice supports
- * binding to Provider in @Provides methods, but Jukito doesn't.
- */
- @RunWith(JukitoRunner.class)
- public class ProvidesBugTest {
- public static void main(String... args) {
- String s = Guice.createInjector(new Mod2()).getInstance(String.class);
- assert "123".equals(s) : "problem: should be 123";
- System.out.println("no problem");
- }
- public static class Mod1 extends JukitoModule {
- @Override protected void configureTest() {
- install(new Mod2());
- }
- }
- public static class Mod2 extends AbstractModule {
- @Override protected void configure() {
- bind(Integer.class).toInstance(123);
- bind(String.class).toProvider(StringProvider.class); // A
- }
- static class StringProvider implements Provider<String> {
- final Provider<Integer> ip;
- @Inject StringProvider(Provider<Integer> ip) { this.ip = ip; }
- public String get() {
- return ip.get().toString();
- }
- }
- //@Provides // B
- String getStringFromInt(Integer i) {
- return i.toString();
- }
- //@Provides // C
- String getStringFromIntProvider(Provider<Integer> i) {
- return i.get().toString();
- }
- }
- @Test public void foo(String s) {
- assertEquals("123", s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement