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.runtime.java.lang;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * @author Taras Puchko
037: */
038: public class _StackTraceElementTestCase extends TestCase {
039:
040: class Holder {
041: public StackTraceElement element;
042:
043: public Holder(StackTraceElement element) {
044: this .element = element;
045: }
046: }
047:
048: class Creator {
049: public StackTraceElement element;
050:
051: public Creator(String declaringClass, String methodName,
052: String fileName, int lineNumber) {
053: this .element = new StackTraceElement(declaringClass,
054: methodName, fileName, lineNumber);
055: }
056: }
057:
058: class CreatingHolder extends Holder {
059: public CreatingHolder(String declaringClass, String methodName,
060: String fileName, int lineNumber) {
061: super (new StackTraceElement(declaringClass, methodName,
062: fileName, lineNumber));
063: }
064: }
065:
066: public void testCreateNewInstance() throws Exception {
067: assertTrace(new StackTraceElement("mypackage.MyClass",
068: "myMethod", "MyFile.java", 123));
069: assertTrace(new Holder(new StackTraceElement(
070: "mypackage.MyClass", "myMethod", "MyFile.java", 123)).element);
071: assertTrace(new Creator("mypackage.MyClass", "myMethod",
072: "MyFile.java", 123).element);
073: assertTrace(new CreatingHolder("mypackage.MyClass", "myMethod",
074: "MyFile.java", 123).element);
075: }
076:
077: private void assertTrace(StackTraceElement element) {
078: assertEquals("mypackage.MyClass", element.getClassName());
079: assertEquals("myMethod", element.getMethodName());
080: assertEquals("MyFile.java", element.getFileName());
081: assertEquals(123, element.getLineNumber());
082: }
083:
084: public void testCreateNewInstance_SourceUnknown() throws Exception {
085: StackTraceElement element = new StackTraceElement("SomeClass",
086: "someMethod", null, 345);
087: assertEquals("SomeClass", element.getClassName());
088: assertEquals("someMethod", element.getMethodName());
089: assertNull(element.getFileName());
090: assertEquals(345, element.getLineNumber());
091: }
092:
093: public void testCreateNewInstance_NPE() throws Exception {
094: try {
095: StackTraceElement element = new StackTraceElement(null,
096: "someMethod", "somefile", 345);
097: fail(element.toString());
098: } catch (NullPointerException e) {
099: //ok
100: }
101: try {
102: StackTraceElement element = new StackTraceElement(
103: "SomeClass", null, "somefile", 345);
104: fail(element.toString());
105: } catch (NullPointerException e) {
106: //ok
107: }
108: }
109: }
|