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.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertTrue;
27 import static org.testng.AssertJUnit.fail;
29 import jalview.gui.JvOptionPane;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
38 public class StringUtilsTest
41 @BeforeClass(alwaysRun = true)
42 public void setUpJvOptionPane()
44 JvOptionPane.setInteractiveMode(false);
45 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
48 @Test(groups = { "Functional" })
49 public void testInsertCharAt()
51 char[] c1 = "ABC".toCharArray();
52 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
53 assertTrue(Arrays.equals(expected,
54 StringUtils.insertCharAt(c1, 3, 2, 'w')));
55 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
56 assertTrue(Arrays.equals(expected,
57 StringUtils.insertCharAt(c1, 4, 2, 'w')));
58 assertTrue(Arrays.equals(expected,
59 StringUtils.insertCharAt(c1, 5, 2, 'w')));
60 assertTrue(Arrays.equals(expected,
61 StringUtils.insertCharAt(c1, 6, 2, 'w')));
62 assertTrue(Arrays.equals(expected,
63 StringUtils.insertCharAt(c1, 7, 2, 'w')));
66 @Test(groups = { "Functional" })
67 public void testDeleteChars()
69 char[] c1 = "ABC".toCharArray();
71 // delete second position
72 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
73 StringUtils.deleteChars(c1, 1, 2)));
75 // delete positions 1 and 2
76 assertTrue(Arrays.equals(new char[] { 'C' },
77 StringUtils.deleteChars(c1, 0, 2)));
79 // delete positions 1-3
80 assertTrue(Arrays.equals(new char[] {},
81 StringUtils.deleteChars(c1, 0, 3)));
84 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
85 StringUtils.deleteChars(c1, 2, 3)));
87 // out of range deletion is ignore
88 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
91 @Test(groups = { "Functional" })
92 public void testGetLastToken()
94 assertNull(StringUtils.getLastToken(null, null));
95 assertNull(StringUtils.getLastToken(null, "/"));
96 assertEquals("a", StringUtils.getLastToken("a", null));
98 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
99 assertEquals("c", StringUtils.getLastToken("abc", "b"));
100 assertEquals("file1.dat", StringUtils.getLastToken(
101 "file://localhost:8080/data/examples/file1.dat", "/"));
104 @Test(groups = { "Functional" })
105 public void testSeparatorListToArray()
107 String[] result = StringUtils.separatorListToArray(
108 "foo=',',min='foo',max='1,2,3',fa=','", ",");
109 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
110 Arrays.toString(result));
112 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
114 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
115 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
118 * String delimited by | containing a quoted | (should not be treated as
121 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
122 .separatorListToArray("abc='|'d|ef|g", "|")));
125 @Test(groups = { "Functional" })
126 public void testArrayToSeparatorList()
128 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
130 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
133 StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
137 StringUtils.arrayToSeparatorList(new String[] { "a", null,
139 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
140 "a", "", "cde" }, "*"));
141 // delimiter within token is not (yet) escaped
142 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
143 { "a", "b*c", "cde" }, "*"));
146 @Test(groups = { "Functional" })
147 public void testListToDelimitedString()
149 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
150 List<String> list = new ArrayList<>();
151 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
153 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
155 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
156 assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
161 list.add("discontent");
162 assertEquals("now is the winter of our discontent",
163 StringUtils.listToDelimitedString(list, " "));
166 @Test(groups = { "Functional" })
167 public void testParseInt()
169 assertEquals(0, StringUtils.parseInt(null));
170 assertEquals(0, StringUtils.parseInt(""));
171 assertEquals(0, StringUtils.parseInt("x"));
172 assertEquals(0, StringUtils.parseInt("1.2"));
173 assertEquals(33, StringUtils.parseInt("33"));
174 assertEquals(33, StringUtils.parseInt("+33"));
175 assertEquals(-123, StringUtils.parseInt("-123"));
176 // too big for an int:
178 StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
181 @Test(groups = { "Functional" })
182 public void testCompareVersions()
184 assertEquals(0, StringUtils.compareVersions(null, null));
185 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
188 * same version returns 0
190 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
191 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
192 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
193 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
194 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
199 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
200 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
201 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
202 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
203 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
204 assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
205 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
206 assertEquals(-1, StringUtils.compareVersions("2", "12"));
207 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
212 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
213 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
214 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
215 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
216 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
217 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
218 assertEquals(1, StringUtils.compareVersions("12", "2"));
219 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
222 @Test(groups = { "Functional" })
223 public void testToSentenceCase()
225 assertEquals("John", StringUtils.toSentenceCase("john"));
226 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
227 assertEquals("John and james",
228 StringUtils.toSentenceCase("JOHN and JAMES"));
229 assertEquals("J", StringUtils.toSentenceCase("j"));
230 assertEquals("", StringUtils.toSentenceCase(""));
231 assertNull(StringUtils.toSentenceCase(null));
234 @Test(groups = { "Functional" })
235 public void testStripHtmlTags()
237 assertNull(StringUtils.stripHtmlTags(null));
238 assertEquals("", StringUtils.stripHtmlTags(""));
240 "<a href=\"something\">label</href>",
242 .stripHtmlTags("<html><a href=\"something\">label</href></html>"));
244 // if no "<html>" tag, < and > get html-encoded (not sure why)
245 assertEquals("<a href=\"something\">label</href>",
246 StringUtils.stripHtmlTags("<a href=\"something\">label</href>"));
248 // </body> gets removed but not <body> (is this intentional?)
249 assertEquals("<body><p>hello",
250 StringUtils.stripHtmlTags("<html><body><p>hello</body></html>"));
252 assertEquals("kdHydro < 12.53",
253 StringUtils.stripHtmlTags("kdHydro < 12.53"));
256 @Test(groups = { "Functional" })
257 public void testIsHexString()
259 assertFalse(StringUtils.isHexString(""));
260 assertTrue(StringUtils.isHexString("0123456789abcdefABCDEF"));
261 assertFalse(StringUtils.isHexString("g"));
264 StringUtils.isHexString(null);
265 fail("expected exception");
266 } catch (NullPointerException e)