JAL-653 JAL-1968 FeaturesFile now handles Jalview or GFF2 or GFF3
[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 import java.util.Map;
31
32 import org.testng.annotations.Test;
33
34 public class StringUtilsTest
35 {
36
37   @Test(groups = { "Functional" })
38   public void testInsertCharAt()
39   {
40     char[] c1 = "ABC".toCharArray();
41     char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
42     assertTrue(Arrays.equals(expected,
43             StringUtils.insertCharAt(c1, 3, 2, 'w')));
44     expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
45     assertTrue(Arrays.equals(expected,
46             StringUtils.insertCharAt(c1, 4, 2, 'w')));
47     assertTrue(Arrays.equals(expected,
48             StringUtils.insertCharAt(c1, 5, 2, 'w')));
49     assertTrue(Arrays.equals(expected,
50             StringUtils.insertCharAt(c1, 6, 2, 'w')));
51     assertTrue(Arrays.equals(expected,
52             StringUtils.insertCharAt(c1, 7, 2, 'w')));
53   }
54
55   @Test(groups = { "Functional" })
56   public void testDeleteChars()
57   {
58     char[] c1 = "ABC".toCharArray();
59
60     // delete second position
61     assertTrue(Arrays.equals(new char[] { 'A', 'C' },
62             StringUtils.deleteChars(c1, 1, 2)));
63
64     // delete positions 1 and 2
65     assertTrue(Arrays.equals(new char[] { 'C' },
66             StringUtils.deleteChars(c1, 0, 2)));
67
68     // delete positions 1-3
69     assertTrue(Arrays.equals(new char[] {},
70             StringUtils.deleteChars(c1, 0, 3)));
71
72     // delete position 3
73     assertTrue(Arrays.equals(new char[] { 'A', 'B' },
74             StringUtils.deleteChars(c1, 2, 3)));
75
76     // out of range deletion is ignore
77     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
78   }
79
80   @Test(groups = { "Functional" })
81   public void testGetLastToken()
82   {
83     assertNull(StringUtils.getLastToken(null, null));
84     assertNull(StringUtils.getLastToken(null, "/"));
85     assertEquals("a", StringUtils.getLastToken("a", null));
86
87     assertEquals("abc", StringUtils.getLastToken("abc", "/"));
88     assertEquals("c", StringUtils.getLastToken("abc", "b"));
89     assertEquals("file1.dat", StringUtils.getLastToken(
90             "file://localhost:8080/data/examples/file1.dat", "/"));
91   }
92
93   @Test(groups = { "Functional" })
94   public void testSeparatorListToArray()
95   {
96     String[] result = StringUtils.separatorListToArray(
97             "foo=',',min='foo',max='1,2,3',fa=','", ",");
98     assertEquals("[foo=',', min='foo', max='1,2,3', fa=',']",
99             Arrays.toString(result));
100     /*
101      * Comma nested in '' is not treated as delimiter; tokens are not trimmed
102      */
103     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
104     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
105
106     /*
107      * String delimited by | containing a quoted | (should not be treated as
108      * delimiter)
109      */
110     assertEquals("[abc='|'d, ef, g]", Arrays.toString(StringUtils
111             .separatorListToArray("abc='|'d|ef|g", "|")));
112   }
113
114   @Test(groups = { "Functional" })
115   public void testArrayToSeparatorList()
116   {
117     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
118     assertEquals("*",
119             StringUtils.arrayToSeparatorList(new String[] {}, "*"));
120     assertEquals(
121             "a*bc*cde",
122             StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
123                 "cde" }, "*"));
124     assertEquals(
125             "a*cde",
126             StringUtils.arrayToSeparatorList(new String[] { "a", null,
127                 "cde" }, "*"));
128     assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
129         "a", "", "cde" }, "*"));
130     // delimiter within token is not (yet) escaped
131     assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
132     { "a", "b*c", "cde" }, "*"));
133   }
134
135   /**
136    * Test the method that parses lines like <br>
137    * ID=2345;Name=Something;
138    */
139   @Test(groups = { "Functional" })
140   public void testParseNameValuePairs()
141   {
142     char[] separators = new char[] { ' ' };
143     assertTrue(StringUtils.parseNameValuePairs(null, ";", separators)
144             .isEmpty());
145     assertTrue(StringUtils.parseNameValuePairs("", ";", separators)
146             .isEmpty());
147     assertTrue(StringUtils.parseNameValuePairs("hello=world", ";",
148             separators).isEmpty());
149
150     Map<String, List<String>> map = StringUtils.parseNameValuePairs(
151             "hello world", ";", separators);
152     assertEquals(1, map.size());
153     assertEquals(1, map.get("hello").size());
154     assertEquals("world", map.get("hello").get(0));
155
156     separators = new char[] { ' ', '=' };
157     map = StringUtils
158             .parseNameValuePairs(
159                     "Method= manual curation ;nothing; Notes F2=S ; Notes=Metal; Type=",
160                     ";", separators);
161
162     // Type is ignored as no value was supplied
163     assertEquals(2, map.size());
164
165     // equals separator used ahead of space separator:
166     assertEquals(1, map.get("Method").size());
167     assertEquals("manual curation", map.get("Method").get(0)); // trimmed
168
169     assertEquals(2, map.get("Notes").size());
170     // space separator used ahead of equals separator
171     assertEquals("F2=S", map.get("Notes").get(0));
172     assertEquals("Metal", map.get("Notes").get(1));
173   }
174
175   @Test(groups = { "Functional" })
176   public void testListToDelimitedString()
177   {
178     assertEquals("", StringUtils.listToDelimitedString(null, ";"));
179     List<String> list = new ArrayList<String>();
180     assertEquals("", StringUtils.listToDelimitedString(list, ";"));
181     list.add("now");
182     assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
183     list.add("is");
184     assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
185     assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
186     list.add("the");
187     list.add("winter");
188     list.add("of");
189     list.add("our");
190     list.add("discontent");
191     assertEquals("now is the winter of our discontent",
192             StringUtils.listToDelimitedString(list, " "));
193   }
194
195   @Test(groups = { "Functional" })
196   public void testParseInt()
197   {
198     assertEquals(0, StringUtils.parseInt(null));
199     assertEquals(0, StringUtils.parseInt(""));
200     assertEquals(0, StringUtils.parseInt("x"));
201     assertEquals(0, StringUtils.parseInt("1.2"));
202     assertEquals(33, StringUtils.parseInt("33"));
203     assertEquals(33, StringUtils.parseInt("+33"));
204     assertEquals(-123, StringUtils.parseInt("-123"));
205     // too big for an int:
206     assertEquals(0,
207             StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
208   }
209 }