JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / jalview / io / JSONFileTest.java
index bce9795..9bdc2b5 100644 (file)
@@ -1,27 +1,52 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 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.io;
 
+import static org.testng.AssertJUnit.assertNotNull;
 
-import static org.junit.Assert.assertNotNull;
+import jalview.api.AlignExportSettingI;
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.ColumnSelection;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
-import jalview.gui.AlignmentPanel;
 import jalview.schemes.ColourSchemeI;
-import jalview.viewmodel.seqfeatures.FeaturesDisplayed;
+import jalview.schemes.ZappoColourScheme;
 
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.testng.Assert;
+import org.testng.AssertJUnit;
+import org.testng.annotations.AfterTest;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.BeforeTest;
+import org.testng.annotations.Test;
 
 public class JSONFileTest
 {
@@ -32,17 +57,31 @@ public class JSONFileTest
 
   private int TEST_ANOT_HEIGHT = 0;
 
-  private AlignFrame af;
+  private int TEST_CS_HEIGHT = 0;
 
-  AlignmentI alignment;
+  private String TEST_JSON_FILE = "examples/example.json";
 
-  AlignmentPanel alignPanel;
+  private Alignment alignment;
 
-  HashMap<String, SequenceI> testSeqs = new HashMap<String, SequenceI>();
-  HashMap<String, AlignmentAnnotation> testAnnots = new HashMap<String, AlignmentAnnotation>();
-  HashMap<String, SequenceGroup> testGrps = new HashMap<String, SequenceGroup>();
+  private HashMap<String, SequenceI> expectedSeqs = new HashMap<String, SequenceI>();
 
-  @Before
+  private HashMap<String, AlignmentAnnotation> expectedAnnots = new HashMap<String, AlignmentAnnotation>();
+
+  private HashMap<String, SequenceGroup> expectedGrps = new HashMap<String, SequenceGroup>();
+
+  private ColumnSelection expectedColSel = new ColumnSelection();
+
+  private SequenceI[] expectedHiddenSeqs = new SequenceI[1];
+
+  private AlignmentI testAlignment;
+
+  private int passedCount;
+
+  private JSONFile testJsonFile;
+
+  private JSONFile jf;
+
+  @BeforeTest(alwaysRun = true)
   public void setup() throws Exception
   {
     // create and add sequences
@@ -58,6 +97,10 @@ public class JSONFileTest
     seqs[4] = new Sequence("Q7XA98_TRIPR",
             "ALYGTAVSTSFMRRQPVPMSV-ATTTTTKAFPSGF", 6, 39);
 
+    SequenceI hiddenSeq = new Sequence("FER_TOCH",
+            "FILGTMISKSFLFRKPAVTSL-KAISNVGE--ALF", 3, 34);
+    expectedHiddenSeqs[0] = hiddenSeq;
+
     // create and add sequence features
     SequenceFeature seqFeature2 = new SequenceFeature("feature_x",
             "desciption", "status", 6, 15, "Jalview");
@@ -69,17 +112,10 @@ public class JSONFileTest
     seqs[3].addSequenceFeature(seqFeature3);
     seqs[4].addSequenceFeature(seqFeature4);
 
-    // add created features to features displayed
-    FeaturesDisplayed fDis = new FeaturesDisplayed();
-    fDis.setVisible("feature_x");
-    // jsonFile.setDisplayedFeatures(fDis);
-    // jsonFile.setShowSeqFeatures(true);
-
     for (Sequence seq : seqs)
     {
       seq.setDatasetSequence(seq);
-      testSeqs.put(seq.getName(), seq);
-      // jsonFile.seqs.add(seq);
+      expectedSeqs.put(seq.getName(), seq);
     }
 
     // create and add sequence groups
@@ -93,8 +129,8 @@ public class JSONFileTest
             scheme, true, true, false, 21, 29);
     seqGrp.setShowNonconserved(false);
     seqGrp.setDescription(null);
-    // jsonFile.seqGroups.add(seqGrp);
-    testGrps.put(seqGrp.getName(), seqGrp);
+
+    expectedGrps.put(seqGrp.getName(), seqGrp);
 
     // create and add annotation
     Annotation[] annot = new Annotation[35];
@@ -136,73 +172,192 @@ public class JSONFileTest
 
     AlignmentAnnotation alignAnnot = new AlignmentAnnotation(
             "Secondary Structure", "New description", annot);
-    // jsonFile.annotations.add(alignAnnot);
-    testAnnots.put(alignAnnot.label, alignAnnot);
+    expectedAnnots.put(alignAnnot.label, alignAnnot);
 
-    // Alignment al = new Alignment(seqs);
-    TEST_SEQ_HEIGHT = testSeqs.size();
-    TEST_GRP_HEIGHT = testGrps.size();
-    TEST_ANOT_HEIGHT = testAnnots.size();
-  }
+    expectedColSel.hideColumns(32, 33);
+    expectedColSel.hideColumns(34, 34);
 
