View Javadoc
1   /*
2   * Copyright 2019-2026 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    *      http://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.bremersee.comparator;
18  
19  import static org.assertj.core.api.Assertions.assertThat;
20  import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21  import static org.mockito.ArgumentMatchers.any;
22  import static org.mockito.ArgumentMatchers.anyString;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.times;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.Mockito.when;
27  
28  import java.util.Comparator;
29  import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  
33  /**
34   * The delegating comparator tests.
35   *
36   * @author Christian Bremer
37   */
38  @ExtendWith(SoftAssertionsExtension.class)
39  class DelegatingComparatorTest {
40  
41    /**
42     * Test delegating comparator.
43     */
44    @Test
45    @SuppressWarnings({"rawtypes", "unchecked"})
46    void testDelegatingComparator() {
47      ValueExtractor valueExtractor = mock(ValueExtractor.class);
48      when(valueExtractor.findValue(any(), anyString())).thenReturn(1);
49  
50      Comparator comparator = mock(Comparator.class);
51      when(comparator.compare(any(), any())).thenReturn(-1);
52  
53      DelegatingComparator delegatingComparator = new DelegatingComparator(
54          "someField",
55          valueExtractor,
56          comparator);
57  
58      int result = delegatingComparator.compare(1, 2);
59  
60      assertThat(result)
61          .isLessThan(0);
62      verify(comparator, times(1)).compare(any(), any());
63      verify(valueExtractor, times(2)).findValue(any(), anyString());
64    }
65  
66    /**
67     * Test delegating comparator and expect illegal argument exception.
68     */
69    @Test
70    void testDelegatingComparatorAndExpectIllegalArgumentException() {
71      assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> {
72        //noinspection ConstantConditions
73        new DelegatingComparator("someField", null);
74      });
75    }
76  
77    /**
78     * Test to string.
79     */
80    @Test
81    void testToString() {
82      ValueExtractor valueExtractor = mock(ValueExtractor.class);
83      when(valueExtractor.toString()).thenReturn("_ValueExtractor_");
84  
85      Comparator<?> comparator = mock(Comparator.class);
86      when(comparator.toString()).thenReturn("_Comparator_");
87  
88      DelegatingComparator delegatingComparator = new DelegatingComparator(
89          "_test_field_",
90          valueExtractor,
91          comparator);
92  
93      assertThat(delegatingComparator.toString())
94          .contains("_ValueExtractor_", "_Comparator_", "_test_field_");
95    }
96  
97  }