4 import java.io.IOException;
5 import java.nio.file.Files;
7 import java.util.HashSet;
10 import org.testng.Assert;
11 import org.testng.annotations.AfterClass;
12 import org.testng.annotations.AfterMethod;
13 import org.testng.annotations.BeforeClass;
14 import org.testng.annotations.DataProvider;
15 import org.testng.annotations.Test;
17 import jalview.bin.argparser.Arg;
18 import jalview.gui.AlignFrame;
19 import jalview.gui.Desktop;
20 import jalview.gui.JvOptionPane;
21 import jalview.util.ArrayUtils;
24 public class CommandsTest
26 private static final String testfiles = "test/jalview/bin/argparser/testfiles";
28 private static final String png1 = testfiles + "/dir1/test1.png";
30 private static final String png2 = testfiles + "/dir2/test1.png";
32 @BeforeClass(alwaysRun = true)
33 public static void setUpBeforeClass() throws Exception
35 Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
36 Date oneHourFromNow = new Date(
37 System.currentTimeMillis() + 3600 * 1000);
38 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
41 @AfterClass(alwaysRun = true)
42 public static void resetProps()
44 Cache.loadProperties("test/jalview/testProps.jvprops");
47 @BeforeClass(alwaysRun = true)
48 public void setUpJvOptionPane()
50 JvOptionPane.setInteractiveMode(false);
51 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
54 @AfterMethod(alwaysRun = true)
55 public void tearDown()
57 if (Desktop.instance != null)
58 Desktop.instance.closeAll_actionPerformed(null);
61 /* --setprops is currently disabled so this test won't work
62 @Test(groups = "Functional")
63 public void setpropsTest()
65 final String MOSTLY_HARMLESS = "MOSTLY_HARMLESS";
66 String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth";
67 String[] args = cmdLine.split("\\s+");
69 Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"),
74 @Test(groups = "Functional", dataProvider = "cmdLines")
75 public void commandsOpenTest(String cmdLine, boolean cmdArgs,
76 int numFrames, String[] sequences)
78 String[] args = cmdLine.split("\\s+");
80 Commands cmds = Jalview.getInstance().getCommands();
81 Assert.assertNotNull(cmds);
82 Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
83 "Commands were not provided in the args");
84 Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
85 "Overall command parse and operation is false");
87 Assert.assertEquals(Desktop.getAlignFrames().length, numFrames,
88 "Wrong number of AlignFrames");
90 if (sequences != null)
92 Set<String> openedSequenceNames = new HashSet<>();
93 AlignFrame[] afs = Desktop.getAlignFrames();
94 for (AlignFrame af : afs)
97 .addAll(af.getViewport().getAlignment().getSequenceNames());
99 for (String sequence : sequences)
101 Assert.assertTrue(openedSequenceNames.contains(sequence),
102 "Sequence '" + sequence
103 + "' was not found in opened alignment files: "
104 + cmdLine + ".\nOpened sequence names are:\n"
105 + String.join("\n", openedSequenceNames));
110 lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
113 @Test(groups = "Functional", dataProvider = "argfileOutputFiles")
114 public void argFilesGlobAndSubstitutionsTest(String cmdLine,
115 String[] filenames) throws IOException
117 cleanupFiles(filenames);
118 String[] args = cmdLine.split("\\s+");
120 Commands cmds = Jalview.getInstance().getCommands();
121 Assert.assertNotNull(cmds);
122 File lastFile = null;
123 for (String filename : filenames)
125 File file = new File(filename);
126 Assert.assertTrue(file.exists(), "File '" + filename
127 + "' was not created by '" + cmdLine + "'");
128 Assert.assertTrue(file.isFile(), "File '" + filename
129 + "' is not a file from '" + cmdLine + "'");
130 Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
131 + "' has no content from '" + cmdLine + "'");
132 // make sure the successive output files get bigger!
133 if (lastFile != null)
135 Files.size(file.toPath()) > Files.size(lastFile.toPath()));
137 cleanupFiles(filenames);
141 @DataProvider(name = "argfileOutputFiles")
142 public Object[][] argfileOutputFiles()
144 return new Object[][] {
145 { "--argfile=" + testfiles + "/**/*.txt", new String[]
146 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png",
147 testfiles + "/dir3/subdir/test0.png" } },
148 { "--argfile=" + testfiles + "/**/argfile.txt", new String[]
149 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
150 { "--argfile=" + testfiles + "/dir*/argfile.txt", new String[]
151 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
152 { "--initsubstitutions --append examples/uniref50.fa --image "
153 + testfiles + "/{basename}.png",
155 { testfiles + "/uniref50.png" } },
156 { "--append examples/uniref50.fa --image " + testfiles
159 { testfiles + "/{basename}.png" } } };
162 @DataProvider(name = "cmdLines")
163 public Object[][] cmdLines()
165 String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
166 "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
167 String[] t1 = new String[] { "TEST1" };
168 String[] t2 = new String[] { "TEST2" };
169 String[] t3 = new String[] { "TEST3" };
170 return new Object[][] {
173 { "--append=examples/uniref50.fa", true, 1, someUniref50Seqs },
174 { "--append examples/uniref50.fa", true, 1, someUniref50Seqs },
175 { "--append=examples/uniref50*.fa", true, 1, someUniref50Seqs },
176 // NOTE we cannot use shell expansion in tests, so list all files!
177 { "--append examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
179 { "--append=[new]examples/uniref50*.fa", true, 2,
181 { "--open=examples/uniref50*.fa", true, 2, someUniref50Seqs },
182 { "examples/uniref50.fa", true, 1, someUniref50Seqs },
183 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
184 ArrayUtils.concatArrays(someUniref50Seqs, t1) },
185 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
186 { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
187 ArrayUtils.concatArrays(t1, t3) },
188 { "--argfile=" + testfiles + "/argfile*.txt", true, 5,
189 ArrayUtils.concatArrays(t1, t2, t3) },
190 { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
191 ArrayUtils.concatArrays(t1, t2) } };
195 public static boolean lookForSequenceName(String sequenceName)
197 AlignFrame[] afs = Desktop.getAlignFrames();
198 for (AlignFrame af : afs)
200 for (String name : af.getViewport().getAlignment().getSequenceNames())
202 if (sequenceName.equals(name))
211 public static void cleanupFiles(String[] filenames)
213 for (String filename : filenames)
215 File file = new File(filename);
223 @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
224 public void allLinkedIdsTest(String cmdLine, Arg a, String[] filenames)
226 String[] args = cmdLine.split("\\s+");
228 Commands cmds = Jalview.getInstance().getCommands();
229 Assert.assertNotNull(cmds);
230 for (String filename : filenames)
232 Assert.assertTrue(new File(filename).exists(),
233 "File '" + filename + "' was not created");
235 cleanupFiles(filenames);
238 @DataProvider(name = "allLinkedIdsData")
239 public Object[][] allLinkedIdsData()
241 return new Object[][] {
244 { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --output={dirname}/{basename}.stk --close",
245 Arg.OUTPUT, new String[]
246 { "test/jalview/bin/argparser/testfiles/test1.stk",
247 "test/jalview/bin/argparser/testfiles/test2.stk",
248 "test/jalview/bin/argparser/testfiles/test3.stk", } },
250 { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --allframes --image={dirname}/{basename}.png --close",
251 Arg.IMAGE, new String[]
252 { "test/jalview/bin/argparser/testfiles/test1.png",
253 "test/jalview/bin/argparser/testfiles/test2.png",
254 "test/jalview/bin/argparser/testfiles/test3.png", } },