61b9f7d4adaf028d139e7da599556b7b8d40876b
[utils] /
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 org.springframework.aop.framework.ProxyFactoryBean;
20 import org.wamblee.test.TimingUtils;
21
22 /**
23  * 
24  */
25 public class LockAdviceTest extends AbstractLockTestCase {
26
27     private class Runner implements Runnable {
28         public void run() {
29             LockAdviceTest.this.getTracker().eventOccurred(ACQUIRED);
30             TimingUtils.sleep(SLEEP_TIME);
31         }
32     }
33
34     private Runnable _target;
35
36     /*
37      * (non-Javadoc)
38      * 
39      * @see org.wamblee.concurrency.AbstractLockTestCase#setUp()
40      */
41     @Override
42     protected void setUp() throws Exception {
43         super.setUp();
44         
45         Runner runner = new Runner();
46         LockAdvice advice = new LockAdvice(new JvmLock());
47         
48         ProxyFactoryBean support = new ProxyFactoryBean();
49         support.setInterfaces(new Class[]{ Runnable.class });
50         support.setTarget(runner);
51         support.addAdvice(advice);
52         _target = (Runnable)support.getObject();
53         
54     }
55
56     /*
57      * (non-Javadoc)
58      * 
59      * @see org.wamblee.concurrency.AbstractLockTestCase#runThread()
60      */
61     @Override
62     protected Thread runThread() {
63         Thread t = new Thread(new Runnable() {
64             public void run() {
65                 try {
66                     getTracker().eventOccurred(STARTED);
67                     _target.run();
68                     getTracker().eventOccurred(RELEASED);
69                 } catch (Throwable e) {
70                     throw new RuntimeException(e);
71                 }
72             };
73         });
74         t.start();
75         return t;
76     }
77
78 }