001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.proxy.interceptor;
019
020 import junit.framework.TestCase;
021 import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
022 import org.apache.commons.proxy.util.Echo;
023 import org.apache.commons.proxy.util.EchoImpl;
024 import EDU.oswego.cs.dl.util.concurrent.Executor;
025 import EDU.oswego.cs.dl.util.concurrent.CountDown;
026
027 public class TestExecutorInterceptor extends TestCase
028 {
029 public void testVoidMethod() throws Exception
030 {
031 final ExecutedEcho impl = new ExecutedEcho();
032 final OneShotExecutor executor = new OneShotExecutor();
033 final Echo proxy = ( Echo ) new CglibProxyFactory()
034 .createInterceptorProxy( impl, new ExecutorInterceptor( executor ), new Class[] { Echo.class } );
035 proxy.echo();
036 executor.getLatch().acquire();
037 assertEquals( executor.getThread(), impl.getExecutionThread() );
038 }
039
040 public void testNonVoidMethod() throws Exception
041 {
042 final ExecutedEcho impl = new ExecutedEcho();
043 final OneShotExecutor executor = new OneShotExecutor();
044 final Echo proxy = ( Echo ) new CglibProxyFactory()
045 .createInterceptorProxy( impl, new ExecutorInterceptor( executor ), new Class[] { Echo.class } );
046 try
047 {
048 proxy.echoBack( "hello" );
049 fail();
050 }
051 catch( IllegalArgumentException e )
052 {
053 }
054 }
055
056 public static class ExecutedEcho extends EchoImpl
057 {
058 private Thread executionThread;
059
060 public void echo()
061 {
062 executionThread = Thread.currentThread();
063 }
064
065 public Thread getExecutionThread()
066 {
067 return executionThread;
068 }
069 }
070
071 private static class OneShotExecutor implements Executor
072 {
073 private Thread thread;
074 private CountDown latch = new CountDown( 1 );
075
076 public void execute( final Runnable command )
077 {
078 thread = new Thread( new Runnable()
079 {
080 public void run()
081 {
082 try
083 {
084 command.run();
085 }
086 finally
087 {
088 latch.release();
089 }
090 }
091 } );
092 thread.start();
093 }
094
095 public Thread getThread()
096 {
097 return thread;
098 }
099
100 public CountDown getLatch()
101 {
102 return latch;
103 }
104 }
105 }