001: /* Copyright 2001 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.lang;
007:
008: import junit.framework.TestCase;
009:
010: /**
011: * <p>The <code>StackTraceElement_Test</code> </p>
012: *
013: * @author <a href="mailto:jnielsen@sct.com">Jan Nielsen</a>
014: *
015: * @version "$Revision: 35086 $"
016: **/
017: public class StackTrace_Test extends TestCase {
018:
019: /** Class version identifier. */
020: public static final String RCS_ID = "@(#) $Header$";
021:
022: /**
023: * Run all the test cases defined in the class.
024: *
025: * @param args not used
026: **/
027: public static void main(String[] args) {
028: junit.textui.TestRunner.run(suite());
029: }
030:
031: /**
032: * Build a test suite using reflection.
033: *
034: * @return test suite for the class
035: **/
036: public static junit.framework.TestSuite suite() {
037: return new junit.framework.TestSuite(StackTrace_Test.class);
038: }
039:
040: /**
041: * Setup for each test method.
042: **/
043: public void setUp() {
044: }
045:
046: /**
047: * Tear down for each test method.
048: **/
049: public void tearDown() {
050: }
051:
052: public StackTrace_Test(String name) {
053: super (name);
054: }
055:
056: public void test_getStackTraceElement() {
057: StackTrace[] frames = StackTrace.getStackTrace();
058: assertNotNull(frames);
059: }
060:
061: public void test_getStackTraceElement_0() {
062: StackTrace frame = StackTrace.getStackTrace(0);
063: assertNotNull(frame);
064: }
065:
066: public void test_getStackTraceElement_1() {
067: StackTrace frame = StackTrace.getStackTrace(1);
068: assertNotNull(frame);
069: }
070:
071: public void test_getStackTraceElement_minus_1() {
072: StackTrace frame = StackTrace.getStackTrace(-1);
073: assertNull(frame);
074: }
075:
076: public void test_getStackTraceElement_Integer_MAX_VALUE() {
077: StackTrace frame = StackTrace.getStackTrace(Integer.MAX_VALUE);
078: assertNull(frame);
079: }
080:
081: public void test_getStackTraceElement_Integer_MIN_VALUE() {
082: StackTrace frame = StackTrace.getStackTrace(Integer.MIN_VALUE);
083: assertNull(frame);
084: }
085:
086: public void test_toString() {
087: StackTrace[] frames = StackTrace.getStackTrace();
088: String trace = StackTrace.toString(frames);
089: assertNotNull(trace);
090: }
091:
092: public void test_getFileName() {
093: StackTrace frame = StackTrace.getStackTrace(0);
094: assertNotNull("Stack trace element should not be null.", frame);
095: String fileName = frame.getFileName();
096: assertNotNull("File name should not be null.", fileName);
097: assertTrue("Invalid file name:" + fileName,
098: "StackTrace_Test.java".equals(fileName)
099: || "Unknown Source".equals(fileName));
100: }
101:
102: public void test_getClassName() {
103: StackTrace frame = StackTrace.getStackTrace(0);
104: assertNotNull(frame);
105: assertEquals(getClass().getName(), frame.getClassName());
106: }
107:
108: public void test_getMethodName() {
109: StackTrace frame = StackTrace.getStackTrace(0);
110: assertNotNull(frame);
111: assertEquals("test_getMethodName", frame.getMethodName());
112: }
113:
114: public void test_getLineNumber() {
115: StackTrace frame = StackTrace.getStackTrace(0);
116: assertNotNull(frame);
117: int lineNumber = frame.getLineNumber();
118: assertTrue("Invalid line number: " + lineNumber,
119: lineNumber == -1 || lineNumber == 133);
120: }
121:
122: public void test_StackTrace() {
123: StackTrace[] trace = null;
124:
125: try {
126: // trigger an exception which contains "Unknown Source",
127: // "Native Method", and source code lines, and validate
128: // each frame.
129: Class.forName("someUnknownClass").newInstance();
130: } catch (Exception x) {
131: trace = StackTrace.getStackTrace(x);
132: }
133:
134: for (int i = 0; i < trace.length; i++) {
135: if ("java.net.URLClassLoader$1".equals(trace[i]
136: .getClassName())) {
137: //TestCase.assertEquals( "Unknown Source", trace[i].getFileName() );
138: //TestCase.assertEquals( -1, trace[i].getLineNumber() );
139: TestCase.assertEquals("run", trace[i].getMethodName());
140: TestCase.assertEquals(false, trace[i].isNativeMethod());
141: } else if ("java.security.AccessController".equals(trace[i]
142: .getClassName())) {
143: //TestCase.assertEquals( "Unknown Source", trace[i].getFileName() );
144: //TestCase.assertEquals( -2, trace[i].getLineNumber() );
145: TestCase.assertEquals("doPrivileged", trace[i]
146: .getMethodName());
147: TestCase.assertEquals(true, trace[i].isNativeMethod());
148: }
149: }
150: }
151: }
|