View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
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    *       https://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  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  }