limpbizkit | 0682897 | 2008-02-18 20:20:45 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2007 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 | |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 17 | package com.google.inject; |
| 18 | |
limpbizkit | eab7647 | 2008-05-26 19:45:12 +0000 | [diff] [blame] | 19 | import static com.google.inject.Asserts.assertContains; |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame^] | 20 | import static com.google.inject.Asserts.getDeclaringSourcePart; |
limpbizkit | 6caa8dc | 2009-05-19 00:40:02 +0000 | [diff] [blame] | 21 | import static com.google.inject.Asserts.reserialize; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 22 | import static java.lang.annotation.ElementType.CONSTRUCTOR; |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 23 | import static java.lang.annotation.ElementType.FIELD; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 24 | import static java.lang.annotation.ElementType.METHOD; |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 25 | import static java.lang.annotation.ElementType.PARAMETER; |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 26 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 27 | |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 28 | import junit.framework.TestCase; |
limpbizkit | eab7647 | 2008-05-26 19:45:12 +0000 | [diff] [blame] | 29 | |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | import java.lang.annotation.Retention; |
| 32 | import java.lang.annotation.Target; |
| 33 | |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 34 | /** |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 35 | * @author jessewilson@google.com (Jesse Wilson) |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 36 | */ |
limpbizkit | eab7647 | 2008-05-26 19:45:12 +0000 | [diff] [blame] | 37 | @SuppressWarnings("UnusedDeclaration") |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 38 | public class ProvisionExceptionTest extends TestCase { |
| 39 | |
| 40 | public void testExceptionsCollapsed() { |
| 41 | try { |
| 42 | Guice.createInjector().getInstance(A.class); |
| 43 | fail(); |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 44 | } catch (ProvisionException e) { |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 45 | assertTrue(e.getCause() instanceof UnsupportedOperationException); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 46 | assertContains(e.getMessage(), "Error injecting constructor", |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 47 | "for parameter 0 at com.google.inject.ProvisionExceptionTest$C.setD", |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 48 | "for field at com.google.inject.ProvisionExceptionTest$B.c", |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 49 | "for parameter 0 at com.google.inject.ProvisionExceptionTest$A"); |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * There's a pass-through of user code in the scope. We want exceptions thrown by Guice to be |
| 55 | * limited to a single exception, even if it passes through user code. |
| 56 | */ |
| 57 | public void testExceptionsCollapsedWithScopes() { |
| 58 | try { |
| 59 | Guice.createInjector(new AbstractModule() { |
| 60 | protected void configure() { |
| 61 | bind(B.class).in(Scopes.SINGLETON); |
| 62 | } |
| 63 | }).getInstance(A.class); |
| 64 | fail(); |
| 65 | } catch (ProvisionException e) { |
| 66 | assertTrue(e.getCause() instanceof UnsupportedOperationException); |
| 67 | assertFalse(e.getMessage().contains("custom provider")); |
| 68 | assertContains(e.getMessage(), "Error injecting constructor", |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 69 | "for parameter 0 at com.google.inject.ProvisionExceptionTest$C.setD", |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 70 | "for field at com.google.inject.ProvisionExceptionTest$B.c", |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 71 | "for parameter 0 at com.google.inject.ProvisionExceptionTest$A"); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | public void testMethodInjectionExceptions() { |
| 76 | try { |
| 77 | Guice.createInjector().getInstance(E.class); |
| 78 | fail(); |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 79 | } catch (ProvisionException e) { |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 80 | assertTrue(e.getCause() instanceof UnsupportedOperationException); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 81 | assertContains(e.getMessage(), "Error injecting method", |
| 82 | "at " + E.class.getName() + ".setObject(ProvisionExceptionTest.java:"); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 86 | public void testBindToProviderInstanceExceptions() { |
| 87 | try { |
| 88 | Guice.createInjector(new AbstractModule() { |
| 89 | protected void configure() { |
| 90 | bind(D.class).toProvider(new DProvider()); |
| 91 | } |
| 92 | }).getInstance(D.class); |
| 93 | fail(); |
| 94 | } catch (ProvisionException e) { |
| 95 | assertTrue(e.getCause() instanceof UnsupportedOperationException); |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 96 | assertContains(e.getMessage(), |
| 97 | "1) Error in custom provider, java.lang.UnsupportedOperationException", |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame^] | 98 | "at " + ProvisionExceptionTest.class.getName(), |
| 99 | getDeclaringSourcePart(getClass())); |
limpbizkit | e4647a6 | 2008-05-25 18:03:35 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 103 | /** |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 104 | * This test demonstrates that if the user throws a ProvisionException, we wrap it to add context. |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 105 | */ |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 106 | public void testProvisionExceptionsAreWrappedForBindToType() { |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 107 | try { |
| 108 | Guice.createInjector().getInstance(F.class); |
| 109 | fail(); |
| 110 | } catch (ProvisionException e) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 111 | assertContains(e.getMessage(), "1) User Exception", |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 112 | "at " + F.class.getName() + ".<init>(ProvisionExceptionTest.java:"); |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 113 | } |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 114 | } |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 115 | |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 116 | public void testProvisionExceptionsAreWrappedForBindToProviderType() { |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 117 | try { |
| 118 | Guice.createInjector(new AbstractModule() { |
| 119 | protected void configure() { |
| 120 | bind(F.class).toProvider(FProvider.class); |
| 121 | } |
| 122 | }).getInstance(F.class); |
| 123 | fail(); |
| 124 | } catch (ProvisionException e) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 125 | assertContains(e.getMessage(), "1) User Exception", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 126 | "while locating ", FProvider.class.getName(), |
| 127 | "while locating ", F.class.getName()); |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
| 131 | public void testProvisionExceptionsAreWrappedForBindToProviderInstance() { |
| 132 | try { |
| 133 | Guice.createInjector(new AbstractModule() { |
| 134 | protected void configure() { |
| 135 | bind(F.class).toProvider(new FProvider()); |
| 136 | } |
| 137 | }).getInstance(F.class); |
| 138 | fail(); |
| 139 | } catch (ProvisionException e) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 140 | assertContains(e.getMessage(), "1) User Exception", |
Christian Edward Gruber | ba5acdf | 2013-10-05 14:05:39 -0700 | [diff] [blame^] | 141 | "at " + ProvisionExceptionTest.class.getName(), |
| 142 | getDeclaringSourcePart(getClass())); |
limpbizkit | eab7647 | 2008-05-26 19:45:12 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | public void testProvisionExceptionIsSerializable() throws IOException { |
| 147 | try { |
| 148 | Guice.createInjector().getInstance(A.class); |
| 149 | fail(); |
| 150 | } catch (ProvisionException expected) { |
limpbizkit | 6caa8dc | 2009-05-19 00:40:02 +0000 | [diff] [blame] | 151 | ProvisionException reserialized = reserialize(expected); |
| 152 | assertContains(reserialized.getMessage(), |
| 153 | "1) Error injecting constructor, java.lang.UnsupportedOperationException", |
| 154 | "at com.google.inject.ProvisionExceptionTest$RealD.<init>()", |
| 155 | "at Key[type=com.google.inject.ProvisionExceptionTest$RealD, annotation=[none]]", |
| 156 | "@com.google.inject.ProvisionExceptionTest$C.setD()[0]", |
| 157 | "at Key[type=com.google.inject.ProvisionExceptionTest$C, annotation=[none]]", |
| 158 | "@com.google.inject.ProvisionExceptionTest$B.c", |
| 159 | "at Key[type=com.google.inject.ProvisionExceptionTest$B, annotation=[none]]", |
| 160 | "@com.google.inject.ProvisionExceptionTest$A.<init>()[0]", |
| 161 | "at Key[type=com.google.inject.ProvisionExceptionTest$A, annotation=[none]]"); |
limpbizkit | 56400ca | 2008-05-23 19:20:08 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 165 | public void testMultipleCauses() { |
| 166 | try { |
| 167 | Guice.createInjector().getInstance(G.class); |
| 168 | fail(); |
| 169 | } catch (ProvisionException e) { |
| 170 | assertContains(e.getMessage(), |
| 171 | "1) Error injecting method, java.lang.IllegalArgumentException", |
| 172 | "Caused by: java.lang.IllegalArgumentException: java.lang.UnsupportedOperationException", |
| 173 | "Caused by: java.lang.UnsupportedOperationException: Unsupported", |
| 174 | "2) Error injecting method, java.lang.NullPointerException: can't inject second either", |
| 175 | "Caused by: java.lang.NullPointerException: can't inject second either", |
| 176 | "2 errors"); |
| 177 | } |
| 178 | } |
| 179 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 180 | public void testInjectInnerClass() throws Exception { |
| 181 | Injector injector = Guice.createInjector(); |
| 182 | try { |
| 183 | injector.getInstance(InnerClass.class); |
| 184 | fail(); |
| 185 | } catch (Exception expected) { |
| 186 | assertContains(expected.getMessage(), |
| 187 | "Injecting into inner classes is not supported.", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 188 | "while locating " + InnerClass.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | public void testInjectLocalClass() throws Exception { |
| 193 | class LocalClass {} |
| 194 | |
| 195 | Injector injector = Guice.createInjector(); |
| 196 | try { |
| 197 | injector.getInstance(LocalClass.class); |
| 198 | fail(); |
| 199 | } catch (Exception expected) { |
| 200 | assertContains(expected.getMessage(), |
| 201 | "Injecting into inner classes is not supported.", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 202 | "while locating " + LocalClass.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | public void testBindingAnnotationsOnMethodsAndConstructors() { |
| 207 | try { |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 208 | Injector injector = Guice.createInjector(); |
| 209 | injector.getInstance(MethodWithBindingAnnotation.class); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 210 | fail(); |
| 211 | } catch (ConfigurationException expected) { |
| 212 | assertContains(expected.getMessage(), MethodWithBindingAnnotation.class.getName() |
| 213 | + ".injectMe() is annotated with @", Green.class.getName() + "(), ", |
| 214 | "but binding annotations should be applied to its parameters instead.", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 215 | "while locating " + MethodWithBindingAnnotation.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | try { |
| 219 | Guice.createInjector().getInstance(ConstructorWithBindingAnnotation.class); |
| 220 | fail(); |
| 221 | } catch (ConfigurationException expected) { |
| 222 | assertContains(expected.getMessage(), ConstructorWithBindingAnnotation.class.getName() |
| 223 | + ".<init>() is annotated with @", Green.class.getName() + "(), ", |
| 224 | "but binding annotations should be applied to its parameters instead.", |
| 225 | "at " + ConstructorWithBindingAnnotation.class.getName() + ".class", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 226 | "while locating " + ConstructorWithBindingAnnotation.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 230 | public void testBindingAnnotationWarningForScala() { |
| 231 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 232 | protected void configure() { |
| 233 | bind(String.class).annotatedWith(Green.class).toInstance("lime!"); |
| 234 | } |
| 235 | }); |
| 236 | injector.getInstance(LikeScala.class); |
| 237 | } |
| 238 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 239 | public void testLinkedBindings() { |
| 240 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 241 | protected void configure() { |
| 242 | bind(D.class).to(RealD.class); |
| 243 | } |
| 244 | }); |
| 245 | |
| 246 | try { |
| 247 | injector.getInstance(D.class); |
| 248 | fail(); |
| 249 | } catch (ProvisionException expected) { |
| 250 | assertContains(expected.getMessage(), |
| 251 | "at " + RealD.class.getName() + ".<init>(ProvisionExceptionTest.java:", |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 252 | "while locating " + RealD.class.getName(), |
| 253 | "while locating " + D.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | public void testProviderKeyBindings() { |
| 258 | Injector injector = Guice.createInjector(new AbstractModule() { |
| 259 | protected void configure() { |
| 260 | bind(D.class).toProvider(DProvider.class); |
| 261 | } |
| 262 | }); |
| 263 | |
| 264 | try { |
| 265 | injector.getInstance(D.class); |
| 266 | fail(); |
| 267 | } catch (ProvisionException expected) { |
| 268 | assertContains(expected.getMessage(), |
limpbizkit | 72d11dd | 2008-11-02 07:59:13 +0000 | [diff] [blame] | 269 | "while locating " + DProvider.class.getName(), |
| 270 | "while locating " + D.class.getName()); |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | private class InnerClass {} |
| 275 | |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 276 | static class A { |
| 277 | @Inject |
| 278 | A(B b) { } |
| 279 | } |
| 280 | static class B { |
| 281 | @Inject C c; |
| 282 | } |
| 283 | static class C { |
| 284 | @Inject |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 285 | void setD(RealD d) { } |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 286 | } |
| 287 | static class E { |
| 288 | @Inject void setObject(Object o) { |
| 289 | throw new UnsupportedOperationException(); |
| 290 | } |
| 291 | } |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 292 | |
| 293 | static class MethodWithBindingAnnotation { |
| 294 | @Inject @Green void injectMe(String greenString) {} |
| 295 | } |
| 296 | |
| 297 | static class ConstructorWithBindingAnnotation { |
| 298 | @Inject @Green ConstructorWithBindingAnnotation(String greenString) {} |
| 299 | } |
| 300 | |
limpbizkit | 9bf6585 | 2008-11-29 22:01:13 +0000 | [diff] [blame] | 301 | /** |
| 302 | * In Scala, fields automatically get accessor methods with the same name. So we don't do |
| 303 | * misplaced-binding annotation detection if the offending method has a matching field. |
| 304 | */ |
| 305 | static class LikeScala { |
| 306 | @Inject @Green String green; |
| 307 | @Inject @Green String green() { return green; } |
| 308 | } |
| 309 | |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 310 | @Retention(RUNTIME) |
| 311 | @Target({ FIELD, PARAMETER, CONSTRUCTOR, METHOD }) |
| 312 | @BindingAnnotation |
| 313 | @interface Green {} |
| 314 | |
| 315 | interface D {} |
| 316 | |
| 317 | static class RealD implements D { |
| 318 | @Inject RealD() { |
| 319 | throw new UnsupportedOperationException(); |
| 320 | } |
| 321 | } |
| 322 | |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 323 | static class DProvider implements Provider<D> { |
| 324 | public D get() { |
limpbizkit | 0689806 | 2008-11-02 05:14:55 +0000 | [diff] [blame] | 325 | throw new UnsupportedOperationException(); |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 329 | static class F { |
| 330 | @Inject public F() { |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 331 | throw new ProvisionException("User Exception", new RuntimeException()); |
limpbizkit | 60ddbc3 | 2008-05-07 06:37:19 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | static class FProvider implements Provider<F> { |
| 336 | public F get() { |
| 337 | return new F(); |
| 338 | } |
| 339 | } |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 340 | |
| 341 | static class G { |
| 342 | @Inject void injectFirst() { |
| 343 | throw new IllegalArgumentException(new UnsupportedOperationException("Unsupported")); |
| 344 | } |
| 345 | @Inject void injectSecond() { |
| 346 | throw new NullPointerException("can't inject second either"); |
| 347 | } |
| 348 | } |
limpbizkit | 1dabcfd | 2007-08-25 08:06:30 +0000 | [diff] [blame] | 349 | } |