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.function.Function;
017
018import org.junit.jupiter.api.Test;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNull;
022
023public class StringUtilsTest {
024
025  @Test
026  public void testIncrease() {
027    assertEquals("Carlb", StringUtils.increase("Carla"));
028    assertEquals("Homa", StringUtils.increase("Holz"));
029    assertEquals("Aua", StringUtils.increase("Atz"));
030    assertEquals("b", StringUtils.increase("a"));
031    assertEquals("aa", StringUtils.increase("z"));
032    assertEquals("AAA", StringUtils.increase("ZZ"));
033    assertEquals("Aaa", StringUtils.increase("Zz"));
034    assertEquals("aaa", StringUtils.increase("zz"));
035    assertEquals("Abiet aaa", StringUtils.increase("Abies zzz"));
036    assertEquals("Alle31.3-a ", StringUtils.increase("Alld31.3-z "));
037    assertEquals("31.3-a a", StringUtils.increase("31.3-z "));
038    assertEquals("aAaa", StringUtils.increase("zZz"));
039    assertEquals("", StringUtils.increase(""));
040    assertNull(StringUtils.increase(null));
041  }
042
043  @Test
044  public void testRandomString() {
045    assertEquals(10, StringUtils.randomString(10).length());
046
047    // all upper case
048    String rnd = StringUtils.randomString(22);
049    assertEquals(rnd, rnd.toUpperCase());
050  }
051
052  @Test
053  public void testDecodeUtf8Garbage() {
054    assertUtf8(null, null);
055    assertUtf8("", "");
056    assertUtf8("a", "a");
057    assertUtf8("ä-üOØ", "ä-üOØ");
058    assertUtf8("(Günther, 1887)", "(Günther, 1887)");
059    assertUtf8("Böhlke, 1955", "Böhlke, 1955");
060    assertUtf8("Nielsen & Quéro, 1991\n", "Nielsen & Quéro, 1991\n");
061    assertUtf8("Rosinés", "Rosinés");
062    assertUtf8("S. Calderón & Standl.", "S. Calderón & Standl.");
063    assertUtf8("Strömman, 1896", "Strömman, 1896");
064    assertUtf8("Sérus.", "Sérus.");
065    assertUtf8("Thér.", "Thér.");
066    assertUtf8("Trécul", "Trécul");
067    assertUtf8("Hale & López-Fig.\n", "Hale & López-Fig.\n");
068  }
069
070  private void assertUtf8(String expected, String src) {
071    String decoded = StringUtils.decodeUtf8Garbage(src);
072    assertEquals(expected, decoded);
073    // make sure if we had gotten the correct string it would not be modified
074    assertEquals(expected, StringUtils.decodeUtf8Garbage(decoded));
075  }
076
077  @Test
078  public void testFoldToAscii() throws Exception {
079    assertNull(StringUtils.foldToAscii(null));
080    assertEquals("", StringUtils.foldToAscii(""));
081    assertEquals("Schulhof, Gymnasium Hurth", StringUtils.foldToAscii("Schulhof, Gymnasium Hürth"));
082    assertEquals("Doring", StringUtils.foldToAscii("Döring"));
083    assertEquals("Desireno", StringUtils.foldToAscii("Désírèñø"));
084    assertEquals("Debreczy & I. Racz", StringUtils.foldToAscii("Debreçzÿ & Ï. Rácz"));
085    assertEquals("Donatia novae-zelandiae", StringUtils.foldToAscii("Donatia novae-zelandiæ"));
086    assertEquals("Carex ×cayouettei", StringUtils.foldToAscii("Carex ×cayouettei"));
087    assertEquals(
088        "Carex comosa × Carex lupulina", StringUtils.foldToAscii("Carex comosa × Carex lupulina"));
089    assertEquals(
090        "Aeropyrum coil-shaped virus", StringUtils.foldToAscii("Aeropyrum coil-shaped virus"));
091    assertEquals("†Lachnus bonneti", StringUtils.foldToAscii("†Lachnus bonneti"));
092
093    assertEquals("lachs", StringUtils.foldToAscii("łachs"));
094
095    String test = "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ";
096    assertEquals(
097        "SOEZsoezY¥µAAAAAAAECEEEEIIIIDNOOOOOOUUUUYssaaaaaaaeceeeeiiiidnoooooouuuuyy",
098        StringUtils.foldToAscii(test));
099  }
100
101  @Test
102  public void testThenJoin() {
103    assertEquals(
104        "",
105        StringUtils.thenJoin(org.apache.commons.lang3.StringUtils::trimToNull, (String[]) null));
106    assertEquals(
107        "", StringUtils.thenJoin(org.apache.commons.lang3.StringUtils::trimToNull, (String) null));
108    assertEquals(
109        "", StringUtils.thenJoin(org.apache.commons.lang3.StringUtils::trimToNull, "", " "));
110    assertEquals(
111        "x", StringUtils.thenJoin(org.apache.commons.lang3.StringUtils::trimToNull, "", "  x "));
112    assertEquals(
113        "x y", StringUtils.thenJoin(org.apache.commons.lang3.StringUtils::trimToNull, "x", "  y "));
114    assertEquals("x   y ", StringUtils.thenJoin(Function.identity(), "x", "  y "));
115  }
116
117  @Test
118  public void testTrim() {
119    assertEquals("str", StringUtils.trim(" str "));
120    assertEquals("str StR", StringUtils.trim(" str StR "));
121    assertEquals("STR  str", StringUtils.trim(" \n\u0085STR  str \u00A0\n\t"));
122  }
123
124  @Test
125  public void testDeleteWhitespace() {
126    assertEquals("str", StringUtils.deleteWhitespace(" str "));
127    assertEquals("strStR", StringUtils.deleteWhitespace(" str StR "));
128    assertEquals("STRstr", StringUtils.deleteWhitespace(" \n\u000CSTR\u00A0   str \u2007\n\t"));
129  }
130}