1
1
/*
2
- * Copyright 2002-2014 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
28
28
import java .util .List ;
29
29
import java .util .Map ;
30
30
31
- import junit .framework .TestCase ;
32
31
import org .aopalliance .aop .Advice ;
33
32
import org .aopalliance .intercept .MethodInterceptor ;
34
33
import org .aopalliance .intercept .MethodInvocation ;
34
+
35
35
import org .junit .After ;
36
36
import org .junit .Before ;
37
37
import org .junit .Test ;
38
- import test .mixin .LockMixin ;
39
- import test .mixin .LockMixinAdvisor ;
40
- import test .mixin .Lockable ;
41
- import test .mixin .LockedException ;
42
38
43
39
import org .springframework .aop .Advisor ;
44
40
import org .springframework .aop .AfterReturningAdvice ;
76
72
import org .springframework .util .SerializationTestUtils ;
77
73
import org .springframework .util .StopWatch ;
78
74
75
+ import test .mixin .LockMixin ;
76
+ import test .mixin .LockMixinAdvisor ;
77
+ import test .mixin .Lockable ;
78
+ import test .mixin .LockedException ;
79
+
79
80
import static org .junit .Assert .*;
80
81
81
82
/**
@@ -120,18 +121,12 @@ protected boolean requiresTarget() {
120
121
}
121
122
122
123
123
- @ Test
124
+ @ Test ( expected = AopConfigException . class )
124
125
public void testNoInterceptorsAndNoTarget () {
125
- AdvisedSupport pc = new AdvisedSupport (new Class <?>[] {ITestBean .class });
126
+ AdvisedSupport pc = new AdvisedSupport (new Class <?>[] { ITestBean .class });
126
127
// 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 ();
135
130
}
136
131
137
132
/**
@@ -171,7 +166,6 @@ public void testManyProxies() {
171
166
sw .start ("Create " + howMany + " proxies" );
172
167
testManyProxies (howMany );
173
168
sw .stop ();
174
- System .out .println (sw .getTotalTimeMillis ());
175
169
assertTrue ("Proxy creation was too slow" , sw .getTotalTimeMillis () < 5000 );
176
170
}
177
171
@@ -243,7 +237,7 @@ public void testSerializationSerializableTargetAndAdvice() throws Throwable {
243
237
p .echo (new IOException ());
244
238
}
245
239
catch (IOException ex ) {
246
-
240
+ /* expected */
247
241
}
248
242
assertEquals (1 , cta .getCalls ());
249
243
@@ -281,7 +275,6 @@ public void testSerializationSerializableTargetAndAdvice() throws Throwable {
281
275
282
276
}
283
277
assertEquals (2 , cta .getCalls ());
284
-
285
278
}
286
279
287
280
/**
@@ -317,7 +310,6 @@ public void testOneAdvisedObjectCallsAnother() {
317
310
pf2 .addAdvice (2 , new CheckMethodInvocationIsSameInAndOutInterceptor ());
318
311
pf2 .addAdvice (1 , new CheckMethodInvocationViaThreadLocalIsSameInAndOutInterceptor ());
319
312
pf2 .addAdvice (0 , ExposeInvocationInterceptor .INSTANCE );
320
- //System.err.println(pf2.toProxyConfigString());
321
313
ITestBean advised2 = (ITestBean ) createProxy (pf2 );
322
314
advised2 .setAge (age2 );
323
315
advised1 .setSpouse (advised2 ); // = 2 invocations
@@ -379,20 +371,14 @@ public void testTargetCanGetProxy() {
379
371
assertEquals ("3 more invocations via AOP as the first call was reentrant through the proxy" , 4 , di .getCount ());
380
372
}
381
373
382
-
383
- @ Test
374
+ @ Test ( expected = IllegalStateException . class )
375
+ // Should fail to get proxy as exposeProxy wasn't set to true
384
376
public void testTargetCantGetProxyByDefault () {
385
377
NeedsToSeeProxy et = new NeedsToSeeProxy ();
386
378
ProxyFactory pf1 = new ProxyFactory (et );
387
379
assertFalse (pf1 .isExposeProxy ());
388
380
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 ();
396
382
}
397
383
398
384
@ Test
@@ -417,7 +403,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
417
403
if (!context ) {
418
404
assertNoInvocationContext ();
419
405
} else {
420
- assertTrue ("have context" , ExposeInvocationInterceptor .currentInvocation () != null );
406
+ assertNotNull ("have context" , ExposeInvocationInterceptor .currentInvocation ());
421
407
}
422
408
return s ;
423
409
}
@@ -436,7 +422,7 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
436
422
assertNoInvocationContext ();
437
423
ITestBean tb = (ITestBean ) aop .getProxy ();
438
424
assertNoInvocationContext ();
439
- assertTrue ("correct return value" , tb .getName () == s );
425
+ assertSame ("correct return value" , s , tb .getName ());
440
426
}
441
427
442
428
/**
@@ -453,7 +439,7 @@ public void testTargetReturnsThis() throws Throwable {
453
439
pc .setTarget (raw );
454
440
455
441
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 ());
457
443
}
458
444
459
445
@ Test
@@ -593,15 +579,6 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
593
579
594
580
ITestBean tb = (ITestBean ) aop .getProxy ();
595
581
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);
605
582
}
606
583
607
584
/**
@@ -645,30 +622,29 @@ private void testTestBeanIntroduction(ProxyFactory pc) {
645
622
int newAge = 65 ;
646
623
ITestBean itb = (ITestBean ) createProxy (pc );
647
624
itb .setAge (newAge );
648
- assertTrue ( itb .getAge () == newAge );
625
+ assertEquals ( newAge , itb .getAge ());
649
626
650
627
Lockable lockable = (Lockable ) itb ;
651
628
assertFalse (lockable .locked ());
652
629
lockable .lock ();
653
630
654
- assertTrue ( itb .getAge () == newAge );
631
+ assertEquals ( newAge , itb .getAge ());
655
632
try {
656
633
itb .setAge (1 );
657
634
fail ("Setters should fail when locked" );
658
635
}
659
636
catch (LockedException ex ) {
660
637
// ok
661
638
}
662
- assertTrue ( itb .getAge () == newAge );
639
+ assertEquals ( newAge , itb .getAge ());
663
640
664
641
// Unlock
665
642
assertTrue (lockable .locked ());
666
643
lockable .unlock ();
667
644
itb .setAge (1 );
668
- assertTrue ( itb .getAge () == 1 );
645
+ assertEquals ( 1 , itb .getAge ());
669
646
}
670
647
671
-
672
648
@ Test
673
649
public void testReplaceArgument () throws Throwable {
674
650
TestBean tb = new TestBean ();
@@ -679,14 +655,14 @@ public void testReplaceArgument() throws Throwable {
679
655
ITestBean t = (ITestBean ) pc .getProxy ();
680
656
int newAge = 5 ;
681
657
t .setAge (newAge );
682
- assertTrue ( t .getAge () == newAge );
658
+ assertEquals ( newAge , t .getAge ());
683
659
String newName = "greg" ;
684
660
t .setName (newName );
685
661
assertEquals (newName , t .getName ());
686
662
687
663
t .setName (null );
688
664
// Null replacement magic should work
689
- assertTrue ( t .getName (). equals ( "" ));
665
+ assertEquals ( "" , t .getName ());
690
666
}
691
667
692
668
@ Test
@@ -1310,9 +1286,9 @@ public void testEquals() {
1310
1286
IOther proxyB = (IOther ) createProxy (pfb );
1311
1287
1312
1288
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 );
1316
1292
assertEquals (proxyA .hashCode (), proxyB .hashCode ());
1317
1293
assertFalse (proxyA .equals (a ));
1318
1294
@@ -1682,9 +1658,7 @@ private static class ProxyMatcherInterceptor implements MethodInterceptor {
1682
1658
public Object invoke (MethodInvocation mi ) throws Throwable {
1683
1659
Object proxy = AopContext .currentProxy ();
1684
1660
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 ());
1688
1662
return ret ;
1689
1663
}
1690
1664
}
@@ -1785,7 +1759,7 @@ public boolean matches(Method m, Class<?> targetClass) {
1785
1759
* Note that trapping the Invocation as in previous version of this test
1786
1760
* isn't safe, as invocations may be reused
1787
1761
* and hence cleared at the end of each invocation.
1788
- * So we trap only the targe .
1762
+ * So we trap only the target .
1789
1763
*/
1790
1764
protected static class TrapTargetInterceptor implements MethodInterceptor {
1791
1765
@@ -2105,11 +2079,10 @@ public void absquatulate() {
2105
2079
static class InvocationCheckExposedInvocationTestBean extends ExposedInvocationTestBean {
2106
2080
@ Override
2107
2081
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 ()));
2111
2085
}
2112
2086
}
2113
2087
2114
2088
}
2115
-
0 commit comments