Clover Coverage Report - FEST Swing 1.2
Coverage timestamp: Tue Jun 1 2010 15:19:25 PDT
../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
30   128   12   4.29
8   64   0.4   3.5
7     1.71  
2    
 
  ScreenLock       Line # 35 30 0% 12 1 97.8% 0.9777778
  ScreenLock.ScreenLockHolder       Line # 123 0 - 0 0 - -1.0
 
No Tests
 
1    /*
2    * Created on May 24, 2007
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10    * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11    * or implied. See the License for the specific language governing permissions and limitations under
12    * the License.
13    *
14    * Copyright @2007-2010 the original author or authors.
15    */
16    package org.fest.swing.lock;
17   
18    import static org.fest.util.Strings.concat;
19   
20    import java.util.concurrent.locks.*;
21   
22    import net.jcip.annotations.GuardedBy;
23    import net.jcip.annotations.ThreadSafe;
24   
25    import org.fest.swing.exception.ScreenLockException;
26   
27    /**
28    * Understands a lock that each GUI test should acquire before being executed, to guarantee sequential execution of
29    * GUI tests and to prevent GUI tests from blocking each other.
30    *
31    * @author Yvonne Wang
32    * @author Alex Ruiz
33    */
34    @ThreadSafe
 
35    public final class ScreenLock {
36   
37    private final Lock lock = new ReentrantLock();
38    private final Condition released = lock.newCondition();
39   
40    @GuardedBy("lock")
41    private Object owner;
42   
43    @GuardedBy("lock")
44    private boolean acquired;
45   
46    /**
47    * Acquires this lock. If this lock was already acquired by another object, this method will block until the lock is
48    * released.
49    * @param newOwner the new owner of the lock.
50    */
 
51  1851 toggle public void acquire(Object newOwner) {
52  1851 lock.lock();
53  1851 try {
54  1 if (alreadyAcquiredBy(newOwner)) return;
55  5 while (acquired) released.await();
56  1850 owner = newOwner;
57  1850 acquired = true;
58    } catch (InterruptedException ignored) {
59  0 Thread.currentThread().interrupt();
60    } finally {
61  1851 lock.unlock();
62    }
63    }
64   
65    /**
66    * Releases this lock.
67    * @param currentOwner the current owner of the lock.
68    * @throws ScreenLockException if the lock has not been previously acquired.
69    * @throws ScreenLockException if the given owner is not the same as the current owner of the lock.
70    */
 
71  1852 toggle public void release(Object currentOwner) {
72  1852 lock.lock();
73  1852 try {
74  1 if (!acquired) throw new ScreenLockException("No lock to release");
75  1 if (owner != currentOwner) throw new ScreenLockException(concat(currentOwner, " is not the lock owner"));
76  1850 acquired = false;
77  1850 owner = null;
78  1850 released.signal();
79    } finally {
80  1852 lock.unlock();
81    }
82    }
83   
84    /**
85    * Indicates whether this lock was acquired by the given object.
86    * @param possibleOwner the given object, which could be owning the lock.
87    * @return <code>true</code> if the given object is owning the lock; <code>false</code> otherwise.
88    */
 
89  1604 toggle public boolean acquiredBy(Object possibleOwner) {
90  1604 lock.lock();
91  1604 try {
92  1604 return alreadyAcquiredBy(possibleOwner);
93    } finally {
94  1604 lock.unlock();
95    }
96    }
97   
 
98  3455 toggle private boolean alreadyAcquiredBy(Object possibleOwner) {
99  3455 return acquired && owner == possibleOwner;
100    }
101   
102    /**
103    * Indicates whether this lock is already acquired.
104    * @return <code>true</code> if the lock is already acquired; <code>false</code> otherwise.
105    * @see #acquiredBy(Object)
106    * @since 1.2
107    */
 
108  10 toggle public boolean acquired() {
109  10 lock.lock();
110  10 try {
111  10 return acquired;
112    } finally {
113  10 lock.unlock();
114    }
115    }
116   
117    /**
118    * Returns the singleton instance of this class.
119    * @return the singleton instance of this class.
120    */
 
121  3678 toggle public static ScreenLock instance() { return ScreenLockHolder.instance; }
122   
 
123    private static class ScreenLockHolder {
124    static ScreenLock instance = new ScreenLock();
125    }
126   
 
127  713 toggle ScreenLock() {}
128    }