| 1 |
/* |
| 2 |
* Copyright 2007 - 2018 ETH Zuerich, CISD and SIS. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package ch.systemsx.cisd.base.exceptions; |
| 18 |
|
| 19 |
import java.io.IOException; |
| 20 |
import java.io.PrintWriter; |
| 21 |
import java.io.StringWriter; |
| 22 |
|
| 23 |
import org.apache.commons.lang3.StringUtils; |
| 24 |
import org.testng.annotations.Test; |
| 25 |
|
| 26 |
import static org.testng.AssertJUnit.*; |
| 27 |
|
| 28 |
/** |
| 29 |
* Test cases for {@link CheckedExceptionTunnel} |
| 30 |
* |
| 31 |
* @author Bernd Rinn |
| 32 |
*/ |
| 33 |
public class CheckedExceptionTunnelTest |
| 34 |
{ |
| 35 |
|
| 36 |
@Test |
| 37 |
public void testGetMessage() |
| 38 |
{ |
| 39 |
final IOException ioe = new IOException("This is the message"); |
| 40 |
final RuntimeException re = CheckedExceptionTunnel.wrapIfNecessary(ioe); |
| 41 |
assertEquals("This is the message", re.getMessage()); |
| 42 |
} |
| 43 |
|
| 44 |
@Test |
| 45 |
public void testToString() |
| 46 |
{ |
| 47 |
final InterruptedException ioe = new InterruptedException("Somehow got interrupted"); |
| 48 |
final RuntimeException re = CheckedExceptionTunnel.wrapIfNecessary(ioe); |
| 49 |
assertEquals("java.lang.InterruptedException: Somehow got interrupted", re.toString()); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void testPrintStackTrace() |
| 54 |
{ |
| 55 |
final InterruptedException ie = new InterruptedException("Somehow got interrupted"); |
| 56 |
ie.fillInStackTrace(); |
| 57 |
final RuntimeException re = CheckedExceptionTunnel.wrapIfNecessary(ie); |
| 58 |
StringWriter sw = new StringWriter(); |
| 59 |
PrintWriter pw = new PrintWriter(sw); |
| 60 |
re.printStackTrace(pw); |
| 61 |
final String[] lines = StringUtils.split(sw.toString(), '\n'); |
| 62 |
assertEquals("java.lang.InterruptedException: Somehow got interrupted", lines[0]); |
| 63 |
assertTrue( |
| 64 |
lines[1], |
| 65 |
lines[1].startsWith("\tat ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnelTest." |
| 66 |
+ "testPrintStackTrace(CheckedExceptionTunnelTest.java")); |
| 67 |
} |
| 68 |
|
| 69 |
@Test |
| 70 |
public void testPrintFullStackTrace() |
| 71 |
{ |
| 72 |
final Exception e = new Exception("Some exceptional condition"); |
| 73 |
e.fillInStackTrace(); |
| 74 |
final CheckedExceptionTunnel re = new CheckedExceptionTunnel(e); |
| 75 |
StringWriter sw = new StringWriter(); |
| 76 |
PrintWriter pw = new PrintWriter(sw); |
| 77 |
re.printFullStackTrace(); |
| 78 |
re.printFullStackTrace(pw); |
| 79 |
final String[] lines = StringUtils.split(sw.toString(), '\n'); |
| 80 |
assertEquals( |
| 81 |
"ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel: Some exceptional condition", |
| 82 |
lines[0]); |
| 83 |
assertTrue( |
| 84 |
lines[1], |
| 85 |
lines[1].startsWith("\tat ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnelTest." |
| 86 |
+ "testPrintFullStackTrace(CheckedExceptionTunnelTest.java:")); |
| 87 |
} |
| 88 |
} |