|
| 1 | +package com.google.inject.util; |
| 2 | + |
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static com.google.common.truth.Truth8.assertThat; |
| 5 | +import static org.junit.Assume.assumeTrue; |
| 6 | + |
| 7 | +import com.google.inject.AbstractModule; |
| 8 | +import com.google.inject.Guice; |
| 9 | +import com.google.inject.Injector; |
| 10 | +import com.google.inject.internal.InternalFlags; |
| 11 | +import com.google.inject.matcher.Matchers; |
| 12 | +import java.util.Optional; |
| 13 | +import org.aopalliance.intercept.MethodInterceptor; |
| 14 | +import org.aopalliance.intercept.MethodInvocation; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.junit.runners.JUnit4; |
| 18 | + |
| 19 | +@RunWith(JUnit4.class) |
| 20 | +public final class EnhancedTest { |
| 21 | + @Test |
| 22 | + public void isEnhanced() { |
| 23 | + assumeTrue(InternalFlags.isBytecodeGenEnabled()); |
| 24 | + |
| 25 | + Injector injector = Guice.createInjector(new InterceptingModule()); |
| 26 | + Foo foo = injector.getInstance(Foo.class); |
| 27 | + Bar bar = injector.getInstance(Bar.class); |
| 28 | + // Validate preconditions: foo is intercepted & bar isn't. |
| 29 | + assertThat(foo.foo()).isEqualTo("intercepted-foo"); |
| 30 | + assertThat(bar.bar()).isEqualTo("bar"); |
| 31 | + |
| 32 | + // The actual tests. |
| 33 | + assertThat(Enhanced.isEnhanced(foo.getClass())).isTrue(); |
| 34 | + assertThat(Enhanced.isEnhanced(bar.getClass())).isFalse(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void unenhancedClass() { |
| 39 | + assumeTrue(InternalFlags.isBytecodeGenEnabled()); |
| 40 | + |
| 41 | + Injector injector = Guice.createInjector(new InterceptingModule()); |
| 42 | + Foo foo = injector.getInstance(Foo.class); |
| 43 | + Bar bar = injector.getInstance(Bar.class); |
| 44 | + // Validate preconditions: foo is intercepted & bar isn't. |
| 45 | + assertThat(foo.foo()).isEqualTo("intercepted-foo"); |
| 46 | + assertThat(bar.bar()).isEqualTo("bar"); |
| 47 | + |
| 48 | + // The actual tests. |
| 49 | + assertThat(Enhanced.unenhancedClass(foo.getClass())).isEqualTo(Optional.of(Foo.class)); |
| 50 | + assertThat(Enhanced.unenhancedClass(bar.getClass())).isEmpty(); |
| 51 | + } |
| 52 | + |
| 53 | + private static class InterceptingModule extends AbstractModule { |
| 54 | + @Override |
| 55 | + protected void configure() { |
| 56 | + binder() |
| 57 | + .bindInterceptor( |
| 58 | + Matchers.only(Foo.class), |
| 59 | + Matchers.any(), |
| 60 | + new MethodInterceptor() { |
| 61 | + @Override |
| 62 | + public Object invoke(MethodInvocation i) throws Throwable { |
| 63 | + return "intercepted-" + i.proceed(); |
| 64 | + } |
| 65 | + }); |
| 66 | + bind(Foo.class); |
| 67 | + bind(Bar.class); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class Foo { |
| 72 | + public String foo() { |
| 73 | + return "foo"; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static class Bar { |
| 78 | + public String bar() { |
| 79 | + return "bar"; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments