JAL-3675 2.11.1.1 java 8 compatible patch for JAL-3615 from feature/JAL-3615gzipXfam
[jalview.git] / test / jalview / io / FileIOTester.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.assertFalse;
24 import static org.testng.Assert.assertTrue;
25
26 import java.io.BufferedInputStream;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31
32 import org.testng.AssertJUnit;
33 import org.testng.annotations.AfterClass;
34 import org.testng.annotations.BeforeClass;
35 import org.testng.annotations.Test;
36
37 import jalview.bin.Cache;
38 import jalview.gui.JvOptionPane;
39
40 /**
41  * @author jimp
42  * 
43  */
44 public class FileIOTester
45 {
46
47   @BeforeClass(alwaysRun = true)
48   public void setUpJvOptionPane()
49   {
50     JvOptionPane.setInteractiveMode(false);
51     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
52   }
53
54   /**
55    * @throws java.lang.Exception
56    */
57   @BeforeClass(alwaysRun = true)
58   public static void setUpBeforeClass() throws Exception
59   {
60     Cache.initLogger();
61   }
62
63   /**
64    * @throws java.lang.Exception
65    */
66   @AfterClass(alwaysRun = true)
67   public static void tearDownAfterClass() throws Exception
68   {
69   }
70
71   // TODO: make a better/more comprehensive test harness for identify/io
72
73   final static File ALIGN_FILE = new File(
74           "test/jalview/io/test_gz_fasta.gz");
75
76   final static File NOTGZALIGN_FILE = new File(
77           "test/jalview/io/test_gz_fasta_notgz.gz");
78
79   final static File STARS_FA_FILE1 = new File(
80           "test/jalview/io/test_fasta_stars.fa");
81
82   final static File STARS_FA_FILE2 = new File(
83           "test/jalview/io/test_fasta_stars2.fa");
84
85   private void assertValidFormat(FileFormatI fmt, String src, FileParse fp)
86           throws FileFormatException
87   {
88     AssertJUnit.assertTrue("Couldn't resolve " + src + " as a valid file",
89             fp.isValid());
90     FileFormatI type = new IdentifyFile().identify(fp);
91     AssertJUnit.assertSame("Data from '" + src + "' Expected to be '" + fmt
92             + "' identified as '" + type + "'", type, fmt);
93   }
94
95   @Test(groups = { "Functional" })
96   public void testStarsInFasta1() throws IOException
97   {
98     String uri;
99     FileParse fp = new FileParse(
100             uri = STARS_FA_FILE1.getAbsoluteFile().toString(),
101             DataSourceType.FILE);
102     assertValidFormat(FileFormat.Fasta, uri, fp);
103   }
104
105   @Test(groups = { "Functional" })
106   public void testStarsInFasta2() throws IOException
107   {
108     String uri;
109     FileParse fp = new FileParse(
110             uri = STARS_FA_FILE2.getAbsoluteFile().toString(),
111             DataSourceType.FILE);
112     assertValidFormat(FileFormat.Fasta, uri, fp);
113   }
114
115   @Test(groups = { "Functional" })
116   public void testGzipIo() throws IOException
117   {
118     String uri;
119     FileParse fp = new FileParse(
120             uri = ALIGN_FILE.getAbsoluteFile().toURI().toString(),
121             DataSourceType.URL);
122     assertValidFormat(FileFormat.Fasta, uri, fp);
123   }
124
125   @Test(groups = { "Functional" })
126   public void testGziplocalFileIO() throws IOException
127   {
128     String filepath;
129     FileParse fp = new FileParse(
130             filepath = ALIGN_FILE.getAbsoluteFile().toString(),
131             DataSourceType.FILE);
132     assertValidFormat(FileFormat.Fasta, filepath, fp);
133   }
134
135   @Test(groups = { "Functional" })
136   public void testIsGzipInputStream() throws IOException
137   {
138     InputStream is = new FileInputStream(ALIGN_FILE);
139     
140     /*
141      * first try fails - FileInputStream does not support mark/reset
142      */
143     assertFalse(FileParse.isGzipStream(is));
144     
145     /*
146      * wrap in a BufferedInputStream and try again
147      */
148     is = new BufferedInputStream(is, 16);
149     assertTrue(FileParse.isGzipStream(is));
150   }
151
152   @Test(groups = { "Functional" })
153   public void testNonGzipURLIO() throws IOException
154   {
155     String uri;
156     FileParse fp = new FileParse(
157             uri = NOTGZALIGN_FILE.getAbsoluteFile().toURI().toString(),
158             DataSourceType.URL);
159     assertValidFormat(FileFormat.Fasta, uri, fp);
160   }
161
162   @Test(groups = { "Functional" })
163   public void testNonGziplocalFileIO() throws IOException
164   {
165     String filepath;
166     FileParse fp = new FileParse(
167             filepath = NOTGZALIGN_FILE.getAbsoluteFile().toString(),
168             DataSourceType.FILE);
169     assertValidFormat(FileFormat.Fasta, filepath, fp);
170   }
171 }