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: package org.apache.commons.lang;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023: import java.lang.reflect.Constructor;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028: import junit.textui.TestRunner;
029:
030: import org.apache.commons.lang.exception.NestableException;
031:
032: /**
033: * JUnit tests.
034: *
035: * @author Matthew Hawthorne
036: * @version $Id: NotImplementedExceptionTest.java 437554 2006-08-28 06:21:41Z bayard $
037: * @see NotImplementedException
038: */
039: public class NotImplementedExceptionTest extends TestCase {
040:
041: public static void main(String[] args) {
042: TestRunner.run(suite());
043: }
044:
045: public static Test suite() {
046: return new TestSuite(NotImplementedExceptionTest.class);
047: }
048:
049: public NotImplementedExceptionTest(String testName) {
050: super (testName);
051: }
052:
053: //-----------------------------------------------------------------------
054: public void testConstructor_() {
055: NotImplementedException ex = new NotImplementedException();
056: assertEquals("Code is not implemented", ex.getMessage());
057: assertEquals(null, ex.getCause());
058: assertEquals("Code is not implemented", ex.getMessage());
059: }
060:
061: public void testConstructor_String1() {
062: NotImplementedException ex = new NotImplementedException(
063: (String) null);
064: assertEquals("Code is not implemented", ex.getMessage());
065: assertEquals(null, ex.getCause());
066: assertEquals("Code is not implemented", ex.getMessage());
067: }
068:
069: public void testConstructor_String2() {
070: NotImplementedException ex = new NotImplementedException("msg");
071: assertEquals("msg", ex.getMessage());
072: assertEquals(null, ex.getCause());
073: assertEquals("msg", ex.getMessage());
074: }
075:
076: public void testConstructor_Throwable1() {
077: NotImplementedException ex = new NotImplementedException(
078: (Throwable) null);
079: assertEquals("Code is not implemented", ex.getMessage());
080: assertEquals(null, ex.getCause());
081: assertEquals("Code is not implemented", ex.getMessage());
082: }
083:
084: public void testConstructor_Throwable2() {
085: Exception npe = new NullPointerException();
086: NotImplementedException ex = new NotImplementedException(npe);
087: assertEquals("Code is not implemented", ex.getMessage());
088: assertSame(npe, ex.getCause());
089: assertEquals("Code is not implemented", ex.getMessage());
090: }
091:
092: public void testConstructor_StringThrowable1() {
093: NotImplementedException ex = new NotImplementedException(
094: (String) null, (Throwable) null);
095: assertEquals("Code is not implemented", ex.getMessage());
096: assertEquals(null, ex.getCause());
097: assertEquals("Code is not implemented", ex.getMessage());
098: }
099:
100: public void testConstructor_StringThrowable2() {
101: Exception npe = new NullPointerException();
102: NotImplementedException ex = new NotImplementedException("msg",
103: npe);
104: assertEquals("msg", ex.getMessage());
105: assertSame(npe, ex.getCause());
106: assertEquals("msg", ex.getMessage());
107: }
108:
109: public void testConstructor_Class1() {
110: NotImplementedException ex = new NotImplementedException(
111: (Class) null);
112: assertEquals("Code is not implemented", ex.getMessage());
113: assertEquals(null, ex.getCause());
114: assertEquals("Code is not implemented", ex.getMessage());
115: }
116:
117: public void testConstructor_Class2() {
118: NotImplementedException ex = new NotImplementedException(
119: String.class);
120: assertEquals(
121: "Code is not implemented in class java.lang.String", ex
122: .getMessage());
123: assertEquals(null, ex.getCause());
124: assertEquals(
125: "Code is not implemented in class java.lang.String", ex
126: .getMessage());
127: }
128:
129: public void testGetMessage_Indexed() throws Exception {
130: if (SystemUtils.isJavaVersionAtLeast(1.4f)) {
131: Exception ex1 = new Exception("nested 2");
132: Constructor con = Exception.class
133: .getConstructor(new Class[] { String.class,
134: Throwable.class });
135: Exception ex2 = (Exception) con.newInstance(new Object[] {
136: "nested 1", ex1 });
137: NotImplementedException ex = new NotImplementedException(
138: ex2);
139: assertEquals("Code is not implemented", ex.getMessage());
140: assertEquals("Code is not implemented", ex.getMessage(0));
141: assertEquals("nested 1", ex.getMessage(1));
142: assertEquals("nested 2", ex.getMessage(2));
143:
144: String[] messages = ex.getMessages();
145: assertEquals(3, messages.length);
146: assertEquals("Code is not implemented", messages[0]);
147: assertEquals("nested 1", messages[1]);
148: assertEquals("nested 2", messages[2]);
149: }
150: }
151:
152: public void testGetThrowable() {
153: NotImplementedException ex = new NotImplementedException(
154: new NestableException("nested 1",
155: new NestableException("nested 2")));
156:
157: assertEquals(3, ex.getThrowableCount());
158:
159: assertEquals(NotImplementedException.class, ex.getThrowable(0)
160: .getClass());
161: assertEquals("Code is not implemented", ex.getThrowable(0)
162: .getMessage());
163: assertEquals(NestableException.class, ex.getThrowable(1)
164: .getClass());
165: assertEquals("nested 1", ex.getThrowable(1).getMessage());
166: assertEquals(NestableException.class, ex.getThrowable(2)
167: .getClass());
168: assertEquals("nested 2", ex.getThrowable(2).getMessage());
169:
170: assertEquals(3, ex.getThrowables().length);
171: assertEquals(NotImplementedException.class,
172: ex.getThrowables()[0].getClass());
173: assertEquals("Code is not implemented", ex.getThrowables()[0]
174: .getMessage());
175: assertEquals(NestableException.class, ex.getThrowables()[1]
176: .getClass());
177: assertEquals("nested 1", ex.getThrowables()[1].getMessage());
178: assertEquals(NestableException.class, ex.getThrowables()[2]
179: .getClass());
180: assertEquals("nested 2", ex.getThrowables()[2].getMessage());
181: }
182:
183: public void testIndexOfThrowable() {
184: NotImplementedException ex = new NotImplementedException(
185: new NestableException("nested 1",
186: new NestableException("nested 2")));
187: assertEquals(0, ex
188: .indexOfThrowable(NotImplementedException.class));
189: assertEquals(1, ex.indexOfThrowable(NestableException.class));
190: }
191:
192: public void testIndexOfThrowable_Index() {
193: NotImplementedException ex = new NotImplementedException(
194: new NestableException("nested 1",
195: new NestableException("nested 2")));
196: assertEquals(1, ex.indexOfThrowable(NestableException.class, 1));
197: }
198:
199: public void testPrintStackTrace() {
200: NotImplementedException ex = new NotImplementedException(
201: new NestableException("nested 1",
202: new NestableException("nested 2")));
203: ByteArrayOutputStream baos = new ByteArrayOutputStream();
204: PrintStream ps = new PrintStream(baos);
205: PrintStream errStream = System.err;
206: System.setErr(ps);
207: ex.printStackTrace();
208: System.setErr(errStream);
209: assertTrue(baos.toString().length() > 0);
210: }
211:
212: public void testPrintStackTrace_Stream() {
213: NotImplementedException ex = new NotImplementedException(
214: new NestableException("nested 1",
215: new NestableException("nested 2")));
216: ByteArrayOutputStream baos = new ByteArrayOutputStream();
217: PrintStream ps = new PrintStream(baos);
218: ex.printStackTrace(ps);
219: assertTrue(baos.toString().length() > 0);
220: }
221:
222: public void testPrintStackTrace_Writer() {
223: NotImplementedException ex = new NotImplementedException(
224: new NestableException("nested 1",
225: new NestableException("nested 2")));
226: StringWriter stringWriter = new StringWriter();
227: PrintWriter writer = new PrintWriter(stringWriter);
228: ex.printStackTrace(writer);
229: assertTrue(stringWriter.toString().length() > 0);
230: }
231:
232: public void testPrintPartialStackTrace_Writer() {
233: NotImplementedException ex = new NotImplementedException(
234: new NestableException("nested 1",
235: new NestableException("nested 2")));
236: StringWriter stringWriter = new StringWriter();
237: PrintWriter writer = new PrintWriter(stringWriter);
238: ex.printPartialStackTrace(writer);
239: assertTrue(stringWriter.toString().length() > 0);
240: }
241: }
|