Skip to content

Commit aeabc56

Browse files
authored
GH-1396: Declarables constructor is too strict
Resolves #1396 * Make constructor more flexible by upper bounded wildcard * Review amendments
1 parent a7fd715 commit aeabc56

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/Declarables.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2019 the original author or authors.
2+
* Copyright 2018-2021 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.
@@ -30,6 +30,7 @@
3030
* broker using a single bean declaration for the collection.
3131
*
3232
* @author Gary Russell
33+
* @author Björn Michael
3334
* @since 2.1
3435
*/
3536
public class Declarables {
@@ -42,7 +43,7 @@ public Declarables(Declarable... declarables) {
4243
}
4344
}
4445

45-
public Declarables(Collection<Declarable> declarables) {
46+
public Declarables(Collection<? extends Declarable> declarables) {
4647
Assert.notNull(declarables, "declarables cannot be null");
4748
this.declarables.addAll(declarables);
4849
}
@@ -58,11 +59,10 @@ public Collection<Declarable> getDeclarables() {
5859
* @return the filtered list.
5960
* @since 2.2
6061
*/
61-
@SuppressWarnings("unchecked")
6262
public <T> List<T> getDeclarablesByType(Class<T> type) {
6363
return this.declarables.stream()
6464
.filter(type::isInstance)
65-
.map(dec -> (T) dec)
65+
.map(type::cast)
6666
.collect(Collectors.toList());
6767
}
6868

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2021 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.amqp.core;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.List;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
/**
26+
* @author Björn Michael
27+
* @since 2.4
28+
*/
29+
public class DeclarablesTests {
30+
31+
@Test
32+
public void getDeclarables() {
33+
List<Queue> queues = List.of(
34+
new Queue("q1", false, false, true),
35+
new Queue("q2", false, false, true));
36+
Declarables declarables = new Declarables(queues);
37+
38+
assertThat(declarables.getDeclarables()).hasSameElementsAs(queues);
39+
}
40+
41+
@Test
42+
public void getDeclarablesByType() {
43+
Queue queue = new Queue("queue");
44+
TopicExchange exchange = new TopicExchange("exchange");
45+
Binding binding = BindingBuilder.bind(queue).to(exchange).with("foo.bar");
46+
Declarables declarables = new Declarables(queue, exchange, binding);
47+
48+
assertThat(declarables.getDeclarablesByType(Queue.class)).containsExactlyInAnyOrder(queue);
49+
assertThat(declarables.getDeclarablesByType(Exchange.class)).containsExactlyInAnyOrder(exchange);
50+
assertThat(declarables.getDeclarablesByType(Binding.class)).containsExactlyInAnyOrder(binding);
51+
assertThat(declarables.getDeclarablesByType(Declarable.class)).containsExactlyInAnyOrder(
52+
queue, exchange, binding);
53+
}
54+
55+
}

0 commit comments

Comments
 (0)