JAL-3368 removed parsing web colours (now on a separate branch)
[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.assertFalse;
25 import static org.testng.AssertJUnit.assertNull;
26 import static org.testng.AssertJUnit.assertTrue;
27 import static org.testng.AssertJUnit.fail;
28
29 import jalview.gui.JvOptionPane;
30
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 public class StringUtilsTest
39 {
40
41   @BeforeClass(alwaysRun = true)
42   public void setUpJvOptionPane()
43   {
44     JvOptionPane.setInteractiveMode(false);
45     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46   }
47
48   @Test(groups = { "Functional" })
49   public void testInsertCharAt()
50   {
51     char[] c1 = "ABC".toCharArray();
52     char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
53     assertTrue(Arrays.equals(expected,
54             StringUtils.insertCharAt(c1, 3, 2, 'w')));
55     expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
56     assertTrue(Arrays.equals(expected,
57             StringUtils.insertCharAt(c1, 4, 2, 'w')));
58     assertTrue(Arrays.equals(expected,
59             StringUtils.insertCharAt(c1, 5, 2, 'w')));
60     assertTrue(Arrays.equals(expected,
61             StringUtils.insertCharAt(c1, 6, 2, 'w')));
62     assertTrue(Arrays.equals(expected,
63             StringUtils.insertCharAt(c1, 7, 2, 'w')));
64   }
65
66   @Test(groups = { "Functional" })
67   public void testDeleteChars()
68   {
69     char[] c1 = "ABC".toCharArray();
70
71     // delete second position
72     assertTrue(Arrays.equals(new char[] { 'A', 'C' },
73             StringUtils.deleteChars(c1, 1, 2)));
74
75     // delete positions 1 and 2
76     assertTrue(Arrays.equals(new char[] { 'C' },
77             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(Arrays.equals(new char[] { 'A', 'B' },
85             StringUtils.deleteChars(c1, 2, 3)));
86
87     // out of range deletion is ignore
88     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
89   }
90
91   @Test(groups = { "Functional" })
92   public void testGetLastToken()
93   {
94     assertNull(StringUtils.getLastToken(null, null));
95     assertNull(StringUtils.getLastToken(null, "/"));
96     assertEquals("a", StringUtils.getLastToken("a", null));
97
98     assertEquals("abc", StringUtils.getLastToken("abc", "/"));
99     assertEquals("c", StringUtils.getLastToken("abc", "b"));
100     assertEquals("file1.dat", StringUtils.getLastToken(
101             "file://localhost:8080/data/examples/file1.dat", "/"));
102   }
103
104   @Test(groups = { "Functional" })
105   public void testSeparatorListToArray()
106   {
107     String[] result = StringUtils.separatorListToArray(
108             "foo=',',min='foo',max='1,2,3',fa=','", ",");
109     assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
110             Arrays.toString(result));
111     /*
112      * Comma nested in '' is not treated as delimiter; tokens are not trimmed
113      */
114     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
115     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
116
117     /*
118      * String delimited by | containing a quoted | (should not be treated as
119      * delimiter)
120      */
121     assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
122             .separatorListToArray("abc='|'d|ef|g", "|")));
123   }
124
125   @Test(groups = { "Functional" })
126   public void testArrayToSeparatorList()
127   {
128     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
129     assertEquals("*",
130             StringUtils.arrayToSeparatorList(new String[] {}, "*"));
131     assertEquals(
132             "a*bc*cde",
133             StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
134                 "cde" }, "*"));
135     assertEquals(
136             "a*cde",
137             StringUtils.arrayToSeparatorList(new String[] { "a", null,
138                 "cde" }, "*"));
139     assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
140         "a", "", "cde" }, "*"));
141     // delimiter within token is not (yet) escaped
142     assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
143     { "a", "b*c", "cde" }, "*"));
144   }
145
146   @Test(groups = { "Functional" })
147   public void testListToDelimitedString()
148   {
149     assertEquals("", StringUtils.listToDelimitedString(null, ";"));
150     List<String> list = new ArrayList<>();
151     assertEquals("", StringUtils.listToDelimitedString(list, ";"));
152     list.add("now");
153     assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
154     list.add("is");
155     assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
156     assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
157     list.add("the");
158     list.add("winter");
159     list.add("of");
160     list.add("our");
161     list.add("discontent");
162     assertEquals("now is the winter of our discontent",
163             StringUtils.listToDelimitedString(list, " "));
164   }
165
166   @Test(groups = { "Functional" })
167   public void testParseInt()
168   {
169     assertEquals(0, StringUtils.parseInt(null));
170     assertEquals(0, StringUtils.parseInt(""));
171     assertEquals(0, StringUtils.parseInt("x"));
172     assertEquals(0, StringUtils.parseInt("1.2"));
173     assertEquals(33, StringUtils.parseInt("33"));
174     assertEquals(33, StringUtils.parseInt("+33"));
175     assertEquals(-123, StringUtils.parseInt("-123"));
176     // too big for an int:
177     assertEquals(0,
178             StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
179   }
180
181   @Test(groups = { "Functional" })
182   public void testCompareVersions()
183   {
184     assertEquals(0, StringUtils.compareVersions(null, null));
185     assertEquals(0, StringUtils.compareVersions("2.8.3", null));
186
187     /*
188      * same version returns 0
189      */
190     assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
191     assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
192     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
193     assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
194     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
195
196     /*
197      * v1 < v2 returns -1
198      */
199     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
200     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
201     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
202     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
203     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
204     assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
205     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
206     assertEquals(-1, StringUtils.compareVersions("2", "12"));
207     assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
208
209     /*
210      * v1 > v2 returns +1
211      */
212     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
213     assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
214     assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
215     assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
216     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
217     assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
218     assertEquals(1, StringUtils.compareVersions("12", "2"));
219     assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
220   }
221
222   @Test(groups = { "Functional" })
223   public void testToSentenceCase()
224   {
225     assertEquals("John", StringUtils.toSentenceCase("john"));
226     assertEquals("John", StringUtils.toSentenceCase("JOHN"));
227     assertEquals("John and james",
228             StringUtils.toSentenceCase("JOHN and JAMES"));
229     assertEquals("J", StringUtils.toSentenceCase("j"));
230     assertEquals("", StringUtils.toSentenceCase(""));
231     assertNull(StringUtils.toSentenceCase(null));
232   }
233
234   @Test(groups = { "Functional" })
235   public void testStripHtmlTags()
236   {
237     assertNull(StringUtils.stripHtmlTags(null));
238     assertEquals("", StringUtils.stripHtmlTags(""));
239     assertEquals(
240             "<a href=\"something\">label</href>",
241             StringUtils
242                     .stripHtmlTags("<html><a href=\"something\">label</href></html>"));
243
244     // if no "<html>" tag, < and > get html-encoded (not sure why)
245     assertEquals("&lt;a href=\"something\"&gt;label&lt;/href&gt;",
246             StringUtils.stripHtmlTags("<a href=\"something\">label</href>"));
247
248     // </body> gets removed but not <body> (is this intentional?)
249     assertEquals("<body><p>hello",
250             StringUtils.stripHtmlTags("<html><body><p>hello</body></html>"));
251
252     assertEquals("kdHydro &lt; 12.53",
253             StringUtils.stripHtmlTags("kdHydro < 12.53"));
254   }
255
256   @Test(groups = { "Functional" })
257   public void testIsHexString()
258   {
259     assertFalse(StringUtils.isHexString(""));
260     assertTrue(StringUtils.isHexString("0123456789abcdefABCDEF"));
261     assertFalse(StringUtils.isHexString("g"));
262     try
263     {
264       StringUtils.isHexString(null);
265       fail("expected exception");
266     } catch (NullPointerException e)
267     {
268       // expected
269     }
270   }
271 }