Skip to content

Commit 22e84a8

Browse files
committed
Polish proxy tests
1 parent ae8a341 commit 22e84a8

File tree

4 files changed

+54
-106
lines changed

4 files changed

+54
-106
lines changed

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

Lines changed: 32 additions & 59 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.
@@ -28,17 +28,13 @@
2828
import java.util.List;
2929
import java.util.Map;
3030

31-
import junit.framework.TestCase;
3231
import org.aopalliance.aop.Advice;
3332
import org.aopalliance.intercept.MethodInterceptor;
3433
import org.aopalliance.intercept.MethodInvocation;
34+
3535
import org.junit.After;
3636
import org.junit.Before;
3737
import org.junit.Test;
38-
import test.mixin.LockMixin;
39-
import test.mixin.LockMixinAdvisor;
40-
import test.mixin.Lockable;
41-
import test.mixin.LockedException;
4238

4339
import org.springframework.aop.Advisor;
4440
import org.springframework.aop.AfterReturningAdvice;
@@ -76,6 +72,11 @@
7672
import org.springframework.util.SerializationTestUtils;
7773
import org.springframework.util.StopWatch;
7874

75+
import test.mixin.LockMixin;
76+
import test.mixin.LockMixinAdvisor;
77+
import test.mixin.Lockable;
78+
import test.mixin.LockedException;
79+
7980
import static org.junit.Assert.*;
8081

8182
/**
@@ -120,18 +121,12 @@ protected boolean requiresTarget() {
120121
}
121122

122123

123-
@Test
124+
@Test(expected = AopConfigException.class)
124125
public void testNoInterceptorsAndNoTarget() {
125-
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
126+
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
126127
// Add no interceptors
127-
try {
128-
AopProxy aop = createAopProxy(pc);
129-
aop.getProxy();
130-
fail("Shouldn't allow no interceptors");
131-
}
132-
catch (AopConfigException ex) {
133-
// Ok
134-
}
128+
AopProxy aop = createAopProxy(pc);
129+
aop.getProxy();
135130
}
136131

137132
/**
@@ -171,7 +166,6 @@ public void testManyProxies() {
171166
sw.start("Create " + howMany + " proxies");
172167
testManyProxies(howMany);
173168
sw.stop();
174-
System.out.println(sw.getTotalTimeMillis());
175169
assertTrue("Proxy creation was too slow", sw.getTotalTimeMillis() < 5000);
176170
}
177171

@@ -243,7 +237,7 @@ public void testSerializationSerializableTargetAndAdvice() throws Throwable {
243237
p.echo(new IOException());
244238
}
245239
catch (IOException ex) {
246-
240+
/* expected */
247241
}
248242
assertEquals(1, cta.getCalls());
249243

@@ -281,7 +275,6 @@ public void testSerializationSerializableTargetAndAdvice() throws Throwable {
281275

282276
}
283277
assertEquals(2, cta.getCalls());
284-
285278
}
286279

