Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.util

File StringUtilsTest.java

 

Code metrics

0
113
15
1
351
238
15
0.13
7.53
15
1

Classes

Class Line # Actions
StringUtilsTest 38 113 15
1.0100%
 

Contributing tests

This file is covered by 15 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 testSeparatorListToArray()
96    {
97  1 String[] result = StringUtils.separatorListToArray(
98    "foo=',',min='foo',max='1,2,3',fa=','", ",");
99  1 assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
100    Arrays.toString(result));
101    /*
102    * Comma nested in '' is not treated as delimiter; tokens are not trimmed
103    */
104  1 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
105  1 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
106   
107    /*
108    * String delimited by | containing a quoted | (should not be treated as
109    * delimiter)
110    */
111  1 assertEquals("[abc='|'d, ef, g]", Arrays.toString(
112    StringUtils.separatorListToArray("abc='|'d|ef|g", "|")));
113    }
114   
 
115  1 toggle @Test(groups = { "Functional" })
116    public void testArrayToSeparatorList()
117    {
118  1 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
119  1 assertEquals("*",
120    StringUtils.arrayToSeparatorList(new String[] {}, "*"));
121  1 assertEquals("a*bc*cde",
122    StringUtils.arrayToSeparatorList(new String[]
123    { "a", "bc", "cde" }, "*"));
124  1 assertEquals("a*cde",
125    StringUtils.arrayToSeparatorList(new String[]
126    { "a", null, "cde" }, "*"));
127  1 assertEquals("a**cde",
128    StringUtils.arrayToSeparatorList(new String[]
129    { "a", "", "cde" }, "*"));
130    // delimiter within token is not (yet) escaped
131  1 assertEquals("a*b*c*cde",
132    StringUtils.arrayToSeparatorList(new String[]
133    { "a", "b*c", "cde" }, "*"));
134    }
135   
 
136  1 toggle @Test(groups = { "Functional" })
137    public void testListToDelimitedString()
138    {
139  1 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
140  1 List<String> list = new ArrayList<>();
141  1 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
142  1 list.add("now");
143  1 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
144  1 list.add("is");
145  1 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
146  1 assertEquals("now ; is",
147    StringUtils.listToDelimitedString(list, " ; "));
148  1 list.add("the");
149  1 list.add("winter");
150  1 list.add("of");
151  1 list.add("our");
152  1 list.add("discontent");
153  1 assertEquals("now is the winter of our discontent",
154    StringUtils.listToDelimitedString(list, " "));
155    }
156   
 
