PageRenderTime 803ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/test/com/wideplay/warp/internal/GuiceTest.java

https://github.com/tbaum/warp-core
Java | 121 lines | 78 code | 31 blank | 12 comment | 3 complexity | 1792a2934afe208a33cb04d90252a5f5 MD5 | raw file
  1. package com.wideplay.warp.internal;
  2. import com.google.inject.*;
  3. import com.wideplay.warp.WarpModule;
  4. import org.testng.annotations.Test;
  5. import java.text.MessageFormat;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. /**
  9. * Created with IntelliJ IDEA.
  10. * On: 20/03/2007
  11. *
  12. * @author Dhanji R. Prasanna (dhanji at gmail com)
  13. * @since 1.0
  14. */
  15. public class GuiceTest {
  16. public static class MyClass {
  17. @Inject List<String> strings;
  18. @Inject List<Integer> integers;
  19. }
  20. @Singleton
  21. public static class MyWiderScopeClass {
  22. @Inject MyNoScopeClass myClass;
  23. }
  24. public static class MyNoScopeClass {
  25. String name;
  26. }
  27. @Test
  28. public final void testTypeLiterals() {
  29. Injector injector = Guice.createInjector(new AbstractModule() {
  30. protected void configure() {
  31. bindObject(String.class);
  32. bindObject(Integer.class);
  33. }
  34. private <T> void bindObject(Class<T> clazz) {
  35. bind(new TypeLiteral<List<T>>() { }).toInstance(new LinkedList<T>());
  36. }
  37. });
  38. MyClass my = injector.getInstance(MyClass.class);
  39. my.strings.add("hello");
  40. assert my.integers.isEmpty();
  41. }
  42. //just some tests to see how guice behaves
  43. @Test
  44. public final void testInjections() {
  45. Injector injector = Guice.createInjector();
  46. //on a java.lang
  47. Object o = injector.getInstance(String.class);
  48. assert o instanceof String;
  49. System.out.println(MessageFormat.format("[{0}]", o));
  50. }
  51. //just some tests to see how guice behaves
  52. @Test
  53. public final void testInjectionsWithModule() {
  54. Injector injector = Guice.createInjector(new AbstractModule() {
  55. protected void configure() {
  56. }
  57. });
  58. assert null == injector.getBinding(Key.get(WarpModule.class));
  59. }
  60. //just some tests to see how guice behaves
  61. @Test
  62. public final void testWideningScopeInjection() {
  63. Injector injector = Guice.createInjector(new AbstractModule() {
  64. protected void configure() {
  65. }
  66. });
  67. MyWiderScopeClass myWiderScopeClass = injector.getInstance(MyWiderScopeClass.class);
  68. assert myWiderScopeClass == injector.getInstance(MyWiderScopeClass.class) : "singleton violated";
  69. assert myWiderScopeClass.myClass == injector.getInstance(MyWiderScopeClass.class).myClass :
  70. "singleton's down-scoped instance was different";
  71. }
  72. //just some tests to see how guice behaves
  73. @Test
  74. public final void testTypelessProvider() {
  75. Injector injector = Guice.createInjector(new AbstractModule() {
  76. @SuppressWarnings("unchecked")
  77. protected void configure() {
  78. bind(GuiceTest.class).toProvider(new Provider() {
  79. public Object get() {
  80. return "hi";
  81. }
  82. });
  83. }
  84. });
  85. injector.getInstance(GuiceTest.class);
  86. }
  87. public static <T> T get() {
  88. return null;
  89. }
  90. }