287280
/**
@@ -317,7 +310,6 @@ public void testOneAdvisedObjectCallsAnother() {
317310
pf2.addAdvice(2, new CheckMethodInvocationIsSameInAndOutInterceptor());
318311
pf2.addAdvice(1, new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor());
319312
pf2.addAdvice(0, ExposeInvocationInterceptor.INSTANCE);
320-
//System.err.println(pf2.toProxyConfigString());
321313
ITestBean advised2 = (ITestBean) createProxy(pf2);
322314
advised2.setAge(age2);
323315
advised1.setSpouse(advised2); // = 2 invocations
@@ -379,20 +371,14 @@ public void testTargetCanGetProxy() {
379371
assertEquals("3 more invocations via AOP as the first call was reentrant through the proxy", 4, di.getCount());
380372
}
381373

382-
383-
@Test
374+
@Test(expected = IllegalStateException.class)
375+
// Should fail to get proxy as exposeProxy wasn't set to true
384376
public void testTargetCantGetProxyByDefault() {
385377
NeedsToSeeProxy et = new NeedsToSeeProxy();
386378
ProxyFactory pf1 = new ProxyFactory(et);
387379
assertFalse(pf1.isExposeProxy());
388380
INeedsToSeeProxy proxied = (INeedsToSeeProxy) createProxy(pf1);
389-
try {
390-
proxied.incrementViaProxy();
391-
fail("Should have failed to get proxy as exposeProxy wasn't set to true");
392-
}
393-
catch (IllegalStateException ex) {
394-
// Ok
395-
}
381+
proxied.incrementViaProxy();
396382
}
397383

398384
@Test
@@ -417,7 +403,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
417403
if (!context) {
418404
assertNoInvocationContext();
419405
} else {
420-
assertTrue("have context", ExposeInvocationInterceptor.currentInvocation() != null);
406+
assertNotNull("have context", ExposeInvocationInterceptor.currentInvocation());
421407
}
422408
return s;
423409
}
@@ -436,7 +422,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
436422
assertNoInvocationContext();
437423
ITestBean tb = (ITestBean) aop.getProxy();
438424
assertNoInvocationContext();
439-
assertTrue("correct return value", tb.getName() == s);
425+
assertSame("correct return value", s, tb.getName());
440426
}
441427

442428
/**
@@ -453,7 +439,7 @@ public void testTargetReturnsThis() throws Throwable {
453439
pc.setTarget(raw);
454440

455441
ITestBean tb = (ITestBean) createProxy(pc);
456-
assertTrue("this return is wrapped in proxy", tb.getSpouse() == tb);
442+
assertSame("this return is wrapped in proxy", tb, tb.getSpouse());
457443
}
458444

459445
@Test
@@ -593,15 +579,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
593579

594580
ITestBean tb = (ITestBean) aop.getProxy();
595581
tb.getName();
596-
// Not safe to trap invocation
597-
//assertTrue(tii.invocation == target.invocation);
598-
599-
//assertTrue(target.invocation.getProxy() == tb);
600-
601-
// ((IOther) tb).absquatulate();
602-
//MethodInvocation minv = tii.invocation;
603-
//assertTrue("invoked on iother, not " + minv.getMethod().getDeclaringClass(), minv.getMethod().getDeclaringClass() == IOther.class);
604-
//assertTrue(target.invocation == tii.invocation);
605582
}
606583

607584
/**
@@ -645,30 +622,29 @@ private void testTestBeanIntroduction(ProxyFactory pc) {
645622
int newAge = 65;
646623
ITestBean itb = (ITestBean) createProxy(pc);
647624
itb.setAge(newAge);
648-
assertTrue(itb.getAge() == newAge);
625+
assertEquals(newAge, itb.getAge());
649626

650627
Lockable lockable = (Lockable) itb;
651628
assertFalse(lockable.locked());
652629
lockable.lock();
653630

654-
assertTrue(itb.getAge() == newAge);
631+
assertEquals(newAge, itb.getAge());
655632
try {
656633
itb.setAge(1);
657634
fail("Setters should fail when locked");
658635
}
659636
catch (LockedException ex) {
660637
// ok
661638
}
662-
assertTrue(itb.getAge() == newAge);
639+
assertEquals(newAge, itb.getAge());
663640

664641
// Unlock
665642
assertTrue(lockable.locked());
666643
lockable.unlock();
667644
itb.setAge(1);
668-
assertTrue(itb.getAge() == 1);
645+
assertEquals(1, itb.getAge());
669646
}
670647

671-
672648
@Test
673649
public void testReplaceArgument() throws Throwable {
674650
TestBean tb = new TestBean();
@@ -679,14 +655,14 @@ public void testReplaceArgument() throws Throwable {
679655
ITestBean t = (ITestBean) pc.getProxy();
680656
int newAge = 5;
681657
t.setAge(newAge);
682-
assertTrue(t.getAge() == newAge);
658+
assertEquals(newAge, t.getAge());
683659
String newName = "greg";
684660
t.setName(newName);
685661
assertEquals(newName, t.getName());
686662

687663
t.setName(null);
688664
// Null replacement magic should work
689-
assertTrue(t.getName().equals(""));
665+
assertEquals("", t.getName());
690666
}
691667

692668
@Test
@@ -1310,9 +1286,9 @@ public void testEquals() {
13101286
IOther proxyB = (IOther) createProxy(pfb);
13111287

13121288
assertEquals(pfa.getAdvisors().length, pfb.getAdvisors().length);
1313-
assertTrue(a.equals(b));
1314-
assertTrue(i1.equals(i2));
1315-
assertTrue(proxyA.equals(proxyB));
1289+
assertEquals(a, b);
1290+
assertEquals(i1, i2);
1291+
assertEquals(proxyA, proxyB);
13161292
assertEquals(proxyA.hashCode(), proxyB.hashCode());
13171293
assertFalse(proxyA.equals(a));
13181294

@@ -1682,9 +1658,7 @@ private static class ProxyMatcherInterceptor implements MethodInterceptor {
16821658
public Object invoke(MethodInvocation mi) throws Throwable {
16831659
Object proxy = AopContext.currentProxy();
16841660
Object ret = mi.proceed();
1685-
// TODO why does this cause stack overflow?
1686-
//assertEquals(proxy, AopContext.currentProxy());
1687-
assertTrue(proxy == AopContext.currentProxy());
1661+
assertEquals(proxy, AopContext.currentProxy());
16881662
return ret;
16891663
}
16901664
}
@@ -1785,7 +1759,7 @@ public boolean matches(Method m, Class<?> targetClass) {
17851759
* Note that trapping the Invocation as in previous version of this test
17861760
* isn't safe, as invocations may be reused
17871761
* and hence cleared at the end of each invocation.
1788-
* So we trap only the targe.
1762+
* So we trap only the target.
17891763
*/
17901764
protected static class TrapTargetInterceptor implements MethodInterceptor {
17911765

@@ -2105,11 +2079,10 @@ public void absquatulate() {
21052079
static class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean {
21062080
@Override
21072081
protected void assertions(MethodInvocation invocation) {
2108-
TestCase.assertTrue(invocation.getThis() == this);
2109-
TestCase.assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
2110-
ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass()));
2082+
assertSame(this, invocation.getThis());
2083+
assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(),
2084+
ITestBean.class.isAssignableFrom(invocation.getMethod().getDeclaringClass()));
21112085
}
21122086
}
21132087

