Skip to content

Commit a8432bc

Browse files
committed
Tests for enum array with varargs (and related refinements)
Issue: SPR-13328
1 parent 1feb757 commit a8432bc

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public AspectJProxyFactory(Object target) {
7676
* Create a new {@code AspectJProxyFactory}.
7777
* No target, only interfaces. Must add interceptors.
7878
*/
79-
public AspectJProxyFactory(Class<?>[] interfaces) {
79+
public AspectJProxyFactory(Class<?>... interfaces) {
8080
setInterfaces(interfaces);
8181
}
8282

spring-aop/src/test/java/org/springframework/aop/aspectj/annotation/AspectProxyFactoryTests.java

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,9 @@
1616

1717
package org.springframework.aop.aspectj.annotation;
1818

19+
import java.util.Arrays;
20+
21+
import org.apache.commons.logging.LogFactory;
1922
import org.aspectj.lang.ProceedingJoinPoint;
2023
import org.aspectj.lang.annotation.Around;
2124
import org.aspectj.lang.annotation.Aspect;
@@ -32,9 +35,9 @@
3235
* @author Juergen Hoeller
3336
* @author Chris Beams
3437
*/
35-
public final class AspectProxyFactoryTests {
38+
public class AspectProxyFactoryTests {
3639

37-
@Test(expected=IllegalArgumentException.class)
40+
@Test(expected = IllegalArgumentException.class)
3841
public void testWithNonAspect() {
3942
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
4043
proxyFactory.addAspect(TestBean.class);
@@ -70,7 +73,7 @@ public void testWithPerThisAspect() throws Exception {
7073
assertEquals(2, proxy1.getAge());
7174
}
7275

73-
@Test(expected=IllegalArgumentException.class)
76+
@Test(expected = IllegalArgumentException.class)
7477
public void testWithInstanceWithNonAspect() throws Exception {
7578
AspectJProxyFactory pf = new AspectJProxyFactory();
7679
pf.addAspect(new TestBean());
@@ -96,14 +99,23 @@ public void testWithInstance() throws Exception {
9699
assertEquals(target.getAge() * multiple, serializedProxy.getAge());
97100
}
98101

99-
@Test(expected=IllegalArgumentException.class)
102+
@Test(expected = IllegalArgumentException.class)
100103
public void testWithNonSingletonAspectInstance() throws Exception {
101104
AspectJProxyFactory pf = new AspectJProxyFactory();
102105
pf.addAspect(new PerThisAspect());
103106
}
104107

108+
@Test // SPR-13328
109+
public void testVarargsWithEnumArray() throws Exception {
110+
AspectJProxyFactory proxyFactory = new AspectJProxyFactory(new TestBean());
111+
proxyFactory.addAspect(LoggingAspect.class);
112+
proxyFactory.setProxyTargetClass(true);
113+
TestBean proxy = proxyFactory.getProxy();
114+
assertTrue(proxy.doWithVarargs(MyEnum.A, MyEnum.B));
115+
}
105116

106-
public static interface ITestBean {
117+
118+
public interface ITestBean {
107119

108120
int getAge();
109121
}
@@ -121,8 +133,32 @@ public int getAge() {
121133
public void setAge(int age) {
122134
this.age = age;
123135
}
136+
137+
public <V extends MyInterface> boolean doWithVarargs(V... args) {
138+
return true;
139+
}
140+
}
141+
142+
143+
public interface MyInterface {
124144
}
125145

146+
147+
public enum MyEnum implements MyInterface {
148+
149+
A, B;
150+
}
151+
152+
153+
@Aspect
154+
public static class LoggingAspect {
155+
156+
@Around("execution(* doWithVarargs(*))")
157+
public Object doLog(ProceedingJoinPoint pjp) throws Throwable {
158+
LogFactory.getLog(LoggingAspect.class).debug(Arrays.asList(pjp.getArgs()));
159+
return pjp.proceed();
160+
}
161+
}
126162
}
127163

128164

spring-context/src/test/java/org/springframework/aop/framework/CglibProxyTests.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,7 @@
4949
* @author Chris Beams
5050
*/
5151
@SuppressWarnings("serial")
52-
public final class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
52+
public class CglibProxyTests extends AbstractAopProxyTests implements Serializable {
5353

5454
private static final String DEPENDENCY_CHECK_CONTEXT =
5555
CglibProxyTests.class.getSimpleName() + "-with-dependency-checking.xml";
@@ -74,6 +74,7 @@ protected boolean requiresTarget() {
7474
return true;
7575
}
7676

77+
7778
@Test
7879
public void testNullConfig() {
7980
try {
@@ -402,6 +403,13 @@ public void testProxyTargetClassInCaseOfNoInterfaces() throws Exception {
402403
assertEquals(4, proxy.add(1, 3));
403404
}
404405

406+
@Test // SPR-13328
407+
public void testVarargsWithEnumArray() throws Exception {
408+
ProxyFactory proxyFactory = new ProxyFactory(new MyBean());
409+
MyBean proxy = (MyBean) proxyFactory.getProxy();
410+
assertTrue(proxy.doWithVarargs(MyEnum.A, MyEnum.B));
411+
}
412+
405413

406414
public static class MyBean {
407415

@@ -418,6 +426,20 @@ public void setName(String name) {
418426
protected int add(int x, int y) {
419427
return x + y;
420428
}
429+
430+
public <V extends MyInterface> boolean doWithVarargs(V... args) {
431+
return true;
432+
}
433+
}
434+
435+
436+
public interface MyInterface {
437+
}
438+
439+
440+
public enum MyEnum implements MyInterface {
441+
442+
A, B;
421443
}
422444

423445

spring-context/src/test/java/org/springframework/aop/framework/JdkDynamicProxyTests.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020

2121
import org.aopalliance.intercept.MethodInterceptor;
2222
import org.aopalliance.intercept.MethodInvocation;
23+
import org.junit.Test;
2324

2425
import org.springframework.aop.interceptor.ExposeInvocationInterceptor;
2526
import org.springframework.aop.support.AopUtils;
@@ -28,7 +29,6 @@
2829
import org.springframework.tests.sample.beans.TestBean;
2930

3031
import static org.junit.Assert.*;
31-
import static org.mockito.BDDMockito.*;
3232

3333
/**
3434
* @since 13.03.2003
@@ -37,7 +37,7 @@
3737
* @author Chris Beams
3838
*/
3939
@SuppressWarnings("serial")
40-
public final class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
40+
public class JdkDynamicProxyTests extends AbstractAopProxyTests implements Serializable {
4141

4242
@Override
4343
protected Object createProxy(ProxyCreatorSupport as) {
@@ -52,6 +52,8 @@ protected AopProxy createAopProxy(AdvisedSupport as) {
5252
return new JdkDynamicAopProxy(as);
5353
}
5454

55+
56+
@Test
5557
public void testNullConfig() {
5658
try {
5759
new JdkDynamicAopProxy(null);
@@ -62,6 +64,7 @@ public void testNullConfig() {
6264
}
6365
}
6466

67+
@Test
6568
public void testProxyIsJustInterface() throws Throwable {
6669
TestBean raw = new TestBean();
6770
raw.setAge(32);
@@ -74,32 +77,32 @@ public void testProxyIsJustInterface() throws Throwable {
7477
assertTrue(!(proxy instanceof TestBean));
7578
}
7679

80+
@Test
7781
public void testInterceptorIsInvokedWithNoTarget() throws Throwable {
7882
// Test return value
79-
int age = 25;
80-
MethodInterceptor mi = mock(MethodInterceptor.class);
83+
final Integer age = 25;
84+
MethodInterceptor mi = (invocation -> age);
8185

82-
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
86+
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
8387
pc.addAdvice(mi);
8488
AopProxy aop = createAopProxy(pc);
8589

86-
given(mi.invoke(null)).willReturn(age);
87-
8890
ITestBean tb = (ITestBean) aop.getProxy();
8991
assertTrue("correct return value", tb.getAge() == age);
9092
}
9193

94+
@Test
9295
public void testTargetCanGetInvocationWithPrivateClass() throws Throwable {
9396
final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() {
9497
@Override
9598
protected void assertions(MethodInvocation invocation) {
9699
assertTrue(invocation.getThis() == this);
97100
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
98-
invocation.getMethod().getDeclaringClass() == ITestBean.class);
101+
invocation.getMethod().getDeclaringClass() == ITestBean.class);
99102
}
100103
};
101104

102-
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class, IOther.class });
105+
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class, IOther.class});
103106
pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
104107
TrapTargetInterceptor tii = new TrapTargetInterceptor() {
105108
@Override
@@ -126,10 +129,11 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
126129
//assertTrue(target.invocation == tii.invocation);
127130
}
128131

132+
@Test
129133
public void testProxyNotWrappedIfIncompatible() {
130134
FooBar bean = new FooBar();
131135
ProxyCreatorSupport as = new ProxyCreatorSupport();
132-
as.setInterfaces(new Class<?>[] {Foo.class});
136+
as.setInterfaces(Foo.class);
133137
as.setTarget(bean);
134138

135139
Foo proxy = (Foo) createProxy(as);
@@ -138,6 +142,7 @@ public void testProxyNotWrappedIfIncompatible() {
138142

139143
}
140144

145+
@Test
141146
public void testEqualsAndHashCodeDefined() throws Exception {
142147
AdvisedSupport as = new AdvisedSupport(new Class<?>[]{Named.class});
143148
as.setTarget(new Person());
@@ -149,16 +154,15 @@ public void testEqualsAndHashCodeDefined() throws Exception {
149154
}
150155

151156

152-
public static interface Foo {
157+
public interface Foo {
153158

154159
Bar getBarThis();
155160

156161
Foo getFooThis();
157162
}
158163

159164

160-
public static interface Bar {
161-
165+
public interface Bar {
162166
}
163167

164168

@@ -176,7 +180,7 @@ public Foo getFooThis() {
176180
}
177181

178182

179-
public static interface Named {
183+
public interface Named {
180184

181185
String getName();
182186

@@ -201,11 +205,8 @@ public String getName() {
201205
public boolean equals(Object o) {
202206
if (this == o) return true;
203207
if (o == null || getClass() != o.getClass()) return false;
204-
205-
final Person person = (Person) o;
206-
208+
Person person = (Person) o;
207209
if (!name.equals(person.name)) return false;
208-
209210
return true;
210211
}
211212

0 commit comments

Comments
 (0)