Merge branch 'develop' into features/filetypeEnum
[jalview.git] / test / jalview / io / Jalview2xmlTests.java
index 3281db1..266b0f8 100644 (file)
@@ -30,21 +30,26 @@ import jalview.api.AlignViewportI;
 import jalview.api.AlignmentViewPanel;
 import jalview.api.ViewStyleI;
 import jalview.bin.Cache;
+import jalview.bin.Jalview;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.HiddenSequences;
+import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceCollectionI;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
+import jalview.gui.AlignmentPanel;
 import jalview.gui.Desktop;
 import jalview.gui.Jalview2XML;
 import jalview.schemes.AnnotationColourGradient;
 import jalview.schemes.ColourSchemeI;
+import jalview.structure.StructureImportSettings;
 import jalview.viewmodel.AlignmentViewport;
 
 import java.io.File;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -65,8 +70,19 @@ public class Jalview2xmlTests
   @BeforeClass(alwaysRun = true)
   public static void setUpBeforeClass() throws Exception
   {
-    jalview.bin.Jalview.main(new String[] { "-props",
-        "test/jalview/io/testProps.jvprops" });
+    /*
+     * use read-only test properties file
+     */
+    Cache.loadProperties("test/jalview/io/testProps.jvprops");
+
+    /*
+     * set news feed last read to a future time to ensure no
+     * 'unread' news item is displayed
+     */
+    Date oneHourFromNow = new Date(System.currentTimeMillis() + 3600 * 1000);
+    Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
+
+    Jalview.main(new String[] {});
   }
 
   /**
@@ -75,7 +91,7 @@ public class Jalview2xmlTests
   @AfterClass(alwaysRun = true)
   public static void tearDownAfterClass() throws Exception
   {
-    jalview.gui.Desktop.instance.closeAll_actionPerformed(null);
+    Desktop.instance.closeAll_actionPerformed(null);
   }
 
   int countDsAnn(jalview.viewmodel.AlignmentViewport avp)
@@ -270,10 +286,11 @@ public class Jalview2xmlTests
   @Test(groups = { "Functional" })
   public void viewRefPdbAnnotation() throws Exception
   {
-    Cache.applicationProperties.setProperty("STRUCT_FROM_PDB",
-            Boolean.TRUE.toString());
-    Cache.applicationProperties.setProperty("ADD_SS_ANN",
-            Boolean.TRUE.toString());
+    // TODO: Make this pass without setting StructureParser.JALVIEW_PARSER
+    // StructureImportSettings
+    // .setDefaultPDBFileParser(StructureParser.JALVIEW_PARSER);
+    StructureImportSettings.setProcessSecondaryStructure(true);
+    StructureImportSettings.setVisibleChainAnnotation(true);
     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
     assertTrue("Didn't read in the example file correctly.", af != null);
@@ -364,37 +381,29 @@ public class Jalview2xmlTests
   }
 
   /**
-   * test store and recovery of expanded views - currently this is disabled
-   * since the Desktop.explodeViews method doesn't seem to result in the views
-   * being expanded to distinct align frames when executed programmatically.
+   * test store and recovery of expanded views
    * 
    * @throws Exception
    */
   @Test(groups = { "Functional" }, enabled = true)
   public void testStoreAndRecoverExpandedviews() throws Exception
   {
+    Desktop.instance.closeAll_actionPerformed(null);
+
     AlignFrame af = new jalview.io.FileLoader().LoadFileWaitTillLoaded(
             "examples/exampleFile_2_7.jar", DataSourceType.FILE);
     assertTrue("Didn't read in the example file correctly.", af != null);
+    Assert.assertEquals(Desktop.getAlignFrames().length, 1);
     String afid = af.getViewport().getSequenceSetId();
-    {
-      final AlignFrame xaf = af;
-      af = null;
-      new Thread(new Runnable()
-      {
-        @Override
-        public void run()
-        {
-          Desktop.instance.explodeViews(xaf);
-        }
-      }).start();
-      Thread.sleep(1000);
-    }
-    // int times = 0;
-    // while (++times < 5 && Desktop.getAlignFrames().length < )
-    // {
-    // Thread.sleep(300);
-    // }
+
+    // check FileLoader returned a reference to the one alignFrame that is
+    // actually on the Desktop
+    assertTrue(
+            "Jalview2XML.loadAlignFrame() didn't return correct AlignFrame reference for multiple view window",
+            af == Desktop.getAlignFrameFor(af.getViewport()));
+
+    Desktop.explodeViews(af);
+
     int oldviews = Desktop.getAlignFrames().length;
     Assert.assertEquals(Desktop.getAlignFrames().length,
             Desktop.getAlignmentPanels(afid).length);
@@ -679,4 +688,92 @@ public class Jalview2xmlTests
               hidden.size(), hs.getSize());
     }
   }
