KickJava   Java API By Example, From Geeks To Geeks.

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


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.reflect.AnnotatedElement JavaDoc;
35 import java.util.*;
36 import java.util.concurrent.*;
37 import junit.framework.TestCase;
38
39 public class GeneralReplacementVisitorTestCase extends TestCase {
40
41     private String JavaDoc hello = "Hello,";
42     private String JavaDoc world = "World!";
43     private StringBuilder JavaDoc builder = new StringBuilder JavaDoc();
44
45     public void testImplicit() {
46         assertEquals("Hello, World!", hello + " " + world);
47     }
48
49     public void testExplicit() {
50         assertEquals("Hello, World!", builder.append(hello).append(" ").append(world).toString());
51     }
52
53     public void testCallable() throws Exception JavaDoc {
54         final String JavaDoc hello = "Hello, World";
55         Callable callable = new Callable() {
56             public Object JavaDoc call() throws Exception JavaDoc {
57                 return hello;
58             }
59         };
60         assertSame(hello, callable.call());
61     }
62
63     public void testConcurrentMap() throws Exception JavaDoc {
64         ConcurrentMap<String JavaDoc, String JavaDoc> map = new ConcurrentHashMap<String JavaDoc, String JavaDoc>();
65         map.put("hi", "Hello");
66         map.put("bye", "Good-bye");
67         assertEquals("Hello", map.get("hi"));
68     }
69
70     public void testDelayQueue() throws Exception JavaDoc {
71         class MyDelayed implements Delayed {
72             public long getDelay(TimeUnit unit) {
73                 return 0;
74             }
75
76             public int compareTo(Delayed o) {
77                 return 0;
78             }
79         }
80         DelayQueue<MyDelayed> delayQueue = new DelayQueue<MyDelayed>();
81         delayQueue.offer(new MyDelayed());
82         delayQueue.put(new MyDelayed());
83         delayQueue.offer(new MyDelayed(), 0, TimeUnit.SECONDS);
84         delayQueue.add(new MyDelayed());
85         assertNotNull(delayQueue.take());
86         assertNotNull(delayQueue.poll(0, TimeUnit.SECONDS));
87         assertNotNull(delayQueue.poll());
88         assertNotNull(delayQueue.peek());
89     }
90
91     public static Queue getQueue() {
92         return new LinkedList();
93     }
94
95     public void testMethod() {
96         assertNotNull(getQueue());
97     }
98
99     public void testCast() {
100         try {
101             String JavaDoc s = (String JavaDoc) new Object JavaDoc();
102             fail();
103         } catch (ClassCastException JavaDoc e) {
104             //ok
105
}
106         Queue queue;
107         Object JavaDoc linkedList = new LinkedList();
108         queue = (Queue) linkedList;
109         queue.peek();
110         Object JavaDoc priorityQueue = new PriorityQueue();
111         queue = (Queue) priorityQueue;
112         Object JavaDoc string = "String";
113         try {
114             queue = (Queue) string;
115             fail();
116         } catch (ClassCastException JavaDoc e) {
117             //ok
118
}
119         queue.peek();
120     }
121
122     public void testArray() {
123         Queue[][][][] array = new Queue[1][][][];
124         array[0] = new Queue[1][1][];
125         array[0][0][0] = new Queue[1];
126         array[0][0][0][0] = new LinkedList();
127         Object JavaDoc object = array;
128         ((Queue[][][][]) object)[0][0][0][0].peek();
129     }
130
131     public void testInstanceOf() throws Exception JavaDoc {
132         assertTrue(new PriorityQueue() instanceof Queue);
133         assertTrue(new LinkedList() instanceof Queue);
134         assertFalse(new Object JavaDoc() instanceof Queue);
135         assertTrue(new Integer JavaDoc(1) instanceof Number JavaDoc);
136         assertFalse(new Object JavaDoc() instanceof Number JavaDoc);
137         assertTrue(this.getClass() instanceof AnnotatedElement JavaDoc);
138         assertFalse((Object JavaDoc) "Hello" instanceof AnnotatedElement JavaDoc);
139         assertTrue(new String JavaDoc[0] instanceof String JavaDoc[]);
140         assertFalse(((Object JavaDoc) new Integer JavaDoc[0]) instanceof String JavaDoc[]);
141     }
142
143 }
Popular Tags