Debian Codegraph @

sourceTest/java/ch/systemsx/cisd/base/exceptions/IOExceptionUncheckedTests.java

raw
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 static org.testng.AssertJUnit.assertEquals;
20 import static org.testng.AssertJUnit.assertNotNull;
21 import static org.testng.AssertJUnit.assertNull;
22 import static org.testng.AssertJUnit.assertTrue;
23 import static org.testng.AssertJUnit.fail;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.lang.reflect.InvocationTargetException;
30 import java.lang.reflect.Method;
31
32 import org.testng.annotations.Test;
33
34 import ch.systemsx.cisd.base.BuildAndEnvironmentInfo;
35 import ch.systemsx.cisd.base.exceptions.CheckedExceptionTunnel;
36 import ch.systemsx.cisd.base.exceptions.IOExceptionUnchecked;
37
38 /**
39 * Test cases for {@link IOExceptionUnchecked}.
40 *
41 * @author Bernd Rinn
42 */
43 public class IOExceptionUncheckedTests
44 {
45 @SuppressWarnings("resource")
46 private void generateFileNotFoundException() throws IOExceptionUnchecked
47 {
48 try
49 {
50 new FileInputStream(new File("doesnt.exist"));
51 } catch (IOException ex)
52 {
53 throw CheckedExceptionTunnel.wrapIfNecessary(ex);
54 }
55 }
56
57 @Test
58 public void testWrapUnwrapIOException()
59 {
60 try
61 {
62 generateFileNotFoundException();
63 fail("No IOException thrown");
64 } catch (IOExceptionUnchecked e)
65 {
66 final Exception ex = CheckedExceptionTunnel.unwrapIfNecessary(e);
67 assertTrue(ex instanceof FileNotFoundException);
68 assertTrue(ex.getMessage().startsWith("doesnt.exist"));
69 }
70 }
71
72 @Test
73 public void testWrapUnwrapNonIOException()
74 {
75 try
76 {
77 throw new IOExceptionUnchecked(new IllegalStateException("Don't like this state"));
78 } catch (CheckedExceptionTunnel e)
79 {
80 final Exception ex = CheckedExceptionTunnel.unwrapIfNecessary(e);
81 assertTrue(ex instanceof IOException);
82 assertEquals("IllegalStateException: Don't like this state", ex.getMessage());
83 assertNotNull(ex.getCause());
84 assertTrue(ex.getCause() instanceof IllegalStateException);
85 assertEquals("Don't like this state", ex.getCause().getMessage());
86 }
87 }
88
89 @Test
90 public void testWrapUnwrapIOExceptionGivingMsg()
91 {
92 try
93 {
94 throw new IOExceptionUnchecked("Some message");
95 } catch (CheckedExceptionTunnel e)
96 {
97 final Exception ex = CheckedExceptionTunnel.unwrapIfNecessary(e);
98 assertTrue(ex instanceof IOException);
99 assertEquals("Some message", ex.getMessage());
100 assertNull(ex.getCause());
101 }
102 }
103
104 public static void main(String[] args) throws Throwable
105 {
106 System.out.println(BuildAndEnvironmentInfo.INSTANCE);
107 System.out.println("Test class: " + IOExceptionUncheckedTests.class.getSimpleName());
108 System.out.println();
109 final IOExceptionUncheckedTests test = new IOExceptionUncheckedTests();
110 for (Method m : IOExceptionUncheckedTests.class.getMethods())
111 {
112 final Test testAnnotation = m.getAnnotation(Test.class);
113 if (testAnnotation == null)
114 {
115 continue;
116 }
117 if (m.getParameterTypes().length == 0)
118 {
119 System.out.println("Running " + m.getName());
120 try
121 {
122 m.invoke(test);
123 } catch (InvocationTargetException wrapperThrowable)
124 {
125 final Throwable th = wrapperThrowable.getCause();
126 boolean exceptionFound = false;
127 for (Class<?> expectedExClazz : testAnnotation.expectedExceptions())
128 {
129 if (expectedExClazz == th.getClass())
130 {
131 exceptionFound = true;
132 break;
133 }
134 }
135 if (exceptionFound == false)
136 {
137 throw th;
138 }
139 }
140 }
141 }
142 System.out.println("Tests OK!");
143 }
144
145 }