01: /**
02: * Copyright (C) 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: */package com.google.inject;
16:
17: import static com.google.inject.matcher.Matchers.any;
18: import junit.framework.TestCase;
19: import org.aopalliance.intercept.MethodInterceptor;
20: import org.aopalliance.intercept.MethodInvocation;
21:
22: /**
23: * @author crazybob@google.com (Bob Lee)
24: */
25: public class IntegrationTest extends TestCase {
26:
27: public void testIntegration() throws CreationException {
28: CountingInterceptor counter = new CountingInterceptor();
29:
30: BinderImpl binder = new BinderImpl();
31: binder.bind(Foo.class);
32: binder.bindInterceptor(any(), any(), counter);
33: Injector injector = binder.createInjector();
34:
35: Foo foo = injector.getInstance(Key.get(Foo.class));
36: foo.foo();
37: assertTrue(foo.invoked);
38: assertEquals(1, counter.count);
39:
40: foo = injector.getInstance(Foo.class);
41: foo.foo();
42: assertTrue(foo.invoked);
43: assertEquals(2, counter.count);
44: }
45:
46: static class Foo {
47: boolean invoked;
48:
49: public void foo() {
50: invoked = true;
51: }
52: }
53:
54: static class CountingInterceptor implements MethodInterceptor {
55:
56: int count;
57:
58: public Object invoke(MethodInvocation methodInvocation)
59: throws Throwable {
60: count++;
61: return methodInvocation.proceed();
62: }
63: }
64:
65: }
|