View Javadoc
1   /*
2    * Copyright 2018 Andrew Rucker Jones.
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  package com.opencsv.bean.comparator;
17  
18  import org.junit.jupiter.api.Test;
19  
20  import java.util.Arrays;
21  
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  
24  @SuppressWarnings("deprecation")
25  public class ComparatorTest {
26  
27      @Test
28      public void testComparatorNull() {
29          String[] array = new String[]{"abc", null, "bcd", "cde", "def", "xyz", "wxy"};
30          Arrays.sort(array, new LiteralComparator<String>(null));
31          assertArrayEquals(new String[]{null, "abc", "bcd", "cde", "def", "wxy", "xyz"}, array);
32      }
33  
34      /**
35       * Tests a {@link LiteralComparator} with an array of {@link java.lang.Integer}s.
36       * <p>Also incidentally tests:</p>
37       * <ul><li>Having a null in the data with no null in the comparator</li>
38       * <li>Having data otherwise not in the comparator</li></ul>
39       */
40      @Test
41      public void testLiteralComparatorInteger() {
42          Integer[] array = new Integer[]{1, 2, 3, 4, 5, 6, null, 12, 10};
43          Arrays.sort(array, new LiteralComparator<>(new Integer[]{2, 4, 6, 8, 1, 3, 5, 7}));
44          assertArrayEquals(new Integer[]{2, 4, 6, 1, 3, 5, null, 10, 12}, array);
45      }
46  
47      /**
48       * Tests a {@link LiteralComparator} with an array of {@link java.lang.String}s.
49       * <p>Also incidentally tests:</p>
50       * <ul><li>Having a null in the data with a null in the comparator</li></ul>
51       */
52      @Test
53      public void testLiteralComparatorString() {
54          String[] array = new String[]{"abc", null, "bcd", "cde", "def", "xyz", "wxy"};
55          Arrays.sort(array, new LiteralComparator<>(new String[]{null, "efg", "bcd", "cde", "abc", "def"}));
56          assertArrayEquals(new String[]{null, "bcd", "cde", "abc", "def", "wxy", "xyz"}, array);
57      }
58  }