-  @After
-  public void tearDown() throws Exception
-  {
-  }
+    TEST_SEQ_HEIGHT = expectedSeqs.size();
+    TEST_GRP_HEIGHT = expectedGrps.size();
+    TEST_ANOT_HEIGHT = expectedAnnots.size();
+    TEST_CS_HEIGHT = expectedColSel.getHiddenColumns().size();
 
-  @Test
-  public void testParse()
-  {
-    String jsonFile = "examples/example.json";
-    AppletFormatAdapter rf = new AppletFormatAdapter();
-    AlignmentI al = null;
+    AlignExportSettingI exportSettings = new AlignExportSettingI()
+    {
+      @Override
+      public boolean isExportHiddenSequences()
+      {
+        return true;
+      }
+
+      @Override
+      public boolean isExportHiddenColumns()
+      {
+        return true;
+      }
+
+      @Override
+      public boolean isExportGroups()
+      {
+        return true;
+      }
+
+      @Override
+      public boolean isExportFeatures()
+      {
+        return true;
+      }
+
+      @Override
+      public boolean isExportAnnotations()
+      {
+        return true;
+      }
+
+      @Override
+      public boolean isCancelled()
+      {
+        return false;
+      }
+    };
+
+    AppletFormatAdapter formatAdapter = new AppletFormatAdapter();
     try
     {
-      al = rf.readFile(jsonFile, AppletFormatAdapter.FILE,
-              JSONFile.FILE_DESC);
+      alignment = (Alignment) formatAdapter.readFile(TEST_JSON_FILE,
+              AppletFormatAdapter.FILE, JSONFile.FILE_DESC);
+      jf = (JSONFile) formatAdapter.getAlignFile();
+
+      AlignFrame af = new AlignFrame(alignment, jf.getHiddenSequences(),
+              jf.getColumnSelection(), AlignFrame.DEFAULT_WIDTH,
+              AlignFrame.DEFAULT_HEIGHT);
+      af.getViewport().setShowSequenceFeatures(jf.isShowSeqFeatures());
+      af.changeColour(jf.getColourScheme());
+      af.getViewport().setFeaturesDisplayed(jf.getDisplayedFeatures());
+
+      formatAdapter = new AppletFormatAdapter(af.alignPanel, exportSettings);
+      String jsonOutput = formatAdapter.formatSequences(JSONFile.FILE_DESC,
+              af.alignPanel.getAlignment(), false);
+
+      formatAdapter = new AppletFormatAdapter();
+      testAlignment = formatAdapter.readFile(jsonOutput,
+              AppletFormatAdapter.PASTE, JSONFile.FILE_DESC);
+      testJsonFile = (JSONFile) formatAdapter.getAlignFile();
+      // System.out.println(jsonOutput);
     } catch (IOException e)
     {
       e.printStackTrace();
     }
-    assertNotNull("Couldn't read supplied alignment data.", al);
 
-    int passedCount = 0;
-    for (SequenceI seq : al.getSequences())
+  }
+
+  @BeforeMethod(alwaysRun = true)
+  public void methodSetup()
+  {
+    passedCount = 0;
+  }
+
+  @AfterTest
+  public void tearDown() throws Exception
+  {
+    testJsonFile = null;
+    alignment = null;
+    expectedSeqs = null;
+    expectedAnnots = null;
+    expectedGrps = null;
+    testAlignment = null;
+    jf = null;
+  }
+
+  @Test(groups = { "Functional" })
+  public void roundTripTest()
+  {
+    assertNotNull("JSON roundtrip test failed!", testJsonFile);
+  }
+
+  @Test(groups = { "Functional" })
+  public void testSeqParsed()
+  {
+    assertNotNull("Couldn't read supplied alignment data.", testAlignment);
+    Assert.assertNotNull(testAlignment.getSequences());
+    for (SequenceI seq : testAlignment.getSequences())
     {
-      SequenceI expectedSeq = testSeqs.get(seq.getName());
-      Assert.assertTrue("Failed Sequence Test  for >>> " + seq.getName(),
+      SequenceI expectedSeq = expectedSeqs.get(seq.getName());
+      AssertJUnit.assertTrue(
+              "Failed Sequence Test  for >>> " + seq.getName(),
               isSeqMatched(expectedSeq, seq));
       passedCount++;
     }
-    Assert.assertEquals("Some Sequences did not pass the test",
+    AssertJUnit.assertEquals("Some Sequences did not pass the test",
             TEST_SEQ_HEIGHT, passedCount);
+  }
 
-    passedCount = 0;
-    for (SequenceGroup seqGrp : al.getGroups())
+  @Test(groups = { "Functional" })
+  public void hiddenColsTest()
+  {
+    ColumnSelection cs = testJsonFile.getColumnSelection();
+    Assert.assertNotNull(cs);
+    Assert.assertNotNull(cs.getHiddenColumns());
+    List<int[]> hiddenCols = cs.getHiddenColumns();
+    Assert.assertEquals(hiddenCols.size(), TEST_CS_HEIGHT);
+    Assert.assertEquals(hiddenCols, expectedColSel.getHiddenColumns(),
+            "Mismatched hidden columns!");
+  }
+
+  @Test(groups = { "Functional" })
+  public void hiddenSeqsTest()
+  {
+    Assert.assertNotNull(testJsonFile.getHiddenSequences(),
+            "Hidden sequence Expected but found Null");
+    Assert.assertEquals(jf.getHiddenSequences().length, 1, "Hidden sequece");
+  }
+
+  @Test(groups = { "Functional" })
+  public void colorSchemeTest()
+  {
+    Assert.assertNotNull(testJsonFile.getColourScheme(),
+            "Colourscheme is null, parsing failed!");
+    Assert.assertTrue(
+            testJsonFile.getColourScheme() instanceof ZappoColourScheme,
+            "Zappo colour scheme expected!");
+  }
+
+  @Test(groups = { "Functional" })
+  public void isShowSeqFeaturesSet()
+  {
+    Assert.assertTrue(testJsonFile.isShowSeqFeatures(),
+            "Sequence feature isDisplayed setting expected to be true");
+  }
+
+  @Test(groups = { "Functional" })
+  public void testGrpParsed()
+  {
+    Assert.assertNotNull(testAlignment.getGroups());
+    for (SequenceGroup seqGrp : testAlignment.getGroups())
     {
-      SequenceGroup expectedGrp = testGrps.get(seqGrp.getName());
-      Assert.assertTrue(
+      SequenceGroup expectedGrp = expectedGrps.get(seqGrp.getName());
+      AssertJUnit.assertTrue(
               "Failed SequenceGroup Test for >>> " + seqGrp.getName(),
               isGroupMatched(expectedGrp, seqGrp));
       passedCount++;
     }
-    Assert.assertEquals("Some SequenceGroups did not pass the test",
+    AssertJUnit.assertEquals("Some SequenceGroups did not pass the test",
             TEST_GRP_HEIGHT, passedCount);
+  }
 
-    passedCount = 0;
-    for (AlignmentAnnotation annot : al.getAlignmentAnnotation())
+  @Test(groups = { "Functional" })
+  public void testAnnotationParsed()
+  {
+    Assert.assertNotNull(testAlignment.getAlignmentAnnotation());
+    for (AlignmentAnnotation annot : testAlignment.getAlignmentAnnotation())
     {
-      AlignmentAnnotation expectedAnnot = testAnnots.get(annot.label);
-      Assert.assertTrue("Failed AlignmentAnnotation Test for >>> "
+      AlignmentAnnotation expectedAnnot = expectedAnnots.get(annot.label);
+      AssertJUnit.assertTrue("Failed AlignmentAnnotation Test for >>> "
               + annot.label, isAnnotationMatched(expectedAnnot, annot));
       passedCount++;
     }
-    Assert.assertEquals("Some Sequences did not pass the test",
+    AssertJUnit.assertEquals("Some Sequences did not pass the test",
             TEST_ANOT_HEIGHT, passedCount);
-
-    // af = new AlignFrame(al, 700, 500);
-    // AlignViewport viewport = af.getViewport();
-    // alignPanel = new AlignmentPanel(af, viewport);
   }
 
   public boolean isAnnotationMatched(AlignmentAnnotation eAnnot,