Search in sources :

Example 46 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project guice by google.

the class LineNumbersTest method testCanHandleLineNumbersForGuiceGeneratedClasses.

@Test
public void testCanHandleLineNumbersForGuiceGeneratedClasses() {
    assumeTrue(InternalFlags.isBytecodeGenEnabled());
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bindInterceptor(Matchers.only(A.class), Matchers.any(), new MethodInterceptor() {

                    @Override
                    public Object invoke(MethodInvocation methodInvocation) {
                        return null;
                    }
                });
                bind(A.class);
            }
        });
        fail();
    } catch (CreationException expected) {
        assertContains(expected.getMessage(), "No implementation for LineNumbersTest$B was bound.", "for 1st parameter b", "at LineNumbersTest$2.configure");
    }
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule) Test(org.junit.Test)

Example 47 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project guice by google.

the class ElementsTest method testBindIntercepor.

public void testBindIntercepor() {
    // Unavoidable since subclassesOf returns raw type
    @SuppressWarnings("rawtypes") final Matcher<Class> classMatcher = Matchers.subclassesOf(List.class);
    final Matcher<Object> methodMatcher = Matchers.any();
    final MethodInterceptor methodInterceptor = new MethodInterceptor() {

        @Override
        public Object invoke(MethodInvocation methodInvocation) {
            return null;
        }
    };
    checkModule(new AbstractModule() {

        @Override
        protected void configure() {
            bindInterceptor(classMatcher, methodMatcher, methodInterceptor);
        }
    }, new FailingElementVisitor() {

        @Override
        public Void visit(InterceptorBinding command) {
            assertSame(classMatcher, command.getClassMatcher());
            assertSame(methodMatcher, command.getMethodMatcher());
            assertEquals(Arrays.asList(methodInterceptor), command.getInterceptors());
            return null;
        }
    });
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) AbstractModule(com.google.inject.AbstractModule)

Example 48 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-security by spring-projects.

the class GlobalMethodSecurityConfigurationTests method methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled.

@Test
public void methodSecurityInterceptorUsesMetadataSourceBeanWhenProxyingDisabled() {
    this.spring.register(CustomMetadataSourceBeanProxyEnabledConfig.class).autowire();
    MethodSecurityInterceptor methodInterceptor = (MethodSecurityInterceptor) this.spring.getContext().getBean(MethodInterceptor.class);
    MethodSecurityMetadataSource methodSecurityMetadataSource = this.spring.getContext().getBean(MethodSecurityMetadataSource.class);
    assertThat(methodInterceptor.getSecurityMetadataSource()).isSameAs(methodSecurityMetadataSource);
}
Also used : MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) MethodSecurityInterceptor(org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor) MethodSecurityMetadataSource(org.springframework.security.access.method.MethodSecurityMetadataSource) Test(org.junit.jupiter.api.Test)

Example 49 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project shiro by apache.

the class ShiroAopModuleTest method testBindShiroInterceptor.

@Test
public void testBindShiroInterceptor() {
    ShiroAopModule underTest = new ShiroAopModule() {

        @Override
        protected void configureInterceptors(AnnotationResolver resolver) {
            bindShiroInterceptor(new MyAnnotationMethodInterceptor());
        }
    };
    List<Element> elements = Elements.getElements(underTest);
    for (Element element : elements) {
        if (element instanceof InterceptorBinding) {
            InterceptorBinding binding = (InterceptorBinding) element;
            assertTrue(binding.getClassMatcher().matches(getClass()));
            Method method = null;
            Class<? extends Annotation> theAnnotation = null;
            for (Class<? extends Annotation> annotation : protectedMethods.keySet()) {
                if (binding.getMethodMatcher().matches(protectedMethods.get(annotation))) {
                    method = protectedMethods.get(annotation);
                    theAnnotation = annotation;
                    protectedMethods.remove(annotation);
                    break;
                }
            }
            if (method == null) {
                fail("Did not expect interceptor binding " + binding.getInterceptors());
            }
            List<MethodInterceptor> interceptors = binding.getInterceptors();
            assertEquals(1, interceptors.size());
            assertTrue(interceptors.get(0) instanceof AopAllianceMethodInterceptorAdapter);
            assertTrue(interceptorTypes.get(theAnnotation).isInstance(((AopAllianceMethodInterceptorAdapter) interceptors.get(0)).shiroInterceptor));
        }
    }
    assertTrue("Not all interceptors were bound.", protectedMethods.isEmpty());
}
Also used : InterceptorBinding(com.google.inject.spi.InterceptorBinding) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) Element(com.google.inject.spi.Element) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 50 with MethodInterceptor

