JAL-2089 patch broken merge to master for Release 2.10.0b1
[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 java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
31 import org.testng.annotations.Test;
32
33 public class StringUtilsTest
34 {
35
36   @Test(groups = { "Functional" })
37   public void testInsertCharAt()
38   {
39     char[] c1 = "ABC".toCharArray();
40     char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
41     assertTrue(Arrays.equals(expected,
42             StringUtils.insertCharAt(c1, 3, 2, 'w')));
43     expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
44     assertTrue(Arrays.equals(expected,
45             StringUtils.insertCharAt(c1, 4, 2, 'w')));
46     assertTrue(Arrays.equals(expected,
47             StringUtils.insertCharAt(c1, 5, 2, 'w')));
48     assertTrue(Arrays.equals(expected,
49             StringUtils.insertCharAt(c1, 6, 2, 'w')));
50     assertTrue(Arrays.equals(expected,
51             StringUtils.insertCharAt(c1, 7, 2, 'w')));
52   }
53
54   @Test(groups = { "Functional" })
55   public void testDeleteChars()
56   {
57     char[] c1 = "ABC".toCharArray();
58
59     // delete second position
60     assertTrue(Arrays.equals(new char[] { 'A', 'C' },
61             StringUtils.deleteChars(c1, 1, 2)));
62
63     // delete positions 1 and 2
64     assertTrue(Arrays.equals(new char[] { 'C' },
65             StringUtils.deleteChars(c1, 0, 2)));
66
67     // delete positions 1-3
68     assertTrue(Arrays.equals(new char[] {},
69             StringUtils.deleteChars(c1, 0, 3)));
70
71     // delete position 3
72     assertTrue(Arrays.equals(new char[] { 'A', 'B' },
73             StringUtils.deleteChars(c1, 2, 3)));
74
75     // out of range deletion is ignore
76     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
77   }
78
79   @Test(groups = { "Functional" })
80   public void testGetLastToken()
81   {
82     assertNull(StringUtils.getLastToken(null, null));
83     assertNull(StringUtils.getLastToken(null, "/"));
84     assertEquals("a", StringUtils.getLastToken("a", null));
85
86     assertEquals("abc", StringUtils.getLastToken("abc", "/"));
87     assertEquals("c", StringUtils.getLastToken("abc", "b"));
88     assertEquals("file1.dat", StringUtils.getLastToken(
89             "file://localhost:8080/data/examples/file1.dat", "/"));
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(StringUtils
110             .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(
120             "a*bc*cde",
121             StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
122                 "cde" }, "*"));
123     assertEquals(
124             "a*cde",
125             StringUtils.arrayToSeparatorList(new String[] { "a", null,
126                 "cde" }, "*"));
127     assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
128         "a", "", "cde" }, "*"));
129     // delimiter within token is not (yet) escaped
130     assertEquals("a*b*c*cde", 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<String>();
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", StringUtils.listToDelimitedString(list, " ; "));
145     list.add("the");
146     list.add("winter");
147     list.add("of");
148     list.add("our");
149     list.add("discontent");
150     assertEquals("now is the winter of our discontent",
151             StringUtils.listToDelimitedString(list, " "));
152   }
153
154   @Test(groups = { "Functional" })
155   public void testParseInt()
156   {
157     assertEquals(0, StringUtils.parseInt(null));
158     assertEquals(0, StringUtils.parseInt(""));
159     assertEquals(0, StringUtils.parseInt("x"));
160     assertEquals(0, StringUtils.parseInt("1.2"));
161     assertEquals(33, StringUtils.parseInt("33"));
162     assertEquals(33, StringUtils.parseInt("+33"));
163     assertEquals(-123, StringUtils.parseInt("-123"));
164     // too big for an int:
165     assertEquals(0,
166             StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
167   }
168
169   @Test(groups = { "Functional" })
170   public void testCompareVersions()
171   {
172     assertEquals(0, StringUtils.compareVersions(null, null));
173     assertEquals(0, StringUtils.compareVersions("2.8.3", null));
174
175     /*
176      * same version returns 0
177      */
178     assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
179     assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
180     assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
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
184     /*
185      * v1 < v2 returns -1
186      */
187     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
188     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
189     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
190     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
191     assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
192     assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
193     assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
194     assertEquals(-1, StringUtils.compareVersions("2", "12"));
195     assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
196
197     /*
198      * v1 > v2 returns +1
199      */
200     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
201     assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
202     assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
203     assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
204     assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
205     assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
206     assertEquals(1, StringUtils.compareVersions("12", "2"));
207     assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
208   }
209
210   @Test(groups = { "Functional" })
211   public void testToSentenceCase()
212   {
213     assertEquals("John", StringUtils.toSentenceCase("john"));
214     assertEquals("John", StringUtils.toSentenceCase("JOHN"));
215     assertEquals("John and james",
216             StringUtils.toSentenceCase("JOHN and JAMES"));
217     assertEquals("J", StringUtils.toSentenceCase("j"));
218     assertEquals("", StringUtils.toSentenceCase(""));
219     assertNull(StringUtils.toSentenceCase(null));
220   }
221 }