1
1
/*
2
- * Copyright 2018-2023 the original author or authors.
2
+ * Copyright 2018-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
25
25
import static org .mockito .Mockito .mock ;
26
26
import static org .mockito .Mockito .times ;
27
27
28
- import java .lang .annotation .ElementType ;
29
28
import java .lang .annotation .Retention ;
30
29
import java .lang .annotation .RetentionPolicy ;
31
- import java .lang .annotation .Target ;
32
30
import java .lang .reflect .Method ;
33
31
import java .util .Collections ;
34
32
47
45
* @author Tomaz Fernandes
48
46
* @author Gary Russell
49
47
* @author Fabio da Silva Jr.
48
+ * @author Wang Zhiyang
49
+ *
50
50
* @since 2.7
51
51
*/
52
52
@ ExtendWith (MockitoExtension .class )
@@ -56,9 +56,7 @@ class RetryTopicConfigurationProviderTests {
56
56
57
57
{
58
58
this .beanFactory = mock (ConfigurableListableBeanFactory .class );
59
- willAnswer (invoc -> {
60
- return invoc .getArgument (0 );
61
- }).given (this .beanFactory ).resolveEmbeddedValue (anyString ());
59
+ willAnswer (invoc -> invoc .getArgument (0 )).given (this .beanFactory ).resolveEmbeddedValue (anyString ());
62
60
}
63
61
64
62
private final String [] topics = {"topic1" , "topic2" };
@@ -81,18 +79,12 @@ private Method getAnnotatedMethod(String methodName) {
81
79
@ Mock
82
80
Object bean ;
83
81
84
- @ Mock
85
- RetryableTopic annotation ;
86
-
87
82
@ Mock
88
83
KafkaOperations <?, ?> kafkaOperations ;
89
84
90
85
@ Mock
91
86
RetryTopicConfiguration retryTopicConfiguration ;
92
87
93
- @ Mock
94
- RetryTopicConfiguration retryTopicConfiguration2 ;
95
-
96
88
@ Test
97
89
void shouldProvideFromAnnotation () {
98
90
@@ -102,10 +94,13 @@ void shouldProvideFromAnnotation() {
102
94
// given
103
95
RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
104
96
RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , annotatedMethod , bean );
97
+ RetryTopicConfiguration configurationFromClass = provider
98
+ .findRetryConfigurationFor (topics , null , AnnotatedClass .class , bean );
105
99
106
100
// then
107
101
then (this .beanFactory ).should (times (0 )).getBeansOfType (RetryTopicConfiguration .class );
108
-
102
+ assertThat (configuration ).isNotNull ();
103
+ assertThat (configurationFromClass ).isNotNull ();
109
104
}
110
105
111
106
@ Test
@@ -119,10 +114,13 @@ void shouldProvideFromBeanFactory() {
119
114
// given
120
115
RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
121
116
RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
117
+ RetryTopicConfiguration configurationFromClass = provider
118
+ .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
122
119
123
120
// then
124
- then (this .beanFactory ).should (times (1 )).getBeansOfType (RetryTopicConfiguration .class );
121
+ then (this .beanFactory ).should (times (2 )).getBeansOfType (RetryTopicConfiguration .class );
125
122
assertThat (configuration ).isEqualTo (retryTopicConfiguration );
123
+ assertThat (configurationFromClass ).isEqualTo (retryTopicConfiguration );
126
124
127
125
}
128
126
@@ -137,10 +135,13 @@ void shouldFindNone() {
137
135
// given
138
136
RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
139
137
RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
138
+ RetryTopicConfiguration configurationFromClass = provider
139
+ .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
140
140
141
141
// then
142
- then (this .beanFactory ).should (times (1 )).getBeansOfType (RetryTopicConfiguration .class );
142
+ then (this .beanFactory ).should (times (2 )).getBeansOfType (RetryTopicConfiguration .class );
143
143
assertThat (configuration ).isNull ();
144
+ assertThat (configurationFromClass ).isNull ();
144
145
145
146
}
146
147
@@ -153,10 +154,15 @@ void shouldProvideFromMetaAnnotation() {
153
154
// given
154
155
RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (beanFactory );
155
156
RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , metaAnnotatedMethod , bean );
157
+ RetryTopicConfiguration configurationFromClass = provider
158
+ .findRetryConfigurationFor (topics , null , MetaAnnotatedClass .class , bean );
156
159
157
160
// then
158
161
then (this .beanFactory ).should (times (0 )).getBeansOfType (RetryTopicConfiguration .class );
162
+ assertThat (configuration ).isNotNull ();
159
163
assertThat (configuration .getConcurrency ()).isEqualTo (3 );
164
+ assertThat (configurationFromClass ).isNotNull ();
165
+ assertThat (configurationFromClass .getConcurrency ()).isEqualTo (3 );
160
166
161
167
}
162
168
@@ -166,9 +172,12 @@ void shouldNotConfigureIfBeanFactoryNull() {
166
172
// given
167
173
RetryTopicConfigurationProvider provider = new RetryTopicConfigurationProvider (null );
168
174
RetryTopicConfiguration configuration = provider .findRetryConfigurationFor (topics , nonAnnotatedMethod , bean );
175
+ RetryTopicConfiguration configurationFromClass
176
+ = provider .findRetryConfigurationFor (topics , null , NonAnnotatedClass .class , bean );
169
177
170
178
// then
171
179
assertThat (configuration ).isNull ();
180
+ assertThat (configurationFromClass ).isNull ();
172
181
173
182
}
174
183
@@ -181,7 +190,6 @@ public void nonAnnotatedMethod() {
181
190
// NoOps
182
191
}
183
192
184
- @ Target ({ElementType .METHOD })
185
193
@ Retention (RetentionPolicy .RUNTIME )
186
194
@ RetryableTopic
187
195
@interface MetaAnnotatedRetryableTopic {
@@ -193,4 +201,19 @@ public void nonAnnotatedMethod() {
193
201
public void metaAnnotatedMethod () {
194
202
// NoOps
195
203
}
204
+
205
+ @ RetryableTopic
206
+ public static class AnnotatedClass {
207
+ // NoOps
208
+ }
209
+
210
+ public static class NonAnnotatedClass {
211
+ // NoOps
212
+ }
213
+
214
+ @ MetaAnnotatedRetryableTopic
215
+ public static class MetaAnnotatedClass {
216
+ // NoOps
217
+ }
218
+
196
219
}
0 commit comments