KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > transformer > SpecificReplacementVisitorTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.transformer;
33
34 import java.lang.ref.*;
35 import java.util.concurrent.locks.*;
36 import junit.framework.TestCase;
37
38 /**
39  * @author Taras Puchko
40  */

41 public class SpecificReplacementVisitorTestCase extends TestCase {
42
43     public void testNanotime() throws Exception JavaDoc {
44         long n = System.nanoTime();
45         long m = System.currentTimeMillis();
46         Thread.sleep(100);
47         m = System.currentTimeMillis() - m;
48         n = System.nanoTime() - n;
49         assertTrue(Math.abs(n / 1000000 - m) <= 100);
50     }
51
52     public void testIllegalArgumentExceptionOneParam() throws Exception JavaDoc {
53         IllegalArgumentException JavaDoc exception = new IllegalArgumentException JavaDoc(new ClassNotFoundException JavaDoc("123"));
54         assertEquals("java.lang.ClassNotFoundException: 123", exception.getMessage());
55         Throwable JavaDoc cause = exception.getCause();
56         assertTrue(cause instanceof ClassNotFoundException JavaDoc);
57         assertEquals("123", cause.getMessage());
58         class Ex extends IllegalArgumentException JavaDoc {
59             public Ex(Throwable JavaDoc cause) {
60                 super(cause);
61             }
62         }
63         Ex ex = new Ex(new IllegalArgumentException JavaDoc());
64         assertEquals("java.lang.IllegalArgumentException", ex.getMessage());
65         assertTrue(ex.getCause() instanceof IllegalArgumentException JavaDoc);
66
67         IllegalArgumentException JavaDoc nullCausedEx = new IllegalArgumentException JavaDoc((Throwable JavaDoc) null);
68         assertNull(nullCausedEx.getMessage());
69         assertNull(nullCausedEx.getCause());
70         try {
71             nullCausedEx.initCause(new Throwable JavaDoc());
72             fail();
73         } catch (IllegalStateException JavaDoc e) {
74             //ok
75
}
76     }
77
78     public void testIllegalArgumentExceptionTwoParam() throws Exception JavaDoc {
79         IllegalArgumentException JavaDoc exception = new IllegalArgumentException JavaDoc("abc", new ClassNotFoundException JavaDoc("123"));
80         assertEquals("abc", exception.getMessage());
81         Throwable JavaDoc cause = exception.getCause();
82         assertTrue(cause instanceof ClassNotFoundException JavaDoc);
83         assertEquals("123", cause.getMessage());
84         class Ex extends IllegalArgumentException JavaDoc {
85             public Ex(String JavaDoc message, Throwable JavaDoc cause) {
86                 super(message, cause);
87             }
88         }
89         Ex ex = new Ex("qwerty", new IllegalArgumentException JavaDoc());
90         assertEquals("qwerty", ex.getMessage());
91         assertTrue(ex.getCause() instanceof IllegalArgumentException JavaDoc);
92     }
93
94     public void testIllegalStateExceptionOneParam() throws Exception JavaDoc {
95         IllegalStateException JavaDoc exception = new IllegalStateException JavaDoc(new ClassNotFoundException JavaDoc("123"));
96         assertEquals("java.lang.ClassNotFoundException: 123", exception.getMessage());
97         Throwable JavaDoc cause = exception.getCause();
98         assertTrue(cause instanceof ClassNotFoundException JavaDoc);
99         assertEquals("123", cause.getMessage());
100         class Ex extends IllegalStateException JavaDoc {
101             public Ex(Throwable JavaDoc cause) {
102                 super(cause);
103             }
104         }
105         Ex ex = new Ex(new IllegalArgumentException JavaDoc());
106         assertEquals("java.lang.IllegalArgumentException", ex.getMessage());
107         assertTrue(ex.getCause() instanceof IllegalArgumentException JavaDoc);
108
109         IllegalStateException JavaDoc nullCausedEx = new IllegalStateException JavaDoc((Throwable JavaDoc) null);
110         assertNull(nullCausedEx.getMessage());
111         assertNull(nullCausedEx.getCause());
112         try {
113             nullCausedEx.initCause(new Throwable JavaDoc());
114             fail();
115         } catch (IllegalStateException JavaDoc e) {
116             //ok
117
}
118     }
119
120     public void testIllegalStateExceptionTwoParam() throws Exception JavaDoc {
121         IllegalStateException JavaDoc exception = new IllegalStateException JavaDoc("abc", new ClassNotFoundException JavaDoc("123"));
122         assertEquals("abc", exception.getMessage());
123         Throwable JavaDoc cause = exception.getCause();
124         assertTrue(cause instanceof ClassNotFoundException JavaDoc);
125         assertEquals("123", cause.getMessage());
126         class Ex extends IllegalStateException JavaDoc {
127             public Ex(String JavaDoc message, Throwable JavaDoc cause) {
128                 super(message, cause);
129             }
130         }
131         Ex ex = new Ex("qwerty", new IllegalArgumentException JavaDoc());
132         assertEquals("qwerty", ex.getMessage());
133         assertTrue(ex.getCause() instanceof IllegalArgumentException JavaDoc);
134     }
135
136     public void testSoftReference() throws Exception JavaDoc {
137         class MyReference<T> extends SoftReference<T> {
138             public MyReference(T referent, ReferenceQueue<? super T> q) {
139                 super(referent, q);
140             }
141         }
142         new MyReference<String JavaDoc>("a", null);
143         new MyReference<String JavaDoc>("b", new ReferenceQueue<String JavaDoc>());
144         new SoftReference<String JavaDoc>("c", null);
145         new SoftReference<String JavaDoc>("d", new ReferenceQueue<String JavaDoc>());
146     }
147
148     public void testWeakReference() throws Exception JavaDoc {
149         class MyReference<T> extends WeakReference<T> {
150             public MyReference(T referent, ReferenceQueue<? super T> q) {
151                 super(referent, q);
152             }
153         }
154         new WeakReference<String JavaDoc>("a", null);
155         new MyReference<String JavaDoc>("b", null);
156         ReferenceQueue<String JavaDoc> queue1 = new ReferenceQueue<String JavaDoc>();
157         ReferenceQueue<String JavaDoc> queue2 = new ReferenceQueue<String JavaDoc>();
158         Reference<String JavaDoc> reference1 = new MyReference<String JavaDoc>(new String JavaDoc("c"), queue1);
159         Reference<String JavaDoc> reference2 = new WeakReference<String JavaDoc>(new String JavaDoc("d"), queue2);
160         gc();
161         assertSame(reference1, queue1.poll());
162         assertSame(reference2, queue2.poll());
163     }
164
165     private static void gc() throws InterruptedException JavaDoc {
166         for (int i = 0; i < 10; i++) {
167             System.gc();
168             Thread.sleep(100);
169         }
170     }
171
172     public void testReentrantReadWriteLock() throws Exception JavaDoc {
173         ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
174         ReentrantReadWriteLock.ReadLock readLock = readWriteLock.readLock();
175         ReentrantReadWriteLock.WriteLock writeLock = readWriteLock.writeLock();
176         assertTrue(readLock.tryLock());
177         readLock.unlock();
178         assertTrue(writeLock.tryLock());
179         writeLock.unlock();
180     }
181
182     public void testCondition_awaitNanos() throws Exception JavaDoc {
183         ReentrantLock lock = new ReentrantLock();
184         lock.lock();
185         lock.newCondition().awaitNanos(1000);
186         lock.unlock();
187         try {
188             lock.newCondition().awaitNanos(1000);
189             fail();
190         } catch (IllegalMonitorStateException JavaDoc e) {
191             //ok
192
}
193     }
194
195
196 }
Popular Tags