diff --git a/.travis.yml b/.travis.yml index 1a271cbde8..321f0956c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,6 @@ sudo: false jdk: - oraclejdk8 - - oraclejdk7 - - openjdk7 - - openjdk6 after_success: - chmod -R 777 ./travis/after_success.sh diff --git a/pom.xml b/pom.xml index 4f5c8b9c41..05fb3cc70e 100644 --- a/pom.xml +++ b/pom.xml @@ -23,11 +23,11 @@ org.mybatis mybatis-parent 28 - + mybatis-spring - 1.3.2-SNAPSHOT + 2.0.0-SNAPSHOT jar mybatis-spring @@ -35,12 +35,11 @@ http://www.mybatis.org/spring/ - Alex Rykov Alex.Rykov@gmail.com - + Andrius Juozapaitis andriusj@gmail.com @@ -103,9 +102,22 @@ 1.2.2 org.mybatis.spring.*,org.mybatis.spring.mapper.*,org.mybatis.spring.support.*,org.mybatis.spring.transaction.* Spring + 1.8 + 1.8 + 1.8 + 1.8 org.springframework.batch.*;resolution:=optional,* * - 4.3.8.RELEASE + + 5.0.0.M5 + 4.0.0.M1 + 1.0.0-M4 + 5.0.0-M4 + + + org.codehaus.mojo.signature + java18 + 1.0 @@ -133,7 +145,7 @@ org.springframework.batch spring-batch-infrastructure - 3.0.7.RELEASE + 4.0.0.M1 provided @@ -143,24 +155,37 @@ com.atomikos transactions-jdbc - 4.0.3 + 4.0.4 test org.apache.derby derby - 10.12.1.1 + 10.13.1.1 + test + + + + org.junit.jupiter + junit-jupiter-api + 5.0.0-M4 test - junit - junit - 4.12 + org.junit.jupiter + junit-jupiter-engine + ${junit.version} test + + org.junit.platform + junit-platform-runner + ${junit-platform.version} + + org.jboss.byteman byteman-bmunit @@ -196,20 +221,32 @@ test + + org.mockito + mockito-core + 2.7.22 + + + + org.assertj + assertj-core + 3.6.2 + + com.mockrunner mockrunner-core - 1.0.1 + 1.1.2 test - commons-logging - commons-logging + commons-logging + commons-logging - com.kirkk - jaranalyzer + com.kirkk + jaranalyzer jdom @@ -224,7 +261,7 @@ com.mockrunner mockrunner-ejb - 1.0.1 + 1.1.2 test @@ -240,28 +277,38 @@ com.mockrunner mockrunner-jdbc - 1.0.1 + 1.1.2 test javax.transaction - transaction-api - 1.1 + javax.transaction-api + 1.2 test javax.servlet javax.servlet-api - 3.0.1 + 3.1.0 test + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone + + false + + + + - org.apache.maven.plugins maven-surefire-plugin @@ -275,10 +322,21 @@ com.atomikos.icatch.log_base_dir ${project.build.directory} - + + + + org.junit.platform + junit-platform-surefire-provider + ${junit-platform.version} + + + org.junit.jupiter + junit-jupiter-engine + ${junit.version} + + - org.apache.maven.plugins maven-site-plugin diff --git a/src/main/java/org/mybatis/logging/Logger.java b/src/main/java/org/mybatis/logging/Logger.java new file mode 100644 index 0000000000..cf604f0d17 --- /dev/null +++ b/src/main/java/org/mybatis/logging/Logger.java @@ -0,0 +1,59 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.logging; + +import org.apache.ibatis.logging.Log; + +import java.util.function.Supplier; + +/** + * Wrapper of {@link Log}, allow log with lambda expressions. + * + * @author Putthiphong Boonphong + */ +public class Logger { + + private Log log; + + Logger(Log log) { + this.log = log; + } + + public void error(Supplier s, Throwable e) { + log.error(s.get(), e); + } + + public void error(Supplier s) { + log.error(s.get()); + } + + public void warn(Supplier s) { + log.warn(s.get()); + } + + public void debug(Supplier s) { + if (log.isDebugEnabled()) { + log.debug(s.get()); + } + } + + public void trace(Supplier s) { + if (log.isTraceEnabled()) { + log.trace(s.get()); + } + } + +} diff --git a/src/main/java/org/mybatis/logging/LoggerFactory.java b/src/main/java/org/mybatis/logging/LoggerFactory.java new file mode 100644 index 0000000000..f64443c5b0 --- /dev/null +++ b/src/main/java/org/mybatis/logging/LoggerFactory.java @@ -0,0 +1,35 @@ +/** + * Copyright 2010-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mybatis.logging; + +import org.apache.ibatis.logging.LogFactory; + +/** + * LoggerFactory is a wrapper around {@link LogFactory} to support {@link Logger}. + * + * @author Putthiphong Boonphong + */ +public class LoggerFactory { + + public static Logger getLogger(Class aClass) { + return new Logger(LogFactory.getLog(aClass)); + } + + public static Logger getLogger(String logger) { + return new Logger(LogFactory.getLog(logger)); + } + +} diff --git a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java index 793e640967..530730f68f 100644 --- a/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java +++ b/src/main/java/org/mybatis/spring/SqlSessionFactoryBean.java @@ -32,8 +32,6 @@ import org.apache.ibatis.cache.Cache; import org.apache.ibatis.executor.ErrorContext; import org.apache.ibatis.io.VFS; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.mapping.DatabaseIdProvider; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.plugin.Interceptor; @@ -44,6 +42,8 @@ import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.type.TypeHandler; +import org.mybatis.logging.Logger; +import org.mybatis.logging.LoggerFactory; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; @@ -64,7 +64,7 @@ * demarcation in combination with a {@code SqlSessionFactory}. JTA should be used for transactions * which span multiple databases or when container managed transactions (CMT) are being used. * - * @author Putthibong Boonbong + * @author Putthiphong Boonphong * @author Hunter Presnall * @author Eduardo Macarron * @author Eddú Meléndez @@ -75,7 +75,7 @@ */ public class SqlSessionFactoryBean implements FactoryBean, InitializingBean, ApplicationListener { - private static final Log LOGGER = LogFactory.getLog(SqlSessionFactoryBean.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SqlSessionFactoryBean.class); private Resource configLocation; @@ -406,9 +406,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { xmlConfigBuilder = new XMLConfigBuilder(this.configLocation.getInputStream(), null, this.configurationProperties); configuration = xmlConfigBuilder.getConfiguration(); } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration"); - } + LOGGER.debug(() -> "Property 'configuration' or 'configLocation' not specified, using default MyBatis Configuration"); configuration = new Configuration(); if (this.configurationProperties != null) { configuration.setVariables(this.configurationProperties); @@ -433,27 +431,21 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { for (String packageToScan : typeAliasPackageArray) { configuration.getTypeAliasRegistry().registerAliases(packageToScan, typeAliasesSuperType == null ? Object.class : typeAliasesSuperType); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Scanned package: '" + packageToScan + "' for aliases"); - } + LOGGER.debug(() -> "Scanned package: '" + packageToScan + "' for aliases"); } } if (!isEmpty(this.typeAliases)) { for (Class typeAlias : this.typeAliases) { configuration.getTypeAliasRegistry().registerAlias(typeAlias); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Registered type alias: '" + typeAlias + "'"); - } + LOGGER.debug(() -> "Registered type alias: '" + typeAlias + "'"); } } if (!isEmpty(this.plugins)) { for (Interceptor plugin : this.plugins) { configuration.addInterceptor(plugin); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Registered plugin: '" + plugin + "'"); - } + LOGGER.debug(() -> "Registered plugin: '" + plugin + "'"); } } @@ -462,18 +454,14 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); for (String packageToScan : typeHandlersPackageArray) { configuration.getTypeHandlerRegistry().register(packageToScan); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Scanned package: '" + packageToScan + "' for type handlers"); - } + LOGGER.debug(() -> "Scanned package: '" + packageToScan + "' for type handlers"); } } if (!isEmpty(this.typeHandlers)) { for (TypeHandler typeHandler : this.typeHandlers) { configuration.getTypeHandlerRegistry().register(typeHandler); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Registered type handler: '" + typeHandler + "'"); - } + LOGGER.debug(() -> "Registered type handler: '" + typeHandler + "'"); } } @@ -492,10 +480,7 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { if (xmlConfigBuilder != null) { try { xmlConfigBuilder.parse(); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Parsed configuration file: '" + this.configLocation + "'"); - } + LOGGER.debug(() -> "Parsed configuration file: '" + this.configLocation + "'"); } catch (Exception ex) { throw new NestedIOException("Failed to parse config resource: " + this.configLocation, ex); } finally { @@ -524,15 +509,10 @@ protected SqlSessionFactory buildSqlSessionFactory() throws IOException { } finally { ErrorContext.instance().reset(); } - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Parsed mapper file: '" + mapperLocation + "'"); - } + LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'"); } } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Property 'mapperLocations' was not specified or no matching resources found"); - } + LOGGER.debug(() -> "Property 'mapperLocations' was not specified or no matching resources found"); } return this.sqlSessionFactoryBuilder.build(configuration); diff --git a/src/main/java/org/mybatis/spring/SqlSessionTemplate.java b/src/main/java/org/mybatis/spring/SqlSessionTemplate.java index d1c7840cfe..0ec3d902f9 100644 --- a/src/main/java/org/mybatis/spring/SqlSessionTemplate.java +++ b/src/main/java/org/mybatis/spring/SqlSessionTemplate.java @@ -67,7 +67,7 @@ * } * * - * @author Putthibong Boonbong + * @author Putthiphong Boonphong * @author Hunter Presnall * @author Eduardo Macarron * @@ -155,7 +155,7 @@ public PersistenceExceptionTranslator getPersistenceExceptionTranslator() { */ @Override public T selectOne(String statement) { - return this.sqlSessionProxy. selectOne(statement); + return this.sqlSessionProxy.selectOne(statement); } /** @@ -163,7 +163,7 @@ public T selectOne(String statement) { */ @Override public T selectOne(String statement, Object parameter) { - return this.sqlSessionProxy. selectOne(statement, parameter); + return this.sqlSessionProxy.selectOne(statement, parameter); } /** @@ -171,7 +171,7 @@ public T selectOne(String statement, Object parameter) { */ @Override public Map selectMap(String statement, String mapKey) { - return this.sqlSessionProxy. selectMap(statement, mapKey); + return this.sqlSessionProxy.selectMap(statement, mapKey); } /** @@ -179,7 +179,7 @@ public Map selectMap(String statement, String mapKey) { */ @Override public Map selectMap(String statement, Object parameter, String mapKey) { - return this.sqlSessionProxy. selectMap(statement, parameter, mapKey); + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey); } /** @@ -187,7 +187,7 @@ public Map selectMap(String statement, Object parameter, String map */ @Override public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) { - return this.sqlSessionProxy. selectMap(statement, parameter, mapKey, rowBounds); + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds); } /** @@ -219,7 +219,7 @@ public Cursor selectCursor(String statement, Object parameter, RowBounds */ @Override public List selectList(String statement) { - return this.sqlSessionProxy. selectList(statement); + return this.sqlSessionProxy.selectList(statement); } /** @@ -227,7 +227,7 @@ public List selectList(String statement) { */ @Override public List selectList(String statement, Object parameter) { - return this.sqlSessionProxy. selectList(statement, parameter); + return this.sqlSessionProxy.selectList(statement, parameter); } /** @@ -235,7 +235,7 @@ public List selectList(String statement, Object parameter) { */ @Override public List selectList(String statement, Object parameter, RowBounds rowBounds) { - return this.sqlSessionProxy. selectList(statement, parameter, rowBounds); + return this.sqlSessionProxy.selectList(statement, parameter, rowBounds); } /** diff --git a/src/main/java/org/mybatis/spring/SqlSessionUtils.java b/src/main/java/org/mybatis/spring/SqlSessionUtils.java index 352c08e799..d6be4e0e7d 100644 --- a/src/main/java/org/mybatis/spring/SqlSessionUtils.java +++ b/src/main/java/org/mybatis/spring/SqlSessionUtils.java @@ -18,12 +18,12 @@ import static org.springframework.util.Assert.notNull; import org.apache.ibatis.exceptions.PersistenceException; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.logging.Logger; +import org.mybatis.logging.LoggerFactory; import org.mybatis.spring.transaction.SpringManagedTransactionFactory; import org.springframework.dao.DataAccessException; import org.springframework.dao.TransientDataAccessResourceException; @@ -41,7 +41,7 @@ */ public final class SqlSessionUtils { - private static final Log LOGGER = LogFactory.getLog(SqlSessionUtils.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SqlSessionUtils.class); private static final String NO_EXECUTOR_TYPE_SPECIFIED = "No ExecutorType specified"; private static final String NO_SQL_SESSION_FACTORY_SPECIFIED = "No SqlSessionFactory specified"; @@ -93,10 +93,7 @@ public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, Executo return session; } - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Creating a new SqlSession"); - } - + LOGGER.debug(() -> "Creating a new SqlSession"); session = sessionFactory.openSession(executorType); registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session); @@ -114,7 +111,7 @@ public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, Executo * * @param sessionFactory sqlSessionFactory used for registration. * @param executorType executorType used for registration. - * @param exceptionTranslator persistenceExceptionTranslater used for registration. + * @param exceptionTranslator persistenceExceptionTranslator used for registration. * @param session sqlSession used for registration. */ private static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType, @@ -124,9 +121,7 @@ private static void registerSessionHolder(SqlSessionFactory sessionFactory, Exec Environment environment = sessionFactory.getConfiguration().getEnvironment(); if (environment.getTransactionFactory() instanceof SpringManagedTransactionFactory) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Registering transaction synchronization for SqlSession [" + session + "]"); - } + LOGGER.debug(() -> "Registering transaction synchronization for SqlSession [" + session + "]"); holder = new SqlSessionHolder(session, executorType, exceptionTranslator); TransactionSynchronizationManager.bindResource(sessionFactory, holder); @@ -135,19 +130,16 @@ private static void registerSessionHolder(SqlSessionFactory sessionFactory, Exec holder.requested(); } else { if (TransactionSynchronizationManager.getResource(environment.getDataSource()) == null) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("SqlSession [" + session + "] was not registered for synchronization because DataSource is not transactional"); - } + LOGGER.debug(() -> "SqlSession [" + session + "] was not registered for synchronization because DataSource is not transactional"); } else { throw new TransientDataAccessResourceException( "SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use Spring transaction synchronization"); } } } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("SqlSession [" + session + "] was not registered for synchronization because synchronization is not active"); - } + LOGGER.debug(() -> "SqlSession [" + session + "] was not registered for synchronization because synchronization is not active"); } + } private static SqlSession sessionHolder(ExecutorType executorType, SqlSessionHolder holder) { @@ -159,10 +151,7 @@ private static SqlSession sessionHolder(ExecutorType executorType, SqlSessionHol holder.requested(); - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Fetched SqlSession [" + holder.getSqlSession() + "] from current transaction"); - } - + LOGGER.debug(() -> "Fetched SqlSession [" + holder.getSqlSession() + "] from current transaction"); session = holder.getSqlSession(); } return session; @@ -182,14 +171,10 @@ public static void closeSqlSession(SqlSession session, SqlSessionFactory session SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); if ((holder != null) && (holder.getSqlSession() == session)) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Releasing transactional SqlSession [" + session + "]"); - } + LOGGER.debug(() -> "Releasing transactional SqlSession [" + session + "]"); holder.released(); } else { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Closing non transactional SqlSession [" + session + "]"); - } + LOGGER.debug(() -> "Closing non transactional SqlSession [" + session + "]"); session.close(); } } @@ -247,9 +232,7 @@ public int getOrder() { @Override public void suspend() { if (this.holderActive) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization suspending SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization suspending SqlSession [" + this.holder.getSqlSession() + "]"); TransactionSynchronizationManager.unbindResource(this.sessionFactory); } } @@ -260,9 +243,7 @@ public void suspend() { @Override public void resume() { if (this.holderActive) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization resuming SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization resuming SqlSession [" + this.holder.getSqlSession() + "]"); TransactionSynchronizationManager.bindResource(this.sessionFactory, this.holder); } } @@ -280,9 +261,7 @@ public void beforeCommit(boolean readOnly) { // TODO This updates 2nd level caches but the tx may be rolledback later on! if (TransactionSynchronizationManager.isActualTransactionActive()) { try { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization committing SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization committing SqlSession [" + this.holder.getSqlSession() + "]"); this.holder.getSqlSession().commit(); } catch (PersistenceException p) { if (this.holder.getPersistenceExceptionTranslator() != null) { @@ -306,14 +285,10 @@ public void beforeCompletion() { // Issue #18 Close SqlSession and deregister it now // because afterCompletion may be called from a different thread if (!this.holder.isOpen()) { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization deregistering SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization deregistering SqlSession [" + this.holder.getSqlSession() + "]"); TransactionSynchronizationManager.unbindResource(sessionFactory); this.holderActive = false; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization closing SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization closing SqlSession [" + this.holder.getSqlSession() + "]"); this.holder.getSqlSession().close(); } } @@ -326,14 +301,10 @@ public void afterCompletion(int status) { if (this.holderActive) { // afterCompletion may have been called from a different thread // so avoid failing if there is nothing in this one - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization deregistering SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization deregistering SqlSession [" + this.holder.getSqlSession() + "]"); TransactionSynchronizationManager.unbindResourceIfPossible(sessionFactory); this.holderActive = false; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Transaction synchronization closing SqlSession [" + this.holder.getSqlSession() + "]"); - } + LOGGER.debug(() -> "Transaction synchronization closing SqlSession [" + this.holder.getSqlSession() + "]"); this.holder.getSqlSession().close(); } this.holder.reset(); diff --git a/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java b/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java index 0e26535f9b..accb8e74b5 100644 --- a/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java +++ b/src/main/java/org/mybatis/spring/annotation/MapperScannerRegistrar.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,9 @@ import java.lang.annotation.Annotation; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; import org.mybatis.spring.mapper.ClassPathMapperScanner; import org.mybatis.spring.mapper.MapperFactoryBean; @@ -40,6 +42,7 @@ * * @author Michael Lanyon * @author Eduardo Macarron + * @author Putthiphong Boonphong * * @see MapperFactoryBean * @see ClassPathMapperScanner @@ -49,6 +52,14 @@ public class MapperScannerRegistrar implements ImportBeanDefinitionRegistrar, Re private ResourceLoader resourceLoader; + /** + * {@inheritDoc} + */ + @Override + public void setResourceLoader(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + /** * {@inheritDoc} */ @@ -86,30 +97,24 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B scanner.setSqlSessionTemplateBeanName(annoAttrs.getString("sqlSessionTemplateRef")); scanner.setSqlSessionFactoryBeanName(annoAttrs.getString("sqlSessionFactoryRef")); - List basePackages = new ArrayList(); - for (String pkg : annoAttrs.getStringArray("value")) { - if (StringUtils.hasText(pkg)) { - basePackages.add(pkg); - } - } - for (String pkg : annoAttrs.getStringArray("basePackages")) { - if (StringUtils.hasText(pkg)) { - basePackages.add(pkg); - } - } - for (Class clazz : annoAttrs.getClassArray("basePackageClasses")) { - basePackages.add(ClassUtils.getPackageName(clazz)); - } + List basePackages = new ArrayList<>(); + basePackages.addAll( + Arrays.stream(annoAttrs.getStringArray("value")) + .filter(StringUtils::hasText) + .collect(Collectors.toList())); + + basePackages.addAll( + Arrays.stream(annoAttrs.getStringArray("basePackages")) + .filter(StringUtils::hasText) + .collect(Collectors.toList())); + + basePackages.addAll( + Arrays.stream(annoAttrs.getClassArray("basePackageClasses")) + .map(ClassUtils::getPackageName) + .collect(Collectors.toList())); + scanner.registerFilters(); scanner.doScan(StringUtils.toStringArray(basePackages)); } - /** - * {@inheritDoc} - */ - @Override - public void setResourceLoader(ResourceLoader resourceLoader) { - this.resourceLoader = resourceLoader; - } - } diff --git a/src/main/java/org/mybatis/spring/batch/MyBatisBatchItemWriter.java b/src/main/java/org/mybatis/spring/batch/MyBatisBatchItemWriter.java index 6f8f425318..7d5e9c30ac 100644 --- a/src/main/java/org/mybatis/spring/batch/MyBatisBatchItemWriter.java +++ b/src/main/java/org/mybatis/spring/batch/MyBatisBatchItemWriter.java @@ -21,11 +21,11 @@ import java.util.List; import org.apache.ibatis.executor.BatchResult; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; +import org.mybatis.logging.Logger; +import org.mybatis.logging.LoggerFactory; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.InitializingBean; @@ -54,7 +54,7 @@ */ public class MyBatisBatchItemWriter implements ItemWriter, InitializingBean { - private static final Log LOGGER = LogFactory.getLog(MyBatisBatchItemWriter.class); + private static final Logger LOGGER = LoggerFactory.getLogger(MyBatisBatchItemWriter.class); private SqlSessionTemplate sqlSessionTemplate; @@ -119,10 +119,7 @@ public void afterPropertiesSet() { public void write(final List items) { if (!items.isEmpty()) { - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("Executing batch with " + items.size() + " items."); - } + LOGGER.debug(() -> "Executing batch with " + items.size() + " items."); for (T item : items) { sqlSessionTemplate.update(statementId, item); diff --git a/src/main/java/org/mybatis/spring/batch/MyBatisCursorItemReader.java b/src/main/java/org/mybatis/spring/batch/MyBatisCursorItemReader.java index 1ebee18d97..ce161f862a 100644 --- a/src/main/java/org/mybatis/spring/batch/MyBatisCursorItemReader.java +++ b/src/main/java/org/mybatis/spring/batch/MyBatisCursorItemReader.java @@ -59,7 +59,7 @@ protected T doRead() throws Exception { @Override protected void doOpen() throws Exception { - Map parameters = new HashMap(); + Map parameters = new HashMap<>(); if (parameterValues != null) { parameters.putAll(parameterValues); } diff --git a/src/main/java/org/mybatis/spring/batch/MyBatisPagingItemReader.java b/src/main/java/org/mybatis/spring/batch/MyBatisPagingItemReader.java index 830a8dab7c..6ff9aa3556 100644 --- a/src/main/java/org/mybatis/spring/batch/MyBatisPagingItemReader.java +++ b/src/main/java/org/mybatis/spring/batch/MyBatisPagingItemReader.java @@ -95,7 +95,7 @@ public void afterPropertiesSet() throws Exception { @Override protected void doReadPage() { - Map parameters = new HashMap(); + Map parameters = new HashMap<>(); if (parameterValues != null) { parameters.putAll(parameterValues); } @@ -103,11 +103,11 @@ protected void doReadPage() { parameters.put("_pagesize", getPageSize()); parameters.put("_skiprows", getPage() * getPageSize()); if (results == null) { - results = new CopyOnWriteArrayList(); + results = new CopyOnWriteArrayList<>(); } else { results.clear(); } - results.addAll(sqlSessionTemplate. selectList(queryId, parameters)); + results.addAll(sqlSessionTemplate.selectList(queryId, parameters)); } @Override diff --git a/src/main/java/org/mybatis/spring/config/mybatis-spring-1.2.xsd b/src/main/java/org/mybatis/spring/config/mybatis-spring-1.2.xsd index 0151059f26..35b4583d71 100644 --- a/src/main/java/org/mybatis/spring/config/mybatis-spring-1.2.xsd +++ b/src/main/java/org/mybatis/spring/config/mybatis-spring-1.2.xsd @@ -1,7 +1,7 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/test/java/org/mybatis/spring/batch/dao/EmployeeMapper.xml b/src/test/java/org/mybatis/spring/batch/dao/EmployeeMapper.xml index d11b65ee33..34f7e8939c 100644 --- a/src/test/java/org/mybatis/spring/batch/dao/EmployeeMapper.xml +++ b/src/test/java/org/mybatis/spring/batch/dao/EmployeeMapper.xml @@ -17,48 +17,47 @@ --> + PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> - + - + + resultOrdered="true"> + select distinct id,name,salary from employees order by id + + select id,name,salary,skill from employees order by id + - - - + + + - + - update employees set salary=#{salary} where id=#{id} - + update employees set salary=#{salary} where id=#{id} + + select sum(salary) from (select distinct id,salary from employees) + - + select count(distinct id)from employees + diff --git a/src/test/java/org/mybatis/spring/config/NamespaceTest.java b/src/test/java/org/mybatis/spring/config/NamespaceTest.java index ebbd687f81..6b3fddf91e 100644 --- a/src/test/java/org/mybatis/spring/config/NamespaceTest.java +++ b/src/test/java/org/mybatis/spring/config/NamespaceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,10 +15,10 @@ */ package org.mybatis.spring.config; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; -import org.junit.After; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.mapper.AnnotatedMapper; @@ -54,7 +54,7 @@ private void startContext() { applicationContext.getBean("sqlSessionFactory"); } - @After + @AfterEach public void assertNoMapperClass() { // concrete classes should always be ignored by MapperScannerPostProcessor assertBeanNotLoaded("mapperClass"); diff --git a/src/test/java/org/mybatis/spring/config/annotation.xml b/src/test/java/org/mybatis/spring/config/annotation.xml index d588900845..85e8fefd35 100644 --- a/src/test/java/org/mybatis/spring/config/annotation.xml +++ b/src/test/java/org/mybatis/spring/config/annotation.xml @@ -23,14 +23,14 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/config/base-package.xml b/src/test/java/org/mybatis/spring/config/base-package.xml index e8c30b2cae..8e5e90e894 100644 --- a/src/test/java/org/mybatis/spring/config/base-package.xml +++ b/src/test/java/org/mybatis/spring/config/base-package.xml @@ -23,14 +23,14 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/config/factory-ref.xml b/src/test/java/org/mybatis/spring/config/factory-ref.xml index 0246088d13..e34a5f468e 100644 --- a/src/test/java/org/mybatis/spring/config/factory-ref.xml +++ b/src/test/java/org/mybatis/spring/config/factory-ref.xml @@ -23,14 +23,14 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/config/marker-and-annotation.xml b/src/test/java/org/mybatis/spring/config/marker-and-annotation.xml index 2e39dd9748..01dfbc2b15 100644 --- a/src/test/java/org/mybatis/spring/config/marker-and-annotation.xml +++ b/src/test/java/org/mybatis/spring/config/marker-and-annotation.xml @@ -23,16 +23,16 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/config/marker-interface.xml b/src/test/java/org/mybatis/spring/config/marker-interface.xml index 1c0570440d..44936c7d8c 100644 --- a/src/test/java/org/mybatis/spring/config/marker-interface.xml +++ b/src/test/java/org/mybatis/spring/config/marker-interface.xml @@ -23,14 +23,14 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/config/name-generator.xml b/src/test/java/org/mybatis/spring/config/name-generator.xml index 545a50efe9..fa03119155 100644 --- a/src/test/java/org/mybatis/spring/config/name-generator.xml +++ b/src/test/java/org/mybatis/spring/config/name-generator.xml @@ -17,11 +17,12 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - + diff --git a/src/test/java/org/mybatis/spring/config/template-ref.xml b/src/test/java/org/mybatis/spring/config/template-ref.xml index 66dbeae7e7..b946bad78e 100644 --- a/src/test/java/org/mybatis/spring/config/template-ref.xml +++ b/src/test/java/org/mybatis/spring/config/template-ref.xml @@ -23,14 +23,14 @@ --> + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> - - + + diff --git a/src/test/java/org/mybatis/spring/mapper/AnnotatedMapper.java b/src/test/java/org/mybatis/spring/mapper/AnnotatedMapper.java index 703f32d388..08f9cdb6c9 100644 --- a/src/test/java/org/mybatis/spring/mapper/AnnotatedMapper.java +++ b/src/test/java/org/mybatis/spring/mapper/AnnotatedMapper.java @@ -21,5 +21,5 @@ // ensures annotated classes are loaded @Component public interface AnnotatedMapper { - public void method(); + void method(); } diff --git a/src/test/java/org/mybatis/spring/mapper/MapperFactoryBeanTest.java b/src/test/java/org/mybatis/spring/mapper/MapperFactoryBeanTest.java index 863574788f..0432d2764f 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperFactoryBeanTest.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperFactoryBeanTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,15 +15,16 @@ */ package org.mybatis.spring.mapper; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.mybatis.spring.AbstractMyBatisSpringTest; import org.mybatis.spring.MyBatisSystemException; import org.mybatis.spring.SqlSessionFactoryBean; @@ -40,7 +41,7 @@ public final class MapperFactoryBeanTest extends AbstractMyBatisSpringTest { private static SqlSessionTemplate sqlSessionTemplate; - @BeforeClass + @BeforeAll public static void setupSqlTemplate() { sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); } @@ -74,7 +75,7 @@ public void testAddToConfigTrue() throws Exception { } // will fail because TestDao's mapper config is never loaded - @Test(expected = org.apache.ibatis.binding.BindingException.class) + @Test public void testAddToConfigFalse() throws Throwable { try { // the default SqlSessionFactory in AbstractMyBatisSpringTest is created with an explicitly @@ -86,8 +87,10 @@ public void testAddToConfigFalse() throws Throwable { SqlSessionFactory sqlSessionFactory = factoryBean.getObject(); - find(new SqlSessionTemplate(sqlSessionFactory), false); - fail("TestDao's mapper xml should not be loaded"); + assertThrows(org.apache.ibatis.binding.BindingException.class, () -> + find(new SqlSessionTemplate(sqlSessionFactory), false) + ); +// fail("TestDao's mapper xml should not be loaded"); } catch (MyBatisSystemException mbse) { // unwrap exception so the exact MyBatis exception can be tested throw mbse.getCause(); @@ -132,7 +135,7 @@ public void testWithNonSpringTransactionFactory() throws Exception { // active transaction using the DataSource, but without a SpringTransactionFactory // this should error - @Test(expected = TransientDataAccessResourceException.class) + @Test public void testNonSpringTxMgrWithTx() throws Exception { Environment original = sqlSessionFactory.getConfiguration().getEnvironment(); Environment nonSpring = new Environment("non-spring", new JdbcTransactionFactory(), dataSource); @@ -146,6 +149,9 @@ public void testNonSpringTxMgrWithTx() throws Exception { find(); fail("should not be able to get an SqlSession using non-Spring tx manager when there is an active Spring tx"); + } catch (TransientDataAccessResourceException e) { + assertThat(e.getMessage()).isEqualTo("SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use" + + " Spring transaction synchronization"); } finally { // rollback required to close connection txManager.rollback(status); @@ -167,7 +173,7 @@ public void testNonSpringWithTx() throws Exception { SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory); - TransactionStatus status = null; + TransactionStatus status; try { status = txManager.getTransaction(new DefaultTransactionDefinition()); @@ -182,8 +188,8 @@ public void testNonSpringWithTx() throws Exception { // SqlSessionTemplate uses its own connection MockConnection mockConnection = (MockConnection) mockDataSource.getConnection(); - assertEquals("should call commit on Connection", 1, mockConnection.getNumberCommits()); - assertEquals("should not call rollback on Connection", 0, mockConnection.getNumberRollbacks()); + assertThat(mockConnection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1); + assertThat(mockConnection.getNumberRollbacks()).as("should not call rollback on Connection").isEqualTo(0); assertCommitSession(); } finally { @@ -202,7 +208,7 @@ private void find(SqlSessionTemplate sqlSessionTemplate) throws Exception { private void find(SqlSessionTemplate sqlSessionTemplate, boolean addToConfig) throws Exception { // recreate the mapper for each test since sqlSessionTemplate or the underlying // SqlSessionFactory could change for each test - MapperFactoryBean mapper = new MapperFactoryBean(); + MapperFactoryBean mapper = new MapperFactoryBean<>(); mapper.setMapperInterface(TestMapper.class); mapper.setSqlSessionTemplate(sqlSessionTemplate); mapper.setAddToConfig(addToConfig); diff --git a/src/test/java/org/mybatis/spring/mapper/MapperInterface.java b/src/test/java/org/mybatis/spring/mapper/MapperInterface.java index a4749355cf..27a49f5af6 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperInterface.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperInterface.java @@ -19,5 +19,5 @@ // when used as a marker interface, // this class should be ignored even though it has methods public interface MapperInterface { - public void method(); + void method(); } diff --git a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java index ac454e9c8e..809971f230 100644 --- a/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java +++ b/src/test/java/org/mybatis/spring/mapper/MapperScannerConfigurerTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,16 +15,16 @@ */ package org.mybatis.spring.mapper; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; import java.util.Properties; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSessionFactory; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.mapper.child.MapperChildInterface; @@ -43,7 +43,7 @@ public final class MapperScannerConfigurerTest { private GenericApplicationContext applicationContext; - @Before + @BeforeEach public void setupContext() { applicationContext = new GenericApplicationContext(); @@ -69,7 +69,7 @@ private void startContext() { applicationContext.getBean("sqlSessionFactory"); } - @After + @AfterEach public void assertNoMapperClass() { // concrete classes should always be ignored by MapperScannerPostProcessor assertBeanNotLoaded("mapperClass"); @@ -210,8 +210,9 @@ public void testScanWithNameConflict() { startContext(); - assertSame("scanner should not overwite existing bean definition", applicationContext - .getBean("mapperInterface").getClass(), Object.class); + assertThat(applicationContext.getBean("mapperInterface").getClass()) + .as("scanner should not overwrite existing bean definition") + .isSameAs(Object.class); } @Test @@ -248,7 +249,7 @@ public void testScanWithPropertyPlaceholders() { // mybatis-config.xml changes the executor from the default SIMPLE type SqlSessionFactory sessionFactory = (SqlSessionFactory) applicationContext .getBean("sqlSessionFactory"); - assertSame(ExecutorType.REUSE, sessionFactory.getConfiguration().getDefaultExecutorType()); + assertThat(sessionFactory.getConfiguration().getDefaultExecutorType()).isSameAs(ExecutorType.REUSE); } private void setupSqlSessionFactory(String name) { diff --git a/src/test/java/org/mybatis/spring/mapper/child/MapperChildInterface.java b/src/test/java/org/mybatis/spring/mapper/child/MapperChildInterface.java index ba1bbe93a7..cf939bcf0b 100644 --- a/src/test/java/org/mybatis/spring/mapper/child/MapperChildInterface.java +++ b/src/test/java/org/mybatis/spring/mapper/child/MapperChildInterface.java @@ -22,5 +22,5 @@ // tests subpackage search @Component public interface MapperChildInterface extends MapperInterface { - public void childMethod(); + void childMethod(); } diff --git a/src/test/java/org/mybatis/spring/sample/AbstractSampleTest.java b/src/test/java/org/mybatis/spring/sample/AbstractSampleTest.java index 34f3f60968..db8d198e8b 100644 --- a/src/test/java/org/mybatis/spring/sample/AbstractSampleTest.java +++ b/src/test/java/org/mybatis/spring/sample/AbstractSampleTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,18 +15,17 @@ */ package org.mybatis.spring.sample; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.assertj.core.api.Assertions.*; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mybatis.spring.sample.domain.User; import org.mybatis.spring.sample.service.FooService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @DirtiesContext public abstract class AbstractSampleTest { @@ -40,8 +39,8 @@ public final void setFooService(FooService fooService) { @Test public final void testFooService() { User user = this.fooService.doSomeBusinessStuff("u1"); - assertNotNull(user); - assertEquals("Pocoyo", user.getName()); + assertThat(user).isNotNull(); + assertThat(user.getName()).isEqualTo("Pocoyo"); } } diff --git a/src/test/java/org/mybatis/spring/sample/SampleBatchTest.java b/src/test/java/org/mybatis/spring/sample/SampleBatchTest.java index c7d853240b..1c99a31023 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleBatchTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleBatchTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package org.mybatis.spring.sample; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Example of MyBatis-Spring batch integration usage. */ -@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-batch.xml" }) +@SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-batch.xml" }) public class SampleBatchTest extends AbstractSampleTest { // Note this does not actually test batch functionality since FooService // only calls one DAO method. This class and associated Spring context diff --git a/src/test/java/org/mybatis/spring/sample/SampleConfigurationTest.java b/src/test/java/org/mybatis/spring/sample/SampleConfigurationTest.java index fc51518f04..348e31b77d 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleConfigurationTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleConfigurationTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,12 +18,13 @@ */ package org.mybatis.spring.sample; +import static org.assertj.core.api.Assertions.assertThat; + import javax.sql.DataSource; import org.apache.ibatis.session.SqlSessionFactory; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.mapper.MapperFactoryBean; @@ -39,12 +40,14 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.transaction.PlatformTransactionManager; -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @ContextConfiguration(loader = AnnotationConfigContextLoader.class) +@SpringJUnitConfig() public class SampleConfigurationTest { @Configuration @@ -69,7 +72,7 @@ public SqlSessionFactory sqlSessionFactory() throws Exception { SqlSessionFactoryBean ss = new SqlSessionFactoryBean(); ss.setDataSource(dataSource()); ss.setMapperLocations(new Resource[] { new ClassPathResource("org/mybatis/spring/sample/mapper/UserMapper.xml") }); - return (SqlSessionFactory) ss.getObject(); + return ss.getObject(); } @Bean @@ -81,7 +84,7 @@ public UserMapper userMapper() throws Exception { @Bean public UserMapper userMapperWithFactory() throws Exception { - MapperFactoryBean mapperFactoryBean = new MapperFactoryBean(); + MapperFactoryBean mapperFactoryBean = new MapperFactoryBean<>(); mapperFactoryBean.setMapperInterface(UserMapper.class); mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory()); mapperFactoryBean.afterPropertiesSet(); @@ -102,7 +105,7 @@ public FooService fooService() throws Exception { @Test public void test() { User user = fooService.doSomeBusinessStuff("u1"); - Assert.assertEquals("Pocoyo", user.getName()); + assertThat(user.getName()).isEqualTo("Pocoyo"); } } diff --git a/src/test/java/org/mybatis/spring/sample/SampleEnableTest.java b/src/test/java/org/mybatis/spring/sample/SampleEnableTest.java index bcbbbaa92a..7d5ccdd308 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleEnableTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleEnableTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,7 @@ import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Test to ensure that the {@link MapperScan} @@ -26,7 +26,7 @@ * * @since 1.2.0 */ -@ContextConfiguration +@SpringJUnitConfig public class SampleEnableTest extends AbstractSampleTest { @Configuration diff --git a/src/test/java/org/mybatis/spring/sample/SampleMapperTest.java b/src/test/java/org/mybatis/spring/sample/SampleMapperTest.java index 11f8c7c7b0..832b01cadb 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleMapperTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleMapperTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package org.mybatis.spring.sample; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Example of MyBatis-Spring integration with a DAO created by * MapperFactoryBean. */ -@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-mapper.xml" }) +@SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-mapper.xml" }) public class SampleMapperTest extends AbstractSampleTest { } diff --git a/src/test/java/org/mybatis/spring/sample/SampleNamespaceTest.java b/src/test/java/org/mybatis/spring/sample/SampleNamespaceTest.java index 0c2cd1c0f8..0bf5b72d98 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleNamespaceTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleNamespaceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package org.mybatis.spring.sample; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Example of MyBatis-Spring integration with a DAO configured via * MapperScannerConfigurer. */ -@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-namespace.xml" }) +@SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-namespace.xml" }) public class SampleNamespaceTest extends AbstractSampleTest { } diff --git a/src/test/java/org/mybatis/spring/sample/SampleScannerTest.java b/src/test/java/org/mybatis/spring/sample/SampleScannerTest.java index aa8eb2eafe..6e9a46e8b1 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleScannerTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleScannerTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package org.mybatis.spring.sample; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Example of MyBatis-Spring integration with a DAO configured via * MapperScannerConfigurer. */ -@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-scanner.xml" }) +@SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-scanner.xml" }) public class SampleScannerTest extends AbstractSampleTest { } diff --git a/src/test/java/org/mybatis/spring/sample/SampleSqlSessionTest.java b/src/test/java/org/mybatis/spring/sample/SampleSqlSessionTest.java index bf09d392bb..fe6da0e235 100644 --- a/src/test/java/org/mybatis/spring/sample/SampleSqlSessionTest.java +++ b/src/test/java/org/mybatis/spring/sample/SampleSqlSessionTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,12 +15,12 @@ */ package org.mybatis.spring.sample; -import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; /** * Example of basic MyBatis-Spring integration usage with a manual DAO * implementation that subclasses SqlSessionDaoSupport. */ -@ContextConfiguration(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-sqlsession.xml" }) +@SpringJUnitConfig(locations = { "classpath:org/mybatis/spring/sample/config/applicationContext-sqlsession.xml" }) public class SampleSqlSessionTest extends AbstractSampleTest { } diff --git a/src/test/java/org/mybatis/spring/sample/domain/User.java b/src/test/java/org/mybatis/spring/sample/domain/User.java index e7630383dd..1a5bea8dc0 100644 --- a/src/test/java/org/mybatis/spring/sample/domain/User.java +++ b/src/test/java/org/mybatis/spring/sample/domain/User.java @@ -50,5 +50,4 @@ public String toString() { buf.append("}"); return buf.toString(); } - } diff --git a/src/test/java/org/mybatis/spring/submitted/autowire/AutowireTest.java b/src/test/java/org/mybatis/spring/submitted/autowire/AutowireTest.java index d430beaa38..eb9258ee41 100644 --- a/src/test/java/org/mybatis/spring/submitted/autowire/AutowireTest.java +++ b/src/test/java/org/mybatis/spring/submitted/autowire/AutowireTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2015 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,9 +15,9 @@ */ package org.mybatis.spring.submitted.autowire; -import static org.junit.Assert.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AutowireTest { @@ -28,11 +28,11 @@ public void shouldReturnMapper() { context = new ClassPathXmlApplicationContext("classpath:org/mybatis/spring/submitted/autowire/spring.xml"); FooMapper fooMapper = (FooMapper) context.getBean("fooMapper"); - assertNotNull(fooMapper); + assertThat(fooMapper).isNotNull(); fooMapper.executeFoo(); BarMapper barMapper = (BarMapper) context.getBean("barMapper"); - assertNotNull(barMapper); + assertThat(barMapper).isNotNull(); barMapper.executeBar(); } diff --git a/src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.java b/src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.java index 7095e5f9eb..03b7be2c07 100644 --- a/src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.java +++ b/src/test/java/org/mybatis/spring/submitted/autowire/BarMapper.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2015 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,5 +16,5 @@ package org.mybatis.spring.submitted.autowire; public interface BarMapper extends IBar { - public String executeBar(); + String executeBar(); } diff --git a/src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.java b/src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.java index bc22e20cc0..229ecbf040 100644 --- a/src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.java +++ b/src/test/java/org/mybatis/spring/submitted/autowire/FooMapper.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2015 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,5 +16,5 @@ package org.mybatis.spring.submitted.autowire; public interface FooMapper extends IFoo { - public String executeFoo(); + String executeFoo(); } diff --git a/src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java b/src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java index 3172b85c70..a8d85d2e95 100644 --- a/src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java +++ b/src/test/java/org/mybatis/spring/submitted/webapp_placeholder/WebappPlaceholderTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,18 +15,18 @@ */ package org.mybatis.spring.submitted.webapp_placeholder; -import static org.junit.Assert.*; +import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.web.WebAppConfiguration; -@RunWith(SpringJUnit4ClassRunner.class) +@ExtendWith(SpringExtension.class) @WebAppConfiguration -@ContextConfiguration(locations = "file:src/test/java/org/mybatis/spring/submitted/webapp_placeholder/spring.xml") +@SpringJUnitConfig(locations = "file:src/test/java/org/mybatis/spring/submitted/webapp_placeholder/spring.xml") public class WebappPlaceholderTest { @Autowired @@ -34,6 +34,6 @@ public class WebappPlaceholderTest { @Test public void testName() throws Exception { - assertNotNull(mapper); + assertThat(mapper).isNotNull(); } } diff --git a/src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java b/src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java index 24cc9d0e71..329027a1cd 100644 --- a/src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java +++ b/src/test/java/org/mybatis/spring/submitted/xa/UserServiceTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2015 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,17 +15,18 @@ */ package org.mybatis.spring.submitted.xa; +import static org.assertj.core.api.Assertions.assertThat; + import javax.transaction.UserTransaction; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -@RunWith(value = SpringJUnit4ClassRunner.class) -@ContextConfiguration(locations = "classpath:org/mybatis/spring/submitted/xa/applicationContext.xml") +@ExtendWith(SpringExtension.class) +@SpringJUnitConfig(locations = "classpath:org/mybatis/spring/submitted/xa/applicationContext.xml") public class UserServiceTest { @Autowired UserTransaction userTransaction; @@ -37,7 +38,7 @@ public class UserServiceTest { public void testCommit() { User user = new User(1, "Pocoyo"); userService.saveWithNoFailure(user); - Assert.assertTrue(userService.checkUserExists(user.getId())); + assertThat(userService.checkUserExists(user.getId())).isTrue(); } @Test @@ -48,7 +49,7 @@ public void testRollback() { } catch (RuntimeException ignore) { // ignored } - Assert.assertFalse(userService.checkUserExists(user.getId())); + assertThat(userService.checkUserExists(user.getId())).isFalse(); } @Test @@ -57,7 +58,7 @@ public void testCommitWithExistingTx() throws Exception { User user = new User(3, "Pocoyo"); userService.saveWithNoFailure(user); userTransaction.commit(); - Assert.assertTrue(userService.checkUserExists(user.getId())); + assertThat(userService.checkUserExists(user.getId())).isTrue(); } // TODO when the outer JTA tx is rolledback, @@ -70,7 +71,7 @@ public void testRollbackWithExistingTx() throws Exception { User user = new User(5, "Pocoyo"); userService.saveWithNoFailure(user); userTransaction.rollback(); - Assert.assertFalse(userService.checkUserExists(user.getId())); + assertThat(userService.checkUserExists(user.getId())).isFalse(); } } diff --git a/src/test/java/org/mybatis/spring/support/SqlSessionDaoSupportTest.java b/src/test/java/org/mybatis/spring/support/SqlSessionDaoSupportTest.java index 2e12c7d874..9e5d68763a 100644 --- a/src/test/java/org/mybatis/spring/support/SqlSessionDaoSupportTest.java +++ b/src/test/java/org/mybatis/spring/support/SqlSessionDaoSupportTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,13 +15,14 @@ */ package org.mybatis.spring.support; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.sql.SQLException; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mybatis.spring.AbstractMyBatisSpringTest; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; @@ -35,12 +36,12 @@ public final class SqlSessionDaoSupportTest extends AbstractMyBatisSpringTest { private GenericApplicationContext applicationContext; - @Before + @BeforeEach public void setup() { sqlSessionDaoSupport = new MockSqlSessionDao(); } - @After + @AfterEach public void closeConnection() throws SQLException { connection.close(); } @@ -51,7 +52,7 @@ public void testWithSqlSessionTemplate() { sqlSessionDaoSupport.setSqlSessionTemplate(sessionTemplate); sqlSessionDaoSupport.afterPropertiesSet(); - assertEquals("should store the Template", sessionTemplate, sqlSessionDaoSupport.getSqlSession()); + assertThat(sqlSessionDaoSupport.getSqlSession()).as("should store the Template").isEqualTo(sessionTemplate); } @Test @@ -59,8 +60,9 @@ public void testWithSqlSessionFactory() { sqlSessionDaoSupport.setSqlSessionFactory(sqlSessionFactory); sqlSessionDaoSupport.afterPropertiesSet(); - assertEquals("should store the Factory", sqlSessionFactory, ((SqlSessionTemplate) sqlSessionDaoSupport - .getSqlSession()).getSqlSessionFactory()); + assertThat(((SqlSessionTemplate) sqlSessionDaoSupport.getSqlSession()).getSqlSessionFactory()) + .as("should store the Factory") + .isEqualTo(sqlSessionFactory); } @Test @@ -70,28 +72,28 @@ public void testWithBothFactoryAndTemplate() { sqlSessionDaoSupport.setSqlSessionFactory(sqlSessionFactory); sqlSessionDaoSupport.afterPropertiesSet(); - assertEquals("should ignore the Factory", sessionTemplate, sqlSessionDaoSupport.getSqlSession()); + assertThat(sqlSessionDaoSupport.getSqlSession()).as("should ignore the Factory").isEqualTo(sessionTemplate); } - @Test(expected = IllegalArgumentException.class) + @Test public void testWithNoFactoryOrSession() { - sqlSessionDaoSupport.afterPropertiesSet(); + assertThrows(IllegalArgumentException.class, sqlSessionDaoSupport::afterPropertiesSet); } - @Test(expected = BeanCreationException.class) + @Test public void testAutowireWithNoFactoryOrSession() { setupContext(); - startContext(); + assertThrows(BeanCreationException.class, this::startContext); } - @Test(expected = BeanCreationException.class) + @Test public void testAutowireWithTwoFactories() { setupContext(); setupSqlSessionFactory("factory1"); setupSqlSessionFactory("factory2"); - startContext(); + assertThrows(BeanCreationException.class, this::startContext); } private void setupContext() { diff --git a/src/test/java/org/mybatis/spring/transaction/SpringTransactionManagerTest.java b/src/test/java/org/mybatis/spring/transaction/SpringTransactionManagerTest.java index 2266923c11..114e1875ba 100644 --- a/src/test/java/org/mybatis/spring/transaction/SpringTransactionManagerTest.java +++ b/src/test/java/org/mybatis/spring/transaction/SpringTransactionManagerTest.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2016 the original author or authors. + * Copyright 2010-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,9 @@ */ package org.mybatis.spring.transaction; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.mybatis.spring.AbstractMyBatisSpringTest; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; @@ -37,8 +35,8 @@ public void shouldNoOpWithTx() throws Exception { transaction.getConnection(); transaction.commit(); transaction.close(); - assertEquals("should not call commit on Connection", 0, connection.getNumberCommits()); - assertFalse("should not close the Connection", connection.isClosed()); + assertThat(connection.getNumberCommits()).as("should not call commit on Connection").isEqualTo(0); + assertThat(connection.isClosed()).as("should not close the Connection").isFalse(); txManager.commit(status); } @@ -66,8 +64,8 @@ public void shouldManageWithNoTx() throws Exception { transaction.getConnection(); transaction.commit(); transaction.close(); - assertEquals("should call commit on Connection", 1, connection.getNumberCommits()); - assertTrue("should close the Connection", connection.isClosed()); + assertThat(connection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1); + assertThat(connection.isClosed()).as("should close the Connection").isTrue(); } @Test @@ -78,8 +76,8 @@ public void shouldNotCommitWithNoTxAndAutocommitIsOn() throws Exception { transaction.getConnection(); transaction.commit(); transaction.close(); - assertEquals("should not call commit on a Connection with autocommit", 0, connection.getNumberCommits()); - assertTrue("should close the Connection", connection.isClosed()); + assertThat(connection.getNumberCommits()).as("should not call commit on a Connection with autocommit").isEqualTo(0); + assertThat(connection.isClosed()).as("should close the Connection").isTrue(); } @Test @@ -89,8 +87,8 @@ public void shouldIgnoreAutocommit() throws Exception { transaction.getConnection(); transaction.commit(); transaction.close(); - assertEquals("should call commit on Connection", 1, connection.getNumberCommits()); - assertTrue("should close the Connection", connection.isClosed()); + assertThat(connection.getNumberCommits()).as("should call commit on Connection").isEqualTo(1); + assertThat(connection.isClosed()).as("should close the Connection").isTrue(); } } diff --git a/src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java b/src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java index f61e5e6b1a..ddcd53cd5e 100644 --- a/src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java +++ b/src/test/java/org/mybatis/spring/type/DummyMapperFactoryBean.java @@ -1,5 +1,5 @@ /** - * Copyright 2010-2015 the original author or authors. + * Copyright 2010-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,10 @@ package org.mybatis.spring.type; import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.log4j.Logger; +import org.mybatis.logging.Logger; +import org.mybatis.logging.LoggerFactory; import org.mybatis.spring.mapper.MapperFactoryBean; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.concurrent.atomic.AtomicInteger; @@ -34,7 +33,7 @@ public DummyMapperFactoryBean(Class mapperInterface) { super(mapperInterface); } - private static final Logger LOGGER = Logger.getLogger(DummyMapperFactoryBean.class); + private static final Logger LOGGER = LoggerFactory.getLogger(DummyMapperFactoryBean.class); private static final AtomicInteger mapperInstanceCount = new AtomicInteger(0); @@ -43,13 +42,13 @@ protected void checkDaoConfig() { super.checkDaoConfig(); // make something more if (isAddToConfig()) { - LOGGER.debug("register mapper for interface : " + getMapperInterface()); + LOGGER.debug(() -> "register mapper for interface : " + getMapperInterface()); } } @Override public T getObject() throws Exception { - MapperFactoryBean mapperFactoryBean = new MapperFactoryBean(); + MapperFactoryBean mapperFactoryBean = new MapperFactoryBean<>(); mapperFactoryBean.setMapperInterface(getMapperInterface()); mapperFactoryBean.setAddToConfig(isAddToConfig()); mapperFactoryBean.setSqlSessionFactory(getCustomSessionFactoryForClass()); @@ -65,19 +64,17 @@ private SqlSessionFactory getCustomSessionFactoryForClass() { return (SqlSessionFactory) Proxy.newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSessionFactory.class}, - new InvocationHandler() { - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + (proxy, method, args) -> { if ("getConfiguration".equals(method.getName())) { return getSqlSession().getConfiguration(); } // dummy return null; - } - }); + } + ); } - public static final int getMapperCount(){ + public static int getMapperCount(){ return mapperInstanceCount.get(); } } diff --git a/src/test/resources/log4j2-test.xml b/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000000..318ea15395 --- /dev/null +++ b/src/test/resources/log4j2-test.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + +