Clover icon

Coverage Report

  1. Project Clover database Wed Dec 3 2025 16:47:11 GMT
  2. Package jalview.util

File StringUtilsTest.java

 

Code metrics

0
119
16
1
364
249
16
0.13
7.44
16
1

Classes

Class Line # Actions
StringUtilsTest 38 119 16
1.0100%
 

Contributing tests

This file is covered by 16 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.util;
22   
23    import static org.testng.AssertJUnit.assertEquals;
24    import static org.testng.AssertJUnit.assertNull;
25    import static org.testng.AssertJUnit.assertTrue;
26   
27    import java.math.BigInteger;
28    import java.util.ArrayList;
29    import java.util.Arrays;
30    import java.util.List;
31   
32    import org.testng.annotations.BeforeClass;
33    import org.testng.annotations.DataProvider;
34    import org.testng.annotations.Test;
35   
36    import jalview.gui.JvOptionPane;
37   
 
38    public class StringUtilsTest
39    {
40   
 
41  1 toggle @BeforeClass(alwaysRun = true)
42    public void setUpJvOptionPane()
43    {
44  1 JvOptionPane.setInteractiveMode(false);
45  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46    }
47   
 
48  1 toggle @Test(groups = { "Functional" })
49    public void testInsertCharAt()
50    {
51  1 char[] c1 = "ABC".toCharArray();
52  1 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
53  1 assertTrue(Arrays.equals(expected,
54    StringUtils.insertCharAt(c1, 3, 2, 'w')));
55  1 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
56  1 assertTrue(Arrays.equals(expected,
57    StringUtils.insertCharAt(c1, 4, 2, 'w')));
58  1 assertTrue(Arrays.equals(expected,
59    StringUtils.insertCharAt(c1, 5, 2, 'w')));
60  1 assertTrue(Arrays.equals(expected,
61    StringUtils.insertCharAt(c1, 6, 2, 'w')));
62  1 assertTrue(Arrays.equals(expected,
63    StringUtils.insertCharAt(c1, 7, 2, 'w')));
64    }
65   
 
66  1 toggle @Test(groups = { "Functional" })
67    public void testDeleteChars()
68    {
69  1 char[] c1 = "ABC".toCharArray();
70   
71    // delete second position
72  1 assertTrue(
73    Arrays.equals(new char[]
74    { 'A', 'C' }, StringUtils.deleteChars(c1, 1, 2)));
75   
76    // delete positions 1 and 2
77  1 assertTrue(
78    Arrays.equals(new char[]
79    { 'C' }, StringUtils.deleteChars(c1, 0, 2)));
80   
81    // delete positions 1-3
82  1 assertTrue(Arrays.equals(new char[] {},
83    StringUtils.deleteChars(c1, 0, 3)));
84   
85    // delete position 3
86  1 assertTrue(
87    Arrays.equals(new char[]
88    { 'A', 'B' }, StringUtils.deleteChars(c1, 2, 3)));
89   
90    // out of range deletion is ignore
91  1 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
92    }
93   
 
94  1 toggle @Test(groups = { "Functional" })
95    public void testGetLastToken()
96    {
97  1 assertNull(StringUtils.getLastToken(null, null));
98  1 assertNull(StringUtils.getLastToken(null, "/"));
99  1 assertEquals("a", StringUtils.getLastToken("a", null));
100   
101  1 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
102  1 assertEquals("c", StringUtils.getLastToken("abc", "b"));
103  1 assertEquals("file1.dat", StringUtils.getLastToken(
104    "file://localhost:8080/data/examples/file1.dat", "/"));
105    }
106   
 
107  1 toggle @Test(groups = { "Functional" })
108    public void testSeparatorListToArray()
109    {
110  1 String[] result = StringUtils.separatorListToArray(
111    "foo=',',min='foo',max='1,2,3',fa=','", ",");
112  1 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
113    Arrays.toString(result));
114    /*
115    * Comma nested in '' is not treated as delimiter; tokens are not trimmed
116    */
117  1 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
118  1 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
119   
120    /*
121    * String delimited by | containing a quoted | (should not be treated as
122    * delimiter)
123    */
124  1 assertEquals("[abc='|'d, ef, g]", Arrays.toString(
125    StringUtils.separatorListToArray("abc='|'d|ef|g", "|")));
126    }
127   
 
