View Javadoc

1   package org.apache.onami.autobind.test.aop.invalid;
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.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();// Matchers.annotatedWith(Intercept.class);
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 }