001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Dmitry B. Yershov
020: * @version $Revision$
021: */package java.lang;
022:
023: import junit.framework.TestCase;
024:
025: public class StackTraceElementTest extends TestCase {
026:
027: /**
028: * Constructor under test StackTraceElement(String, int, String,
029: * String, boolean)
030: */
031: public void testStackTraceElement() {
032: String[] fileName = new String[] { null, "", "file" };
033: int[] lineNumber = new int[] { -10, 0, 10 };
034: String declaringClass = "class";
035: String methodName = "method";
036: try {
037: for (int i = 0; i < 3; i++)
038: for (int j = 0; j < 3; j++)
039: new StackTraceElement(declaringClass, methodName,
040: fileName[i], lineNumber[j]);
041: } catch (Throwable th) {
042: fail("Constructor should work with all types of data");
043: }
044: }
045:
046: /**
047: * Method under test boolean Equals(Object)
048: */
049: public void testEquals() {
050: StackTraceElement ste = new StackTraceElement("class",
051: "method", "file", 2);
052: StackTraceElement ste1 = new StackTraceElement("class",
053: "method", "file", -2);
054: StackTraceElement ste2 = new StackTraceElement("class1",
055: "method", "file", 2);
056: StackTraceElement ste3 = new StackTraceElement("class",
057: "method1", "file", 2);
058: StackTraceElement ste4 = new StackTraceElement("class",
059: "method", "file1", 2);
060: StackTraceElement ste5 = new StackTraceElement("class",
061: "method", "file", 2);
062: assertFalse("Assert 0: objects should differ", ste
063: .equals(new Object()));
064: assertFalse("Assert 1: objects should differ", ste.equals(ste1));
065: assertFalse("Assert 2: objects should differ", ste.equals(ste2));
066: assertFalse("Assert 3: objects should differ", ste.equals(ste3));
067: assertFalse("Assert 4: objects should differ", ste.equals(ste4));
068: assertTrue("Assert 5: objects should equal", ste.equals(ste5));
069: }
070:
071: /**
072: * Method under test String getClassName()
073: */
074: public void testGetClassName() {
075: StackTraceElement ste = new StackTraceElement("class",
076: "method", "file", 1);
077: assertEquals("incorrect class name", "class", ste
078: .getClassName());
079: }
080:
081: /**
082: * Method under test String getFileName()
083: */
084: public void testGetFileName() {
085: StackTraceElement ste = new StackTraceElement("class",
086: "method", "file", 1);
087: assertEquals("incorrect file name", "file", ste.getFileName());
088: }
089:
090: /**
091: * Method under test int getLineNumber()
092: */
093: public void testGetLineNumber() {
094: StackTraceElement ste = new StackTraceElement("class",
095: "method", "file", 1);
096: assertEquals("incorrect line number", 1, ste.getLineNumber());
097: }
098:
099: /**
100: * Method under test String getMethodName()
101: */
102: public void testGetMethodName() {
103: StackTraceElement ste = new StackTraceElement("class",
104: "method", "file", 1);
105: assertEquals("incorrect file name", "method", ste
106: .getMethodName());
107: }
108:
109: /**
110: * Method under test int hashCode()
111: */
112: public void testHashCode() {
113: StackTraceElement ste = new StackTraceElement("class",
114: "method", "file", 1);
115: StackTraceElement ste1 = new StackTraceElement("class",
116: "method", "file", 1);
117: assertEquals("hash codes should equal", ste.hashCode(), ste1
118: .hashCode());
119: }
120:
121: /**
122: * Method under test boolean isNativeMethod()
123: * when file name is null
124: */
125: public void testIsNativeMethod() {
126: StackTraceElement ste = new StackTraceElement("class",
127: "method", null, -2);
128: assertTrue("method should be native", ste.isNativeMethod());
129: ste = new StackTraceElement("class", "method", "file", 1);
130: assertFalse("method should not be native", ste.isNativeMethod());
131: }
132:
133: /**
134: * Method under test boolean isNativeMethod()
135: * when file name is not null
136: */
137: public void testIsNativeMethod_FileNotNull() {
138: StackTraceElement ste = new StackTraceElement("class",
139: "method", "file", -2);
140: assertTrue("method should be native", ste.isNativeMethod());
141: ste = new StackTraceElement("class", "method", "file", 1);
142: assertFalse("method should not be native", ste.isNativeMethod());
143: }
144:
145: /**
146: * Method under test String toString()
147: */
148: public void testToString() {
149: StackTraceElement ste = new StackTraceElement("class",
150: "method", "file", 1);
151: assertEquals("Assert 0: incorrect output",
152: "class.method(file:1)", ste.toString());
153: ste = new StackTraceElement("class", "method", "file", -1);
154: assertEquals("Assert 0: incorrect output",
155: "class.method(file)", ste.toString());
156: ste = new StackTraceElement("class", "method", null, 1);
157: assertEquals("Assert 0: incorrect output",
158: "class.method(Unknown Source)", ste.toString());
159: ste = new StackTraceElement("class", "method", null, -1);
160: assertEquals("Assert 0: incorrect output",
161: "class.method(Unknown Source)", ste.toString());
162: ste = new StackTraceElement("class", "method", null, -2);
163: assertEquals("Assert 0: incorrect output",
164: "class.method(Native Method)", ste.toString());
165: }
166:
167: /**
168: * Method under test String toString()
169: * when file name is null and line number is -2
170: */
171: public void testToString_FileNotNullLineMinus2() {
172: StackTraceElement ste = new StackTraceElement("class",
173: "method", "file", -2);
174: assertEquals("incorrect output", "class.method(file)", ste
175: .toString());
176: }
177: }
|