128  1 toggle @Test(groups = { "Functional" })
129    public void testArrayToSeparatorList()
130    {
131  1 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
132  1 assertEquals("*",
133    StringUtils.arrayToSeparatorList(new String[] {}, "*"));
134  1 assertEquals("a*bc*cde",
135    StringUtils.arrayToSeparatorList(new String[]
136    { "a", "bc", "cde" }, "*"));
137  1 assertEquals("a*cde",
138    StringUtils.arrayToSeparatorList(new String[]
139    { "a", null, "cde" }, "*"));
140  1 assertEquals("a**cde",
141    StringUtils.arrayToSeparatorList(new String[]
142    { "a", "", "cde" }, "*"));
143    // delimiter within token is not (yet) escaped
144  1 assertEquals("a*b*c*cde",
145    StringUtils.arrayToSeparatorList(new String[]
146    { "a", "b*c", "cde" }, "*"));
147    }
148   
 
149  1 toggle @Test(groups = { "Functional" })
150    public void testListToDelimitedString()
151    {
152  1 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
153  1 List<String> list = new ArrayList<>();
154  1 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
155  1 list.add("now");
156  1 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
157  1 list.add("is");
158  1 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
159  1 assertEquals("now ; is",
160    StringUtils.listToDelimitedString(list, " ; "));
161  1 list.add("the");
162  1 list.add("winter");
163  1 list.add("of");
164  1 list.add("our");
165  1 list.add("discontent");
166  1 assertEquals("now is the winter of our discontent",
167    StringUtils.listToDelimitedString(list, " "));
168    }
169   
 
