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 = { BindingException.class, CacheException.class, DataSourceException.class,
53 ExecutorException.class, LogException.class, ParsingException.class, BuilderException.class,
54 PluginException.class, ReflectionException.class, PersistenceException.class, SqlSessionException.class,
55 TransactionException.class, TypeException.class, ScriptingException.class };
56 for (Class<?> exceptionType : exceptionTypes) {
57 testExceptionConstructors(exceptionType);
58 }
59
60 }
61
62 private void testExceptionConstructors(Class<?> exceptionType)
63 throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
64 Exception e = (Exception) exceptionType.getDeclaredConstructor().newInstance();
65 testThrowException(e);
66 e = (Exception) exceptionType.getConstructor(String.class).newInstance(EXPECTED_MESSAGE);
67 testThrowException(e);
68 e = (Exception) exceptionType.getConstructor(String.class, Throwable.class).newInstance(EXPECTED_MESSAGE,
69 EXPECTED_CAUSE);
70 testThrowException(e);
71 e = (Exception) exceptionType.getConstructor(Throwable.class).newInstance(EXPECTED_CAUSE);
72 testThrowException(e);
73 }
74
75 private void testThrowException(Exception thrown) {
76 try {
77 throw thrown;
78 } catch (Exception caught) {
79 assertEquals(thrown.getMessage(), caught.getMessage());
80 assertEquals(thrown.getCause(), caught.getCause());
81 }
82 }
83
84 }