PageRenderTime 105ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/com/google/inject/ProvisionExceptionsTest.java

https://gitlab.com/metamorphiccode/guice
Java | 178 lines | 139 code | 13 blank | 26 comment | 2 complexity | 822ab981bd7215446772d46f33955af5 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.inject;
  17. import com.google.inject.internal.Errors;
  18. import com.google.inject.name.Named;
  19. import com.google.inject.name.Names;
  20. import junit.framework.TestCase;
  21. import java.io.IOException;
  22. /**
  23. * Tests that ProvisionExceptions are readable and clearly indicate to the user what went wrong with
  24. * their code.
  25. *
  26. * @author sameb@google.com (Sam Berlin)
  27. */
  28. public class ProvisionExceptionsTest extends TestCase {
  29. public void testConstructorRuntimeException() {
  30. Injector injector = Guice.createInjector(new AbstractModule() {
  31. @Override
  32. protected void configure() {
  33. bindConstant().annotatedWith(Names.named("runtime")).to(true);
  34. bind(Exploder.class).to(Explosion.class);
  35. bind(Tracer.class).to(TracerImpl.class);
  36. }
  37. });
  38. try {
  39. injector.getInstance(Tracer.class);
  40. fail();
  41. } catch(ProvisionException pe) {
  42. // Make sure our initial error message gives the user exception.
  43. Asserts.assertContains(pe.getMessage(),
  44. "1) Error injecting constructor", "java.lang.IllegalStateException: boom!");
  45. assertEquals(1, pe.getErrorMessages().size());
  46. assertEquals(IllegalStateException.class, pe.getCause().getClass());
  47. assertEquals(IllegalStateException.class, Errors.getOnlyCause(pe.getErrorMessages()).getClass());
  48. }
  49. }
  50. public void testConstructorCheckedException() {
  51. Injector injector = Guice.createInjector(new AbstractModule() {
  52. @Override
  53. protected void configure() {
  54. bindConstant().annotatedWith(Names.named("runtime")).to(false);
  55. bind(Exploder.class).to(Explosion.class);
  56. bind(Tracer.class).to(TracerImpl.class);
  57. }
  58. });
  59. try {
  60. injector.getInstance(Tracer.class);
  61. fail();
  62. } catch(ProvisionException pe) {
  63. // Make sure our initial error message gives the user exception.
  64. Asserts.assertContains(pe.getMessage(),
  65. "1) Error injecting constructor", "java.io.IOException: boom!");
  66. assertEquals(1, pe.getErrorMessages().size());
  67. assertEquals(IOException.class, pe.getCause().getClass());
  68. assertEquals(IOException.class, Errors.getOnlyCause(pe.getErrorMessages()).getClass());
  69. }
  70. }
  71. public void testCustomProvidersRuntimeException() {
  72. Injector injector = Guice.createInjector(new AbstractModule() {
  73. @Override
  74. protected void configure() {
  75. bind(Exploder.class).toProvider(new Provider<Exploder>() {
  76. public Exploder get() {
  77. return Explosion.createRuntime();
  78. }
  79. });
  80. bind(Tracer.class).to(TracerImpl.class);
  81. }
  82. });
  83. try {
  84. injector.getInstance(Tracer.class);
  85. fail();
  86. } catch(ProvisionException pe) {
  87. // Make sure our initial error message gives the user exception.
  88. Asserts.assertContains(pe.getMessage(),
  89. "1) Error in custom provider", "java.lang.IllegalStateException: boom!");
  90. assertEquals(1, pe.getErrorMessages().size());
  91. assertEquals(IllegalStateException.class, pe.getCause().getClass());
  92. assertEquals(IllegalStateException.class, Errors.getOnlyCause(pe.getErrorMessages()).getClass());
  93. }
  94. }
  95. public void testProviderMethodRuntimeException() {
  96. Injector injector = Guice.createInjector(new AbstractModule() {
  97. @Override
  98. protected void configure() {
  99. bind(Tracer.class).to(TracerImpl.class);
  100. }
  101. @Provides Exploder exploder() {
  102. return Explosion.createRuntime();
  103. }
  104. });
  105. try {
  106. injector.getInstance(Tracer.class);
  107. fail();
  108. } catch(ProvisionException pe) {
  109. // Make sure our initial error message gives the user exception.
  110. Asserts.assertContains(pe.getMessage(),
  111. "1) Error in custom provider", "java.lang.IllegalStateException: boom!");
  112. assertEquals(1, pe.getErrorMessages().size());
  113. assertEquals(IllegalStateException.class, pe.getCause().getClass());
  114. assertEquals(IllegalStateException.class, Errors.getOnlyCause(pe.getErrorMessages()).getClass());
  115. }
  116. }
  117. public void testProviderMethodCheckedException() {
  118. Injector injector = Guice.createInjector(new AbstractModule() {
  119. @Override
  120. protected void configure() {
  121. bind(Tracer.class).to(TracerImpl.class);
  122. }
  123. @Provides Exploder exploder() throws IOException {
  124. return Explosion.createChecked();
  125. }
  126. });
  127. try {
  128. injector.getInstance(Tracer.class);
  129. fail();
  130. } catch(ProvisionException pe) {
  131. pe.printStackTrace();
  132. // Make sure our initial error message gives the user exception.
  133. Asserts.assertContains(pe.getMessage(),
  134. "1) Error in custom provider", "java.io.IOException: boom!");
  135. assertEquals(1, pe.getErrorMessages().size());
  136. assertEquals(IOException.class, pe.getCause().getClass());
  137. assertEquals(IOException.class, Errors.getOnlyCause(pe.getErrorMessages()).getClass());
  138. }
  139. }
  140. private static interface Exploder {}
  141. public static class Explosion implements Exploder {
  142. @Inject public Explosion(@Named("runtime") boolean runtime) throws IOException {
  143. if(runtime) {
  144. throw new IllegalStateException("boom!");
  145. } else {
  146. throw new IOException("boom!");
  147. }
  148. }
  149. public static Explosion createRuntime() {
  150. try {
  151. return new Explosion(true);
  152. } catch(IOException iox) {
  153. throw new RuntimeException();
  154. }
  155. }
  156. public static Explosion createChecked() throws IOException {
  157. return new Explosion(false);
  158. }
  159. }
  160. private static interface Tracer {}
  161. private static class TracerImpl implements Tracer {
  162. @Inject TracerImpl(Exploder explosion) {
  163. }
  164. }
  165. }