(no commit message)
[utils] / support / test / org / wamblee / concurrency / LockAdviceTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.concurrency;
18
19 import java.lang.reflect.Method;
20 import java.util.ArrayList;
21
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.springframework.aop.framework.AdvisedSupport;
24 import org.springframework.aop.framework.ProxyFactoryBean;
25 import org.springframework.aop.framework.ReflectiveMethodInvocation;
26 import org.wamblee.test.TimingUtils;
27
28 /**
29  * 
30  */
31 public class LockAdviceTest extends AbstractLockTestCase {
32
33     private class Runner implements Runnable {
34         public void run() {
35             LockAdviceTest.this.getTracker().eventOccurred(ACQUIRED);
36             TimingUtils.sleep(SLEEP_TIME);
37         }
38     }
39
40     private Runnable _target;
41
42     /*
43      * (non-Javadoc)
44      * 
45      * @see org.wamblee.concurrency.AbstractLockTestCase#setUp()
46      */
47     @Override
48     protected void setUp() throws Exception {
49         super.setUp();
50         
51         Runner runner = new Runner();
52         LockAdvice advice = new LockAdvice(new JvmLock());
53         
54         ProxyFactoryBean support = new ProxyFactoryBean();
55         support.setInterfaces(new Class[]{ Runnable.class });
56         support.setTarget(runner);
57         support.addAdvice(advice);
58         _target = (Runnable)support.getObject();
59         
60     }
61
62     /*
63      * (non-Javadoc)
64      * 
65      * @see org.wamblee.concurrency.AbstractLockTestCase#runThread()
66      */
67     @Override
68     protected Thread runThread() {
69         Thread t = new Thread(new Runnable() {
70             public void run() {
71                 try {
72                     getTracker().eventOccurred(STARTED);
73                     _target.run();
74                     getTracker().eventOccurred(RELEASED);
75                 } catch (Throwable e) {
76                     throw new RuntimeException(e);
77                 }
78             };
79         });
80         t.start();
81         return t;
82     }
83
84 }