PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/spice-inject/guice-patches/vanilla.test/com/google/inject/NullableInjectionPointTest.java

https://github.com/peterlynch/spice
Java | 194 lines | 164 code | 20 blank | 10 comment | 0 complexity | 189954fe825a144ffbf70c8bc5a0e5b7 MD5 | raw file
  1. package com.google.inject;
  2. import static com.google.inject.Asserts.assertContains;
  3. import com.google.inject.internal.Nullable;
  4. import junit.framework.TestCase;
  5. /**
  6. * @author jessewilson@google.com (Jesse Wilson)
  7. */
  8. public class NullableInjectionPointTest extends TestCase {
  9. public void testInjectNullIntoNotNullableConstructor() {
  10. try {
  11. createInjector().getInstance(FooConstructor.class);
  12. fail("Injecting null should fail with an error");
  13. }
  14. catch (ProvisionException expected) {
  15. assertContains(expected.getMessage(),
  16. "null returned by binding at " + getClass().getName(),
  17. "parameter 0 of " + FooConstructor.class.getName() + ".<init>() is not @Nullable");
  18. }
  19. }
  20. public void testInjectNullIntoNotNullableMethod() {
  21. try {
  22. createInjector().getInstance(FooMethod.class);
  23. fail("Injecting null should fail with an error");
  24. }
  25. catch (ProvisionException expected) {
  26. assertContains(expected.getMessage(),
  27. "null returned by binding at " + getClass().getName(),
  28. "parameter 0 of " + FooMethod.class.getName() + ".setFoo() is not @Nullable");
  29. }
  30. }
  31. public void testInjectNullIntoNotNullableField() {
  32. try {
  33. createInjector().getInstance(FooField.class);
  34. fail("Injecting null should fail with an error");
  35. }
  36. catch (ProvisionException expected) {
  37. assertContains(expected.getMessage(),
  38. "null returned by binding at " + getClass().getName(),
  39. " but " + FooField.class.getName() + ".foo is not @Nullable");
  40. }
  41. }
  42. /**
  43. * Provider.getInstance() is allowed to return null via direct calls to
  44. * getInstance().
  45. */
  46. public void testGetInstanceOfNull() {
  47. assertNull(createInjector().getInstance(Foo.class));
  48. }
  49. public void testInjectNullIntoNullableConstructor() {
  50. NullableFooConstructor nfc
  51. = createInjector().getInstance(NullableFooConstructor.class);
  52. assertNull(nfc.foo);
  53. }
  54. public void testInjectNullIntoNullableMethod() {
  55. NullableFooMethod nfm
  56. = createInjector().getInstance(NullableFooMethod.class);
  57. assertNull(nfm.foo);
  58. }
  59. public void testInjectNullIntoNullableField() {
  60. NullableFooField nff
  61. = createInjector().getInstance(NullableFooField.class);
  62. assertNull(nff.foo);
  63. }
  64. private Injector createInjector() {
  65. return Guice.createInjector(
  66. new AbstractModule() {
  67. protected void configure() {
  68. bind(Foo.class).toProvider(new Provider<Foo>() {
  69. public Foo get() {
  70. return null;
  71. }
  72. });
  73. }
  74. });
  75. }
  76. /**
  77. * We haven't decided on what the desired behaviour of this test should be...
  78. */
  79. public void testBindNullToInstance() {
  80. try {
  81. Guice.createInjector(new AbstractModule() {
  82. protected void configure() {
  83. bind(Foo.class).toInstance(null);
  84. }
  85. });
  86. fail();
  87. } catch (CreationException expected) {
  88. assertContains(expected.getMessage(),
  89. "Binding to null instances is not allowed.",
  90. "at " + getClass().getName(), ".configure(NullableInjectionPointTest.java:");
  91. }
  92. }
  93. public void testBindNullToProvider() {
  94. Injector injector = Guice.createInjector(new AbstractModule() {
  95. protected void configure() {
  96. bind(Foo.class).toProvider(new Provider<Foo>() {
  97. public Foo get() {
  98. return null;
  99. }
  100. });
  101. }
  102. });
  103. assertNull(injector.getInstance(NullableFooField.class).foo);
  104. try {
  105. injector.getInstance(FooField.class);
  106. }
  107. catch(ProvisionException expected) {
  108. assertContains(expected.getMessage(), "null returned by binding at");
  109. }
  110. }
  111. public void testBindScopedNull() {
  112. Injector injector = Guice.createInjector(new AbstractModule() {
  113. protected void configure() {
  114. bind(Foo.class).toProvider(new Provider<Foo>() {
  115. public Foo get() {
  116. return null;
  117. }
  118. }).in(Scopes.SINGLETON);
  119. }
  120. });
  121. assertNull(injector.getInstance(NullableFooField.class).foo);
  122. try {
  123. injector.getInstance(FooField.class);
  124. }
  125. catch(ProvisionException expected) {
  126. assertContains(expected.getMessage(), "null returned by binding at");
  127. }
  128. }
  129. public void testBindNullAsEagerSingleton() {
  130. Injector injector = Guice.createInjector(new AbstractModule() {
  131. protected void configure() {
  132. bind(Foo.class).toProvider(new Provider<Foo>() {
  133. public Foo get() {
  134. return null;
  135. }
  136. }).asEagerSingleton();
  137. }
  138. });
  139. assertNull(injector.getInstance(NullableFooField.class).foo);
  140. try {
  141. injector.getInstance(FooField.class);
  142. fail();
  143. } catch(ProvisionException expected) {
  144. assertContains(expected.getMessage(), "null returned by binding "
  145. + "at com.google.inject.NullableInjectionPointTest");
  146. }
  147. }
  148. static class Foo { }
  149. static class FooConstructor {
  150. @Inject FooConstructor(Foo foo) { }
  151. }
  152. static class FooField {
  153. @Inject Foo foo;
  154. }
  155. static class FooMethod {
  156. @Inject
  157. void setFoo(Foo foo) { }
  158. }
  159. static class NullableFooConstructor {
  160. Foo foo;
  161. @Inject NullableFooConstructor(@Nullable Foo foo) {
  162. this.foo = foo;
  163. }
  164. }
  165. static class NullableFooField {
  166. @Inject @Nullable Foo foo;
  167. }
  168. static class NullableFooMethod {
  169. Foo foo;
  170. @Inject void setFoo(@Nullable Foo foo) {
  171. this.foo = foo;
  172. }
  173. }
  174. }