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.transformer.smart.*;
034: import net.sf.retrotranslator.tests.TestCaseBase;
035: import java.math.*;
036: import java.security.cert.CertificateEncodingException;
037:
038: /**
039: * @author Taras Puchko
040: */
041: public class SmartReplacementVisitorTestCase extends TestCaseBase {
042:
043: public void testField() {
044: assertEquals("Unchanged", FullDerivedClass.FIELD);
045: Object baseValue = BackportedClass.FIELD;
046: Object derivedValue = EmptyDerivedClass.FIELD;
047: if (isSmart()) {
048: assertEquals("Backported", baseValue);
049: assertEquals("Backported", derivedValue);
050: } else {
051: assertNull(baseValue);
052: assertNull(derivedValue);
053: }
054: }
055:
056: public void testStaticMethod() {
057: assertEquals(21, FullDerivedClass.add(2, 5));
058: int baseValue = BackportedClass.add(2, 5);
059: int derivedValue = EmptyDerivedClass.add(2, 5);
060: if (isSmart()) {
061: assertEquals(14, baseValue);
062: assertEquals(14, derivedValue);
063: } else {
064: assertEquals(7, baseValue);
065: assertEquals(7, derivedValue);
066: }
067: }
068:
069: public void testInstanceMethod() {
070: assertEquals(210, new FullDerivedClass().multiply(7));
071: int baseValue = new BackportedClass().multiply(7);
072: int derivedValue = new EmptyDerivedClass().multiply(7);
073: if (isSmart()) {
074: assertEquals(140, baseValue);
075: assertEquals(140, derivedValue);
076: } else {
077: assertEquals(70, baseValue);
078: assertEquals(70, derivedValue);
079: }
080: }
081:
082: public void testConstructor() {
083: Exception exception1 = new CertificateEncodingException();
084: Exception exception2 = new CertificateEncodingException("test2");
085: Exception exception3 = new CertificateEncodingException(
086: "test3", new Exception("cause3"));
087: Exception exception4 = new CertificateEncodingException(
088: new Exception("cause4"));
089: assertNull(exception1.getMessage());
090: assertNull(exception1.getCause());
091: assertEquals("test2", exception2.getMessage());
092: assertNull(exception2.getCause());
093: if (isSmart()) {
094: assertEquals("message and cause", exception3.getMessage());
095: assertNull(exception3.getCause());
096: assertEquals("cause", exception4.getMessage());
097: assertNull(exception4.getCause());
098: } else {
099: assertEquals("test3", exception3.getMessage());
100: assertEquals("cause3", exception3.getCause().getMessage());
101: assertEquals("java.lang.Exception: cause4", exception4
102: .getMessage());
103: assertEquals("cause4", exception4.getCause().getMessage());
104: }
105: }
106:
107: private boolean isSmart() {
108: return Boolean
109: .getBoolean("net.sf.retrotranslator.tests.smart-test");
110: }
111:
112: public void testBigDecimal() {
113: if (isSmart() || isJava5AtLeast()) {
114: class MyDecimal extends BigDecimal {
115: public MyDecimal(int val) {
116: super (val);
117: }
118: }
119: MyDecimal myDecimal = new MyDecimal(123);
120: assertEquals(0, myDecimal.scale());
121: BigDecimal decimal = myDecimal.setScale(-1,
122: MyDecimal.ROUND_DOWN);
123: assertEquals(120, decimal.intValue());
124: }
125: }
126:
127: }
|