+
+  /**
+   * Test save and reload of PDBEntry in Jalview project
+   * 
+   * @throws Exception
+   */
+  @Test(groups = { "Functional" })
+  public void testStoreAndRecoverPDBEntry() throws Exception
+  {
+    Desktop.instance.closeAll_actionPerformed(null);
+    String exampleFile = "examples/3W5V.pdb";
+    AlignFrame af = new FileLoader().LoadFileWaitTillLoaded(exampleFile,
+            DataSourceType.FILE);
+    assertTrue("Didn't read in the example file correctly.", af != null);
+    String afid = af.getViewport().getSequenceSetId();
+
+    AlignmentPanel[] alignPanels = Desktop.getAlignmentPanels(afid);
+    System.out.println();
+    AlignmentViewPanel ap = alignPanels[0];
+    String tfileBase = new File(".").getAbsolutePath().replace(".", "");
+    String testFile = tfileBase + exampleFile;
+    AlignmentI alignment = ap.getAlignment();
+    System.out.println("blah");
+    SequenceI[] seqs = alignment.getSequencesArray();
+    Assert.assertNotNull(seqs[0]);
+    Assert.assertNotNull(seqs[1]);
+    Assert.assertNotNull(seqs[2]);
+    Assert.assertNotNull(seqs[3]);
+    Assert.assertNotNull(seqs[0].getDatasetSequence());
+    Assert.assertNotNull(seqs[1].getDatasetSequence());
+    Assert.assertNotNull(seqs[2].getDatasetSequence());
+    Assert.assertNotNull(seqs[3].getDatasetSequence());
+    PDBEntry[] pdbEntries = new PDBEntry[4];
+    pdbEntries[0] = new PDBEntry("3W5V", "A", null, testFile);
+    pdbEntries[1] = new PDBEntry("3W5V", "B", null, testFile);
+    pdbEntries[2] = new PDBEntry("3W5V", "C", null, testFile);
+    pdbEntries[3] = new PDBEntry("3W5V", "D", null, testFile);
+    Assert.assertTrue(seqs[0].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[0]));
+    Assert.assertTrue(seqs[1].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[1]));
+    Assert.assertTrue(seqs[2].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[2]));
+    Assert.assertTrue(seqs[3].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[3]));
+
+    File tfile = File.createTempFile("testStoreAndRecoverPDBEntry", ".jvp");
+    try
+    {
+      new Jalview2XML(false).saveState(tfile);
+    } catch (Throwable e)
+    {
+      Assert.fail("Didn't save the state", e);
+    }
+    Desktop.instance.closeAll_actionPerformed(null);
+    if (Desktop.getAlignFrames() != null)
+    {
+      Assert.assertEquals(Desktop.getAlignFrames().length, 0);
+    }
+
+    AlignFrame restoredFrame = new FileLoader().LoadFileWaitTillLoaded(
+            tfile.getAbsolutePath(), DataSourceType.FILE);
+    String rfid = restoredFrame.getViewport().getSequenceSetId();
+    AlignmentPanel[] rAlignPanels = Desktop.getAlignmentPanels(rfid);
+    AlignmentViewPanel rap = rAlignPanels[0];
+    AlignmentI rAlignment = rap.getAlignment();
+    System.out.println("blah");
+    SequenceI[] rseqs = rAlignment.getSequencesArray();
+    Assert.assertNotNull(rseqs[0]);
+    Assert.assertNotNull(rseqs[1]);
+    Assert.assertNotNull(rseqs[2]);
+    Assert.assertNotNull(rseqs[3]);
+    Assert.assertNotNull(rseqs[0].getDatasetSequence());
+    Assert.assertNotNull(rseqs[1].getDatasetSequence());
+    Assert.assertNotNull(rseqs[2].getDatasetSequence());
+    Assert.assertNotNull(rseqs[3].getDatasetSequence());
+
+    // The Asserts below are expected to fail until the PDB chainCode is
+    // recoverable from a Jalview projects
+    Assert.assertTrue(rseqs[0].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[0]));
+    Assert.assertTrue(rseqs[1].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[1]));
+    Assert.assertTrue(rseqs[2].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[2]));
+    Assert.assertTrue(rseqs[3].getDatasetSequence().getAllPDBEntries()
+            .get(0).equals(pdbEntries[3]));
+  }
 }