Skip to content

Commit a9a90ca

Browse files
philwebbcbeams
authored andcommitted
Protect against non-deterministic method order in JDK7
- Allow reset of GlobalAdvisorAdapterRegistry Provide a reset() method allowing the GlobalAdvisorAdapterRegistry instance to be replaced with a fresh instance. This method has primarily been added to allow unit tests to leave the registry in a known state. - Protect against the fact that calls to configuration class methods my occur in a random order. Issue: SPR-9779
1 parent 8e7622b commit a9a90ca

File tree

3 files changed

+50
-33
lines changed

3 files changed

+50
-33
lines changed
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
3-
*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -21,20 +21,30 @@
2121
*
2222
* @author Rod Johnson
2323
* @author Juergen Hoeller
24+
* @author Phillip Webb
2425
* @see DefaultAdvisorAdapterRegistry
2526
*/
2627
public abstract class GlobalAdvisorAdapterRegistry {
2728

2829
/**
2930
* Keep track of a single instance so we can return it to classes that request it.
3031
*/
31-
private static final AdvisorAdapterRegistry instance = new DefaultAdvisorAdapterRegistry();
32-
32+
private static AdvisorAdapterRegistry instance = new DefaultAdvisorAdapterRegistry();
33+
3334
/**
34-
* Return the singleton DefaultAdvisorAdapterRegistry instance.
35+
* Return the singleton {@link DefaultAdvisorAdapterRegistry} instance.
3536
*/
3637
public static AdvisorAdapterRegistry getInstance() {
3738
return instance;
3839
}
3940

41+
/**
42+
* Reset the singleton {@link DefaultAdvisorAdapterRegistry}, removing any
43+
* {@link AdvisorAdapterRegistry#registerAdvisorAdapter(AdvisorAdapter) registered}
44+
* adapters.
45+
*/
46+
static void reset() {
47+
instance = new DefaultAdvisorAdapterRegistry();
48+
}
49+
4050
}

spring-context/src/test/java/org/springframework/aop/framework/adapter/AdvisorAdapterRegistrationTests.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Copyright 2002-2005 the original author or authors.
3-
*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,6 +23,8 @@
2323
import org.aopalliance.aop.Advice;
2424
import org.aopalliance.intercept.MethodInterceptor;
2525
import org.aopalliance.intercept.MethodInvocation;
26+
import org.junit.After;
27+
import org.junit.Before;
2628
import org.junit.Test;
2729
import org.springframework.aop.Advisor;
2830
import org.springframework.aop.BeforeAdvice;
@@ -38,6 +40,12 @@
3840
*/
3941
public final class AdvisorAdapterRegistrationTests {
4042

43+
@Before
44+
@After
45+
public void resetGlobalAdvisorAdapterRegistry() {
46+
GlobalAdvisorAdapterRegistry.reset();
47+
}
48+
4149
@Test
4250
public void testAdvisorAdapterRegistrationManagerNotPresentInContext() {
4351
ClassPathXmlApplicationContext ctx =
@@ -101,7 +109,7 @@ public MethodInterceptor getInterceptor(Advisor advisor) {
101109

102110

103111
class SimpleBeforeAdviceImpl implements SimpleBeforeAdvice {
104-
112+
105113
private int invocationCounter;
106114

107115
public void before() throws Throwable {
@@ -116,9 +124,9 @@ public int getInvocationCounter() {
116124

117125

118126
final class SimpleBeforeAdviceInterceptor implements MethodInterceptor {
119-
127+
120128
private SimpleBeforeAdvice advice;
121-
129+
122130
public SimpleBeforeAdviceInterceptor(SimpleBeforeAdvice advice) {
123131
this.advice = advice;
124132
}
@@ -127,5 +135,4 @@ public Object invoke(MethodInvocation mi) throws Throwable {
127135
advice.before();
128136
return mi.proceed();
129137
}
130-
131-
}
138+
}

spring-test/src/test/java/org/springframework/test/context/junit4/spr9051/AnnotatedConfigClassesWithoutAtConfigurationTests.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.test.context.junit4.spr9051;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertTrue;
19+
import static org.hamcrest.Matchers.*;
20+
import static org.hamcrest.Matchers.is;
21+
import static org.junit.Assert.*;
2322

23+
import java.util.Arrays;
24+
import java.util.HashSet;
25+
import java.util.Set;
2426
import java.util.concurrent.atomic.AtomicInteger;
2527

2628
import org.junit.Test;
@@ -30,22 +32,24 @@
3032
import org.springframework.test.context.ContextConfiguration;
3133
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3234

35+
3336
/**
3437
* This set of tests refutes the claims made in
3538
* <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a>.
36-
*
39+
*
3740
* <p><b>The Claims</b>:
38-
*
41+
*
3942
* <blockquote>
4043
* When a {@code @ContextConfiguration} test class references a config class
4144
* missing an {@code @Configuration} annotation, {@code @Bean} dependencies are
4245
* wired successfully but the bean lifecycle is not applied (no init methods are
4346
* invoked, for example). Adding the missing {@code @Configuration} annotation
44-
* solves the problem, however the problem and solution isn't obvious since
47+
* solves the problem, however the problem and solution isn't obvious since
4548
* wiring/injection appeared to work.
4649
* </blockquote>
47-
*
50+
*
4851
* @author Sam Brannen
52+
* @author Phillip Webb
4953
* @since 3.2
5054
*/
5155
@RunWith(SpringJUnit4ClassRunner.class)
@@ -85,18 +89,14 @@ public LifecycleBean lifecycleBean() {
8589
@Autowired
8690
private LifecycleBean lifecycleBean;
8791

88-
8992
@Test
90-
public void simpleStringBean() {
93+
public void testSPR_9051() throws Exception {
9194
assertNotNull(enigma);
92-
assertEquals("enigma #1", enigma);
93-
}
94-
95-
@Test
96-
public void beanWithLifecycleCallback() {
9795
assertNotNull(lifecycleBean);
98-
assertEquals("enigma #2", lifecycleBean.getName());
9996
assertTrue(lifecycleBean.isInitialized());
97+
Set<String> names = new HashSet<String>();
98+
names.add(enigma.toString());
99+
names.add(lifecycleBean.getName());
100+
assertEquals(names, new HashSet<String>(Arrays.asList("enigma #1", "enigma #2")));
100101
}
101-
102102
}

0 commit comments

Comments
 (0)