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
71 Arrays.equals(new char[]
72 { 'A', 'C' }, StringUtils.deleteChars(c1, 1, 2)));
74 // delete positions 1 and 2
76 Arrays.equals(new char[]
77 { 'C' }, StringUtils.deleteChars(c1, 0, 2)));
79 // delete positions 1-3
80 assertTrue(Arrays.equals(new char[] {},
81 StringUtils.deleteChars(c1, 0, 3)));
85 Arrays.equals(new char[]
86 { 'A', 'B' }, StringUtils.deleteChars(c1, 2, 3)));
88 // out of range deletion is ignore
89 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
92 @Test(groups = { "Functional" })
93 public void testGetLastToken()
95 assertNull(StringUtils.getLastToken(null, null));
96 assertNull(StringUtils.getLastToken(null, "/"));
97 assertEquals("a", StringUtils.getLastToken("a", null));
99 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
100 assertEquals("c", StringUtils.getLastToken("abc", "b"));
101 assertEquals("file1.dat", StringUtils.getLastToken(
102 "file://localhost:8080/data/examples/file1.dat", "/"));
105 @Test(groups = { "Functional" })
106 public void testSeparatorListToArray()
108 String[] result = StringUtils.separatorListToArray(
109 "foo=',',min='foo',max='1,2,3',fa=','", ",");
110 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
111 Arrays.toString(result));
113 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
115 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
116 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
119 * String delimited by | containing a quoted | (should not be treated as
122 assertEquals("[abc='|'d, ef, g]", Arrays.toString(
123 StringUtils.separatorListToArray("abc='|'d|ef|g", "|")));
126 @Test(groups = { "Functional" })
127 public void testArrayToSeparatorList()
129 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
131 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
132 assertEquals("a*bc*cde",
133 StringUtils.arrayToSeparatorList(new String[]
134 { "a", "bc", "cde" }, "*"));
135 assertEquals("a*cde",
136 StringUtils.arrayToSeparatorList(new String[]
137 { "a", null, "cde" }, "*"));
138 assertEquals("a**cde",
139 StringUtils.arrayToSeparatorList(new String[]
140 { "a", "", "cde" }, "*"));
141 // delimiter within token is not (yet) escaped
142 assertEquals("a*b*c*cde",
143 StringUtils.arrayToSeparatorList(new String[]
144 { "a", "b*c", "cde" }, "*"));
147 @Test(groups = { "Functional" })
148 public void testListToDelimitedString()
150 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
151 List<String> list = new ArrayList<>();
152 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
154 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
156 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
157 assertEquals("now ; is",
158 StringUtils.listToDelimitedString(list, " ; "));
163 list.add("discontent");
164 assertEquals("now is the winter of our discontent",
165 StringUtils.listToDelimitedString(list, " "));
168 @Test(groups = { "Functional" })
169 public void testParseInt()
171 assertEquals(0, StringUtils.parseInt(null));
172 assertEquals(0, StringUtils.parseInt(""));
173 assertEquals(0, StringUtils.parseInt("x"));
174 assertEquals(0, StringUtils.parseInt("1.2"));
175 assertEquals(33, StringUtils.parseInt("33"));
176 assertEquals(33, StringUtils.parseInt("+33"));
177 assertEquals(-123, StringUtils.parseInt("-123"));
178 // too big for an int:
180 StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
183 @Test(groups = { "Functional" })
184 public void testCompareVersions()
186 assertEquals(0, StringUtils.compareVersions(null, null));
187 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
190 * same version returns 0
192 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
193 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
194 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
195 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
196 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
201 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
202 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
203 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
204 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
205 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
207 StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
208 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
209 assertEquals(-1, StringUtils.compareVersions("2", "12"));
210 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
215 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
216 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
217 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
218 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
219 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
220 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
221 assertEquals(1, StringUtils.compareVersions("12", "2"));
222 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
225 @Test(groups = { "Functional" })
226 public void testToSentenceCase()
228 assertEquals("John", StringUtils.toSentenceCase("john"));
229 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
230 assertEquals("John and james",
231 StringUtils.toSentenceCase("JOHN and JAMES"));
232 assertEquals("J", StringUtils.toSentenceCase("j"));
233 assertEquals("", StringUtils.toSentenceCase(""));
234 assertNull(StringUtils.toSentenceCase(null));
237 @Test(groups = { "Functional" })
238 public void testStripHtmlTags()
240 assertNull(StringUtils.stripHtmlTags(null));
241 assertEquals("", StringUtils.stripHtmlTags(""));
242 assertEquals("<a href=\"something\">label</href>",
243 StringUtils.stripHtmlTags(
244 "<html><a href=\"something\">label</href></html>"));
246 // if no "<html>" tag, < and > get html-encoded (not sure why)
247 assertEquals("<a href=\"something\">label</href>",
249 .stripHtmlTags("<a href=\"something\">label</href>"));
251 // </body> gets removed but not <body> (is this intentional?)
252 assertEquals("<body><p>hello", StringUtils
253 .stripHtmlTags("<html><body><p>hello</body></html>"));
255 assertEquals("kdHydro < 12.53",
256 StringUtils.stripHtmlTags("kdHydro < 12.53"));
259 @Test(groups = { "Functional" })
260 public void testUrlEncode()
263 assertNull(StringUtils.urlEncode(null, ";,"));
264 assertEquals("", StringUtils.urlEncode("", ""));
265 assertEquals("", StringUtils.urlEncode("", ";,"));
267 // sanity checks, see
268 // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
269 assertEquals("+", StringUtils.urlEncode(" ", " "));
270 assertEquals("%25", StringUtils.urlEncode("%", "%"));
271 assertEquals(".", StringUtils.urlEncode(".", ".")); // note . is not encoded
272 assertEquals("%3A", StringUtils.urlEncode(":", ":"));
273 assertEquals("%3B", StringUtils.urlEncode(";", ";"));
274 assertEquals("%3D", StringUtils.urlEncode("=", "="));
275 assertEquals("%2C", StringUtils.urlEncode(",", ","));
277 // check % does not get recursively encoded!
278 assertEquals("a%25b%3Dc%3Bd%3Ae%2C%2C",
279 StringUtils.urlEncode("a%b=c;d:e,,", "=,;:%"));
281 // = not in the list for encoding
282 assertEquals("a=b", StringUtils.urlEncode("a=b", ";,"));
284 // encode = (as %3B) and ; (as %3D)
285 assertEquals("a%3Db.c%3B", StringUtils.urlEncode("a=b.c;", ";=,"));
287 // . and space not in the list for encoding
288 assertEquals("a%3Db.c d", StringUtils.urlEncode("a=b.c d", ";=,"));
290 // encode space also (as +)
291 assertEquals("a%3Db.c+d", StringUtils.urlEncode("a=b.c d", ";=, "));
293 // . does not get encoded even if requested - behaviour of URLEncoder
294 assertEquals("a%3Db.c+d.e%3Df",
295 StringUtils.urlEncode("a=b.c d.e=f", ";=,. "));
298 @Test(groups = { "Functional" })
299 public void testUrlDecode()
302 assertNull(StringUtils.urlDecode(null, ";,"));
303 assertEquals("", StringUtils.urlDecode("", ""));
304 assertEquals("", StringUtils.urlDecode("", ";,"));
306 // = not in the list for encoding
307 assertEquals("a%3Db", StringUtils.urlDecode("a%3Db", ";,"));
309 // decode = and ; but not .
310 assertEquals("a=b%3Ec; d",
311 StringUtils.urlDecode("a%3Db%3Ec; d", ";=,"));
313 // space not in the list for decoding
314 assertEquals("a=b;c+d", StringUtils.urlDecode("a%3Db%3Bc+d", ";=,"));
316 // decode space also; %3E is not decoded to .
317 assertEquals("a=b%3Ec d=,",
318 StringUtils.urlDecode("a%3Db%3Ec+d%3D%2C", ";=, "));
320 // decode encoded % (%25)
321 assertEquals("a,=;\t%z",
322 StringUtils.urlDecode("a%2C%3D%3B%09%25z", ";=,\t%"));