Clover icon

jalviewX

  1. Project Clover database Wed Oct 31 2018 15:13:58 GMT
  2. Package jalview.util

File StringUtilsTest.java

 

Code metrics

0
91
11
1
253
179
11
0.12
8.27
11
1

Classes

Class Line # Actions
StringUtilsTest 36 91 11 0
1.0100%
 

Contributing tests

This file is covered by 10 tests. .

Source view

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  1 toggle @BeforeClass(alwaysRun = true)
40    public void setUpJvOptionPane()
41    {
42  1 JvOptionPane.setInteractiveMode(false);
43  1 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
44    }
45   
 
46  1 toggle @Test(groups = { "Functional" })
47    public void testInsertCharAt()
48    {
49  1 char[] c1 = "ABC".toCharArray();
50  1 char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
51  1 assertTrue(Arrays.equals(expected,
52    StringUtils.insertCharAt(c1, 3, 2, 'w')));
53  1 expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
54  1 assertTrue(Arrays.equals(expected,
55    StringUtils.insertCharAt(c1, 4, 2, 'w')));
56  1 assertTrue(Arrays.equals(expected,
57    StringUtils.insertCharAt(c1, 5, 2, 'w')));
58  1 assertTrue(Arrays.equals(expected,
59    StringUtils.insertCharAt(c1, 6, 2, 'w')));
60  1 assertTrue(Arrays.equals(expected,
61    StringUtils.insertCharAt(c1, 7, 2, 'w')));
62    }
63   
 
64  1 toggle @Test(groups = { "Functional" })
65    public void testDeleteChars()
66    {
67  1 char[] c1 = "ABC".toCharArray();
68   
69    // delete second position
70  1 assertTrue(Arrays.equals(new char[] { 'A', 'C' },
71    StringUtils.deleteChars(c1, 1, 2)));
72   
73    // delete positions 1 and 2
74  1 assertTrue(Arrays.equals(new char[] { 'C' },
75    StringUtils.deleteChars(c1, 0, 2)));
76   
77    // delete positions 1-3
78  1 assertTrue(Arrays.equals(new char[] {},
79    StringUtils.deleteChars(c1, 0, 3)));
80   
81    // delete position 3
82  1 assertTrue(Arrays.equals(new char[] { 'A', 'B' },
83    StringUtils.deleteChars(c1, 2, 3)));
84   
85    // out of range deletion is ignore
86  1 assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
87    }
88   
 
89  1 toggle @Test(groups = { "Functional" })
90    public void testGetLastToken()
91    {
92  1 assertNull(StringUtils.getLastToken(null, null));
93  1 assertNull(StringUtils.getLastToken(null, "/"));
94  1 assertEquals("a", StringUtils.getLastToken("a", null));
95   
96  1 assertEquals("abc", StringUtils.getLastToken("abc", "/"));
97  1 assertEquals("c", StringUtils.getLastToken("abc", "b"));
98  1 assertEquals("file1.dat", StringUtils.getLastToken(
99    "file://localhost:8080/data/examples/file1.dat", "/"));
100    }
101   
 
102  1 toggle @Test(groups = { "Functional" })
103    public void testSeparatorListToArray()
104    {
105  1 String[] result = StringUtils.separatorListToArray(
106    "foo=',',min='foo',max='1,2,3',fa=','", ",");
107  1 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  1 result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
113  1 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  1 assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
120    .separatorListToArray("abc='|'d|ef|g", "|")));
121    }
122   
 
123  1 toggle @Test(groups = { "Functional" })
124    public void testArrayToSeparatorList()
125    {
126  1 assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
127  1 assertEquals("*",
128    StringUtils.arrayToSeparatorList(new String[] {}, "*"));
129  1 assertEquals(
130    "a*bc*cde",
131    StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
132    "cde" }, "*"));
133  1 assertEquals(
134    "a*cde",
135    StringUtils.arrayToSeparatorList(new String[] { "a", null,
136    "cde" }, "*"));
137  1 assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
138    "a", "", "cde" }, "*"));
139    // delimiter within token is not (yet) escaped
140  1 assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
141    { "a", "b*c", "cde" }, "*"));
142    }
143   
 
