001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.transformer;
032:
033: import net.sf.retrotranslator.tests.TestCaseBase;
034:
035: import java.lang.ref.*;
036: import java.util.concurrent.Callable;
037: import java.util.concurrent.locks.*;
038:
039: /**
040: * @author Taras Puchko
041: */
042: public class SpecificReplacementVisitorTestCase extends TestCaseBase {
043:
044: public void testNanotime() throws Exception {
045: long n = System.nanoTime();
046: long m = System.currentTimeMillis();
047: Thread.sleep(100);
048: m = System.currentTimeMillis() - m;
049: n = System.nanoTime() - n;
050: assertTrue(Math.abs(n / 1000000 - m) <= 100);
051: }
052:
053: public void testSoftReference() throws Exception {
054: class MyReference<T> extends SoftReference<T> {
055: public MyReference(T referent, ReferenceQueue<? super T> q) {
056: super (referent, q);
057: }
058: }
059: new MyReference<String>("a", null);
060: new MyReference<String>("b", new ReferenceQueue<String>());
061: new SoftReference<String>("c", null);
062: new SoftReference<String>("d", new ReferenceQueue<String>());
063: }
064:
065: public void testWeakReference() throws Exception {
066: class MyReference<T> extends WeakReference<T> {
067: public MyReference(T referent, ReferenceQueue<? super T> q) {
068: super (referent, q);
069: }
070: }
071: new WeakReference<String>("a", null);
072: new MyReference<String>("b", null);
073: final ReferenceQueue<String> queue1 = new ReferenceQueue<String>();
074: final ReferenceQueue<String> queue2 = new ReferenceQueue<String>();
075: Reference<String> reference1 = new MyReference<String>(
076: new String("c"), queue1);
077: Reference<String> reference2 = new WeakReference<String>(
078: new String("d"), queue2);
079: final Reference[] references = new Reference[2];
080: gc(new Callable<Boolean>() {
081: public Boolean call() throws Exception {
082: if (references[0] == null) {
083: references[0] = queue1.poll();
084: }
085: if (references[1] == null) {
086: references[1] = queue2.poll();
087: }
088: return references[0] == null || references[1] == null;
089: }
090: });
091: assertSame(reference1, references[0]);
092: assertSame(reference2, references[1]);
093: }
094:
095: public void testReentrantReadWriteLock() throws Exception {
096: ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
097: ReentrantReadWriteLock.ReadLock readLock = readWriteLock
098: .readLock();
099: ReentrantReadWriteLock.WriteLock writeLock = readWriteLock
100: .writeLock();
101: assertTrue(readLock.tryLock());
102: readLock.unlock();
103: assertTrue(writeLock.tryLock());
104: writeLock.unlock();
105: }
106:
107: public void testCondition_awaitNanos() throws Exception {
108: ReentrantLock lock = new ReentrantLock();
109: lock.lock();
110: lock.newCondition().awaitNanos(1000);
111: lock.unlock();
112: try {
113: lock.newCondition().awaitNanos(1000);
114: fail();
115: } catch (IllegalMonitorStateException e) {
116: //ok
117: }
118: }
119:
120: }
|