157  1 toggle @Test(groups = { "Functional" })
158    public void testParseInt()
159    {
160  1 assertEquals(0, StringUtils.parseInt(null));
161  1 assertEquals(0, StringUtils.parseInt(""));
162  1 assertEquals(0, StringUtils.parseInt("x"));
163  1 assertEquals(0, StringUtils.parseInt("1.2"));
164  1 assertEquals(33, StringUtils.parseInt("33"));
165  1 assertEquals(33, StringUtils.parseInt("+33"));
166  1 assertEquals(-123, StringUtils.parseInt("-123"));
167    // too big for an int:
168  1 assertEquals(0,
169    StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
170    }
171   
 
172  1 toggle @Test(groups = { "Functional" })
173    public void testCompareVersions()
174    {
175  1 assertEquals(0, StringUtils.compareVersions(null, null));
176  1 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
177   
178    /*
179    * same version returns 0
180    */
181  1 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
182  1 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
183  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
184  1 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
185  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
186   
187    /*
188    * v1 < v2 returns -1
189    */
190  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
191  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
192  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
193  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
194  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
195  1 assertEquals(-1,
196    StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
197  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
198  1 assertEquals(-1, StringUtils.compareVersions("2", "12"));
199  1 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
200   
201    /*
202    * v1 > v2 returns +1
203    */
204  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
205  1 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
206  1 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
207  1 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
208  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
209  1 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
210  1 assertEquals(1, StringUtils.compareVersions("12", "2"));
211  1 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
212    }
213   
 
214  1 toggle @Test(groups = { "Functional" })
215    public void testToSentenceCase()
216    {
217  1 assertEquals("John", StringUtils.toSentenceCase("john"));
218  1 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
219  1 assertEquals("John and james",
220    StringUtils.toSentenceCase("JOHN and JAMES"));
221  1 assertEquals("J", StringUtils.toSentenceCase("j"));
222  1 assertEquals("", StringUtils.toSentenceCase(""));
223  1 assertNull(StringUtils.toSentenceCase(null));
224    }
225   
 
226  1 toggle @Test(groups = { "Functional" })
227    public void testStripHtmlTags()
228    {
229  1 assertNull(StringUtils.stripHtmlTags(null));
230  1 assertEquals("", StringUtils.stripHtmlTags(""));
231  1 assertEquals("<a href=\"something\">label</href>",
232    StringUtils.stripHtmlTags(
233    "<html><a href=\"something\">label</href></html>"));
234   
235    // if no "<html>" tag, < and > get html-encoded (not sure why)
236  1 assertEquals("&lt;a href=\"something\"&gt;label&lt;/href&gt;",
237    StringUtils
238    .stripHtmlTags("<a href=\"something\">label</href>"));
239   
240    // </body> gets removed but not <body> (is this intentional?)
241  1 assertEquals("<body><p>hello", StringUtils
242    .stripHtmlTags("<html><body><p>hello</body></html>"));
243   
244  1 assertEquals("kdHydro &lt; 12.53",
245    StringUtils.stripHtmlTags("kdHydro < 12.53"));
246    }
247   
 
248  1 toggle @Test(groups = { "Functional" })
249    public void testUrlEncode()
250    {
251    // degenerate cases
252  1 assertNull(StringUtils.urlEncode(null, ";,"));
253  1 assertEquals("", StringUtils.urlEncode("", ""));
254  1 assertEquals("", StringUtils.urlEncode("", ";,"));
255   
256    // sanity checks, see
257    // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
258  1 assertEquals("+", StringUtils.urlEncode(" ", " "));
259  1 assertEquals("%25", StringUtils.urlEncode("%", "%"));
260  1 assertEquals(".", StringUtils.urlEncode(".", ".")); // note . is not encoded
261  1 assertEquals("%3A", StringUtils.urlEncode(":", ":"));
262  1 assertEquals("%3B", StringUtils.urlEncode(";", ";"));
263  1 assertEquals("%3D", StringUtils.urlEncode("=", "="));
264  1 assertEquals("%2C", StringUtils.urlEncode(",", ","));
265   
266    // check % does not get recursively encoded!
267  1 assertEquals("a%25b%3Dc%3Bd%3Ae%2C%2C",
268    StringUtils.urlEncode("a%b=c;d:e,,", "=,;:%"));
269   
270    // = not in the list for encoding
271  1 assertEquals("a=b", StringUtils.urlEncode("a=b", ";,"));
272   
273    // encode = (as %3B) and ; (as %3D)
274  1 assertEquals("a%3Db.c%3B", StringUtils.urlEncode("a=b.c;", ";=,"));
275   
276    // . and space not in the list for encoding
277  1 assertEquals("a%3Db.c d", StringUtils.urlEncode("a=b.c d", ";=,"));
278   
279    // encode space also (as +)
280  1 assertEquals("a%3Db.c+d", StringUtils.urlEncode("a=b.c d", ";=, "));
281   
282    // . does not get encoded even if requested - behaviour of URLEncoder
283  1 assertEquals("a%3Db.c+d.e%3Df",
284    StringUtils.urlEncode("a=b.c d.e=f", ";=,. "));
285    }
286   
 
287  1 toggle @Test(groups = { "Functional" })
288    public void testUrlDecode()
289    {
290    // degenerate cases
291  1 assertNull(StringUtils.urlDecode(null, ";,"));
292  1 assertEquals("", StringUtils.urlDecode("", ""));
293  1 assertEquals("", StringUtils.urlDecode("", ";,"));
294   
295    // = not in the list for encoding
296  1 assertEquals("a%3Db", StringUtils.urlDecode("a%3Db", ";,"));
297   
298    // decode = and ; but not .
299  1 assertEquals("a=b%3Ec; d",
300    StringUtils.urlDecode("a%3Db%3Ec; d", ";=,"));
301   
302    // space not in the list for decoding
303  1 assertEquals("a=b;c+d", StringUtils.urlDecode("a%3Db%3Bc+d", ";=,"));
304   
305    // decode space also; %3E is not decoded to .
306  1 assertEquals("a=b%3Ec d=,",
307    StringUtils.urlDecode("a%3Db%3Ec+d%3D%2C", ";=, "));
308   
309    // decode encoded % (%25)
310  1 assertEquals("a,=;\t%z",
311    StringUtils.urlDecode("a%2C%3D%3B%09%25z", ";=,\t%"));
312    }
313   
 
314  5 toggle @Test(groups = { "Functional" }, dataProvider = "digestExamples")
315    public void testGetHashedBytes(String string, String digest)
316    {
317  5 byte[] digestBytes = StringUtils.getHashedBytes(string);
318    // assertEquals("Digest not as expected", bytesToHex(digestBytes),
319    // assertEquals("Digest not as expected", encodeHexString(digestBytes),
320  5 assertEquals("Digest not as expected", bytesToHex(digestBytes), digest);
321    }
322   
 
323  1 toggle @DataProvider(name = "digestExamples")
324    public Object[][] digestExamples()
325    {
326  1 return new Object[][] {
327    //
328    /*
329    */
330    { "hello world",
331    "66c6b89905aa05e7c85fa40defea77c9bd3dac9c787f7188024cd9197b9b8221" },
332    { "HELLO WORLD",
333    "9af79914944f6185f9b5953949a530242c806b34534261890817a92b8c2e1f79" },
334    { "hello",
335    "5f14bb644ea7ddbc036f230dafa8b98d2e2c6c86659b27020a878e8e13ae9eb3" },
336    { "world",
337    "3ca0153aab68e7cf6b8a59aa5fc7b70eba53ebec0c30c2c0946097ba69fc7d4b" },
338    { "HELLO world",
339    "4efdfeaec7bfc381a04a5a1f738a2110f6876eecca79fca034e94fe0479b83f1" },
340    /*
341    */
342    //
343    };
344    }
345   
 
346  5 toggle public static String bytesToHex(byte[] bytes)
347    {
348  5 return new BigInteger(1, bytes).toString(16);
349    }
350   
351    }