1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }