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 java.lang.reflect.AnnotatedElement;
034: import java.util.*;
035: import java.util.concurrent.*;
036: import junit.framework.TestCase;
037:
038: public class GeneralReplacementVisitorTestCase extends TestCase {
039:
040: private String hello = "Hello,";
041: private String world = "World!";
042: private StringBuilder builder = new StringBuilder();
043:
044: public void testImplicit() {
045: assertEquals("Hello, World!", hello + " " + world);
046: }
047:
048: public void testExplicit() {
049: assertEquals("Hello, World!", builder.append(hello).append(" ")
050: .append(world).toString());
051: }
052:
053: public void testQueue() throws Exception {
054: assertTrue(Queue.class.isInterface());
055: assertTrue(Collection.class.isAssignableFrom(Queue.class));
056: }
057:
058: public void testCallable() throws Exception {
059: final String hello = "Hello, World";
060: Callable callable = new Callable() {
061: public Object call() throws Exception {
062: return hello;
063: }
064: };
065: assertSame(hello, callable.call());
066: }
067:
068: public void testConcurrentMap() throws Exception {
069: ConcurrentMap<String, String> map = new ConcurrentHashMap<String, String>();
070: map.put("hi", "Hello");
071: map.put("bye", "Good-bye");
072: assertEquals("Hello", map.get("hi"));
073: }
074:
075: public void testDelayQueue() throws Exception {
076: class MyDelayed implements Delayed {
077: public long getDelay(TimeUnit unit) {
078: return 0;
079: }
080:
081: public int compareTo(Delayed o) {
082: return 0;
083: }
084: }
085: DelayQueue<MyDelayed> delayQueue = new DelayQueue<MyDelayed>();
086: delayQueue.offer(new MyDelayed());
087: delayQueue.put(new MyDelayed());
088: delayQueue.offer(new MyDelayed(), 0, TimeUnit.SECONDS);
089: delayQueue.add(new MyDelayed());
090: assertNotNull(delayQueue.take());
091: assertNotNull(delayQueue.poll(0, TimeUnit.SECONDS));
092: assertNotNull(delayQueue.poll());
093: assertNotNull(delayQueue.peek());
094: }
095:
096: public static Queue getQueue() {
097: return new LinkedList();
098: }
099:
100: public void testMethod() {
101: assertNotNull(getQueue());
102: }
103:
104: public void testCast() {
105: try {
106: String s = (String) new Object();
107: fail();
108: } catch (ClassCastException e) {
109: //ok
110: }
111: Queue queue;
112: Object linkedList = new LinkedList();
113: queue = (Queue) linkedList;
114: queue.peek();
115: Object priorityQueue = new PriorityQueue();
116: queue = (Queue) priorityQueue;
117: Object string = "String";
118: try {
119: queue = (Queue) string;
120: fail();
121: } catch (ClassCastException e) {
122: //ok
123: }
124: queue.peek();
125: }
126:
127: public void testArray() {
128: Queue[][][][] array = new Queue[1][][][];
129: array[0] = new Queue[1][1][];
130: array[0][0][0] = new Queue[1];
131: array[0][0][0][0] = new LinkedList();
132: Object object = array;
133: ((Queue[][][][]) object)[0][0][0][0].peek();
134: }
135:
136: public void testInstanceOf() throws Exception {
137: assertTrue(new PriorityQueue() instanceof Queue);
138: assertTrue(new LinkedList() instanceof Queue);
139: assertFalse(new Object() instanceof Queue);
140: assertTrue(new Integer(1) instanceof Number);
141: assertFalse(new Object() instanceof Number);
142: assertTrue(this .getClass() instanceof AnnotatedElement);
143: assertFalse((Object) "Hello" instanceof AnnotatedElement);
144: assertTrue(new String[0] instanceof String[]);
145: assertFalse(((Object) new Integer[0]) instanceof String[]);
146: }
147:
148: }
|