Merge branch 'documentation/JAL-3766_relnotes' into releases/Release_2_11_1_Branch
[jalview.git] / test / jalview / io / CrossRef2xmlTests.java
index b3db4de..ae5ed25 100644 (file)
  */
 package jalview.io;
 
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
 import jalview.analysis.CrossRef;
 import jalview.api.AlignmentViewPanel;
 import jalview.datamodel.AlignedCodonFrame;
@@ -29,8 +33,8 @@ import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.CrossRefAction;
 import jalview.gui.Desktop;
-import jalview.gui.Jalview2XML;
 import jalview.gui.JvOptionPane;
+import jalview.project.Jalview2XML;
 import jalview.util.DBRefUtils;
 
 import java.io.File;
@@ -41,12 +45,13 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import junit.extensions.PA;
-
 import org.testng.Assert;
 import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
+import junit.extensions.PA;
+
 @Test(singleThreaded = true)
 public class CrossRef2xmlTests extends Jalview2xmlBase
 {
@@ -59,6 +64,59 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
   }
 
+  @Test(groups = { "Functional" }, enabled = true)
+  public void openCrossrefsForEnsemblTwice()
+  {
+    AlignFrame af = new FileLoader(false).LoadFileWaitTillLoaded(
+            "examples/testdata/CantShowEnsemblCrossrefsTwice.jvp",
+            DataSourceType.FILE);
+    assertNotNull(af, "Couldn't load test's project.");
+    AlignmentI origAlig = af.getViewport().getAlignment();
+    List<String> source = new CrossRef(origAlig.getSequencesArray(),
+            origAlig.getDataset()).findXrefSourcesForSequences(true);
+    assertEquals(source.size(), 1, "Expected just one crossref to show.");
+    List<AlignmentViewPanel> views;
+    {
+      // try to show once - in a code block so handler is forgotten about
+      CrossRefAction xref1 = CrossRefAction.getHandlerFor(
+              origAlig.getSequencesArray(), true, source.get(0), af);
+      try
+      {
+        xref1.run();
+        views = (List<AlignmentViewPanel>) PA.getValue(xref1, "xrefViews");
+        assertTrue(views.size() > 0,
+                "Couldn't get cross ref on first attempt (SERIOUS FAIL).");
+      } catch (Exception ex)
+      {
+        Assert.fail("Unexpected Exception for first xref action", ex);
+      }
+    }
+
+    views = null;
+    // now just try it again
+    CrossRefAction xref2 = CrossRefAction.getHandlerFor(
+            origAlig.getSequencesArray(), true, source.get(0), af);
+    try
+    {
+      xref2.run();
+      views = (List<AlignmentViewPanel>) PA.getValue(xref2, "xrefViews");
+      assertTrue(views.size() > 0,
+              "Couldn't get cross ref on second attempt (SERIOUS FAIL).");
+    } catch (Exception ex)
+    {
+      Assert.fail("Unexpected Exception for second xref action", ex);
+    }
+    // TODO : check that both views contain the same data
+  }
+
+  @DataProvider(name = "initialAccessions")
+  static Object[][] getAccessions()
+  {
+    return new String[][] { { "UNIPROT", "P00338" },
+        { "UNIPROT", "Q8Z9G6" },
+        { "ENSEMBLGENOMES", "CAD01290" } };
+  }
+
   /**
    * test store and recovery of all reachable cross refs from all reachable
    * crossrefs for one or more fetched db refs. Currently, this test has a known
@@ -66,8 +124,13 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
    * 
    * @throws Exception
    */
-  @Test(groups = { "Operational" }, enabled = true)
-  public void testRetrieveAndShowCrossref() throws Exception
+  @Test(
+    groups =
+    { "Operational" },
+    dataProvider = "initialAccessions",
+    enabled = true)
+  public void testRetrieveAndShowCrossref(String forSource,
+          String forAccession) throws Exception
   {
 
     List<String> failedDBRetr = new ArrayList<>();
@@ -97,8 +160,8 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
     List<String> keyseq = new ArrayList<>();
     Map<String, File> savedProjects = new HashMap<>();
 
-    for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
-    {
+//    for (String[] did : new String[][] { { "UNIPROT", "P00338" } })
+//    {
       // pass counters - 0 - first pass, 1 means retrieve project rather than
       // perform action
       int pass1 = 0, pass2 = 0, pass3 = 0;
@@ -108,7 +171,7 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
       // { pass 2 = 0 { pass 3 = 0 } }
       do
       {
-        String first = did[0] + " " + did[1];
+        String first = forSource + " " + forAccession;//did[0] + " " + did[1];
         AlignFrame af = null;
         boolean dna;
         AlignmentI retral;
@@ -120,7 +183,8 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
           // retrieve dbref
 
           List<AlignFrame> afs = jalview.gui.SequenceFetcher.fetchAndShow(
-                  did[0], did[1]);
+                forSource, forAccession);
+        // did[0], did[1]);
           if (afs.size() == 0)
           {
             failedDBRetr.add("Didn't retrieve " + first);
@@ -416,7 +480,7 @@ public class CrossRef2xmlTests extends Jalview2xmlBase
           pass1++;
         }
       } while (pass1 < 3);
-    }
+
     if (failedXrefMenuItems.size() > 0)
     {
       for (String s : failedXrefMenuItems)