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[][] {
146 { "--argfile=" + testfiles + "/**/*.txt", new String[]
147 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png",
148 testfiles + "/dir3/subdir/test0.png" } },
149 { "--argfile=" + testfiles + "/**/argfile.txt", new String[]
150 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
151 { "--argfile=" + testfiles + "/dir*/argfile.txt", new String[]
152 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
153 { "--initsubstitutions --append examples/uniref50.fa --image "
154 + testfiles + "/{basename}.png",
156 { testfiles + "/uniref50.png" } },
157 { "--append examples/uniref50.fa --image " + testfiles
160 { testfiles + "/{basename}.png" } }
166 @DataProvider(name = "cmdLines")
167 public Object[][] cmdLines()
169 String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
170 "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
171 String[] t1 = new String[] { "TEST1" };
172 String[] t2 = new String[] { "TEST2" };
173 String[] t3 = new String[] { "TEST3" };
174 return new Object[][] {
177 { "--append=examples/uniref50.fa", true, 1, someUniref50Seqs },
178 { "--append examples/uniref50.fa", true, 1, someUniref50Seqs },
179 { "--append=examples/uniref50*.fa", true, 1, someUniref50Seqs },
180 // NOTE we cannot use shell expansion in tests, so list all files!
181 { "--append examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
183 { "--append=[new]examples/uniref50*.fa", true, 2,
185 { "--open=examples/uniref50*.fa", true, 2, someUniref50Seqs },
186 { "examples/uniref50.fa", true, 1, someUniref50Seqs },
187 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
188 ArrayUtils.concatArrays(someUniref50Seqs, t1) },
189 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
190 { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
191 ArrayUtils.concatArrays(t1, t3) },
192 { "--argfile=" + testfiles + "/argfile*.txt", true, 5,
193 ArrayUtils.concatArrays(t1, t2, t3) },
194 { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
195 ArrayUtils.concatArrays(t1, t2) } };
199 public static boolean lookForSequenceName(String sequenceName)
201 AlignFrame[] afs = Desktop.getAlignFrames();
202 for (AlignFrame af : afs)
204 for (String name : af.getViewport().getAlignment().getSequenceNames())
206 if (sequenceName.equals(name))
215 public static void cleanupFiles(String[] filenames)
217 for (String filename : filenames)
219 File file = new File(filename);
227 @Test(groups = "Functional", dataProvider = "allLinkedIdsData")
228 public void allLinkedIdsTest(String cmdLine, Arg a, String[] filenames)
230 String[] args = cmdLine.split("\\s+");
232 Commands cmds = Jalview.getInstance().getCommands();
233 Assert.assertNotNull(cmds);
234 for (String filename : filenames)
236 Assert.assertTrue(new File(filename).exists(),
237 "File '" + filename + "' was not created");
239 cleanupFiles(filenames);
242 @DataProvider(name = "allLinkedIdsData")
243 public Object[][] allLinkedIdsData()
245 return new Object[][] {
248 { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --output={dirname}/{basename}.stk --close",
249 Arg.OUTPUT, new String[]
250 { "test/jalview/bin/argparser/testfiles/test1.stk",
251 "test/jalview/bin/argparser/testfiles/test2.stk",
252 "test/jalview/bin/argparser/testfiles/test3.stk", } },
254 { "--open=test/jalview/bin/argparser/testfiles/*.fa --substitutions --all --image={dirname}/{basename}.png --close",
255 Arg.IMAGE, new String[]
256 { "test/jalview/bin/argparser/testfiles/test1.png",
257 "test/jalview/bin/argparser/testfiles/test2.png",
258 "test/jalview/bin/argparser/testfiles/test3.png", } },