From aeabc562ef6cc59e4cfc08c8361ef3aa9da64b37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Michael?= Date: Thu, 2 Dec 2021 04:24:21 +0100 Subject: [PATCH] GH-1396: Declarables constructor is too strict Resolves https://github.com/spring-projects/spring-amqp/issues/1396 * Make constructor more flexible by upper bounded wildcard * Review amendments --- .../amqp/core/Declarables.java | 8 +-- .../amqp/core/DeclarablesTests.java | 55 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 spring-amqp/src/test/java/org/springframework/amqp/core/DeclarablesTests.java diff --git a/spring-amqp/src/main/java/org/springframework/amqp/core/Declarables.java b/spring-amqp/src/main/java/org/springframework/amqp/core/Declarables.java index a6726d85..a5892ef9 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/core/Declarables.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/core/Declarables.java @@ -1,5 +1,5 @@ /* - * Copyright 2018-2019 the original author or authors. + * Copyright 2018-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import org.springframework.util.ObjectUtils; * broker using a single bean declaration for the collection. * * @author Gary Russell + * @author Björn Michael * @since 2.1 */ public class Declarables { @@ -42,7 +43,7 @@ public class Declarables { } } - public Declarables(Collection declarables) { + public Declarables(Collection declarables) { Assert.notNull(declarables, "declarables cannot be null"); this.declarables.addAll(declarables); } @@ -58,11 +59,10 @@ public class Declarables { * @return the filtered list. * @since 2.2 */ - @SuppressWarnings("unchecked") public List getDeclarablesByType(Class type) { return this.declarables.stream() .filter(type::isInstance) - .map(dec -> (T) dec) + .map(type::cast) .collect(Collectors.toList()); } diff --git a/spring-amqp/src/test/java/org/springframework/amqp/core/DeclarablesTests.java b/spring-amqp/src/test/java/org/springframework/amqp/core/DeclarablesTests.java new file mode 100644 index 00000000..a1a9f12f --- /dev/null +++ b/spring-amqp/src/test/java/org/springframework/amqp/core/DeclarablesTests.java @@ -0,0 +1,55 @@ +/* + * Copyright 2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.amqp.core; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +/** + * @author Björn Michael + * @since 2.4 + */ +public class DeclarablesTests { + + @Test + public void getDeclarables() { + List queues = List.of( + new Queue("q1", false, false, true), + new Queue("q2", false, false, true)); + Declarables declarables = new Declarables(queues); + + assertThat(declarables.getDeclarables()).hasSameElementsAs(queues); + } + + @Test + public void getDeclarablesByType() { + Queue queue = new Queue("queue"); + TopicExchange exchange = new TopicExchange("exchange"); + Binding binding = BindingBuilder.bind(queue).to(exchange).with("foo.bar"); + Declarables declarables = new Declarables(queue, exchange, binding); + + assertThat(declarables.getDeclarablesByType(Queue.class)).containsExactlyInAnyOrder(queue); + assertThat(declarables.getDeclarablesByType(Exchange.class)).containsExactlyInAnyOrder(exchange); + assertThat(declarables.getDeclarablesByType(Binding.class)).containsExactlyInAnyOrder(binding); + assertThat(declarables.getDeclarablesByType(Declarable.class)).containsExactlyInAnyOrder( + queue, exchange, binding); + } + +}