2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNull;
25 import static org.testng.AssertJUnit.assertTrue;
27 import jalview.gui.JvOptionPane;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.Test;
36 public class StringUtilsTest
39 @BeforeClass(alwaysRun = true)
40 public void setUpJvOptionPane()
42 JvOptionPane.setInteractiveMode(false);
43 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
46 @Test(groups = { "Functional" })
47 public void testInsertCharAt()
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')));
64 @Test(groups = { "Functional" })
65 public void testDeleteChars()
67 char[] c1 = "ABC".toCharArray();
69 // delete second position
70 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
71 StringUtils.deleteChars(c1, 1, 2)));
73 // delete positions 1 and 2
74 assertTrue(Arrays.equals(new char[] { 'C' },
75 StringUtils.deleteChars(c1, 0, 2)));
77 // delete positions 1-3
78 assertTrue(Arrays.equals(new char[] {},
79 StringUtils.deleteChars(c1, 0, 3)));
82 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
83 StringUtils.deleteChars(c1, 2, 3)));
85 // out of range deletion is ignore
86 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
89 @Test(groups = { "Functional" })
90 public void testGetLastToken()
92 assertNull(StringUtils.getLastToken(null, null));
93 assertNull(StringUtils.getLastToken(null, "/"));
94 assertEquals("a", StringUtils.getLastToken("a", null));
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", "/"));
102 @Test(groups = { "Functional" })
103 public void testSeparatorListToArray()
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));
110 * Comma nested in '' is not treated as delimiter; tokens are not trimmed
112 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
113 assertEquals("[minsize='2', sep=',']", Arrays.toString(result));
116 * String delimited by | containing a quoted | (should not be treated as
119 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
120 .separatorListToArray("abc='|'d|ef|g", "|")));
123 @Test(groups = { "Functional" })
124 public void testArrayToSeparatorList()
126 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
128 StringUtils.arrayToSeparatorList(new String[] {}, "*"));
131 StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
135 StringUtils.arrayToSeparatorList(new String[] { "a", null,
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" }, "*"));
144 @Test(groups = { "Functional" })
145 public void testListToDelimitedString()
147 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
148 List<String> list = new ArrayList<String>();
149 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
151 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
153 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
154 assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
159 list.add("discontent");
160 assertEquals("now is the winter of our discontent",
161 StringUtils.listToDelimitedString(list, " "));
164 @Test(groups = { "Functional" })
165 public void testParseInt()
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:
176 StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
179 @Test(groups = { "Functional" })
180 public void testCompareVersions()
182 assertEquals(0, StringUtils.compareVersions(null, null));
183 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
186 * same version returns 0
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"));
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"));
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"));
220 @Test(groups = { "Functional" })
221 public void testToSentenceCase()
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));