Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ protected AbstractReactiveFeignConfigurator(int order) {

@Override
public int compareTo(ReactiveFeignConfigurator configurator){
if (this == configurator) {
return 0;
}
int compare = Integer.compare(order, ((AbstractReactiveFeignConfigurator) configurator).order);
if(compare == 0){
throw new IllegalArgumentException(String.format("Same order for different configurators: [%s], [%s]",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package reactivefeign.spring.config;

import org.junit.Test;
import reactivefeign.ReactiveFeignBuilder;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class AbstractReactiveFeignConfiguratorTest {

@Test
public void shouldCompareSameInstance() {
TestConfigurator configurator = new TestConfigurator(1);
assertThat(configurator.compareTo(configurator)).isEqualTo(0);
}

@Test
public void shouldCompareDifferentOrder() {
TestConfigurator configurator1 = new TestConfigurator(1);
TestConfigurator configurator2 = new TestConfigurator(2);

assertThat(configurator1.compareTo(configurator2)).isLessThan(0);
assertThat(configurator2.compareTo(configurator1)).isGreaterThan(0);
}

@Test
public void shouldThrowExceptionWhenSameOrder() {
TestConfigurator configurator1 = new TestConfigurator(1);
TestConfigurator configurator2 = new TestConfigurator(1);

assertThatThrownBy(() -> configurator1.compareTo(configurator2))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Same order for different configurators");
}

@Test
public void shouldBeTransitive() {
TestConfigurator configurator1 = new TestConfigurator(1);
TestConfigurator configurator2 = new TestConfigurator(2);
TestConfigurator configurator3 = new TestConfigurator(3);

// Verifies that if c1 < c2 and c2 < c3, then c1 < c3
assertThat(configurator1.compareTo(configurator2)).isLessThan(0);
assertThat(configurator2.compareTo(configurator3)).isLessThan(0);
assertThat(configurator1.compareTo(configurator3)).isLessThan(0);
}

private static class TestConfigurator extends AbstractReactiveFeignConfigurator {
protected TestConfigurator(int order) {
super(order);
}

@Override
public ReactiveFeignBuilder configure(ReactiveFeignBuilder builder, ReactiveFeignNamedContext namedContext) {
return builder;
}
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of compareTo in AbstractReactiveFeignConfigurator is not robust against different implementations of ReactiveFeignConfigurator, as it will throw a ClassCastException. This violates the general contract of Comparable. It would be beneficial to add a test case to highlight this issue, which would encourage a more robust implementation.

For example, you could add a test that uses a different implementation of ReactiveFeignConfigurator:

    @Test
    public void shouldCompareWithDifferentImplementationsWithoutCastingError() {
        TestConfigurator configurator = new TestConfigurator(1);

        // A simple implementation that doesn't extend AbstractReactiveFeignConfigurator
        ReactiveFeignConfigurator otherImplementation = new ReactiveFeignConfigurator() {
            @Override
            public ReactiveFeignBuilder configure(ReactiveFeignBuilder builder, ReactiveFeignNamedContext namedContext) {
                return builder;
            }

            @Override
            public int compareTo(ReactiveFeignConfigurator o) {
                // A real implementation would have more logic here
                return -1;
            }
        };

        // This should not throw ClassCastException
        assertThatCode(() -> configurator.compareTo(otherImplementation))
                .doesNotThrow(ClassCastException.class);
    }

This test will currently fail, revealing the issue in AbstractReactiveFeignConfigurator. A potential fix would be to use an instanceof check and define a consistent ordering for different types.

Loading