From abba98072948d5fe9e86b7b253aba7b4136d673f Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:16:02 -0500 Subject: [PATCH 1/7] [tests] Use junit 5 style --- .../org/mybatis/cdi/FooServiceJTATest.java | 14 +++++++------- .../java/org/mybatis/cdi/FooServiceTest.java | 18 +++++++++--------- .../org/mybatis/cdi/MybatisExtensionTest.java | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/test/java/org/mybatis/cdi/FooServiceJTATest.java b/src/test/java/org/mybatis/cdi/FooServiceJTATest.java index ed7e83a7..7eec786b 100644 --- a/src/test/java/org/mybatis/cdi/FooServiceJTATest.java +++ b/src/test/java/org/mybatis/cdi/FooServiceJTATest.java @@ -29,7 +29,7 @@ // @EnableWeld // TODO Remove the following once we drop cdi 1.1 support @ExtendWith(WeldJunit5Extension.class) -public class FooServiceJTATest { +class FooServiceJTATest { // TODO Add the following once we drop cdi 1.1 support // @WeldSetup @@ -42,14 +42,14 @@ public class FooServiceJTATest { private UserTransaction userTransaction; @Test - public void jtaShouldGetAUserWithNoTX() throws Exception { + void jtaShouldGetAUserWithNoTX() throws Exception { this.userTransaction.begin(); Assertions.assertEquals("1-User1", this.fooServiceJTA.getUserWithNoTransaction(1).getName()); this.userTransaction.commit(); } @Test - public void jtaShouldInsertAUserAndCommit() { + void jtaShouldInsertAUserAndCommit() { User user = new User(); user.setId(20); user.setName("User20"); @@ -58,7 +58,7 @@ public void jtaShouldInsertAUserAndCommit() { } @Test - public void jtaShouldInsertAUserAndRollItBack() { + void jtaShouldInsertAUserAndRollItBack() { User user = new User(); user.setId(30); user.setName("User30"); @@ -71,7 +71,7 @@ public void jtaShouldInsertAUserAndRollItBack() { } @Test - public void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception { + void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception { User user = new User(); user.setId(40); user.setName("User40"); @@ -82,7 +82,7 @@ public void jtaShouldInsertAUserWithExistingJtaTxAndCommit() throws Exception { } @Test - public void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exception { + void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exception { User user = new User(); user.setId(50); user.setName("User50"); @@ -92,4 +92,4 @@ public void jtaShouldInsertAUserWithExistingJtaTxAndRollItBack() throws Exceptio Assertions.assertNull(this.fooServiceJTA.getUserWithNoTransaction(user.getId())); } -} \ No newline at end of file +} diff --git a/src/test/java/org/mybatis/cdi/FooServiceTest.java b/src/test/java/org/mybatis/cdi/FooServiceTest.java index eac4c149..14cbee8e 100644 --- a/src/test/java/org/mybatis/cdi/FooServiceTest.java +++ b/src/test/java/org/mybatis/cdi/FooServiceTest.java @@ -33,7 +33,7 @@ // @EnableWeld // TODO Remove the following once we drop cdi 1.1 support @ExtendWith(WeldJunit5Extension.class) -public class FooServiceTest { +class FooServiceTest { // TODO Add the following once we drop cdi 1.1 support // @WeldSetup @@ -46,7 +46,7 @@ public class FooServiceTest { private SerializableFooService serFooService; @Test - public void shouldGetAUser() { + void shouldGetAUser() { Assertions.assertEquals("1-User1", this.fooService.getUserFromSqlSession(1).getName()); Assertions.assertEquals("1-User1", this.fooService.getUser(1).getName()); Assertions.assertEquals("2-User2", this.fooService.getUser2(2).getName()); @@ -55,14 +55,14 @@ public void shouldGetAUser() { } @Test - public void shouldInjectTheSameMapper() { + void shouldInjectTheSameMapper() { Assertions.assertEquals(this.fooService.getUser2(1).getName(), this.fooService.getUserDummy(1).getName()); Assertions.assertEquals(this.fooService.getUser2(2).getName(), this.fooService.getUserDummy(2).getName()); Assertions.assertEquals(this.fooService.getUser2(3).getName(), this.fooService.getUserDummy(3).getName()); } @Test - public void shouldInsertAUserAndCommit() { + void shouldInsertAUserAndCommit() { User user = new User(); user.setId(20); user.setName("User20"); @@ -71,7 +71,7 @@ public void shouldInsertAUserAndCommit() { } @Test - public void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() { + void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() { User user = new User(); user.setId(30); user.setName("User40"); @@ -84,7 +84,7 @@ public void shouldInsertAUserThatFailsWithRuntimeAndRollItBack() { } @Test - public void shouldInsertAUserThatFailsWithACheckedAndCommit() { + void shouldInsertAUserThatFailsWithACheckedAndCommit() { User user = new User(); user.setId(30); user.setName("User30"); @@ -97,7 +97,7 @@ public void shouldInsertAUserThatFailsWithACheckedAndCommit() { } @Test - public void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRollItBack() { + void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRollItBack() { User user = new User(); user.setId(30); user.setName("User30"); @@ -110,7 +110,7 @@ public void shouldInsertAUserThatFailsWithACustomExceptionMarkedToRollbackAndRol } @Test - public void injectedMappersAreSerializable() throws Exception { + void injectedMappersAreSerializable() throws Exception { ObjectOutputStream oout = new ObjectOutputStream(new FileOutputStream("target/mapper.ser")); oout.writeObject(this.serFooService); oout.close(); @@ -120,4 +120,4 @@ public void injectedMappersAreSerializable() throws Exception { Assertions.assertEquals(this.serFooService.getUser(1).getName(), unserialized.getUser(1).getName()); } -} \ No newline at end of file +} diff --git a/src/test/java/org/mybatis/cdi/MybatisExtensionTest.java b/src/test/java/org/mybatis/cdi/MybatisExtensionTest.java index e88cb20b..433e72f3 100644 --- a/src/test/java/org/mybatis/cdi/MybatisExtensionTest.java +++ b/src/test/java/org/mybatis/cdi/MybatisExtensionTest.java @@ -40,10 +40,10 @@ import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) -public class MybatisExtensionTest { +class MybatisExtensionTest { @Test - public void mappersFoundAfterTheBeanUsingTheMapperInAnInjectionPointHasBeenScannedShouldBeInstantiated() + void mappersFoundAfterTheBeanUsingTheMapperInAnInjectionPointHasBeenScannedShouldBeInstantiated() throws Exception { MybatisExtension extension = new MybatisExtension(); @@ -94,4 +94,4 @@ private void processAnnotatedType(MybatisExtension extension, Type type) { extension.processAnnotatedType(pat); } -} \ No newline at end of file +} From 7fc82eba4b3987fef0a39f3383033fea1d8cf84f Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:33:59 -0500 Subject: [PATCH 2/7] [ci] Use diamond operators for jdk7+ --- src/main/java/org/mybatis/cdi/MyBatisBean.java | 4 ++-- src/main/java/org/mybatis/cdi/MybatisExtension.java | 12 ++++++------ .../org/mybatis/cdi/SqlSessionManagerRegistry.java | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MyBatisBean.java b/src/main/java/org/mybatis/cdi/MyBatisBean.java index 261f9372..40ee5ccb 100644 --- a/src/main/java/org/mybatis/cdi/MyBatisBean.java +++ b/src/main/java/org/mybatis/cdi/MyBatisBean.java @@ -67,7 +67,7 @@ public MyBatisBean(String id, Class type, Set qualifiers, Stri this.type = type; this.sqlSessionFactoryName = sqlSessionFactoryName; if (qualifiers == null || qualifiers.isEmpty()) { - this.qualifiers = new HashSet(); + this.qualifiers = new HashSet<>(); this.qualifiers.add(new CDIUtils.SerializableDefaultAnnotationLiteral()); this.qualifiers.add(new CDIUtils.SerializableAnyAnnotationLiteral()); } else { @@ -77,7 +77,7 @@ public MyBatisBean(String id, Class type, Set qualifiers, Stri @Override public Set getTypes() { - Set types = new HashSet(); + Set types = new HashSet<>(); types.add(this.type); return types; } diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index d61998d5..6f7af032 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -54,11 +54,11 @@ public class MybatisExtension implements Extension { private static final Logger LOGGER = Logger.getLogger(MybatisExtension.class.getName()); - private final Set sessionProducers = new HashSet(); + private final Set sessionProducers = new HashSet<>(); - private final Set mapperTypes = new HashSet(); + private final Set mapperTypes = new HashSet<>(); - private final Set injectionPoints = new HashSet(); + private final Set injectionPoints = new HashSet<>(); /** * Collect types of all mappers annotated with Mapper. @@ -136,8 +136,8 @@ protected void processInjectionTarget(@Observes ProcessInjectionTarget ev protected void afterBeanDiscovery(@Observes AfterBeanDiscovery abd, BeanManager bm) { LOGGER.log(Level.INFO, "MyBatis CDI Module - Activated"); - Set mappers = new HashSet(); - Set sessionTargets = new HashSet(); + Set mappers = new HashSet<>(); + Set sessionTargets = new HashSet<>(); for (InjectionPoint ip : injectionPoints) { if (this.mapperTypes.contains(ip.getAnnotated().getBaseType())) { @@ -212,7 +212,7 @@ public BeanKey(Class type, Set annotations) { } private Set filterQualifiers(Set annotations) { - final Set set = new HashSet(); + final Set set = new HashSet<>(); for (Annotation a : annotations) { if (a.annotationType().isAnnotationPresent(Qualifier.class)) { set.add(a); diff --git a/src/main/java/org/mybatis/cdi/SqlSessionManagerRegistry.java b/src/main/java/org/mybatis/cdi/SqlSessionManagerRegistry.java index 78abfb38..e7c23afb 100644 --- a/src/main/java/org/mybatis/cdi/SqlSessionManagerRegistry.java +++ b/src/main/java/org/mybatis/cdi/SqlSessionManagerRegistry.java @@ -50,7 +50,7 @@ public void init() { if (this.factories.isUnsatisfied()) { throw new MybatisCdiConfigurationException("There are no SqlSessionFactory producers properly configured."); } - Map m = new HashMap(); + Map m = new HashMap<>(); for (SqlSessionFactory factory : this.factories) { SqlSessionManager manager = SqlSessionManager.newInstance(factory); m.put(factory, manager); From 7ed97955307533711075f6016a66187a795d1b5d Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:37:32 -0500 Subject: [PATCH 3/7] [ci] Combine if statement in equals method --- src/main/java/org/mybatis/cdi/MybatisExtension.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index 6f7af032..6ad750f1 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -246,10 +246,7 @@ public int hashCode() { @Override public boolean equals(Object obj) { - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { + if (obj == null || this.getClass() != obj.getClass()) { return false; } final BeanKey other = (BeanKey) obj; From b27962c788063af25d833f4077154dc3cc9293c3 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:38:25 -0500 Subject: [PATCH 4/7] [ci] Return has result directly --- src/main/java/org/mybatis/cdi/MybatisExtension.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index 6ad750f1..3b49ed2b 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -240,8 +240,7 @@ public int compareTo(BeanKey o) { @Override public int hashCode() { int hash = 3; - hash = 43 * hash + (this.key != null ? this.key.hashCode() : 0); - return hash; + return 43 * hash + (this.key != null ? this.key.hashCode() : 0); } @Override From fd2d55ee6be94001c97159af17cb5d78e78fcd2d Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:39:31 -0500 Subject: [PATCH 5/7] [ci] Use addAll instead of for looping to add all on set --- src/main/java/org/mybatis/cdi/MybatisExtension.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index 3b49ed2b..59f3e948 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -120,9 +120,7 @@ protected void processProducer(@Observes final ProcessProducer pp) */ protected void processInjectionTarget(@Observes ProcessInjectionTarget event) { final InjectionTarget it = event.getInjectionTarget(); - for (final InjectionPoint ip : it.getInjectionPoints()) { - injectionPoints.add(ip); - } + this.injectionPoints.addAll(it.getInjectionPoints()); } /** From 686a218e5188a45fd46bc485c559134051c2fe45 Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:41:08 -0500 Subject: [PATCH 6/7] [ci] Rework isAnnotated / isSqlSessionFactory if statement to clear sonar issues with duplication --- .../org/mybatis/cdi/MybatisExtension.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index 59f3e948..4305ab86 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -95,15 +95,17 @@ protected void processProducer(@Observes final ProcessProducer pp) final boolean isAnnotated = am.isAnnotationPresent(SessionFactoryProvider.class); final boolean isSqlSessionFactory = am.getBaseType().equals(SqlSessionFactory.class); final Object[] logData = { am.getJavaMember().getDeclaringClass().getSimpleName(), am.getJavaMember().getName() }; - if (isAnnotated && isSqlSessionFactory) { - LOGGER.log(Level.INFO, "MyBatis CDI Module - SqlSessionFactory producer {0}.{1}", logData); - this.sessionProducers.add(new BeanKey((Class) (Type) SqlSession.class, am.getAnnotations())); - } else if (isAnnotated && !isSqlSessionFactory) { - LOGGER.log(Level.SEVERE, "MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {0}.{1}", - logData); - pp.addDefinitionError(new MybatisCdiConfigurationException(String - .format("SessionFactoryProvider producers must return SqlSessionFactory (%s.%s)", logData[0], logData[1]))); - } else if (!isAnnotated && isSqlSessionFactory) { + if (isAnnotated) { + if (isSqlSessionFactory) { + LOGGER.log(Level.INFO, "MyBatis CDI Module - SqlSessionFactory producer {0}.{1}", logData); + this.sessionProducers.add(new BeanKey((Class) (Type) SqlSession.class, am.getAnnotations())); + } else { + LOGGER.log(Level.SEVERE, "MyBatis CDI Module - Invalid return type (Must be SqlSessionFactory): {0}.{1}", + logData); + pp.addDefinitionError(new MybatisCdiConfigurationException(String + .format("SessionFactoryProvider producers must return SqlSessionFactory (%s.%s)", logData[0], logData[1]))); + } + } else if (isSqlSessionFactory) { LOGGER.log(Level.WARNING, "MyBatis CDI Module - Ignored SqlSessionFactory producer because it is not annotated with @SessionFactoryProvider: {0}.{1}", logData); From 51b36975c0a4e97bb065099b05ddbf24fd286bda Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Wed, 29 Dec 2021 15:42:37 -0500 Subject: [PATCH 7/7] [ci] Use lambda for sort comparator logic --- src/main/java/org/mybatis/cdi/MybatisExtension.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/mybatis/cdi/MybatisExtension.java b/src/main/java/org/mybatis/cdi/MybatisExtension.java index 4305ab86..b0084ef9 100644 --- a/src/main/java/org/mybatis/cdi/MybatisExtension.java +++ b/src/main/java/org/mybatis/cdi/MybatisExtension.java @@ -19,7 +19,6 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -222,13 +221,8 @@ private Set filterQualifiers(Set annotations) { } private List sort(Set annotations) { - final List list = new ArrayList(annotations); - Collections.sort(list, new Comparator() { - @Override - public int compare(Annotation a, Annotation b) { - return a.getClass().getName().compareTo(b.getClass().getName()); - } - }); + final List list = new ArrayList<>(annotations); + Collections.sort(list, (a, b) -> a.getClass().getName().compareTo(b.getClass().getName())); return list; }