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.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
32 import org.testng.annotations.Test;
34 public class StringUtilsTest
37 @Test(groups = { "Functional" })
38 public void testInsertCharAt()
40 char[] c1 = "ABC".toCharArray();
41 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
42 assertTrue(Arrays.equals(expected,
43 StringUtils.insertCharAt(c1, 3, 2, 'w')));
44 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
45 assertTrue(Arrays.equals(expected,
46 StringUtils.insertCharAt(c1, 4, 2, 'w')));
47 assertTrue(Arrays.equals(expected,
48 StringUtils.insertCharAt(c1, 5, 2, 'w')));
49 assertTrue(Arrays.equals(expected,
50 StringUtils.insertCharAt(c1, 6, 2, 'w')));
51 assertTrue(Arrays.equals(expected,
52 StringUtils.insertCharAt(c1, 7, 2, 'w')));
55 @Test(groups = { "Functional" })
56 public void testDeleteChars()
58 char[] c1 = "ABC".toCharArray();
60 // delete second position
61 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
62 StringUtils.deleteChars(c1, 1, 2)));
64 // delete positions 1 and 2
65 assertTrue(Arrays.equals(new char[] { 'C' },
66 StringUtils.deleteChars(c1, 0, 2)));
68 // delete positions 1-3
69 assertTrue(Arrays.equals(new char[] {},
70 StringUtils.deleteChars(c1, 0, 3)));
73 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
74 StringUtils.deleteChars(c1, 2, 3)));
76 // out of range deletion is ignore
77 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
80 @Test(groups = { "Functional" })
81 public void testGetLastToken()
83 assertNull(StringUtils.getLastToken(null, null));
84 assertNull(StringUtils.getLastToken(null, "/"));
85 assertEquals("a", StringUtils.getLastToken("a", null));
87 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
88 assertEquals("c", StringUtils.getLastToken("abc", "b"));
89 assertEquals("file1.dat", StringUtils.getLastToken(
90 "file://localhost:8080/data/examples/file1.dat", "/"));
93 @Test(groups = { "Functional" })
94 public void testSeparatorListToArray()
96 String[] result = StringUtils.separatorListToArray(
97 "foo=',',min='foo',max='1,2,3',fa=','", ",");
98 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
99 Arrays.toString(result));
101 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
103 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
104 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
107 * String delimited by | containing a quoted | (should not be treated as
110 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
111 .separatorListToArray("abc='|'d|ef|g", "|")));
114 @Test(groups = { "Functional" })
115 public void testArrayToSeparatorList()
117 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
119 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
122 StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
126 StringUtils.arrayToSeparatorList(new String[] { "a", null,
128 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
129 "a", "", "cde" }, "*"));
130 // delimiter within token is not (yet) escaped
131 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
132 { "a", "b*c", "cde" }, "*"));
136 * Test the method that parses lines like <br>
137 * ID=2345;Name=Something;
139 @Test(groups = { "Functional" })
140 public void testParseNameValuePairs()
142 char[] separators = new char[] { ' ' };
143 assertTrue(StringUtils.parseNameValuePairs(null, ";", separators)
145 assertTrue(StringUtils.parseNameValuePairs("", ";", separators)
147 assertTrue(StringUtils.parseNameValuePairs("hello=world", ";",
148 separators).isEmpty());
150 Map<String, List<String>> map = StringUtils.parseNameValuePairs(
151 "hello world", ";", separators);
152 assertEquals(1, map.size());
153 assertEquals(1, map.get("hello").size());
154 assertEquals("world", map.get("hello").get(0));
156 separators = new char[] { ' ', '=' };
158 .parseNameValuePairs(
159 "Method= manual curation ;nothing; Notes F2=S ; Notes=Metal; Type=",
162 // Type is ignored as no value was supplied
163 assertEquals(2, map.size());
165 // equals separator used ahead of space separator:
166 assertEquals(1, map.get("Method").size());
167 assertEquals("manual curation", map.get("Method").get(0)); // trimmed
169 assertEquals(2, map.get("Notes").size());
170 // space separator used ahead of equals separator
171 assertEquals("F2=S", map.get("Notes").get(0));
172 assertEquals("Metal", map.get("Notes").get(1));
175 @Test(groups = { "Functional" })
176 public void testListToDelimitedString()
178 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
179 List<String> list = new ArrayList<String>();
180 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
182 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
184 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
185 assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
190 list.add("discontent");
191 assertEquals("now is the winter of our discontent",
192 StringUtils.listToDelimitedString(list, " "));
195 @Test(groups = { "Functional" })
196 public void testParseInt()
198 assertEquals(0, StringUtils.parseInt(null));
199 assertEquals(0, StringUtils.parseInt(""));
200 assertEquals(0, StringUtils.parseInt("x"));
201 assertEquals(0, StringUtils.parseInt("1.2"));
202 assertEquals(33, StringUtils.parseInt("33"));
203 assertEquals(33, StringUtils.parseInt("+33"));
204 assertEquals(-123, StringUtils.parseInt("-123"));
205 // too big for an int:
207 StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));