From 13637ec3b5e6f2a0abca9b2896087d29b2f29e0a Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 2 Aug 2018 14:41:27 +0200 Subject: [PATCH] Polishing (cherry picked from commit dc36bb34c7388e12f821de0f6cb08de58dd5202c) --- .../config/CustomScopeConfigurerTests.java | 46 +++++++-------- .../config/DeprecatedBeanWarnerTests.java | 11 +--- .../quartz/SchedulerFactoryBean.java | 6 +- .../orm/jpa/SharedEntityManagerCreator.java | 15 ++--- .../jpa/SharedEntityManagerCreatorTests.java | 57 ++++++++++++++++++- 5 files changed, 90 insertions(+), 45 deletions(-) diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java index 25a9a14a38..4c45809067 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2018 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. @@ -19,7 +19,6 @@ package org.springframework.beans.factory.config; import java.util.HashMap; import java.util.Map; -import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.support.DefaultListableBeanFactory; @@ -34,27 +33,24 @@ import static org.mockito.BDDMockito.*; * @author Juergen Hoeller * @author Chris Beams */ -public final class CustomScopeConfigurerTests { +public class CustomScopeConfigurerTests { private static final String FOO_SCOPE = "fooScope"; - private ConfigurableListableBeanFactory factory; - @Before - public void setUp() { - factory = new DefaultListableBeanFactory(); - } + private final ConfigurableListableBeanFactory factory = new DefaultListableBeanFactory(); + @Test - public void testWithNoScopes() throws Exception { + public void testWithNoScopes() { CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.postProcessBeanFactory(factory); } @Test - public void testSunnyDayWithBonaFideScopeInstance() throws Exception { + public void testSunnyDayWithBonaFideScopeInstance() { Scope scope = mock(Scope.class); factory.registerScope(FOO_SCOPE, scope); - Map scopes = new HashMap(); + Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, scope); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); @@ -62,8 +58,8 @@ public final class CustomScopeConfigurerTests { } @Test - public void testSunnyDayWithBonaFideScopeClass() throws Exception { - Map scopes = new HashMap(); + public void testSunnyDayWithBonaFideScopeClass() { + Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, NoOpScope.class); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); @@ -72,8 +68,8 @@ public final class CustomScopeConfigurerTests { } @Test - public void testSunnyDayWithBonaFideScopeClassname() throws Exception { - Map scopes = new HashMap(); + public void testSunnyDayWithBonaFideScopeClassName() { + Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, NoOpScope.class.getName()); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); @@ -81,29 +77,29 @@ public final class CustomScopeConfigurerTests { assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope); } - @Test(expected=IllegalArgumentException.class) - public void testWhereScopeMapHasNullScopeValueInEntrySet() throws Exception { - Map scopes = new HashMap(); + @Test(expected = IllegalArgumentException.class) + public void testWhereScopeMapHasNullScopeValueInEntrySet() { + Map scopes = new HashMap<>(); scopes.put(FOO_SCOPE, null); CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); } - @Test(expected=IllegalArgumentException.class) - public void testWhereScopeMapHasNonScopeInstanceInEntrySet() throws Exception { - Map scopes = new HashMap(); - scopes.put(FOO_SCOPE, this); // <-- not a valid value... + @Test(expected = IllegalArgumentException.class) + public void testWhereScopeMapHasNonScopeInstanceInEntrySet() { + Map scopes = new HashMap<>(); + scopes.put(FOO_SCOPE, this); // <-- not a valid value... CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); } @SuppressWarnings("unchecked") - @Test(expected=ClassCastException.class) - public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() throws Exception { + @Test(expected = ClassCastException.class) + public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() { Map scopes = new HashMap(); - scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)... + scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)... CustomScopeConfigurer figurer = new CustomScopeConfigurer(); figurer.setScopes(scopes); figurer.postProcessBeanFactory(factory); diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java index 4ed025b09b..07752bf048 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2018 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. @@ -28,28 +28,23 @@ import static org.junit.Assert.*; */ public class DeprecatedBeanWarnerTests { - private DefaultListableBeanFactory beanFactory; - private String beanName; private BeanDefinition beanDefinition; - private DeprecatedBeanWarner warner; - @Test @SuppressWarnings("deprecation") public void postProcess() { - beanFactory = new DefaultListableBeanFactory(); + DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class); String beanName = "deprecated"; beanFactory.registerBeanDefinition(beanName, def); - warner = new MyDeprecatedBeanWarner(); + DeprecatedBeanWarner warner = new MyDeprecatedBeanWarner(); warner.postProcessBeanFactory(beanFactory); assertEquals(beanName, this.beanName); assertEquals(def, this.beanDefinition); - } diff --git a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java index d27069295d..9d27309172 100644 --- a/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java @@ -272,7 +272,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe * @see #setQuartzProperties * @see LocalTaskExecutorThreadPool * @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - * @see org.springframework.scheduling.commonj.WorkManagerTaskExecutor + * @see org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor */ public void setTaskExecutor(Executor taskExecutor) { this.taskExecutor = taskExecutor; @@ -324,7 +324,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe *

Note: When using persistent Jobs whose JobDetail will be kept in the * database, do not put Spring-managed beans or an ApplicationContext * reference into the JobDataMap but rather into the SchedulerContext. - * @param schedulerContextAsMap Map with String keys and any objects as + * @param schedulerContextAsMap a Map with String keys and any objects as * values (for example Spring-managed beans) * @see JobDetailFactoryBean#setJobDataAsMap */ @@ -735,7 +735,7 @@ public class SchedulerFactoryBean extends SchedulerAccessor implements FactoryBe @Override public Class getObjectType() { - return (this.scheduler != null) ? this.scheduler.getClass() : Scheduler.class; + return (this.scheduler != null ? this.scheduler.getClass() : Scheduler.class); } @Override diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java index 7f0dcb57db..b2ebfa9f83 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java @@ -126,7 +126,7 @@ public abstract class SharedEntityManagerCreator { /** * Create a transactional EntityManager proxy for the given EntityManagerFactory. - * @param emf EntityManagerFactory to obtain EntityManagers from as needed + * @param emf the EntityManagerFactory to obtain EntityManagers from as needed * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @param entityManagerInterfaces the interfaces to be implemented by the @@ -141,7 +141,7 @@ public abstract class SharedEntityManagerCreator { /** * Create a transactional EntityManager proxy for the given EntityManagerFactory. - * @param emf EntityManagerFactory to obtain EntityManagers from as needed + * @param emf the EntityManagerFactory to obtain EntityManagers from as needed * @param properties the properties to be passed into the * {@code createEntityManager} call (may be {@code null}) * @param synchronizedWithTransaction whether to automatically join ongoing @@ -187,6 +187,7 @@ public abstract class SharedEntityManagerCreator { public SharedEntityManagerInvocationHandler( EntityManagerFactory target, Map properties, boolean synchronizedWithTransaction) { + this.targetFactory = target; this.properties = properties; this.synchronizedWithTransaction = synchronizedWithTransaction; @@ -339,11 +340,11 @@ public abstract class SharedEntityManagerCreator { private final Query target; - private EntityManager em; + private EntityManager entityManager; - public DeferredQueryInvocationHandler(Query target, EntityManager em) { + public DeferredQueryInvocationHandler(Query target, EntityManager entityManager) { this.target = target; - this.em = em; + this.entityManager = entityManager; } @Override @@ -381,8 +382,8 @@ public abstract class SharedEntityManagerCreator { if (queryTerminatingMethods.contains(method.getName())) { // Actual execution of the query: close the EntityManager right // afterwards, since that was the only reason we kept it open. - EntityManagerFactoryUtils.closeEntityManager(this.em); - this.em = null; + EntityManagerFactoryUtils.closeEntityManager(this.entityManager); + this.entityManager = null; } } } diff --git a/spring-orm/src/test/java/org/springframework/orm/jpa/SharedEntityManagerCreatorTests.java b/spring-orm/src/test/java/org/springframework/orm/jpa/SharedEntityManagerCreatorTests.java index 06b705ab22..4724c7b8e2 100644 --- a/spring-orm/src/test/java/org/springframework/orm/jpa/SharedEntityManagerCreatorTests.java +++ b/spring-orm/src/test/java/org/springframework/orm/jpa/SharedEntityManagerCreatorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2018 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,18 +18,23 @@ package org.springframework.orm.jpa; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; +import javax.persistence.Query; import javax.persistence.TransactionRequiredException; import org.junit.Test; import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.mockito.BDDMockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.withSettings; /** * Unit tests for {@link SharedEntityManagerCreator}. * * @author Oliver Gierke + * @author Juergen Hoeller */ public class SharedEntityManagerCreatorTests { @@ -83,4 +88,52 @@ public class SharedEntityManagerCreatorTests { em.refresh(new Object()); } + @Test + public void deferredQueryWithUpdate() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager targetEm = mock(EntityManager.class); + Query query = mock(Query.class); + given(emf.createEntityManager()).willReturn(targetEm); + given(targetEm.createQuery("x")).willReturn(query); + given(targetEm.isOpen()).willReturn(true); + + EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); + em.createQuery("x").executeUpdate(); + + verify(query).executeUpdate(); + verify(targetEm).close(); + } + + @Test + public void deferredQueryWithSingleResult() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager targetEm = mock(EntityManager.class); + Query query = mock(Query.class); + given(emf.createEntityManager()).willReturn(targetEm); + given(targetEm.createQuery("x")).willReturn(query); + given(targetEm.isOpen()).willReturn(true); + + EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); + em.createQuery("x").getSingleResult(); + + verify(query).getSingleResult(); + verify(targetEm).close(); + } + + @Test + public void deferredQueryWithResultList() { + EntityManagerFactory emf = mock(EntityManagerFactory.class); + EntityManager targetEm = mock(EntityManager.class); + Query query = mock(Query.class); + given(emf.createEntityManager()).willReturn(targetEm); + given(targetEm.createQuery("x")).willReturn(query); + given(targetEm.isOpen()).willReturn(true); + + EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf); + em.createQuery("x").getResultList(); + + verify(query).getResultList(); + verify(targetEm).close(); + } + }