21142088
}
2115-

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

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.aopalliance.intercept.MethodInterceptor;
2222
import org.aopalliance.intercept.MethodInvocation;
2323
import org.junit.Test;
24+
2425
import test.mixin.LockMixinAdvisor;
2526

2627
import org.springframework.aop.ClassFilter;
@@ -74,30 +75,17 @@ protected boolean requiresTarget() {
7475
return true;
7576
}
7677

77-
78-
@Test
78+
@Test(expected = IllegalArgumentException.class)
7979
public void testNullConfig() {
80-
try {
81-
new CglibAopProxy(null);
82-
fail("Shouldn't allow null interceptors");
83-
}
84-
catch (IllegalArgumentException ex) {
85-
// Ok
86-
}
80+
new CglibAopProxy(null);
8781
}
8882

89-
@Test
83+
@Test(expected = AopConfigException.class)
9084
public void testNoTarget() {
91-
AdvisedSupport pc = new AdvisedSupport(new Class<?>[]{ITestBean.class});
85+
AdvisedSupport pc = new AdvisedSupport(new Class<?>[] { ITestBean.class });
9286
pc.addAdvice(new NopInterceptor());
93-
try {
94-
AopProxy aop = createAopProxy(pc);
95-
aop.getProxy();
96-
fail("Shouldn't allow no target with CGLIB proxy");
97-
}
98-
catch (AopConfigException ex) {
99-
// Ok
100-
}
87+
AopProxy aop = createAopProxy(pc);
88+
aop.getProxy();
10189
}
10290

10391
@Test
@@ -210,7 +198,7 @@ public void testMultipleProxies() {
210198

211199
ITestBean proxy1 = getAdvisedProxy(target);
212200
ITestBean proxy2 = getAdvisedProxy(target2);
213-
assertTrue(proxy1.getClass() == proxy2.getClass());
201+
assertSame(proxy1.getClass(), proxy2.getClass());
214202
assertEquals(target.getAge(), proxy1.getAge());
215203
assertEquals(target2.getAge(), proxy2.getAge());
216204
}
@@ -256,7 +244,7 @@ public void testMultipleProxiesForIntroductionAdvisor() {
256244

257245
ITestBean proxy1 = getIntroductionAdvisorProxy(target);
258246
ITestBean proxy2 = getIntroductionAdvisorProxy(target2);
259-
assertTrue("Incorrect duplicate creation of proxy classes", proxy1.getClass() == proxy2.getClass());
247+
assertSame("Incorrect duplicate creation of proxy classes", proxy1.getClass(), proxy2.getClass());
260248
}
261249

262250
private ITestBean getIntroductionAdvisorProxy(TestBean target) {
@@ -427,6 +415,7 @@ protected int add(int x, int y) {
427415
return x + y;
428416
}
429417

418+
@SuppressWarnings("unchecked")
430419
public <V extends MyInterface> boolean doWithVarargs(V... args) {
431420
return true;
432421
}

0 commit comments

Comments
 (0)