1 package org.apache.onami.autobind.test.aop.invalid;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.fail;
24
25 import java.lang.reflect.Method;
26
27 import javax.interceptor.Interceptor;
28
29 import org.aopalliance.intercept.MethodInvocation;
30 import org.apache.onami.autobind.annotations.Bind;
31 import org.apache.onami.autobind.aop.ClassMatcher;
32 import org.apache.onami.autobind.aop.Intercept;
33 import org.apache.onami.autobind.aop.Invoke;
34 import org.apache.onami.autobind.aop.MethodMatcher;
35 import org.apache.onami.autobind.aop.feature.InterceptorFeature;
36 import org.apache.onami.autobind.configuration.StartupModule;
37 import org.apache.onami.autobind.scanner.PackageFilter;
38 import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
39 import org.junit.Test;
40
41 import com.google.inject.Guice;
42 import com.google.inject.Injector;
43 import com.google.inject.matcher.Matcher;
44 import com.google.inject.matcher.Matchers;
45
46 public class InvalidInterceptorTests
47 {
48
49 @Test
50 public void createDynamicModule()
51 {
52 StartupModule startup =
53 StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( InvalidInterceptorTests.class ) );
54 startup.addFeature( InterceptorFeature.class );
55
56 Injector injector = Guice.createInjector( startup );
57 assertNotNull( injector );
58 }
59
60 @Test
61 public void createInvalidInterceptor()
62 {
63 StartupModule startup =
64 StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( InvalidInterceptorTests.class ) );
65 startup.addFeature( InterceptorFeature.class );
66
67 Injector injector = Guice.createInjector( startup );
68 assertNotNull( injector );
69
70 TestInterface instance = injector.getInstance( TestInterface.class );
71 instance.sayHello();
72 }
73
74 @Interceptor
75 public static class InvalidMethodInterceptor
76 {
77
78 @Invoke
79 public Object invoke( MethodInvocation invocation, Object obj )
80 throws Throwable
81 {
82 fail( "This is an invalid Interceptor, it should never be called." );
83 return invocation.proceed();
84 }
85
86 @ClassMatcher
87 public Matcher<? super Class<?>> getClassMatcher()
88 {
89 return Matchers.any();
90 }
91
92 @MethodMatcher
93 public Matcher<? super Method> getMethodMatcher()
94 {
95 return Matchers.any();
96 }
97
98 }
99
100 public static interface TestInterface
101 {
102 String sayHello();
103 }
104
105 @Bind
106 public static class TestInterfaceImplementation
107 implements TestInterface
108 {
109 public static final String TEST = "test";
110
111 @Override
112 @Intercept
113 public String sayHello()
114 {
115 return TEST;
116 }
117 }
118
119 }