use of org.aopalliance.intercept.MethodInterceptor in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testDeclaredException.

@Test
public void testDeclaredException() throws Throwable {
    final Exception expectedException = new Exception();
    // Test return value
    MethodInterceptor mi = invocation -> {
        throw expectedException;
    };
    AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
    pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
    pc.addAdvice(mi);
    // We don't care about the object
    mockTargetSource.setTarget(new TestBean());
    pc.setTargetSource(mockTargetSource);
    AopProxy aop = createAopProxy(pc);
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        ITestBean tb = (ITestBean) aop.getProxy();
        // Note: exception param below isn't used
        tb.exceptional(expectedException);
    }).matches(expectedException::equals);
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StaticMethodMatcherPointcutAdvisor(org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor) LockMixin(test.mixin.LockMixin) AopUtils(org.springframework.aop.support.AopUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MyThrowsHandler(org.springframework.aop.testfixture.advice.MyThrowsHandler) TargetSource(org.springframework.aop.TargetSource) Pointcuts(org.springframework.aop.support.Pointcuts) Lockable(test.mixin.Lockable) DelegatingIntroductionInterceptor(org.springframework.aop.support.DelegatingIntroductionInterceptor) MethodInvocation(org.aopalliance.intercept.MethodInvocation) CountingAfterReturningAdvice(org.springframework.aop.testfixture.advice.CountingAfterReturningAdvice) Map(java.util.Map) TestBean(org.springframework.beans.testfixture.beans.TestBean) Method(java.lang.reflect.Method) CountingBeforeAdvice(org.springframework.aop.testfixture.advice.CountingBeforeAdvice) ThrowsAdvice(org.springframework.aop.ThrowsAdvice) ExposeInvocationInterceptor(org.springframework.aop.interceptor.ExposeInvocationInterceptor) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) MethodCounter(org.springframework.aop.testfixture.advice.MethodCounter) FileNotFoundException(java.io.FileNotFoundException) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test) List(java.util.List) LockMixinAdvisor(test.mixin.LockMixinAdvisor) SerializablePerson(org.springframework.beans.testfixture.beans.SerializablePerson) NopInterceptor(org.springframework.aop.testfixture.interceptor.NopInterceptor) TimestampIntroductionInterceptor(org.springframework.aop.testfixture.interceptor.TimestampIntroductionInterceptor) DefaultPointcutAdvisor(org.springframework.aop.support.DefaultPointcutAdvisor) HashMap(java.util.HashMap) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) ArrayList(java.util.ArrayList) TimeStamped(org.springframework.core.testfixture.TimeStamped) SQLException(java.sql.SQLException) DynamicIntroductionAdvice(org.springframework.aop.DynamicIntroductionAdvice) IOther(org.springframework.beans.testfixture.beans.IOther) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Advice(org.aopalliance.aop.Advice) MarshalException(java.rmi.MarshalException) Nullable(org.springframework.lang.Nullable) Advisor(org.springframework.aop.Advisor) SerializationTestUtils(org.springframework.core.testfixture.io.SerializationTestUtils) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) Iterator(java.util.Iterator) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) DynamicMethodMatcherPointcut(org.springframework.aop.support.DynamicMethodMatcherPointcut) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) NameMatchMethodPointcut(org.springframework.aop.support.NameMatchMethodPointcut) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) AfterEach(org.junit.jupiter.api.AfterEach) MethodBeforeAdvice(org.springframework.aop.MethodBeforeAdvice) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Person(org.springframework.beans.testfixture.beans.Person) DefaultIntroductionAdvisor(org.springframework.aop.support.DefaultIntroductionAdvisor) AfterReturningAdvice(org.springframework.aop.AfterReturningAdvice) SerializableNopInterceptor(org.springframework.aop.testfixture.interceptor.SerializableNopInterceptor) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor) TestBean(org.springframework.beans.testfixture.beans.TestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)56 Test (org.junit.Test)30 MethodInvocation (org.aopalliance.intercept.MethodInvocation)28 Method (java.lang.reflect.Method)19 List (java.util.List)19 ArrayList (java.util.ArrayList)18 Test (org.junit.jupiter.api.Test)15 Advice (org.aopalliance.aop.Advice)14 Map (java.util.Map)13 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)13 AopUtils (org.springframework.aop.support.AopUtils)12 TestBean (org.springframework.beans.testfixture.beans.TestBean)12 HashMap (java.util.HashMap)10 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)10 NopInterceptor (org.springframework.aop.testfixture.interceptor.NopInterceptor)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)9 Advisor (org.springframework.aop.Advisor)9 Message (org.springframework.messaging.Message)8 Assert.assertEquals (org.junit.Assert.assertEquals)7