Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / test / jalview / io / FileFormatsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.io;
22
23 import static org.testng.Assert.assertEquals;
24 import static org.testng.Assert.assertFalse;
25 import static org.testng.Assert.assertNotEquals;
26 import static org.testng.Assert.assertNull;
27 import static org.testng.Assert.assertSame;
28 import static org.testng.Assert.assertTrue;
29
30 import java.util.Iterator;
31
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeMethod;
34 import org.testng.annotations.Test;
35
36 public class FileFormatsTest
37 {
38   @AfterMethod(alwaysRun = true)
39   public void tearDown()
40   {
41     FileFormats.getInstance().reset();
42   }
43
44   @BeforeMethod(alwaysRun = true)
45   public void setUp()
46   {
47     FileFormats.getInstance().reset();
48   }
49
50   @Test(groups = "Functional")
51   public void testIsIdentifiable()
52   {
53     FileFormats formats = FileFormats.getInstance();
54     assertTrue(formats.isIdentifiable(formats.forName(FileFormat.Fasta
55             .getName())));
56     assertTrue(formats.isIdentifiable(formats.forName(FileFormat.MMCif
57             .getName())));
58     assertTrue(formats.isIdentifiable(formats.forName(FileFormat.Jnet
59             .getName())));
60     assertTrue(formats.isIdentifiable(formats.forName(FileFormat.Jalview
61             .getName())));
62     assertFalse(formats.isIdentifiable(null));
63
64     /*
65      * remove and re-add a format: it is still 'identifiable'
66      */
67     formats.deregisterFileFormat(FileFormat.Fasta.getName());
68     assertNull(formats.forName(FileFormat.Fasta.getName()));
69     formats.registerFileFormat(FileFormat.Fasta);
70     assertSame(FileFormat.Fasta,
71             formats.forName(FileFormat.Fasta.getName()));
72     assertTrue(formats.isIdentifiable(FileFormat.Fasta));
73   }
74
75   @Test(groups = "Functional")
76   public void testGetReadableFormats()
77   {
78     String expected = "[Fasta, PFAM, Stockholm, PIR, BLC, AMSA, HTML, RNAML, JSON, PileUp, MSF, Clustal, PHYLIP, GenBank Flatfile, ENA Flatfile, GFF or Jalview features, PDB, mmCIF, Jalview]";
79     FileFormats formats = FileFormats.getInstance();
80     assertEquals(formats.getReadableFormats().toString(), expected);
81   }
82
83   @Test(groups = "Functional")
84   public void testGetWritableFormats()
85   {
86     String expected = "[Fasta, PFAM, Stockholm, PIR, BLC, AMSA, JSON, PileUp, MSF, Clustal, PHYLIP, HMMER3]";
87     FileFormats formats = FileFormats.getInstance();
88     assertEquals(formats.getWritableFormats(true).toString(), expected);
89     expected = "[Fasta, PFAM, Stockholm, PIR, BLC, AMSA, JSON, PileUp, MSF, Clustal, PHYLIP, Jalview, HMMER3]";
90     assertEquals(formats.getWritableFormats(false).toString(), expected);
91   }
92
93   @Test(groups = "Functional")
94   public void testDeregisterFileFormat()
95   {
96     String writable = "[Fasta, PFAM, Stockholm, PIR, BLC, AMSA, JSON, PileUp, MSF, Clustal, PHYLIP, HMMER3]";
97     String readable = "[Fasta, PFAM, Stockholm, PIR, BLC, AMSA, HTML, RNAML, JSON, PileUp, MSF, Clustal, PHYLIP, GenBank Flatfile, ENA Flatfile, GFF or Jalview features, PDB, mmCIF, Jalview, HMMER3, BSML]";
98     FileFormats formats = FileFormats.getInstance();
99     System.out.println(formats.getReadableFormats().toString());
100     assertEquals(formats.getWritableFormats(true).toString(), writable);
101     assertEquals(formats.getReadableFormats().toString(), readable);
102
103     formats.deregisterFileFormat(FileFormat.Fasta.getName());
104     writable = "[PFAM, Stockholm, PIR, BLC, AMSA, JSON, PileUp, MSF, Clustal, PHYLIP, HMMER3]";
105     readable = "[PFAM, Stockholm, PIR, BLC, AMSA, HTML, RNAML, JSON, PileUp, MSF, Clustal, PHYLIP, GenBank Flatfile, ENA Flatfile, GFF or Jalview features, PDB, mmCIF, Jalview, HMMER3, BSML]";
106     assertEquals(formats.getWritableFormats(true).toString(), writable);
107     assertEquals(formats.getReadableFormats().toString(), readable);
108
109     /*
110      * re-register the format: it gets added to the end of the list
111      */
112     formats.registerFileFormat(FileFormat.Fasta);
113     writable = "[PFAM, Stockholm, PIR, BLC, AMSA, JSON, PileUp, MSF, Clustal, PHYLIP, HMMER3, Fasta]";
114     readable = "[PFAM, Stockholm, PIR, BLC, AMSA, HTML, RNAML, JSON, PileUp, MSF, Clustal, PHYLIP, GenBank Flatfile, ENA Flatfile, GFF or Jalview features, PDB, mmCIF, Jalview, HMMER3, BSML, Fasta]";
115     assertEquals(formats.getWritableFormats(true).toString(), writable);
116     assertEquals(formats.getReadableFormats().toString(), readable);
117   }
118
119   @Test(groups = "Functional")
120   public void testForName()
121   {
122     FileFormats formats = FileFormats.getInstance();
123     for (FileFormatI ff : FileFormat.values())
124     {
125       assertSame(ff, formats.forName(ff.getName()));
126       assertSame(ff,
127               formats.forName(ff.getName().toUpperCase(Locale.ROOT)));
128       assertSame(ff,
129               formats.forName(ff.getName().toLowerCase(Locale.ROOT)));
130     }
131     assertNull(formats.forName(null));
132     assertNull(formats.forName("rubbish"));
133   }
134
135   @Test(groups = "Functional")
136   public void testRegisterFileFormat()
137   {
138     FileFormats formats = FileFormats.getInstance();
139     assertSame(FileFormat.MMCif,
140             formats.forName(FileFormat.MMCif.getName()));
141     assertTrue(formats.isIdentifiable(FileFormat.MMCif));
142
143     /*
144      * deregister mmCIF format
145      */
146     formats.deregisterFileFormat(FileFormat.MMCif.getName());
147     assertNull(formats.forName(FileFormat.MMCif.getName()));
148
149     /*
150      * re-register mmCIF format
151      * it is reinstated (still 'identifiable')
152      */
153     formats.registerFileFormat(FileFormat.MMCif);
154     assertSame(FileFormat.MMCif,
155             formats.forName(FileFormat.MMCif.getName()));
156     assertTrue(formats.isIdentifiable(FileFormat.MMCif));
157     // repeating does nothing
158     formats.registerFileFormat(FileFormat.MMCif);
159     assertSame(FileFormat.MMCif,
160             formats.forName(FileFormat.MMCif.getName()));
161   }
162
163   @Test(groups = "Functional")
164   public void testGetFormats()
165   {
166     /*
167      * verify the list of file formats registered matches the enum values
168      */
169     FileFormats instance = FileFormats.getInstance();
170     Iterator<FileFormatI> formats = instance.getFormats()
171             .iterator();
172     FileFormatI[] builtIn = FileFormat.values();
173
174     for (FileFormatI ff : builtIn)
175     {
176       assertSame(ff, formats.next());
177     }
178     assertFalse(formats.hasNext());
179
180     /*
181      * remove the first format, check it is no longer in 
182      * the list of formats
183      */
184     String firstFormatName = instance.getFormats().iterator().next()
185             .getName();
186     instance.deregisterFileFormat(firstFormatName);
187     assertNotEquals(instance.getFormats().iterator().next().getName(),
188             firstFormatName);
189   }
190 }