Skip to content

Commit 9465ff0

Browse files
committed
Add support for merging two BeanRegistrationAotContribution instances
Closes gh-31446
1 parent 22db1ac commit 9465ff0

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotContribution.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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,6 +19,7 @@
1919
import java.util.function.UnaryOperator;
2020

2121
import org.springframework.aot.generate.GenerationContext;
22+
import org.springframework.lang.Nullable;
2223
import org.springframework.util.Assert;
2324

2425
/**
@@ -82,4 +83,31 @@ public void applyTo(GenerationContext generationContext,
8283
};
8384
}
8485

86+
/**
87+
* Create a contribution that applies the contribution of the first contribution
88+
* followed by the second contribution. Any contribution can be {@code null} to be
89+
* ignored and the concatenated contribution is {@code null} if both inputs are
90+
* {@code null}.
91+
* @param a the first contribution
92+
* @param b the second contribution
93+
* @return the concatenation of the two contributions, or {@code null} if
94+
* they are both {@code null}.
95+
* @since 6.1
96+
*/
97+
@Nullable
98+
static BeanRegistrationAotContribution concat(@Nullable BeanRegistrationAotContribution a,
99+
@Nullable BeanRegistrationAotContribution b) {
100+
101+
if (a == null) {
102+
return b;
103+
}
104+
if (b == null) {
105+
return a;
106+
}
107+
return (generationContext, beanRegistrationCode) -> {
108+
a.applyTo(generationContext, beanRegistrationCode);
109+
b.applyTo(generationContext, beanRegistrationCode);
110+
};
111+
}
112+
85113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.beans.factory.aot;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.InOrder;
21+
22+
import org.springframework.aot.test.generate.TestGenerationContext;
23+
import org.springframework.beans.testfixture.beans.factory.aot.MockBeanRegistrationCode;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.Mockito.inOrder;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.verifyNoInteractions;
29+
30+
/**
31+
* Tests for {@link BeanRegistrationAotContribution}.
32+
*
33+
* @author Stephane Nicoll
34+
*/
35+
class BeanRegistrationAotContributionTests {
36+
37+
@Test
38+
void concatWithBothNullReturnsNull() {
39+
assertThat(BeanRegistrationAotContribution.concat(null, null)).isNull();
40+
}
41+
42+
@Test
43+
void concatWithFirstNullReturnsSecondAsIs() {
44+
BeanRegistrationAotContribution contribution = mock(BeanRegistrationAotContribution.class);
45+
assertThat(BeanRegistrationAotContribution.concat(null, contribution)).isSameAs(contribution);
46+
verifyNoInteractions(contribution);
47+
}
48+
49+
@Test
50+
void concatWithSecondNullReturnsFirstAsIs() {
51+
BeanRegistrationAotContribution contribution = mock(BeanRegistrationAotContribution.class);
52+
assertThat(BeanRegistrationAotContribution.concat(contribution, null)).isSameAs(contribution);
53+
verifyNoInteractions(contribution);
54+
}
55+
56+
@Test
57+
void concatApplyContributionsInOrder() {
58+
BeanRegistrationAotContribution first = mock(BeanRegistrationAotContribution.class);
59+
BeanRegistrationAotContribution second = mock(BeanRegistrationAotContribution.class);
60+
BeanRegistrationAotContribution combined = BeanRegistrationAotContribution.concat(first, second);
61+
assertThat(combined).isNotNull();
62+
TestGenerationContext generationContext = new TestGenerationContext();
63+
BeanRegistrationCode beanRegistrationCode = new MockBeanRegistrationCode(generationContext);
64+
combined.applyTo(generationContext, beanRegistrationCode);
65+
InOrder ordered = inOrder(first, second);
66+
ordered.verify(first).applyTo(generationContext, beanRegistrationCode);
67+
ordered.verify(second).applyTo(generationContext, beanRegistrationCode);
68+
}
69+
70+
}

0 commit comments

Comments
 (0)