Advertisement
tpeierls

Jukito issue 34: Provider bindings in @Provides methods

Jan 26th, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.48 KB | None | 0 0
  1. package org.jukito.provbug;
  2.  
  3. import javax.inject.Inject;
  4. import javax.inject.Provider;
  5.  
  6. import com.google.inject.AbstractModule;
  7. import com.google.inject.Guice;
  8. import com.google.inject.Injector;
  9. import com.google.inject.Provides;
  10.  
  11. import org.jukito.JukitoModule;
  12. import org.jukito.JukitoRunner;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import static org.junit.Assert.assertEquals;
  16.  
  17. /**
  18.  * Uncomment exactly one of the lines marked A, B, or C and run as a test
  19.  * or as a main program.
  20.  * <p>
  21.  * When A is uncommented, the test passes and the main program runs with
  22.  * no problem, because we aren't binding the Provider<Integer> in a
  23.  * @Provides method.
  24.  * <p>
  25.  * When B is uncommented, the test passes and the main program runs with
  26.  * no problem, because String is injected with Integer rather than a
  27.  * Provider<Integer>.
  28.  * <p>
  29.  * When C is uncommented, the test gets an error but the main program
  30.  * still runs with no problem. This is the issue 34 bug: Guice supports
  31.  * binding to Provider in @Provides methods, but Jukito doesn't.
  32.  */
  33. @RunWith(JukitoRunner.class)
  34. public class ProvidesBugTest {
  35.  
  36.     public static void main(String... args) {
  37.         String s = Guice.createInjector(new Mod2()).getInstance(String.class);
  38.         assert "123".equals(s) : "problem: should be 123";
  39.         System.out.println("no problem");
  40.     }
  41.  
  42.     public static class Mod1 extends JukitoModule {
  43.         @Override protected void configureTest() {
  44.             install(new Mod2());
  45.         }
  46.     }
  47.  
  48.     public static class Mod2 extends AbstractModule {
  49.         @Override protected void configure() {
  50.             bind(Integer.class).toInstance(123);
  51.             bind(String.class).toProvider(StringProvider.class);      // A
  52.         }
  53.  
  54.         static class StringProvider implements Provider<String> {
  55.             final Provider<Integer> ip;
  56.             @Inject StringProvider(Provider<Integer> ip) { this.ip = ip; }
  57.             public String get() {
  58.                 return ip.get().toString();
  59.             }
  60.         }
  61.  
  62.         //@Provides                                                     // B
  63.         String getStringFromInt(Integer i) {
  64.             return i.toString();
  65.         }
  66.  
  67.         //@Provides                                                     // C
  68.         String getStringFromIntProvider(Provider<Integer> i) {
  69.             return i.get().toString();
  70.         }
  71.     }
  72.  
  73.     @Test public void foo(String s) {
  74.         assertEquals("123", s);
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement