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 jalview.gui.JvOptionPane;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
36 public class StringUtilsTest
39 @BeforeClass(alwaysRun = true)
40 public void setUpJvOptionPane()
42 JvOptionPane.setInteractiveMode(false);
43 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46 @Test(groups = { "Functional" })
47 public void testInsertCharAt()
49 char[] c1 = "ABC".toCharArray();
50 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
51 assertTrue(Arrays.equals(expected,
52 StringUtils.insertCharAt(c1, 3, 2, 'w')));
53 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
54 assertTrue(Arrays.equals(expected,
55 StringUtils.insertCharAt(c1, 4, 2, 'w')));
56 assertTrue(Arrays.equals(expected,
57 StringUtils.insertCharAt(c1, 5, 2, 'w')));
58 assertTrue(Arrays.equals(expected,
59 StringUtils.insertCharAt(c1, 6, 2, 'w')));
60 assertTrue(Arrays.equals(expected,
61 StringUtils.insertCharAt(c1, 7, 2, 'w')));
64 @Test(groups = { "Functional" })
65 public void testDeleteChars()
67 char[] c1 = "ABC".toCharArray();
69 // delete second position
70 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
71 StringUtils.deleteChars(c1, 1, 2)));
73 // delete positions 1 and 2
74 assertTrue(Arrays.equals(new char[] { 'C' },
75 StringUtils.deleteChars(c1, 0, 2)));
77 // delete positions 1-3
78 assertTrue(Arrays.equals(new char[] {},
79 StringUtils.deleteChars(c1, 0, 3)));
82 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
83 StringUtils.deleteChars(c1, 2, 3)));
85 // out of range deletion is ignore
86 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
89 @Test(groups = { "Functional" })
90 public void testSeparatorListToArray()
92 String[] result = StringUtils.separatorListToArray(
93 "foo=',',min='foo',max='1,2,3',fa=','", ",");
94 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
95 Arrays.toString(result));
97 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
99 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
100 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
103 * String delimited by | containing a quoted | (should not be treated as
106 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
107 .separatorListToArray("abc='|'d|ef|g", "|")));
110 @Test(groups = { "Functional" })
111 public void testArrayToSeparatorList()
113 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
115 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
118 StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
122 StringUtils.arrayToSeparatorList(new String[] { "a", null,
124 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
125 "a", "", "cde" }, "*"));
126 // delimiter within token is not (yet) escaped
127 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
128 { "a", "b*c", "cde" }, "*"));
131 @Test(groups = { "Functional" })
132 public void testListToDelimitedString()
134 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
135 List<String> list = new ArrayList<>();
136 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
138 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
140 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
141 assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
146 list.add("discontent");
147 assertEquals("now is the winter of our discontent",
148 StringUtils.listToDelimitedString(list, " "));
151 @Test(groups = { "Functional" })
152 public void testParseInt()
154 assertEquals(0, StringUtils.parseInt(null));
155 assertEquals(0, StringUtils.parseInt(""));
156 assertEquals(0, StringUtils.parseInt("x"));
157 assertEquals(0, StringUtils.parseInt("1.2"));
158 assertEquals(33, StringUtils.parseInt("33"));
159 assertEquals(33, StringUtils.parseInt("+33"));
160 assertEquals(-123, StringUtils.parseInt("-123"));
161 // too big for an int:
163 StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
166 @Test(groups = { "Functional" })
167 public void testCompareVersions()
169 assertEquals(0, StringUtils.compareVersions(null, null));
170 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
173 * same version returns 0
175 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
176 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
177 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
178 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
179 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
184 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
185 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
186 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
187 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
188 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
189 assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
190 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
191 assertEquals(-1, StringUtils.compareVersions("2", "12"));
192 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
197 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
198 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
199 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
200 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
201 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
202 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
203 assertEquals(1, StringUtils.compareVersions("12", "2"));
204 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
207 @Test(groups = { "Functional" })
208 public void testToSentenceCase()
210 assertEquals("John", StringUtils.toSentenceCase("john"));
211 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
212 assertEquals("John and james",
213 StringUtils.toSentenceCase("JOHN and JAMES"));
214 assertEquals("J", StringUtils.toSentenceCase("j"));
215 assertEquals("", StringUtils.toSentenceCase(""));
216 assertNull(StringUtils.toSentenceCase(null));
219 @Test(groups = { "Functional" })
220 public void testStripHtmlTags()
222 assertNull(StringUtils.stripHtmlTags(null));
223 assertEquals("", StringUtils.stripHtmlTags(""));
225 "<a href=\"something\">label</href>",
227 .stripHtmlTags("<html><a href=\"something\">label</href></html>"));
229 // if no "<html>" tag, < and > get html-encoded (not sure why)
230 assertEquals("<a href=\"something\">label</href>",
231 StringUtils.stripHtmlTags("<a href=\"something\">label</href>"));
233 // </body> gets removed but not <body> (is this intentional?)
234 assertEquals("<body><p>hello",
235 StringUtils.stripHtmlTags("<html><body><p>hello</body></html>"));
237 assertEquals("kdHydro < 12.53",
238 StringUtils.stripHtmlTags("kdHydro < 12.53"));