2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertTrue;
27 import java.util.Arrays;
29 import org.testng.annotations.Test;
31 public class StringUtilsTest
34 @Test(groups = { "Functional" })
35 public void testInsertCharAt()
37 char[] c1 = "ABC".toCharArray();
38 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
39 assertTrue(Arrays.equals(expected,
40 StringUtils.insertCharAt(c1, 3, 2, 'w')));
41 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
42 assertTrue(Arrays.equals(expected,
43 StringUtils.insertCharAt(c1, 4, 2, 'w')));
44 assertTrue(Arrays.equals(expected,
45 StringUtils.insertCharAt(c1, 5, 2, 'w')));
46 assertTrue(Arrays.equals(expected,
47 StringUtils.insertCharAt(c1, 6, 2, 'w')));
48 assertTrue(Arrays.equals(expected,
49 StringUtils.insertCharAt(c1, 7, 2, 'w')));
52 @Test(groups = { "Functional" })
53 public void testDeleteChars()
55 char[] c1 = "ABC".toCharArray();
57 // delete second position
58 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
59 StringUtils.deleteChars(c1, 1, 2)));
61 // delete positions 1 and 2
62 assertTrue(Arrays.equals(new char[] { 'C' },
63 StringUtils.deleteChars(c1, 0, 2)));
65 // delete positions 1-3
66 assertTrue(Arrays.equals(new char[] {},
67 StringUtils.deleteChars(c1, 0, 3)));
70 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
71 StringUtils.deleteChars(c1, 2, 3)));
73 // out of range deletion is ignore
74 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
77 @Test(groups = { "Functional" })
78 public void testGetLastToken()
80 assertNull(StringUtils.getLastToken(null, null));
81 assertNull(StringUtils.getLastToken(null, "/"));
82 assertEquals("a", StringUtils.getLastToken("a", null));
84 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
85 assertEquals("c", StringUtils.getLastToken("abc", "b"));
86 assertEquals("file1.dat", StringUtils.getLastToken(
87 "file://localhost:8080/data/examples/file1.dat", "/"));
90 @Test(groups = { "Functional" })
91 public void testSeparatorListToArray()
93 String[] result = StringUtils.separatorListToArray(
94 "foo=',',min='foo',max='1,2,3',fa=','", ",");
95 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
96 Arrays.toString(result));
98 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
100 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
101 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
104 * String delimited by | containing a quoted | (should not be treated as
107 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
108 .separatorListToArray("abc='|'d|ef|g", "|")));
111 @Test(groups = { "Functional" })
112 public void testArrayToSeparatorList()
114 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
116 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
119 StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
123 StringUtils.arrayToSeparatorList(new String[] { "a", null,
125 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
126 "a", "", "cde" }, "*"));
127 // delimiter within token is not (yet) escaped
128 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
129 { "a", "b*c", "cde" }, "*"));