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 junit.framework.TestCase;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class InnerClassVisitorTestCase extends TestCase {
039:
040: private static final StringBuilder builder = new StringBuilder();
041:
042: private String message = "Hello";
043:
044: private class InnerClass {
045:
046: public InnerClass() {
047: }
048:
049: public InnerClass(String s) {
050: this ();
051: if (message.equals(s)) {
052: throw new IllegalArgumentException();
053: }
054: }
055:
056: public String getMessage() {
057: return message;
058: }
059:
060: public class TwiceInnerClass {
061: public String getMessage() {
062: return message;
063: }
064: }
065: }
066:
067: private class BaseClass {
068:
069: public final int n;
070:
071: public BaseClass(int n, Object firstObject, Object secondObject) {
072: this .n = firstObject != secondObject ? n : 0;
073: }
074:
075: public String getMessage() {
076: return message;
077: }
078: }
079:
080: private class DerivedClass extends BaseClass {
081:
082: public final double m;
083:
084: public DerivedClass(int n, double m) {
085: super (n, new BaseClass(1, new BaseClass(1, "test",
086: new Object()), new Object()), new BaseClass(1,
087: new Object(), "test"));
088: this .m = m;
089: }
090:
091: public String getMessage() {
092: return message;
093: }
094: }
095:
096: public void testInnerClass() {
097: assertSame(message, new InnerClass().getMessage());
098: }
099:
100: public void testTwiceInnerClass() {
101: assertSame(message,
102: new InnerClass("test").new TwiceInnerClass()
103: .getMessage());
104: }
105:
106: public void testBaseClass() {
107: assertSame(message, new BaseClass(7, "test", new Object())
108: .getMessage());
109: }
110:
111: public void testDerivedClass() {
112: assertSame(message, new DerivedClass(7, 0.4).getMessage());
113: }
114:
115: public void testLocalClass() {
116: class LocalClass {
117: public String getMessage() {
118: return message;
119: }
120: }
121: assertSame(message, new LocalClass().getMessage());
122: }
123:
124: public void testTwiceLocalClass() {
125: class LocalClass {
126: class TwiceLocalClass {
127: public String getMessage() {
128: return message;
129: }
130: }
131: }
132: assertSame(message, new LocalClass().new TwiceLocalClass()
133: .getMessage());
134: }
135:
136: public synchronized void testNonstaticContext() {
137: builder.setLength(0);
138: final Number a = Integer.valueOf("3");
139: new Runnable() {
140: public void run() {
141: builder.append(a);
142: }
143: }.run();
144: assertEquals("3", builder.toString());
145: }
146:
147: public synchronized void testStaticContext() {
148: executeInStaticMethod();
149: }
150:
151: private static void executeInStaticMethod() {
152: builder.setLength(0);
153: final Number a = Integer.valueOf("1");
154: final Number b = Integer.valueOf("2");
155: new Runnable() {
156: public void run() {
157: builder.append(a).append(":").append(b);
158: }
159: }.run();
160: assertEquals("1:2", builder.toString());
161: }
162:
163: }
|