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