Skip to content

Commit

Permalink
Put the direct cause in the error message
Browse files Browse the repository at this point in the history
And let the maven plugins do the cleanup.
  • Loading branch information
harawata committed Dec 27, 2024
1 parent c29ce33 commit f6c28a6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -784,11 +784,10 @@ private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw
List<Object> constructorArgs, Constructor<?> constructor, boolean foundValues) throws SQLException {
Class<?>[] parameterTypes = constructor.getParameterTypes();

// constructor parameter is allowed to be less than or equal to the number of result set columns, but not greater than
if (parameterTypes.length > rsw.getClassNames().size()) {
throw new ExecutorException(MessageFormat.format(
"Column order based constructor auto-mapping of ''{0}'' failed. Because result set type is ''{1}''.",
constructor, rsw.getClassNames()));
"Constructor auto-mapping of ''{0}'' failed. The constructor takes ''{1}'' arguments, but there are only ''{2}'' columns in the result set.",
constructor, parameterTypes.length, rsw.getClassNames().size()));
}

for (int i = 0; i < parameterTypes.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.Reader;
import java.text.MessageFormat;
import java.util.List;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
Expand All @@ -39,14 +42,14 @@ class ColumnOrderBasedConstructorAutomappingTest {
static void setUp() throws Exception {
// create an SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader(
"org/apache/ibatis/submitted/column_order_based_constructor_automapping/mybatis-config.xml")) {
"org/apache/ibatis/submitted/column_order_based_constructor_automapping/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(false);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/column_order_based_constructor_automapping/CreateDB.sql");
"org/apache/ibatis/submitted/column_order_based_constructor_automapping/CreateDB.sql");
}

@Test
Expand Down Expand Up @@ -120,10 +123,14 @@ void shouldNotHandleConstructorGreaterThanResultSet() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);

PersistenceException persistenceException = assertThrows(PersistenceException.class, mapper::finaAllByConstructorGreaterThanResultSet);
PersistenceException persistenceException = assertThrows(PersistenceException.class,
mapper::finaAllByConstructorGreaterThanResultSet);
assertNotNull(persistenceException);
assertNotNull(persistenceException.getMessage());
assertTrue(persistenceException.getMessage().contains("Column order based constructor auto-mapping of"));
String message = persistenceException.getMessage();
assertNotNull(message);
assertTrue(message.contains(MessageFormat.format(
"Constructor auto-mapping of ''{0}'' failed. The constructor takes ''{1}'' arguments, but there are only ''{2}'' columns in the result set.",
UserConstructorGreaterThanResultSet.class.getConstructors()[0], 4, 3)));
}
}
}

0 comments on commit f6c28a6

Please sign in to comment.