Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / test / jalview / datamodel / ResidueCountTest.java
index f98e3d3..fc6c99a 100644 (file)
@@ -1,16 +1,50 @@
+/*
+ * 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.datamodel;
 
+import java.util.Locale;
+
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 
 import jalview.datamodel.ResidueCount.SymbolCounts;
+import jalview.gui.JvOptionPane;
+
+import java.util.Arrays;
 
 import org.junit.Assert;
+import org.testng.annotations.BeforeClass;
 import org.testng.annotations.Test;
 
 public class ResidueCountTest
 {
+
+  @BeforeClass(alwaysRun = true)
+  public void setUpJvOptionPane()
+  {
+    JvOptionPane.setInteractiveMode(false);
+    JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
+  }
+
   /**
    * Test a mix of add and put for nucleotide counting
    */
@@ -39,6 +73,7 @@ public class ResidueCountTest
     assertEquals(rc.getCount('N'), 1);
     assertEquals(rc.getCount('?'), 0);
     assertEquals(rc.getCount('-'), 0);
+    assertEquals(rc.getTotalResidueCount(), 11);
 
     assertFalse(rc.isCountingInts());
     assertFalse(rc.isUsingOtherData());
@@ -55,13 +90,26 @@ public class ResidueCountTest
     rc.add('-');
     rc.add('.');
     rc.add(' ');
-    
+
     assertEquals(rc.getGapCount(), 4);
     assertEquals(rc.getCount(' '), 4);
     assertEquals(rc.getCount('-'), 4);
     assertEquals(rc.getCount('.'), 4);
+    assertEquals(rc.getTotalResidueCount(), 0);
     assertFalse(rc.isUsingOtherData());
     assertFalse(rc.isCountingInts());
+
+    rc.set(ResidueCount.GAP_COUNT, Short.MAX_VALUE - 2);
+    assertEquals(rc.getGapCount(), Short.MAX_VALUE - 2);
+    assertFalse(rc.isCountingInts());
+    rc.addGap();
+    assertEquals(rc.getGapCount(), Short.MAX_VALUE - 1);
+    assertFalse(rc.isCountingInts());
+    rc.addGap();
+    assertEquals(rc.getGapCount(), Short.MAX_VALUE);
+    rc.addGap();
+    assertTrue(rc.isCountingInts());
+    assertEquals(rc.getGapCount(), Short.MAX_VALUE + 1);
   }
 
   @Test(groups = "Functional")
@@ -128,6 +176,7 @@ public class ResidueCountTest
     assertEquals(rc.getCount('m'), 13);
     assertEquals(rc.getCount('G'), 0);
     assertEquals(rc.getCount('-'), 0);
+    assertEquals(rc.getTotalResidueCount(), 27);
 
     assertFalse(rc.isCountingInts());
     assertFalse(rc.isUsingOtherData());
@@ -139,7 +188,7 @@ public class ResidueCountTest
     ResidueCount rc = new ResidueCount(false);
     // expected characters (upper or lower case):
     String aas = "ACDEFGHIKLMNPQRSTVWXY";
-    String lower = aas.toLowerCase();
+    String lower = aas.toLowerCase(Locale.ROOT);
     for (int i = 0; i < aas.length(); i++)
     {
       rc.put(aas.charAt(i), i);
@@ -164,7 +213,7 @@ public class ResidueCountTest
     ResidueCount rc = new ResidueCount(true);
     // expected characters (upper or lower case):
     String nucs = "ACGTUN";
-    String lower = nucs.toLowerCase();
+    String lower = nucs.toLowerCase(Locale.ROOT);
     for (int i = 0; i < nucs.length(); i++)
     {
       rc.put(nucs.charAt(i), i);
@@ -242,7 +291,7 @@ public class ResidueCountTest
     rc.add('P');
     assertEquals(rc.getResiduesForCount(Short.MAX_VALUE + 1), "P");
     assertEquals(rc.getResiduesForCount(1), "C");
-  
+
     // modal count is in the 'other data' counts
     rc = new ResidueCount();
     rc.add('Q');
@@ -373,7 +422,7 @@ public class ResidueCountTest
       rc.add('K');
     }
     rc.add('F');
-    
+
     /*
      * percentages are rounded (0.5 rounded up)
      * 10/40 9/40 7/40 6/40 1/40
@@ -409,4 +458,23 @@ public class ResidueCountTest
     assertEquals(rc.getCount('?'), 6);
     assertEquals(rc.getCount('!'), 7);
   }
+
+  @Test(groups = "Functional")
+  public void testConstructor_forSequences()
+  {
+    SequenceI seq1 = new Sequence("seq1", "abcde--. FCD");
+    SequenceI seq2 = new Sequence("seq2", "ab.kKqBd-.");
+    ResidueCount rc = new ResidueCount(Arrays.asList(seq1, seq2));
+
+    assertEquals(rc.getGapCount(), 7);
+    assertEquals(rc.getTotalResidueCount(), 15); // excludes gaps
+    assertEquals(rc.getCount('a'), 2);
+    assertEquals(rc.getCount('A'), 2);
+    assertEquals(rc.getCount('B'), 3);
+    assertEquals(rc.getCount('c'), 2);
+    assertEquals(rc.getCount('D'), 3);
+    assertEquals(rc.getCount('f'), 1);
+    assertEquals(rc.getCount('K'), 2);
+    assertEquals(rc.getCount('Q'), 1);
+  }
 }