Tests for enum array with varargs (and related refinements)
Issue: SPR-13328
This commit is contained in:
parent
1feb757c54
commit
a8432bc8a1
|
@ -76,7 +76,7 @@ public class AspectJProxyFactory extends ProxyCreatorSupport {
|
||||||
* Create a new {@code AspectJProxyFactory}.
|
* Create a new {@code AspectJProxyFactory}.
|
||||||
* No target, only interfaces. Must add interceptors.
|
* No target, only interfaces. Must add interceptors.
|
||||||
*/
|
*/
|
||||||
public AspectJProxyFactory(Class<?>[] interfaces) {
|
public AspectJProxyFactory(Class<?>... interfaces) {
|
||||||
setInterfaces(interfaces);
|
setInterfaces(interfaces);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,6 +16,9 @@
|
||||||
|
|
||||||
package org.springframework.aop.aspectj.annotation;
|
package org.springframework.aop.aspectj.annotation;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Around;
|
||||||
import org.aspectj.lang.annotation.Aspect;
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
@ -32,9 +35,9 @@ import static org.junit.Assert.*;
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
*/
|
*/
|
||||||
public final class AspectProxyFactoryTests {
|
public class AspectProxyFactoryTests {
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testWithNonAspect() {
|
public void testWithNonAspect() {
|
||||||
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
|
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
|
||||||
proxyFactory.addAspect(TestBean.class);
|
proxyFactory.addAspect(TestBean.class);
|
||||||
|
@ -70,7 +73,7 @@ public final class AspectProxyFactoryTests {
|
||||||
assertEquals(2, proxy1.getAge());
|
assertEquals(2, proxy1.getAge());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testWithInstanceWithNonAspect() throws Exception {
|
public void testWithInstanceWithNonAspect() throws Exception {
|
||||||
AspectJProxyFactory pf = new AspectJProxyFactory();
|
AspectJProxyFactory pf = new AspectJProxyFactory();
|
||||||
pf.addAspect(new TestBean());
|
pf.addAspect(new TestBean());
|
||||||
|
@ -96,14 +99,23 @@ public final class AspectProxyFactoryTests {
|
||||||
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
|
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=IllegalArgumentException.class)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
public void testWithNonSingletonAspectInstance() throws Exception {
|
public void testWithNonSingletonAspectInstance() throws Exception {
|
||||||
AspectJProxyFactory pf = new AspectJProxyFactory();
|
AspectJProxyFactory pf = new AspectJProxyFactory();
|
||||||
pf.addAspect(new PerThisAspect());
|
pf.addAspect(new PerThisAspect());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-13328
|
||||||
|
public void testVarargsWithEnumArray() throws Exception {
|
||||||
|
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
|
||||||
|
proxyFactory.addAspect(LoggingAspect.class);
|
||||||
|
proxyFactory.setProxyTargetClass(true);
|
||||||
|
TestBean proxy = proxyFactory.getProxy();
|
||||||
|
assertTrue(proxy.doWithVarargs(MyEnum.A, MyEnum.B));
|
||||||
|
}
|
||||||
|
|
||||||
public static interface ITestBean {
|
|
||||||
|
public interface ITestBean {
|
||||||
|
|
||||||
int getAge();
|
int getAge();
|
||||||
}
|
}
|
||||||
|
@ -121,8 +133,32 @@ public final class AspectProxyFactoryTests {
|
||||||
public void setAge(int age) {
|
public void setAge(int age) {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <V extends MyInterface> boolean doWithVarargs(V... args) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface MyInterface {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public enum MyEnum implements MyInterface {
|
||||||
|
|
||||||
|
A, B;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
public static class LoggingAspect {
|
||||||
|
|
||||||
|
@Around("execution(* doWithVarargs(*))")
|
||||||
|
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
LogFactory.getLog(LoggingAspect.class).debug(Arrays.asList(pjp.getArgs()));
|
||||||
|
return pjp.proceed();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2014 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -49,7 +49,7 @@ import static org.junit.Assert.*;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public final class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
|
public class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
|
||||||
|
|
||||||
private static final String DEPENDENCY_CHECK_CONTEXT =
|
private static final String DEPENDENCY_CHECK_CONTEXT =
|
||||||
CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
|
CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
|
||||||
|
@ -74,6 +74,7 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNullConfig() {
|
public void testNullConfig() {
|
||||||
try {
|
try {
|
||||||
|
@ -402,6 +403,13 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
|
||||||
assertEquals(4, proxy.add(1, 3));
|
assertEquals(4, proxy.add(1, 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-13328
|
||||||
|
public void testVarargsWithEnumArray() throws Exception {
|
||||||
|
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
|
||||||
|
MyBean proxy = (MyBean) proxyFactory.getProxy();
|
||||||
|
assertTrue(proxy.doWithVarargs(MyEnum.A, MyEnum.B));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class MyBean {
|
public static class MyBean {
|
||||||
|
|
||||||
|
@ -418,6 +426,20 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
|
||||||
protected int add(int x, int y) {
|
protected int add(int x, int y) {
|
||||||
return x + y;
|
return x + y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <V extends MyInterface> boolean doWithVarargs(V... args) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public interface MyInterface {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public enum MyEnum implements MyInterface {
|
||||||
|
|
||||||
|
A, B;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2015 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -20,6 +20,7 @@ import java.io.Serializable;
|
||||||
|
|
||||||
import org.aopalliance.intercept.MethodInterceptor;
|
import org.aopalliance.intercept.MethodInterceptor;
|
||||||
import org.aopalliance.intercept.MethodInvocation;
|
import org.aopalliance.intercept.MethodInvocation;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
|
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
|
||||||
import org.springframework.aop.support.AopUtils;
|
import org.springframework.aop.support.AopUtils;
|
||||||
|
@ -28,7 +29,6 @@ import org.springframework.tests.sample.beans.ITestBean;
|
||||||
import org.springframework.tests.sample.beans.TestBean;
|
import org.springframework.tests.sample.beans.TestBean;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.BDDMockito.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @since 13.03.2003
|
* @since 13.03.2003
|
||||||
|
@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.*;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("serial")
|
@SuppressWarnings("serial")
|
||||||
public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
|
public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object createProxy(ProxyCreatorSupport as) {
|
protected Object createProxy(ProxyCreatorSupport as) {
|
||||||
|
@ -52,6 +52,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
return new JdkDynamicAopProxy(as);
|
return new JdkDynamicAopProxy(as);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testNullConfig() {
|
public void testNullConfig() {
|
||||||
try {
|
try {
|
||||||
new JdkDynamicAopProxy(null);
|
new JdkDynamicAopProxy(null);
|
||||||
|
@ -62,6 +64,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testProxyIsJustInterface() throws Throwable {
|
public void testProxyIsJustInterface() throws Throwable {
|
||||||
TestBean raw = new TestBean();
|
TestBean raw = new TestBean();
|
||||||
raw.setAge(32);
|
raw.setAge(32);
|
||||||
|
@ -74,32 +77,32 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
assertTrue(!(proxy instanceof TestBean));
|
assertTrue(!(proxy instanceof TestBean));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
|
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
|
||||||
// Test return value
|
// Test return value
|
||||||
int age = 25;
|
final Integer age = 25;
|
||||||
MethodInterceptor mi = mock(MethodInterceptor.class);
|
MethodInterceptor mi = (invocation -> age);
|
||||||
|
|
||||||
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
|
||||||
pc.addAdvice(mi);
|
pc.addAdvice(mi);
|
||||||
AopProxy aop = createAopProxy(pc);
|
AopProxy aop = createAopProxy(pc);
|
||||||
|
|
||||||
given(mi.invoke(null)).willReturn(age);
|
|
||||||
|
|
||||||
ITestBean tb = (ITestBean) aop.getProxy();
|
ITestBean tb = (ITestBean) aop.getProxy();
|
||||||
assertTrue("correct return value", tb.getAge() == age);
|
assertTrue("correct return value", tb.getAge() == age);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
|
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
|
||||||
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
|
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
|
||||||
@Override
|
@Override
|
||||||
protected void assertions(MethodInvocation invocation) {
|
protected void assertions(MethodInvocation invocation) {
|
||||||
assertTrue(invocation.getThis() == this);
|
assertTrue(invocation.getThis() == this);
|
||||||
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
|
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
|
||||||
invocation.getMethod().getDeclaringClass() == ITestBean.class);
|
invocation.getMethod().getDeclaringClass() == ITestBean.class);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class, IOther.class });
|
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class});
|
||||||
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
|
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
|
||||||
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
|
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -126,10 +129,11 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
//assertTrue(target.invocation == tii.invocation);
|
//assertTrue(target.invocation == tii.invocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testProxyNotWrappedIfIncompatible() {
|
public void testProxyNotWrappedIfIncompatible() {
|
||||||
FooBar bean = new FooBar();
|
FooBar bean = new FooBar();
|
||||||
ProxyCreatorSupport as = new ProxyCreatorSupport();
|
ProxyCreatorSupport as = new ProxyCreatorSupport();
|
||||||
as.setInterfaces(new Class<?>[] {Foo.class});
|
as.setInterfaces(Foo.class);
|
||||||
as.setTarget(bean);
|
as.setTarget(bean);
|
||||||
|
|
||||||
Foo proxy = (Foo) createProxy(as);
|
Foo proxy = (Foo) createProxy(as);
|
||||||
|
@ -138,6 +142,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
public void testEqualsAndHashCodeDefined() throws Exception {
|
public void testEqualsAndHashCodeDefined() throws Exception {
|
||||||
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
|
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
|
||||||
as.setTarget(new Person());
|
as.setTarget(new Person());
|
||||||
|
@ -149,7 +154,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static interface Foo {
|
public interface Foo {
|
||||||
|
|
||||||
Bar getBarThis();
|
Bar getBarThis();
|
||||||
|
|
||||||
|
@ -157,8 +162,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static interface Bar {
|
public interface Bar {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -176,7 +180,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static interface Named {
|
public interface Named {
|
||||||
|
|
||||||
String getName();
|
String getName();
|
||||||
|
|
||||||
|
@ -201,11 +205,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Person person = (Person) o;
|
||||||
final Person person = (Person) o;
|
|
||||||
|
|
||||||
if (!name.equals(person.name)) return false;
|
if (!name.equals(person.name)) return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue