JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / util / StringUtilsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 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.Arrays;
28
29 import org.testng.annotations.Test;
30
31 public class StringUtilsTest
32 {
33
34   @Test(groups = { "Functional" })
35   public void testInsertCharAt()
36   {
37     char[] c1 = "ABC".toCharArray();
38     char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
39     assertTrue(Arrays.equals(expected,
40             StringUtils.insertCharAt(c1, 3, 2, 'w')));
41     expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
42     assertTrue(Arrays.equals(expected,
43             StringUtils.insertCharAt(c1, 4, 2, 'w')));
44     assertTrue(Arrays.equals(expected,
45             StringUtils.insertCharAt(c1, 5, 2, 'w')));
46     assertTrue(Arrays.equals(expected,
47             StringUtils.insertCharAt(c1, 6, 2, 'w')));
48     assertTrue(Arrays.equals(expected,
49             StringUtils.insertCharAt(c1, 7, 2, 'w')));
50   }
51
52   @Test(groups = { "Functional" })
53   public void testDeleteChars()
54   {
55     char[] c1 = "ABC".toCharArray();
56
57     // delete second position
58     assertTrue(Arrays.equals(new char[] { 'A', 'C' },
59             StringUtils.deleteChars(c1, 1, 2)));
60
61     // delete positions 1 and 2
62     assertTrue(Arrays.equals(new char[] { 'C' },
63             StringUtils.deleteChars(c1, 0, 2)));
64
65     // delete positions 1-3
66     assertTrue(Arrays.equals(new char[] {},
67             StringUtils.deleteChars(c1, 0, 3)));
68
69     // delete position 3
70     assertTrue(Arrays.equals(new char[] { 'A', 'B' },
71             StringUtils.deleteChars(c1, 2, 3)));
72
73     // out of range deletion is ignore
74     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
75   }
76
77   @Test(groups = { "Functional" })
78   public void testGetLastToken()
79   {
80     assertNull(StringUtils.getLastToken(null, null));
81     assertNull(StringUtils.getLastToken(null, "/"));
82     assertEquals("a", StringUtils.getLastToken("a", null));
83
84     assertEquals("abc", StringUtils.getLastToken("abc", "/"));
85     assertEquals("c", StringUtils.getLastToken("abc", "b"));
86     assertEquals("file1.dat", StringUtils.getLastToken(
87             "file://localhost:8080/data/examples/file1.dat", "/"));
88   }
89
90   @Test(groups = { "Functional" })
91   public void testSeparatorListToArray()
92   {
93     String[] result = StringUtils.separatorListToArray(
94             "foo=',',min='foo',max='1,2,3',fa=','", ",");
95     assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
96             Arrays.toString(result));
97     /*
98      * Comma nested in '' is not treated as delimiter; tokens are not trimmed
99      */
100     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
101     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
102
103     /*
104      * String delimited by | containing a quoted | (should not be treated as
105      * delimiter)
106      */
107     assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
108             .separatorListToArray("abc='|'d|ef|g", "|")));
109   }
110
111   @Test(groups = { "Functional" })
112   public void testArrayToSeparatorList()
113   {
114     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
115     assertEquals("*",
116             StringUtils.arrayToSeparatorList(new String[] {}, "*"));
117     assertEquals(
118             "a*bc*cde",
119             StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
120                 "cde" }, "*"));
121     assertEquals(
122             "a*cde",
123             StringUtils.arrayToSeparatorList(new String[] { "a", null,
124                 "cde" }, "*"));
125     assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
126         "a", "", "cde" }, "*"));
127     // delimiter within token is not (yet) escaped
128     assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
129     { "a", "b*c", "cde" }, "*"));
130   }
131 }