001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.utils.text; 015 016import java.util.ArrayList; 017import java.util.Comparator; 018import java.util.List; 019 020import org.junit.jupiter.api.Test; 021 022import static org.junit.jupiter.api.Assertions.assertEquals; 023 024public class LineComparatorTest { 025 026 @Test 027 public void testLineComparatorCSV() { 028 LineComparator comp = new LineComparator(3, ",", '"'); 029 List<String> lines = new ArrayList<>(); 030 String l1 = "121,432423,9099053,\"Frieda karla L.,Ahrens\""; 031 String l2 = "adshjhg,fsdfsd,fdsfdsfsd,Forunkel forunculus,Janssen"; 032 String l3 = ",,,,zzz"; 033 String l4 = "321,2453,432423,berndef,ahrene32"; 034 lines.add(l1); 035 lines.add(l2); 036 lines.add(l3); 037 lines.add(l4); 038 039 lines.sort(comp); 040 041 assertEquals(l2, lines.get(0)); 042 assertEquals(l1, lines.get(1)); 043 assertEquals(l4, lines.get(2)); 044 assertEquals(l3, lines.get(3)); 045 } 046 047 @Test 048 public void testLineComparatorPipe() { 049 LineComparator comp = new LineComparator(3, "|"); 050 List<String> lines = new ArrayList<>(); 051 String l1 = "121|432423|9099053|Frieda karla L.|Ahrens"; 052 String l2 = "adshjhg|fsdfsd|fdsfdsfsd|Forunkel forunculus|Janssen"; 053 String l3 = "||||zzz"; 054 String l4 = "321|2453|432423|berndef|ahrene32"; 055 lines.add(l1); 056 lines.add(l2); 057 lines.add(l3); 058 lines.add(l4); 059 060 lines.sort(comp); 061 062 assertEquals(l2, lines.get(0)); 063 assertEquals(l1, lines.get(1)); 064 assertEquals(l4, lines.get(2)); 065 assertEquals(l3, lines.get(3)); 066 } 067 068 @Test 069 public void testLineComparatorTab() { 070 LineComparator comp = new LineComparator(3, "\t"); 071 List<String> lines = new ArrayList<>(); 072 String l1 = "121\t432423\t9099053\tFrieda karla L.\tAhrens"; 073 String l2 = "adshjhg\tfsdfsd\tfdsfdsfsd\tForunkel forunculus\tJanssen"; 074 String l3 = "\t\t\t\tzzz"; 075 String l4 = "321\t2453\t432423\tberndef\tahrene32"; 076 lines.add(l1); 077 lines.add(l2); 078 lines.add(l3); 079 lines.add(l4); 080 081 lines.sort(comp); 082 083 assertEquals(l2, lines.get(0)); 084 assertEquals(l1, lines.get(1)); 085 assertEquals(l4, lines.get(2)); 086 assertEquals(l3, lines.get(3)); 087 } 088 089 // Direct copy from IPT codebase 090 // see 091 // https://code.google.com/p/gbif-providertoolkit/source/browse/trunk/gbif-ipt/src/main/java/org/gbif/ipt/task/GenerateDwca.java#93 092 private static final Comparator<String> IGNORE_CASE_COMPARATOR = 093 Comparator.nullsFirst(String::compareToIgnoreCase); 094 095 /** 096 * Test for respecting equals and compareTo to ensure comparator respects java contracts. 097 * http://dev.gbif.org/issues/browse/POR-2730 098 */ 099 @Test 100 public void testEqualsCompareToContract() { 101 102 LineComparator comp = new LineComparator(0, ",", null, IGNORE_CASE_COMPARATOR); 103 104 String s1 = ",2,3"; // null in first instance 105 String s2 = ",2,3"; // null in first instance 106 107 // http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html#compare(T,%20T) 108 int sign1 = (int) Math.signum(comp.compare(s1, s2)); 109 int sign2 = (int) Math.signum(comp.compare(s2, s1)); 110 111 assertEquals(sign1 * -1, sign2); 112 } 113}