1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.proxy.interceptor;
19
20 import junit.framework.TestCase;
21 import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
22 import org.apache.commons.proxy.util.Echo;
23 import org.apache.commons.proxy.util.EchoImpl;
24 import EDU.oswego.cs.dl.util.concurrent.Executor;
25 import EDU.oswego.cs.dl.util.concurrent.CountDown;
26
27 public class TestExecutorInterceptor extends TestCase
28 {
29 public void testVoidMethod() throws Exception
30 {
31 final ExecutedEcho impl = new ExecutedEcho();
32 final OneShotExecutor executor = new OneShotExecutor();
33 final Echo proxy = ( Echo ) new CglibProxyFactory()
34 .createInterceptorProxy( impl, new ExecutorInterceptor( executor ), new Class[] { Echo.class } );
35 proxy.echo();
36 executor.getLatch().acquire();
37 assertEquals( executor.getThread(), impl.getExecutionThread() );
38 }
39
40 public void testNonVoidMethod() throws Exception
41 {
42 final ExecutedEcho impl = new ExecutedEcho();
43 final OneShotExecutor executor = new OneShotExecutor();
44 final Echo proxy = ( Echo ) new CglibProxyFactory()
45 .createInterceptorProxy( impl, new ExecutorInterceptor( executor ), new Class[] { Echo.class } );
46 try
47 {
48 proxy.echoBack( "hello" );
49 fail();
50 }
51 catch( IllegalArgumentException e )
52 {
53 }
54 }
55
56 public static class ExecutedEcho extends EchoImpl
57 {
58 private Thread executionThread;
59
60 public void echo()
61 {
62 executionThread = Thread.currentThread();
63 }
64
65 public Thread getExecutionThread()
66 {
67 return executionThread;
68 }
69 }
70
71 private static class OneShotExecutor implements Executor
72 {
73 private Thread thread;
74 private CountDown latch = new CountDown( 1 );
75
76 public void execute( final Runnable command )
77 {
78 thread = new Thread( new Runnable()
79 {
80 public void run()
81 {
82 try
83 {
84 command.run();
85 }
86 finally
87 {
88 latch.release();
89 }
90 }
91 } );
92 thread.start();
93 }
94
95 public Thread getThread()
96 {
97 return thread;
98 }
99
100 public CountDown getLatch()
101 {
102 return latch;
103 }
104 }
105 }