170  1 toggle @Test(groups = { "Functional" })
171    public void testParseInt()
172    {
173  1 assertEquals(0, StringUtils.parseInt(null));
174  1 assertEquals(0, StringUtils.parseInt(""));
175  1 assertEquals(0, StringUtils.parseInt("x"));
176  1 assertEquals(0, StringUtils.parseInt("1.2"));
177  1 assertEquals(33, StringUtils.parseInt("33"));
178  1 assertEquals(33, StringUtils.parseInt("+33"));
179  1 assertEquals(-123, StringUtils.parseInt("-123"));
180    // too big for an int:
181  1 assertEquals(0,
182    StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
183    }
184   
 
185  1 toggle @Test(groups = { "Functional" })
186    public void testCompareVersions()
187    {
188  1 assertEquals(0, StringUtils.compareVersions(null, null));
189  1 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
190   
191    /*
192    * same version returns 0
193    */
194  1 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
195  1 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
196  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
197  1 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
198  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
199   
200    /*
201    * v1 < v2 returns -1
202    */
203  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
204  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
205  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
206  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
207  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
208  1 assertEquals(-1,
209    StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
210  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
211  1 assertEquals(-1, StringUtils.compareVersions("2", "12"));
212  1 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
213   
214    /*
215    * v1 > v2 returns +1
216    */
217  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
218  1 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
219  1 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
220  1 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
221  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
222  1 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
223  1 assertEquals(1, StringUtils.compareVersions("12", "2"));
224  1 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
225    }
226   
 
227  1 toggle @Test(groups = { "Functional" })
228    public void testToSentenceCase()
229    {
230  1 assertEquals("John", StringUtils.toSentenceCase("john"));
231  1 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
232  1 assertEquals("John and james",
233    StringUtils.toSentenceCase("JOHN and JAMES"));
234  1 assertEquals("J", StringUtils.toSentenceCase("j"));
235  1 assertEquals("", StringUtils.toSentenceCase(""));
236  1 assertNull(StringUtils.toSentenceCase(null));
237    }
238   
 
239  1 toggle @Test(groups = { "Functional" })
240    public void testStripHtmlTags()
241    {
242  1 assertNull(StringUtils.stripHtmlTags(null));
243  1 assertEquals("", StringUtils.stripHtmlTags(""));
244  1 assertEquals("<a href=\"something\">label</href>",
245    StringUtils.stripHtmlTags(
246    "<html><a href=\"something\">label</href></html>"));
247   
248    // if no "<html>" tag, < and > get html-encoded (not sure why)
249  1 assertEquals("&lt;a href=\"something\"&gt;label&lt;/href&gt;",
250    StringUtils
251    .stripHtmlTags("<a href=\"something\">label</href>"));
252   
253    // </body> gets removed but not <body> (is this intentional?)
254  1 assertEquals("<body><p>hello", StringUtils
255    .stripHtmlTags("<html><body><p>hello</body></html>"));
256   
257  1 assertEquals("kdHydro &lt; 12.53",
258    StringUtils.stripHtmlTags("kdHydro < 12.53"));
259    }
260   
 
261  1 toggle @Test(groups = { "Functional" })
262    public void testUrlEncode()
263    {
264    // degenerate cases
265  1 assertNull(StringUtils.urlEncode(null, ";,"));
266  1 assertEquals("", StringUtils.urlEncode("", ""));
267  1 assertEquals("", StringUtils.urlEncode("", ";,"));
268   
269    // sanity checks, see
270    // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
271  1 assertEquals("+", StringUtils.urlEncode(" ", " "));
272  1 assertEquals("%25", StringUtils.urlEncode("%", "%"));
273  1 assertEquals(".", StringUtils.urlEncode(".", ".")); // note . is not encoded
274  1 assertEquals("%3A", StringUtils.urlEncode(":", ":"));
275  1 assertEquals("%3B", StringUtils.urlEncode(";", ";"));
276  1 assertEquals("%3D", StringUtils.urlEncode("=", "="));
277  1 assertEquals("%2C", StringUtils.urlEncode(",", ","));
278   
279    // check % does not get recursively encoded!
280  1 assertEquals("a%25b%3Dc%3Bd%3Ae%2C%2C",
281    StringUtils.urlEncode("a%b=c;d:e,,", "=,;:%"));
282   
283    // = not in the list for encoding
284  1 assertEquals("a=b", StringUtils.urlEncode("a=b", ";,"));
285   
286    // encode = (as %3B) and ; (as %3D)
287  1 assertEquals("a%3Db.c%3B", StringUtils.urlEncode("a=b.c;", ";=,"));
288   
289    // . and space not in the list for encoding
290  1 assertEquals("a%3Db.c d", StringUtils.urlEncode("a=b.c d", ";=,"));
291   
292    // encode space also (as +)
293  1 assertEquals("a%3Db.c+d", StringUtils.urlEncode("a=b.c d", ";=, "));
294   
295    // . does not get encoded even if requested - behaviour of URLEncoder
296  1 assertEquals("a%3Db.c+d.e%3Df",
297    StringUtils.urlEncode("a=b.c d.e=f", ";=,. "));
298    }
299   
 
300  1 toggle @Test(groups = { "Functional" })
301    public void testUrlDecode()
302    {
303    // degenerate cases
304  1 assertNull(StringUtils.urlDecode(null, ";,"));
305  1 assertEquals("", StringUtils.urlDecode("", ""));
306  1 assertEquals("", StringUtils.urlDecode("", ";,"));
307   
308    // = not in the list for encoding
309  1 assertEquals("a%3Db", StringUtils.urlDecode("a%3Db", ";,"));
310   
311    // decode = and ; but not .
312  1 assertEquals("a=b%3Ec; d",
313    StringUtils.urlDecode("a%3Db%3Ec; d", ";=,"));
314   
315    // space not in the list for decoding
316  1 assertEquals("a=b;c+d", StringUtils.urlDecode("a%3Db%3Bc+d", ";=,"));
317   
318    // decode space also; %3E is not decoded to .
319  1 assertEquals("a=b%3Ec d=,",
320    StringUtils.urlDecode("a%3Db%3Ec+d%3D%2C", ";=, "));
321   
322    // decode encoded % (%25)
323  1 assertEquals("a,=;\t%z",
324    StringUtils.urlDecode("a%2C%3D%3B%09%25z", ";=,\t%"));
325    }
326   
 
327  5 toggle @Test(groups = { "Functional" }, dataProvider = "digestExamples")
328    public void testGetHashedBytes(String string, String digest)
329    {
330  5 byte[] digestBytes = StringUtils.getHashedBytes(string);
331    // assertEquals("Digest not as expected", bytesToHex(digestBytes),
332    // assertEquals("Digest not as expected", encodeHexString(digestBytes),
333  5 assertEquals("Digest not as expected", bytesToHex(digestBytes), digest);
334    }
335   
 
336  1 toggle @DataProvider(name = "digestExamples")
337    public Object[][] digestExamples()
338    {
339  1 return new Object[][] {
340    //
341    /*
342    */
343    { "hello world",
344    "66c6b89905aa05e7c85fa40defea77c9bd3dac9c787f7188024cd9197b9b8221" },
345    { "HELLO WORLD",
346    "9af79914944f6185f9b5953949a530242c806b34534261890817a92b8c2e1f79" },
347    { "hello",
348    "5f14bb644ea7ddbc036f230dafa8b98d2e2c6c86659b27020a878e8e13ae9eb3" },
349    { "world",
350    "3ca0153aab68e7cf6b8a59aa5fc7b70eba53ebec0c30c2c0946097ba69fc7d4b" },
351    { "HELLO world",
352    "4efdfeaec7bfc381a04a5a1f738a2110f6876eecca79fca034e94fe0479b83f1" },
353    /*
354    */
355    //
356    };
357    }
358   
 
359  5 toggle public static String bytesToHex(byte[] bytes)
360    {
361  5 return new BigInteger(1, bytes).toString(16);
362    }
363   
364    }