Skip to content

Commit 13637ec

Browse files
committed
Polishing
(cherry picked from commit dc36bb3)
1 parent 7da02fb commit 13637ec

File tree

5 files changed

+90
-45
lines changed

5 files changed

+90
-45
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/config/CustomScopeConfigurerTests.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22-
import org.junit.Before;
2322
import org.junit.Test;
2423

2524
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@@ -34,36 +33,33 @@
3433
* @author Juergen Hoeller
3534
* @author Chris Beams
3635
*/
37-
public final class CustomScopeConfigurerTests {
36+
public class CustomScopeConfigurerTests {
3837

3938
private static final String FOO_SCOPE = "fooScope";
40-
private ConfigurableListableBeanFactory factory;
4139

42-
@Before
43-
public void setUp() {
44-
factory = new DefaultListableBeanFactory();
45-
}
40+
private final ConfigurableListableBeanFactory factory = new DefaultListableBeanFactory();
41+
4642

4743
@Test
48-
public void testWithNoScopes() throws Exception {
44+
public void testWithNoScopes() {
4945
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
5046
figurer.postProcessBeanFactory(factory);
5147
}
5248

5349
@Test
54-
public void testSunnyDayWithBonaFideScopeInstance() throws Exception {
50+
public void testSunnyDayWithBonaFideScopeInstance() {
5551
Scope scope = mock(Scope.class);
5652
factory.registerScope(FOO_SCOPE, scope);
57-
Map<String, Object> scopes = new HashMap<String, Object>();
53+
Map<String, Object> scopes = new HashMap<>();
5854
scopes.put(FOO_SCOPE, scope);
5955
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
6056
figurer.setScopes(scopes);
6157
figurer.postProcessBeanFactory(factory);
6258
}
6359

6460
@Test
65-
public void testSunnyDayWithBonaFideScopeClass() throws Exception {
66-
Map<String, Object> scopes = new HashMap<String, Object>();
61+
public void testSunnyDayWithBonaFideScopeClass() {
62+
Map<String, Object> scopes = new HashMap<>();
6763
scopes.put(FOO_SCOPE, NoOpScope.class);
6864
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
6965
figurer.setScopes(scopes);
@@ -72,38 +68,38 @@ public void testSunnyDayWithBonaFideScopeClass() throws Exception {
7268
}
7369

7470
@Test
75-
public void testSunnyDayWithBonaFideScopeClassname() throws Exception {
76-
Map<String, Object> scopes = new HashMap<String, Object>();
71+
public void testSunnyDayWithBonaFideScopeClassName() {
72+
Map<String, Object> scopes = new HashMap<>();
7773
scopes.put(FOO_SCOPE, NoOpScope.class.getName());
7874
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
7975
figurer.setScopes(scopes);
8076
figurer.postProcessBeanFactory(factory);
8177
assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
8278
}
8379

84-
@Test(expected=IllegalArgumentException.class)
85-
public void testWhereScopeMapHasNullScopeValueInEntrySet() throws Exception {
86-
Map<String, Object> scopes = new HashMap<String, Object>();
80+
@Test(expected = IllegalArgumentException.class)
81+
public void testWhereScopeMapHasNullScopeValueInEntrySet() {
82+
Map<String, Object> scopes = new HashMap<>();
8783
scopes.put(FOO_SCOPE, null);
8884
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
8985
figurer.setScopes(scopes);
9086
figurer.postProcessBeanFactory(factory);
9187
}
9288

93-
@Test(expected=IllegalArgumentException.class)
94-
public void testWhereScopeMapHasNonScopeInstanceInEntrySet() throws Exception {
95-
Map<String, Object> scopes = new HashMap<String, Object>();
96-
scopes.put(FOO_SCOPE, this); // <-- not a valid value...
89+
@Test(expected = IllegalArgumentException.class)
90+
public void testWhereScopeMapHasNonScopeInstanceInEntrySet() {
91+
Map<String, Object> scopes = new HashMap<>();
92+
scopes.put(FOO_SCOPE, this); // <-- not a valid value...
9793
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
9894
figurer.setScopes(scopes);
9995
figurer.postProcessBeanFactory(factory);
10096
}
10197

10298
@SuppressWarnings("unchecked")
103-
@Test(expected=ClassCastException.class)
104-
public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() throws Exception {
99+
@Test(expected = ClassCastException.class)
100+
public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() {
105101
Map scopes = new HashMap();
106-
scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)...
102+
scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)...
107103
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
108104
figurer.setScopes(scopes);
109105
figurer.postProcessBeanFactory(factory);

spring-beans/src/test/java/org/springframework/beans/factory/config/DeprecatedBeanWarnerTests.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,28 +28,23 @@
2828
*/
2929
public class DeprecatedBeanWarnerTests {
3030

31-
private DefaultListableBeanFactory beanFactory;
32-
3331
private String beanName;
3432

3533
private BeanDefinition beanDefinition;
3634

37-
private DeprecatedBeanWarner warner;
38-
3935

4036
@Test
4137
@SuppressWarnings("deprecation")
4238
public void postProcess() {
43-
beanFactory = new DefaultListableBeanFactory();
39+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
4440
BeanDefinition def = new RootBeanDefinition(MyDeprecatedBean.class);
4541
String beanName = "deprecated";
4642
beanFactory.registerBeanDefinition(beanName, def);
4743

48-
warner = new MyDeprecatedBeanWarner();
44+
DeprecatedBeanWarner warner = new MyDeprecatedBeanWarner();
4945
warner.postProcessBeanFactory(beanFactory);
5046
assertEquals(beanName, this.beanName);
5147
assertEquals(def, this.beanDefinition);
52-
5348
}
5449

5550

spring-context-support/src/main/java/org/springframework/scheduling/quartz/SchedulerFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ public void setQuartzProperties(Properties quartzProperties) {
272272
* @see #setQuartzProperties
273273
* @see LocalTaskExecutorThreadPool
274274
* @see org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
275-
* @see org.springframework.scheduling.commonj.WorkManagerTaskExecutor
275+
* @see org.springframework.scheduling.concurrent.DefaultManagedTaskExecutor
276276
*/
277277
public void setTaskExecutor(Executor taskExecutor) {
278278
this.taskExecutor = taskExecutor;
@@ -324,7 +324,7 @@ public void setNonTransactionalDataSource(DataSource nonTransactionalDataSource)
324324
* <p>Note: When using persistent Jobs whose JobDetail will be kept in the
325325
* database, do not put Spring-managed beans or an ApplicationContext
326326
* reference into the JobDataMap but rather into the SchedulerContext.
327-
* @param schedulerContextAsMap Map with String keys and any objects as
327+
* @param schedulerContextAsMap a Map with String keys and any objects as
328328
* values (for example Spring-managed beans)
329329
* @see JobDetailFactoryBean#setJobDataAsMap
330330
*/
@@ -735,7 +735,7 @@ public Scheduler getObject() {
735735

736736
@Override
737737
public Class<? extends Scheduler> getObjectType() {
738-
return (this.scheduler != null) ? this.scheduler.getClass() : Scheduler.class;
738+
return (this.scheduler != null ? this.scheduler.getClass() : Scheduler.class);
739739
}
740740

741741
@Override

spring-orm/src/main/java/org/springframework/orm/jpa/SharedEntityManagerCreator.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static EntityManager createSharedEntityManager(
126126

127127
/**
128128
* Create a transactional EntityManager proxy for the given EntityManagerFactory.
129-
* @param emf EntityManagerFactory to obtain EntityManagers from as needed
129+
* @param emf the EntityManagerFactory to obtain EntityManagers from as needed
130130
* @param properties the properties to be passed into the
131131
* {@code createEntityManager} call (may be {@code null})
132132
* @param entityManagerInterfaces the interfaces to be implemented by the
@@ -141,7 +141,7 @@ public static EntityManager createSharedEntityManager(
141141

142142
/**
143143
* Create a transactional EntityManager proxy for the given EntityManagerFactory.
144-
* @param emf EntityManagerFactory to obtain EntityManagers from as needed
144+
* @param emf the EntityManagerFactory to obtain EntityManagers from as needed
145145
* @param properties the properties to be passed into the
146146
* {@code createEntityManager} call (may be {@code null})
147147
* @param synchronizedWithTransaction whether to automatically join ongoing
@@ -187,6 +187,7 @@ private static class SharedEntityManagerInvocationHandler implements InvocationH
187187

188188
public SharedEntityManagerInvocationHandler(
189189
EntityManagerFactory target, Map<?, ?> properties, boolean synchronizedWithTransaction) {
190+
190191
this.targetFactory = target;
191192
this.properties = properties;
192193
this.synchronizedWithTransaction = synchronizedWithTransaction;
@@ -339,11 +340,11 @@ private static class DeferredQueryInvocationHandler implements InvocationHandler
339340

340341
private final Query target;
341342

342-
private EntityManager em;
343+
private EntityManager entityManager;
343344

344-
public DeferredQueryInvocationHandler(Query target, EntityManager em) {
345+
public DeferredQueryInvocationHandler(Query target, EntityManager entityManager) {
345346
this.target = target;
346-
this.em = em;
347+
this.entityManager = entityManager;
347348
}
348349

349350
@Override
@@ -381,8 +382,8 @@ else if (targetClass.isInstance(proxy)) {
381382
if (queryTerminatingMethods.contains(method.getName())) {
382383
// Actual execution of the query: close the EntityManager right
383384
// afterwards, since that was the only reason we kept it open.
384-
EntityManagerFactoryUtils.closeEntityManager(this.em);
385-
this.em = null;
385+
EntityManagerFactoryUtils.closeEntityManager(this.entityManager);
386+
this.entityManager = null;
386387
}
387388
}
388389
}

spring-orm/src/test/java/org/springframework/orm/jpa/SharedEntityManagerCreatorTests.java

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,18 +18,23 @@
1818

1919
import javax.persistence.EntityManager;
2020
import javax.persistence.EntityManagerFactory;
21+
import javax.persistence.Query;
2122
import javax.persistence.TransactionRequiredException;
2223

2324
import org.junit.Test;
2425

2526
import static org.hamcrest.CoreMatchers.*;
2627
import static org.junit.Assert.*;
27-
import static org.mockito.Mockito.*;
28+
import static org.mockito.BDDMockito.*;
29+
import static org.mockito.Mockito.mock;
30+
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.withSettings;
2832

2933
/**
3034
* Unit tests for {@link SharedEntityManagerCreator}.
3135
*
3236
* @author Oliver Gierke
37+
* @author Juergen Hoeller
3338
*/
3439
public class SharedEntityManagerCreatorTests {
3540

@@ -83,4 +88,52 @@ public void transactionRequiredExceptionOnRefresh() {
8388
em.refresh(new Object());
8489
}
8590

91+
@Test
92+
public void deferredQueryWithUpdate() {
93+
EntityManagerFactory emf = mock(EntityManagerFactory.class);
94+
EntityManager targetEm = mock(EntityManager.class);
95+
Query query = mock(Query.class);
96+
given(emf.createEntityManager()).willReturn(targetEm);
97+
given(targetEm.createQuery("x")).willReturn(query);
98+
given(targetEm.isOpen()).willReturn(true);
99+
100+
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
101+
em.createQuery("x").executeUpdate();
102+
103+
verify(query).executeUpdate();
104+
verify(targetEm).close();
105+
}
106+
107+
@Test
108+
public void deferredQueryWithSingleResult() {
109+
EntityManagerFactory emf = mock(EntityManagerFactory.class);
110+
EntityManager targetEm = mock(EntityManager.class);
111+
Query query = mock(Query.class);
112+
given(emf.createEntityManager()).willReturn(targetEm);
113+
given(targetEm.createQuery("x")).willReturn(query);
114+
given(targetEm.isOpen()).willReturn(true);
115+
116+
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
117+
em.createQuery("x").getSingleResult();
118+
119+
verify(query).getSingleResult();
120+
verify(targetEm).close();
121+
}
122+
123+
@Test
124+
public void deferredQueryWithResultList() {
125+
EntityManagerFactory emf = mock(EntityManagerFactory.class);
126+
EntityManager targetEm = mock(EntityManager.class);
127+
Query query = mock(Query.class);
128+
given(emf.createEntityManager()).willReturn(targetEm);
129+
given(targetEm.createQuery("x")).willReturn(query);
130+
given(targetEm.isOpen()).willReturn(true);
131+
132+
EntityManager em = SharedEntityManagerCreator.createSharedEntityManager(emf);
133+
em.createQuery("x").getResultList();
134+
135+
verify(query).getResultList();
136+
verify(targetEm).close();
137+
}
138+
86139
}

0 commit comments

Comments
 (0)