Tests for enum array with varargs (and related refinements)

Issue: SPR-13328
This commit is contained in:
Juergen Hoeller 2015-09-08 11:13:23 +02:00
parent 1feb757c54
commit a8432bc8a1
4 changed files with 87 additions and 28 deletions

View File

@ -76,7 +76,7 @@ public class AspectJProxyFactory extends ProxyCreatorSupport {
* Create a new {@code AspectJProxyFactory}.
* No target, only interfaces. Must add interceptors.
*/
public AspectJProxyFactory(Class<?>[] interfaces) {
public AspectJProxyFactory(Class<?>... interfaces) {
setInterfaces(interfaces);
}

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -16,6 +16,9 @@
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.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@ -32,9 +35,9 @@ import static org.junit.Assert.*;
* @author Juergen Hoeller
* @author Chris Beams
*/
public final class AspectProxyFactoryTests {
public class AspectProxyFactoryTests {
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void testWithNonAspect() {
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
proxyFactory.addAspect(TestBean.class);
@ -70,7 +73,7 @@ public final class AspectProxyFactoryTests {
assertEquals(2, proxy1.getAge());
}
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void testWithInstanceWithNonAspect() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory();
pf.addAspect(new TestBean());
@ -96,14 +99,23 @@ public final class AspectProxyFactoryTests {
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
}
@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void testWithNonSingletonAspectInstance() throws Exception {
AspectJProxyFactory pf = new AspectJProxyFactory();
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();
}
@ -121,8 +133,32 @@ public final class AspectProxyFactoryTests {
public void setAge(int 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();
}
}
}

View File

@ -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");
* you may not use this file except in compliance with the License.
@ -49,7 +49,7 @@ import static org.junit.Assert.*;
* @author Chris Beams
*/
@SuppressWarnings("serial")
public final class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
public class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
private static final String DEPENDENCY_CHECK_CONTEXT =
CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
@ -74,6 +74,7 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
return true;
}
@Test
public void testNullConfig() {
try {
@ -402,6 +403,13 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
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 {
@ -418,6 +426,20 @@ public final class CglibProxyTests extends AbstractAopProxyTests implements Seri
protected int add(int x, int y) {
return x + y;
}
public <V extends MyInterface> boolean doWithVarargs(V... args) {
return true;
}
}
public interface MyInterface {
}
public enum MyEnum implements MyInterface {
A, B;
}

View File

@ -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");
* 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.MethodInvocation;
import org.junit.Test;
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
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 static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @since 13.03.2003
@ -37,7 +37,7 @@ import static org.mockito.BDDMockito.*;
* @author Chris Beams
*/
@SuppressWarnings("serial")
public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
@Override
protected Object createProxy(ProxyCreatorSupport as) {
@ -52,6 +52,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
return new JdkDynamicAopProxy(as);
}
@Test
public void testNullConfig() {
try {
new JdkDynamicAopProxy(null);
@ -62,6 +64,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
}
}
@Test
public void testProxyIsJustInterface() throws Throwable {
TestBean raw = new TestBean();
raw.setAge(32);
@ -74,32 +77,32 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
assertTrue(!(proxy instanceof TestBean));
}
@Test
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
// Test return value
int age = 25;
MethodInterceptor mi = mock(MethodInterceptor.class);
final Integer age = 25;
MethodInterceptor mi = (invocation -> age);
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
pc.addAdvice(mi);
AopProxy aop = createAopProxy(pc);
given(mi.invoke(null)).willReturn(age);
ITestBean tb = (ITestBean) aop.getProxy();
assertTrue("correct return value", tb.getAge() == age);
}
@Test
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
@Override
protected void assertions(MethodInvocation invocation) {
assertTrue(invocation.getThis() == this);
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);
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
@Override
@ -126,10 +129,11 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
//assertTrue(target.invocation == tii.invocation);
}
@Test
public void testProxyNotWrappedIfIncompatible() {
FooBar bean = new FooBar();
ProxyCreatorSupport as = new ProxyCreatorSupport();
as.setInterfaces(new Class<?>[] {Foo.class});
as.setInterfaces(Foo.class);
as.setTarget(bean);
Foo proxy = (Foo) createProxy(as);
@ -138,6 +142,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
}
@Test
public void testEqualsAndHashCodeDefined() throws Exception {
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
as.setTarget(new Person());
@ -149,7 +154,7 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
}
public static interface Foo {
public interface Foo {
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();
@ -201,11 +205,8 @@ public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Person person = (Person) o;
Person person = (Person) o;
if (!name.equals(person.name)) return false;
return true;
}