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