crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2006 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 | */ |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 16 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 17 | package com.google.inject.internal; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 18 | |
limpbizkit | 5ae41eb | 2009-06-06 17:51:27 +0000 | [diff] [blame^] | 19 | import com.google.inject.Inject; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 20 | import static com.google.inject.matcher.Matchers.annotatedWith; |
| 21 | import static com.google.inject.matcher.Matchers.any; |
| 22 | import static com.google.inject.matcher.Matchers.not; |
| 23 | import static com.google.inject.matcher.Matchers.only; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 24 | import com.google.inject.spi.InjectionPoint; |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 25 | import java.lang.annotation.Retention; |
| 26 | import java.lang.annotation.RetentionPolicy; |
kevinb9n | 2394ca6 | 2007-04-20 14:33:40 +0000 | [diff] [blame] | 27 | import java.lang.reflect.InvocationTargetException; |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 28 | import java.util.List; |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 29 | import junit.framework.TestCase; |
| 30 | import org.aopalliance.intercept.MethodInterceptor; |
| 31 | import org.aopalliance.intercept.MethodInvocation; |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * @author crazybob@google.com (Bob Lee) |
| 35 | */ |
| 36 | public class ProxyFactoryTest extends TestCase { |
| 37 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 38 | List<MethodAspect> aspects = Lists.newArrayList(); |
| 39 | |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 40 | public void testSimpleCase() |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 41 | throws NoSuchMethodException, InvocationTargetException, ErrorsException { |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 42 | SimpleInterceptor interceptor = new SimpleInterceptor(); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 43 | InjectionPoint injectionPoint = InjectionPoint.forConstructorOf(Simple.class); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 44 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 45 | aspects.add(new MethodAspect(any(), any(), interceptor)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 46 | ProxyFactory<Simple> factory = new ProxyFactory<Simple>(injectionPoint, aspects); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 47 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 48 | ConstructionProxy<Simple> constructionProxy = factory.create(); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 49 | |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 50 | Simple simple = constructionProxy.newInstance(); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 51 | simple.invoke(); |
| 52 | assertTrue(simple.invoked); |
| 53 | assertTrue(interceptor.invoked); |
| 54 | } |
| 55 | |
| 56 | static class Simple { |
| 57 | boolean invoked = false; |
| 58 | public void invoke() { |
| 59 | invoked = true; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | static class SimpleInterceptor implements MethodInterceptor { |
| 64 | |
| 65 | boolean invoked = false; |
| 66 | |
| 67 | public Object invoke(MethodInvocation methodInvocation) throws Throwable { |
| 68 | invoked = true; |
| 69 | return methodInvocation.proceed(); |
| 70 | } |
| 71 | } |
| 72 | |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 73 | public void testInterceptOneMethod() |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 74 | throws NoSuchMethodException, InvocationTargetException, ErrorsException { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 75 | SimpleInterceptor interceptor = new SimpleInterceptor(); |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 76 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 77 | aspects.add(new MethodAspect(only(Bar.class), annotatedWith(Intercept.class), interceptor)); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 78 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 79 | ConstructionProxy<Foo> fooFactory |
| 80 | = new ProxyFactory<Foo>(InjectionPoint.forConstructorOf(Foo.class), aspects).create(); |
| 81 | ConstructionProxy<Bar> barFactory |
| 82 | = new ProxyFactory<Bar>(InjectionPoint.forConstructorOf(Bar.class), aspects).create(); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 83 | |
| 84 | Foo foo = fooFactory.newInstance(); |
| 85 | Bar bar = barFactory.newInstance(); |
| 86 | |
| 87 | foo.foo(); |
| 88 | assertTrue(foo.fooCalled); |
| 89 | assertFalse(interceptor.invoked); |
| 90 | |
| 91 | bar.bar(); |
| 92 | assertTrue(bar.barCalled); |
| 93 | assertFalse(interceptor.invoked); |
| 94 | |
| 95 | bar.intercepted(); |
| 96 | assertTrue(bar.interceptedCalled); |
| 97 | assertTrue(interceptor.invoked); |
| 98 | } |
| 99 | |
| 100 | static class Foo { |
| 101 | boolean fooCalled; |
| 102 | @Intercept |
| 103 | void foo() { |
| 104 | fooCalled = true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static class Bar { |
| 109 | |
| 110 | boolean barCalled; |
| 111 | void bar() { |
| 112 | barCalled = true; |
| 113 | } |
| 114 | |
| 115 | boolean interceptedCalled; |
| 116 | |
| 117 | @Intercept |
| 118 | void intercepted() { |
| 119 | interceptedCalled = true; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | @Retention(RetentionPolicy.RUNTIME) |
| 124 | @interface Intercept {} |
| 125 | |
| 126 | public void testWithConstructorArguments() |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 127 | throws InvocationTargetException, NoSuchMethodException, ErrorsException { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 128 | SimpleInterceptor interceptor = new SimpleInterceptor(); |
| 129 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 130 | aspects.add(new MethodAspect(any(), any(), interceptor)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 131 | ProxyFactory<A> factory |
| 132 | = new ProxyFactory<A>(InjectionPoint.forConstructorOf(A.class), aspects); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 133 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 134 | ConstructionProxy<A> constructor = factory.create(); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 135 | |
| 136 | A a = constructor.newInstance(5); |
| 137 | a.a(); |
| 138 | assertEquals(5, a.i); |
| 139 | } |
| 140 | |
| 141 | public void testNotProxied() |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 142 | throws NoSuchMethodException, InvocationTargetException, ErrorsException { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 143 | SimpleInterceptor interceptor = new SimpleInterceptor(); |
| 144 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 145 | aspects.add(new MethodAspect(not(any()), not(any()), interceptor)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 146 | ProxyFactory<A> factory |
| 147 | = new ProxyFactory<A>(InjectionPoint.forConstructorOf(A.class), aspects); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 148 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 149 | ConstructionProxy<A> constructor = factory.create(); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 150 | |
| 151 | A a = constructor.newInstance(5); |
| 152 | assertEquals(A.class, a.getClass()); |
| 153 | } |
| 154 | |
| 155 | static class A { |
| 156 | final int i; |
limpbizkit | a6e0e78 | 2008-09-03 06:19:56 +0000 | [diff] [blame] | 157 | @Inject public A(int i) { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 158 | this.i = i; |
| 159 | } |
| 160 | public void a() {} |
| 161 | } |
| 162 | |
| 163 | public void testMultipleInterceptors() |
limpbizkit | 163c48a | 2008-06-16 02:58:08 +0000 | [diff] [blame] | 164 | throws NoSuchMethodException, InvocationTargetException, ErrorsException { |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 165 | DoubleInterceptor doubleInterceptor = new DoubleInterceptor(); |
| 166 | CountingInterceptor countingInterceptor = new CountingInterceptor(); |
| 167 | |
limpbizkit | 5fb9d92 | 2008-10-14 23:35:56 +0000 | [diff] [blame] | 168 | aspects.add(new MethodAspect(any(), any(), doubleInterceptor, countingInterceptor)); |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 169 | ProxyFactory<Counter> factory |
| 170 | = new ProxyFactory<Counter>(InjectionPoint.forConstructorOf(Counter.class), aspects); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 171 | |
limpbizkit | 03b81a6 | 2009-03-18 05:34:39 +0000 | [diff] [blame] | 172 | ConstructionProxy<Counter> constructor = factory.create(); |
crazyboblee | 62fcdde | 2007-02-03 02:10:13 +0000 | [diff] [blame] | 173 | |
| 174 | Counter counter = constructor.newInstance(); |
| 175 | counter.inc(); |
| 176 | assertEquals(2, counter.count); |
| 177 | assertEquals(2, countingInterceptor.count); |
| 178 | } |
| 179 | |
| 180 | static class CountingInterceptor implements MethodInterceptor { |
| 181 | |
| 182 | int count; |
| 183 | |
| 184 | public Object invoke(MethodInvocation methodInvocation) throws Throwable { |
| 185 | count++; |
| 186 | return methodInvocation.proceed(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static class DoubleInterceptor implements MethodInterceptor { |
| 191 | |
| 192 | public Object invoke(MethodInvocation methodInvocation) throws Throwable { |
| 193 | methodInvocation.proceed(); |
| 194 | return methodInvocation.proceed(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static class Counter { |
| 199 | int count; |
| 200 | void inc() { |
| 201 | count++; |
| 202 | } |
| 203 | } |
crazyboblee | 1b82a8f | 2007-02-02 23:30:42 +0000 | [diff] [blame] | 204 | } |