JAL-2711 removed unused and flaky utility method
[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 testSeparatorListToArray()
91   {
92     String[] result = StringUtils.separatorListToArray(
93             "foo=',',min='foo',max='1,2,3',fa=','", ",");
94     assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
95             Arrays.toString(result));
96     /*
97      * Comma nested in '' is not treated as delimiter; tokens are not trimmed
98      */
99     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
100     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
101
102     /*
103      * String delimited by | containing a quoted | (should not be treated as
104      * delimiter)
105      */
106     assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
107             .separatorListToArray("abc='|'d|ef|g", "|")));
108   }
109
110   @Test(groups = { "Functional" })
111   public void testArrayToSeparatorList()
112   {
113     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
114     assertEquals("*",
115             StringUtils.arrayToSeparatorList(new String[] {}, "*"));
116     assertEquals(
117             "a*bc*cde",
118             StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
119                 "cde" }, "*"));
120     assertEquals(
121             "a*cde",
122             StringUtils.arrayToSeparatorList(new String[] { "a", null,
123                 "cde" }, "*"));
124     assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
125         "a", "", "cde" }, "*"));
126     // delimiter within token is not (yet) escaped
127     assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
128     { "a", "b*c", "cde" }, "*"));
129   }
130
131   @Test(groups = { "Functional" })
132   public void testListToDelimitedString()
133   {
134     assertEquals("", StringUtils.listToDelimitedString(null, ";"));
135     List<String> list = new ArrayList<>();
136     assertEquals("", StringUtils.listToDelimitedString(list, ";"));
137     list.add("now");
138     assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
139     list.add("is");
140     assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
141     assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
142     list.add("the");
143     list.add("winter");
144     list.add("of");
145     list.add("our");
146     list.add("discontent");
147     assertEquals("now is the winter of our discontent",
148             StringUtils.listToDelimitedString(list, " "));
149   }
150
151   @Test(groups = { "Functional" })
152   public void testParseInt()
153   {
154     assertEquals(0, StringUtils.parseInt(null));
155     assertEquals(0, StringUtils.parseInt(""));
156     assertEquals(0, StringUtils.parseInt("x"));
157     assertEquals(0, StringUtils.parseInt("1.2"));
158     assertEquals(33, StringUtils.parseInt("33"));
159     assertEquals(33, StringUtils.parseInt("+33"));
160     assertEquals(-123, StringUtils.parseInt("-123"));
161     // too big for an int:
162     assertEquals(0,
163             StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
164   }
165
166   @Test(groups = { "Functional" })
167   public void testCompareVersions()
168   {
169     assertEquals(0, StringUtils.compareVersions(null, null));
170     assertEquals(0, StringUtils.compareVersions("2.8.3", null));
171
172     /*
173      * same version returns 0
174      */
175     assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
176     assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
177     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
178     assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
179     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
180
181     /*
182      * v1 < v2 returns -1
183      */
184     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
185     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
186     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
187     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
188     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
189     assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
190     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
191     assertEquals(-1, StringUtils.compareVersions("2", "12"));
192     assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
193
194     /*
195      * v1 > v2 returns +1
196      */
197     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
198     assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
199     assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
200     assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
201     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
202     assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
203     assertEquals(1, StringUtils.compareVersions("12", "2"));
204     assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
205   }
206
207   @Test(groups = { "Functional" })
208   public void testToSentenceCase()
209   {
210     assertEquals("John", StringUtils.toSentenceCase("john"));
211     assertEquals("John", StringUtils.toSentenceCase("JOHN"));
212     assertEquals("John and james",
213             StringUtils.toSentenceCase("JOHN and JAMES"));
214     assertEquals("J", StringUtils.toSentenceCase("j"));
215     assertEquals("", StringUtils.toSentenceCase(""));
216     assertNull(StringUtils.toSentenceCase(null));
217   }
218
219   @Test(groups = { "Functional" })
220   public void testStripHtmlTags()
221   {
222     assertNull(StringUtils.stripHtmlTags(null));
223     assertEquals("", StringUtils.stripHtmlTags(""));
224     assertEquals(
225             "<a href=\"something\">label</href>",
226             StringUtils
227                     .stripHtmlTags("<html><a href=\"something\">label</href></html>"));
228
229     // if no "<html>" tag, < and > get html-encoded (not sure why)
230     assertEquals("&lt;a href=\"something\"&gt;label&lt;/href&gt;",
231             StringUtils.stripHtmlTags("<a href=\"something\">label</href>"));
232
233     // </body> gets removed but not <body> (is this intentional?)
234     assertEquals("<body><p>hello",
235             StringUtils.stripHtmlTags("<html><body><p>hello</body></html>"));
236
237     assertEquals("kdHydro &lt; 12.53",
238             StringUtils.stripHtmlTags("kdHydro < 12.53"));
239   }
240 }