JAL-653 test for 'resolving GFF mappings to sequences' (wip)
[jalview.git] / test / jalview / util / StringUtilsTest.java
index e8de3bd..0b776d1 100644 (file)
@@ -1,26 +1,47 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.util;
 
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertNull;
 import static org.testng.AssertJUnit.assertTrue;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
 
 import org.testng.annotations.Test;
 
 public class StringUtilsTest
 {
 
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testInsertCharAt()
   {
     char[] c1 = "ABC".toCharArray();
-    char[] expected = new char[]
-    { 'A', 'B', 'C', 'w', 'w' };
+    char[] expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
     assertTrue(Arrays.equals(expected,
             StringUtils.insertCharAt(c1, 3, 2, 'w')));
-    expected = new char[]
-    { 'A', 'B', 'C', 'w', 'w' };
+    expected = new char[] { 'A', 'B', 'C', 'w', 'w' };
     assertTrue(Arrays.equals(expected,
             StringUtils.insertCharAt(c1, 4, 2, 'w')));
     assertTrue(Arrays.equals(expected,
@@ -31,32 +52,32 @@ public class StringUtilsTest
             StringUtils.insertCharAt(c1, 7, 2, 'w')));
   }
 
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testDeleteChars()
   {
     char[] c1 = "ABC".toCharArray();
 
     // delete second position
-    assertTrue(Arrays.equals(new char[]
-    { 'A', 'C' }, StringUtils.deleteChars(c1, 1, 2)));
+    assertTrue(Arrays.equals(new char[] { 'A', 'C' },
+            StringUtils.deleteChars(c1, 1, 2)));
 
     // delete positions 1 and 2
-    assertTrue(Arrays.equals(new char[]
-    { 'C' }, StringUtils.deleteChars(c1, 0, 2)));
+    assertTrue(Arrays.equals(new char[] { 'C' },
+            StringUtils.deleteChars(c1, 0, 2)));
 
     // delete positions 1-3
-    assertTrue(Arrays.equals(new char[]
-    {}, StringUtils.deleteChars(c1, 0, 3)));
+    assertTrue(Arrays.equals(new char[] {},
+            StringUtils.deleteChars(c1, 0, 3)));
 
     // delete position 3
-    assertTrue(Arrays.equals(new char[]
-    { 'A', 'B' }, StringUtils.deleteChars(c1, 2, 3)));
+    assertTrue(Arrays.equals(new char[] { 'A', 'B' },
+            StringUtils.deleteChars(c1, 2, 3)));
 
     // out of range deletion is ignore
     assertTrue(Arrays.equals(c1, StringUtils.deleteChars(c1, 3, 4)));
   }
 
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testGetLastToken()
   {
     assertNull(StringUtils.getLastToken(null, null));
@@ -69,7 +90,7 @@ public class StringUtilsTest
             "file://localhost:8080/data/examples/file1.dat", "/"));
   }
 
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testSeparatorListToArray()
   {
     String[] result = StringUtils.separatorListToArray(
@@ -81,7 +102,7 @@ public class StringUtilsTest
      */
     result = StringUtils.separatorListToArray("minsize='2', sep=','", ",");
     assertEquals("[minsize='2',  sep=',']", Arrays.toString(result));
-    
+
     /*
      * String delimited by | containing a quoted | (should not be treated as
      * delimiter)
@@ -90,20 +111,99 @@ public class StringUtilsTest
             .separatorListToArray("abc='|'d|ef|g", "|")));
   }
 
-  @Test(groups ={ "Functional" })
+  @Test(groups = { "Functional" })
   public void testArrayToSeparatorList()
   {
     assertEquals("*", StringUtils.arrayToSeparatorList(null, "*"));
-    assertEquals("*", StringUtils.arrayToSeparatorList(new String[]
-    {}, "*"));
-    assertEquals("a*bc*cde", StringUtils.arrayToSeparatorList(new String[]
-    { "a", "bc", "cde" }, "*"));
-    assertEquals("a*cde", StringUtils.arrayToSeparatorList(new String[]
-    { "a", null, "cde" }, "*"));
-    assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[]
-    { "a", "", "cde" }, "*"));
+    assertEquals("*",
+            StringUtils.arrayToSeparatorList(new String[] {}, "*"));
+    assertEquals(
+            "a*bc*cde",
+            StringUtils.arrayToSeparatorList(new String[] { "a", "bc",
+                "cde" }, "*"));
+    assertEquals(
+            "a*cde",
+            StringUtils.arrayToSeparatorList(new String[] { "a", null,
+                "cde" }, "*"));
+    assertEquals("a**cde", StringUtils.arrayToSeparatorList(new String[] {
+        "a", "", "cde" }, "*"));
     // delimiter within token is not (yet) escaped
     assertEquals("a*b*c*cde", StringUtils.arrayToSeparatorList(new String[]
     { "a", "b*c", "cde" }, "*"));
   }
+
+  /**
+   * Test the method that parses lines like <br>
+   * ID=2345;Name=Something;
+   */
+  @Test(groups = { "Functional" })
+  public void testParseNameValuePairs()
+  {
+    char[] separators = new char[] { ' ' };
+    assertTrue(StringUtils.parseNameValuePairs(null, ";", separators)
+            .isEmpty());
+    assertTrue(StringUtils.parseNameValuePairs("", ";", separators)
+            .isEmpty());
+    assertTrue(StringUtils.parseNameValuePairs("hello=world", ";",
+            separators).isEmpty());
+
+    Map<String, List<String>> map = StringUtils.parseNameValuePairs(
+            "hello world", ";", separators);
+    assertEquals(1, map.size());
+    assertEquals(1, map.get("hello").size());
+    assertEquals("world", map.get("hello").get(0));
+
+    separators = new char[] { ' ', '=' };
+    map = StringUtils
+            .parseNameValuePairs(
+                    "Method= manual curation ;nothing; Notes F2=S ; Notes=Metal; Type=",
+                    ";", separators);
+
+    // Type is ignored as no value was supplied
+    assertEquals(2, map.size());
+
+    // equals separator used ahead of space separator:
+    assertEquals(1, map.get("Method").size());
+    assertEquals("manual curation", map.get("Method").get(0)); // trimmed
+
+    assertEquals(2, map.get("Notes").size());
+    // space separator used ahead of equals separator
+    assertEquals("F2=S", map.get("Notes").get(0));
+    assertEquals("Metal", map.get("Notes").get(1));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testListToDelimitedString()
+  {
+    assertEquals("", StringUtils.listToDelimitedString(null, ";"));
+    List<String> list = new ArrayList<String>();
+    assertEquals("", StringUtils.listToDelimitedString(list, ";"));
+    list.add("now");
+    assertEquals("now", StringUtils.listToDelimitedString(list, ";"));
+    list.add("is");
+    assertEquals("now;is", StringUtils.listToDelimitedString(list, ";"));
+    assertEquals("now ; is", StringUtils.listToDelimitedString(list, " ; "));
+    list.add("the");
+    list.add("winter");
+    list.add("of");
+    list.add("our");
+    list.add("discontent");
+    assertEquals("now is the winter of our discontent",
+            StringUtils.listToDelimitedString(list, " "));
+  }
+
+  @Test(groups = { "Functional" })
+  public void testParseInt()
+  {
+    assertEquals(0, StringUtils.parseInt(null));
+    assertEquals(0, StringUtils.parseInt(""));
+    assertEquals(0, StringUtils.parseInt("x"));
+    assertEquals(0, StringUtils.parseInt("1.2"));
+    assertEquals(33, StringUtils.parseInt("33"));
+    assertEquals(33, StringUtils.parseInt("+33"));
+    assertEquals(-123, StringUtils.parseInt("-123"));
+    // too big for an int:
+    assertEquals(0,
+            StringUtils.parseInt(String.valueOf(Integer.MAX_VALUE) + "1"));
+  }
 }