Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / test / jalview / util / StringUtilsTest.java
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 jalview.gui.JvOptionPane;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
35
36 public class StringUtilsTest
37 {
38
39   @BeforeClass(alwaysRun = true)
40   public void setUpJvOptionPane()
41   {
42     JvOptionPane.setInteractiveMode(false);
43     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44   }
45
46   @Test(groups = { "Functional" })
47   public void testInsertCharAt()
48   {
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')));
62   }
63
64   @Test(groups = { "Functional" })
65   public void testDeleteChars()
66   {
67     char[] c1 = "ABC".toCharArray();
68
69     // delete second position
70     assertTrue(
71             Arrays.equals(new char[]
72             { 'A', 'C' }, StringUtils.deleteChars(c1, 1, 2)));
73
74     // delete positions 1 and 2
75     assertTrue(
76             Arrays.equals(new char[]
77             { 'C' }, StringUtils.deleteChars(c1, 0, 2)));
78
79     // delete positions 1-3
80     assertTrue(Arrays.equals(new char[] {},
81             StringUtils.deleteChars(c1, 0, 3)));
82
83     // delete position 3
84     assertTrue(
85             Arrays.equals(new char[]
86             { 'A', 'B' }, StringUtils.deleteChars(c1, 2, 3)));
87
88     // out of range deletion is ignore
89     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
90   }
91
92   @Test(groups = { "Functional" })
93   public void testSeparatorListToArray()
94   {
95     String[] result = StringUtils.separatorListToArray(
96             "foo=',',min='foo',max='1,2,3',fa=','", ",");
97     assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
98             Arrays.toString(result));
99     /*
100      * Comma nested in '' is not treated as delimiter; tokens are not trimmed
101      */
102     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
103     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
104
105     /*
106      * String delimited by | containing a quoted | (should not be treated as
107      * delimiter)
108      */
109     assertEquals("[abc='|'d, ef, g]", Arrays.toString(
110             StringUtils.separatorListToArray("abc='|'d|ef|g", "|")));
111   }
112
113   @Test(groups = { "Functional" })
114   public void testArrayToSeparatorList()
115   {
116     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
117     assertEquals("*",
118             StringUtils.arrayToSeparatorList(new String[] {}, "*"));
119     assertEquals("a*bc*cde",
120             StringUtils.arrayToSeparatorList(new String[]
121             { "a", "bc", "cde" }, "*"));
122     assertEquals("a*cde",
123             StringUtils.arrayToSeparatorList(new String[]
124             { "a", null, "cde" }, "*"));
125     assertEquals("a**cde",
126             StringUtils.arrayToSeparatorList(new String[]
127             { "a", "", "cde" }, "*"));
128     // delimiter within token is not (yet) escaped
129     assertEquals("a*b*c*cde",
130             StringUtils.arrayToSeparatorList(new String[]
131             { "a", "b*c", "cde" }, "*"));
132   }
133
134   @Test(groups = { "Functional" })
135   public void testListToDelimitedString()
136   {
137     assertEquals("", StringUtils.listToDelimitedString(null, ";"));
138     List<String> list = new ArrayList<>();
139     assertEquals("", StringUtils.listToDelimitedString(list, ";"));
140     list.add("now");
141     assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
142     list.add("is");
143     assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
144     assertEquals("now ; is",
145             StringUtils.listToDelimitedString(list, " ; "));
146     list.add("the");
147     list.add("winter");
148     list.add("of");
149     list.add("our");
150     list.add("discontent");
151     assertEquals("now is the winter of our discontent",
152             StringUtils.listToDelimitedString(list, " "));
153   }
154
155   @Test(groups = { "Functional" })
156   public void testParseInt()
157   {
158     assertEquals(0, StringUtils.parseInt(null));
159     assertEquals(0, StringUtils.parseInt(""));
160     assertEquals(0, StringUtils.parseInt("x"));
161     assertEquals(0, StringUtils.parseInt("1.2"));
162     assertEquals(33, StringUtils.parseInt("33"));
163     assertEquals(33, StringUtils.parseInt("+33"));
164     assertEquals(-123, StringUtils.parseInt("-123"));
165     // too big for an int:
166     assertEquals(0,
167             StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
168   }
169
170   @Test(groups = { "Functional" })
171   public void testCompareVersions()
172   {
173     assertEquals(0, StringUtils.compareVersions(null, null));
174     assertEquals(0, StringUtils.compareVersions("2.8.3", null));
175
176     /*
177      * same version returns 0
178      */
179     assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
180     assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
181     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
182     assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
183     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
184
185     /*
186      * v1 < v2 returns -1
187      */
188     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
189     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
190     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
191     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
192     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
193     assertEquals(-1,
194             StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
195     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
196     assertEquals(-1, StringUtils.compareVersions("2", "12"));
197     assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
198
199     /*
200      * v1 > v2 returns +1
201      */
202     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
203     assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
204     assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
205     assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
206     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
207     assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
208     assertEquals(1, StringUtils.compareVersions("12", "2"));
209     assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
210   }
211
212   @Test(groups = { "Functional" })
213   public void testToSentenceCase()
214   {
215     assertEquals("John", StringUtils.toSentenceCase("john"));
216     assertEquals("John", StringUtils.toSentenceCase("JOHN"));
217     assertEquals("John and james",
218             StringUtils.toSentenceCase("JOHN and JAMES"));
219     assertEquals("J", StringUtils.toSentenceCase("j"));
220     assertEquals("", StringUtils.toSentenceCase(""));
221     assertNull(StringUtils.toSentenceCase(null));
222   }
223
224   @Test(groups = { "Functional" })
225   public void testStripHtmlTags()
226   {
227     assertNull(StringUtils.stripHtmlTags(null));
228     assertEquals("", StringUtils.stripHtmlTags(""));
229     assertEquals("<a href=\"something\">label</href>",
230             StringUtils.stripHtmlTags(
231                     "<html><a href=\"something\">label</href></html>"));
232
233     // if no "<html>" tag, < and > get html-encoded (not sure why)
234     assertEquals("&lt;a href=\"something\"&gt;label&lt;/href&gt;",
235             StringUtils
236                     .stripHtmlTags("<a href=\"something\">label</href>"));
237
238     // </body> gets removed but not <body> (is this intentional?)
239     assertEquals("<body><p>hello", StringUtils
240             .stripHtmlTags("<html><body><p>hello</body></html>"));
241
242     assertEquals("kdHydro &lt; 12.53",
243             StringUtils.stripHtmlTags("kdHydro < 12.53"));
244   }
245
246   @Test(groups = { "Functional" })
247   public void testUrlEncode()
248   {
249     // degenerate cases
250     assertNull(StringUtils.urlEncode(null, ";,"));
251     assertEquals("", StringUtils.urlEncode("", ""));
252     assertEquals("", StringUtils.urlEncode("", ";,"));
253
254     // sanity checks, see
255     // https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_reserved_characters
256     assertEquals("+", StringUtils.urlEncode(" ", " "));
257     assertEquals("%25", StringUtils.urlEncode("%", "%"));
258     assertEquals(".", StringUtils.urlEncode(".", ".")); // note . is not encoded
259     assertEquals("%3A", StringUtils.urlEncode(":", ":"));
260     assertEquals("%3B", StringUtils.urlEncode(";", ";"));
261     assertEquals("%3D", StringUtils.urlEncode("=", "="));
262     assertEquals("%2C", StringUtils.urlEncode(",", ","));
263
264     // check % does not get recursively encoded!
265     assertEquals("a%25b%3Dc%3Bd%3Ae%2C%2C",
266             StringUtils.urlEncode("a%b=c;d:e,,", "=,;:%"));
267
268     // = not in the list for encoding
269     assertEquals("a=b", StringUtils.urlEncode("a=b", ";,"));
270
271     // encode = (as %3B) and ; (as %3D)
272     assertEquals("a%3Db.c%3B", StringUtils.urlEncode("a=b.c;", ";=,"));
273
274     // . and space not in the list for encoding
275     assertEquals("a%3Db.c d", StringUtils.urlEncode("a=b.c d", ";=,"));
276
277     // encode space also (as +)
278     assertEquals("a%3Db.c+d", StringUtils.urlEncode("a=b.c d", ";=, "));
279
280     // . does not get encoded even if requested - behaviour of URLEncoder
281     assertEquals("a%3Db.c+d.e%3Df",
282             StringUtils.urlEncode("a=b.c d.e=f", ";=,. "));
283   }
284
285   @Test(groups = { "Functional" })
286   public void testUrlDecode()
287   {
288     // degenerate cases
289     assertNull(StringUtils.urlDecode(null, ";,"));
290     assertEquals("", StringUtils.urlDecode("", ""));
291     assertEquals("", StringUtils.urlDecode("", ";,"));
292
293     // = not in the list for encoding
294     assertEquals("a%3Db", StringUtils.urlDecode("a%3Db", ";,"));
295
296     // decode = and ; but not .
297     assertEquals("a=b%3Ec; d",
298             StringUtils.urlDecode("a%3Db%3Ec; d", ";=,"));
299
300     // space not in the list for decoding
301     assertEquals("a=b;c+d", StringUtils.urlDecode("a%3Db%3Bc+d", ";=,"));
302
303     // decode space also; %3E is not decoded to .
304     assertEquals("a=b%3Ec d=,",
305             StringUtils.urlDecode("a%3Db%3Ec+d%3D%2C", ";=, "));
306
307     // decode encoded % (%25)
308     assertEquals("a,=;\t%z",
309             StringUtils.urlDecode("a%2C%3D%3B%09%25z", ";=,\t%"));
310   }
311 }