1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.exceptions;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertTrue;
20
21 import java.lang.reflect.InvocationTargetException;
22
23 import org.apache.ibatis.binding.BindingException;
24 import org.apache.ibatis.builder.BuilderException;
25 import org.apache.ibatis.cache.CacheException;
26 import org.apache.ibatis.datasource.DataSourceException;
27 import org.apache.ibatis.executor.ExecutorException;
28 import org.apache.ibatis.logging.LogException;
29 import org.apache.ibatis.parsing.ParsingException;
30 import org.apache.ibatis.plugin.PluginException;
31 import org.apache.ibatis.reflection.ReflectionException;
32 import org.apache.ibatis.scripting.ScriptingException;
33 import org.apache.ibatis.session.SqlSessionException;
34 import org.apache.ibatis.transaction.TransactionException;
35 import org.apache.ibatis.type.TypeException;
36 import org.junit.jupiter.api.Test;
37
38 class GeneralExceptionsTest {
39
40 private static final String EXPECTED_MESSAGE = "Test Message";
41 private static final Exception EXPECTED_CAUSE = new Exception("Nested Exception");
42
43 @Test
44 void should() {
45 RuntimeException thrown = ExceptionFactory.wrapException(EXPECTED_MESSAGE, EXPECTED_CAUSE);
46 assertTrue(thrown instanceof PersistenceException, "Exception should be wrapped in RuntimeSqlException.");
47 testThrowException(thrown);
48 }
49
50 @Test
51 void shouldInstantiateAndThrowAllCustomExceptions() throws Exception {
52 Class<?>[] exceptionTypes = {
53 BindingException.class,
54 CacheException.class,
55 DataSourceException.class,
56 ExecutorException.class,
57 LogException.class,
58 ParsingException.class,
59 BuilderException.class,
60 PluginException.class,
61 ReflectionException.class,
62 PersistenceException.class,
63 SqlSessionException.class,
64 TransactionException.class,
65 TypeException.class,
66 ScriptingException.class
67 };
68 for (Class<?> exceptionType : exceptionTypes) {
69 testExceptionConstructors(exceptionType);
70 }
71
72 }
73
74 private void testExceptionConstructors(Class<?> exceptionType) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
75 Exception e = (Exception) exceptionType.getDeclaredConstructor().newInstance();
76 testThrowException(e);
77 e = (Exception) exceptionType.getConstructor(String.class).newInstance(EXPECTED_MESSAGE);
78 testThrowException(e);
79 e = (Exception) exceptionType.getConstructor(String.class, Throwable.class).newInstance(EXPECTED_MESSAGE, EXPECTED_CAUSE);
80 testThrowException(e);
81 e = (Exception) exceptionType.getConstructor(Throwable.class).newInstance(EXPECTED_CAUSE);
82 testThrowException(e);
83 }
84
85 private void testThrowException(Exception thrown) {
86 try {
87 throw thrown;
88 } catch (Exception caught) {
89 assertEquals(thrown.getMessage(), caught.getMessage());
90 assertEquals(thrown.getCause(), caught.getCause());
91 }
92 }
93
94 }