View Javadoc

1   package org.apache.onami.autobind.test.aop.inherited;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
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.GuiceMethodInterceptor;
32  import org.apache.onami.autobind.aop.Intercept;
33  import org.apache.onami.autobind.aop.feature.InterceptorFeature;
34  import org.apache.onami.autobind.configuration.StartupModule;
35  import org.apache.onami.autobind.scanner.PackageFilter;
36  import org.apache.onami.autobind.scanner.asm.ASMClasspathScanner;
37  import org.junit.Test;
38  
39  import com.google.inject.Guice;
40  import com.google.inject.Injector;
41  import com.google.inject.matcher.Matcher;
42  import com.google.inject.matcher.Matchers;
43  
44  public class InheritedInterceptorTests
45  {
46  
47      private static ThreadLocal<Boolean> called = new ThreadLocal<Boolean>();
48  
49      @Test
50      public void createDynamicModule()
51      {
52          StartupModule startup =
53              StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( InheritedInterceptorTests.class ) );
54          startup.addFeature( InterceptorFeature.class );
55  
56          Injector injector = Guice.createInjector( startup );
57          assertNotNull( injector );
58      }
59  
60      @Test
61      public void createInheritedInterceptor()
62      {
63          called.set( false );
64  
65          StartupModule startup =
66              StartupModule.create( ASMClasspathScanner.class, PackageFilter.create( InheritedInterceptorTests.class ) );
67          startup.addFeature( InterceptorFeature.class );
68  
69          Injector injector = Guice.createInjector( startup );
70          assertNotNull( injector );
71  
72          TestInterface instance = injector.getInstance( TestInterface.class );
73          instance.sayHello(); // should be intercepted
74          instance.sayGoodBye(); // if intercepted an exception is thrown
75  
76          assertTrue( "Interceptor was not invoked", called.get() );
77      }
78  
79      @Interceptor
80      public static class InheritedMethodInterceptor
81          extends GuiceMethodInterceptor
82      {
83  
84          @Override
85          public Object invoke( MethodInvocation invocation )
86              throws Throwable
87          {
88              assertTrue( invocation.getMethod().getName().equals( "sayHello" ) );
89              called.set( true );
90              return invocation.proceed();
91          }
92  
93          @Override
94          public Matcher<? super Class<?>> getClassMatcher()
95          {
96              return Matchers.any();
97          }
98  
99          @Override
100         public Matcher<? super Method> getMethodMatcher()
101         {
102             return Matchers.annotatedWith( Intercept.class );
103         }
104 
105     }
106 
107     public static interface TestInterface
108     {
109 
110         String sayHello();
111 
112         String sayGoodBye();
113 
114     }
115 
116     @Bind
117     public static class TestInterfaceImplementation
118         implements TestInterface
119     {
120 
121         public static final String TEST = "test";
122 
123         @Override
124         @Intercept
125         public String sayHello()
126         {
127             return TEST;
128         }
129 
130         @Override
131         public String sayGoodBye()
132         {
133             return "Good Bye!";
134         }
135     }
136 
137 }