144  1 toggle @Test(groups = { "Functional" })
145    public void testListToDelimitedString()
146    {
147  1 assertEquals("", StringUtils.listToDelimitedString(null, ";"));
148  1 List<String> list = new ArrayList<String>();
149  1 assertEquals("", StringUtils.listToDelimitedString(list, ";"));
150  1 list.add("now");
151  1 assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
152  1 list.add("is");
153  1 assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
154  1 assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
155  1 list.add("the");
156  1 list.add("winter");
157  1 list.add("of");
158  1 list.add("our");
159  1 list.add("discontent");
160  1 assertEquals("now is the winter of our discontent",
161    StringUtils.listToDelimitedString(list, " "));
162    }
163   
 
164  1 toggle @Test(groups = { "Functional" })
165    public void testParseInt()
166    {
167  1 assertEquals(0, StringUtils.parseInt(null));
168  1 assertEquals(0, StringUtils.parseInt(""));
169  1 assertEquals(0, StringUtils.parseInt("x"));
170  1 assertEquals(0, StringUtils.parseInt("1.2"));
171  1 assertEquals(33, StringUtils.parseInt("33"));
172  1 assertEquals(33, StringUtils.parseInt("+33"));
173  1 assertEquals(-123, StringUtils.parseInt("-123"));
174    // too big for an int:
175  1 assertEquals(0,
176    StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
177    }
178   
 
179  1 toggle @Test(groups = { "Functional" })
180    public void testCompareVersions()
181    {
182  1 assertEquals(0, StringUtils.compareVersions(null, null));
183  1 assertEquals(0, StringUtils.compareVersions("2.8.3", null));
184   
185    /*
186    * same version returns 0
187    */
188  1 assertEquals(0, StringUtils.compareVersions("2.8", "2.8"));
189  1 assertEquals(0, StringUtils.compareVersions("2.8.3", "2.8.3"));
190  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3b1", "b"));
191  1 assertEquals(0, StringUtils.compareVersions("2.8.3B1", "2.8.3b1", "b"));
192  1 assertEquals(0, StringUtils.compareVersions("2.8.3b1", "2.8.3B1", "b"));
193   
194    /*
195    * v1 < v2 returns -1
196    */
197  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.4"));
198  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9"));
199  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.9.2"));
200  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.3"));
201  1 assertEquals(-1, StringUtils.compareVersions("2.8.3", "2.8.3b1", "b"));
202  1 assertEquals(-1, StringUtils.compareVersions("2.8.3b1", "2.8.3b2", "b"));
203  1 assertEquals(-1, StringUtils.compareVersions("2.8", "2.8.0", "b"));
204  1 assertEquals(-1, StringUtils.compareVersions("2", "12"));
205  1 assertEquals(-1, StringUtils.compareVersions("3.2.4", "3.12.11"));
206   
207    /*
208    * v1 > v2 returns +1
209    */
210  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8"));
211  1 assertEquals(1, StringUtils.compareVersions("2.8.0", "2.8"));
212  1 assertEquals(1, StringUtils.compareVersions("2.8.4", "2.8.3"));
213  1 assertEquals(1, StringUtils.compareVersions("2.8.3b1", "2.8.3", "b"));
214  1 assertEquals(1, StringUtils.compareVersions("2.8.3", "2.8.2b1", "b"));
215  1 assertEquals(1, StringUtils.compareVersions("2.8.0b2", "2.8.0b1", "b"));
216  1 assertEquals(1, StringUtils.compareVersions("12", "2"));
217  1 assertEquals(1, StringUtils.compareVersions("3.12.11", "3.2.4"));
218    }
219   
 
220  1 toggle @Test(groups = { "Functional" })
221    public void testToSentenceCase()
222    {
223  1 assertEquals("John", StringUtils.toSentenceCase("john"));
224  1 assertEquals("John", StringUtils.toSentenceCase("JOHN"));
225  1 assertEquals("John and james",
226    StringUtils.toSentenceCase("JOHN and JAMES"));
227  1 assertEquals("J", StringUtils.toSentenceCase("j"));
228  1 assertEquals("", StringUtils.toSentenceCase(""));
229  1 assertNull(StringUtils.toSentenceCase(null));
230    }
231   
 
232  1 toggle @Test(groups = { "Functional" })
233    public void testStripHtmlTags()
234    {
235  1 assertNull(StringUtils.stripHtmlTags(null));
236  1 assertEquals("", StringUtils.stripHtmlTags(""));
237  1 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  1 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  1 assertEquals("<body><p>hello",
248    StringUtils.stripHtmlTags("<html><body><p>hello</body></html>"));
249   
250  1 assertEquals("kdHydro &lt; 12.53",
251    StringUtils.stripHtmlTags("kdHydro < 12.53"));
252    }
253    }