package jalview.bin; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.Date; import java.util.HashSet; import java.util.Set; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import jalview.gui.AlignFrame; import jalview.gui.Desktop; import jalview.gui.JvOptionPane; import jalview.util.ArrayUtils; @Test(singleThreaded = true) public class CommandsTest { private static final String testfiles = "test/jalview/bin/argparser/testfiles"; private static final String png1 = testfiles + "/dir1/test1.png"; private static final String png2 = testfiles + "/dir2/test1.png"; @BeforeClass(alwaysRun = true) public static void setUpBeforeClass() throws Exception { Cache.loadProperties("test/jalview/gui/quitProps.jvprops"); Date oneHourFromNow = new Date( System.currentTimeMillis() + 3600 * 1000); Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow); } @AfterClass(alwaysRun = true) public static void resetProps() { Cache.loadProperties("test/jalview/testProps.jvprops"); } @BeforeClass(alwaysRun = true) public void setUpJvOptionPane() { JvOptionPane.setInteractiveMode(false); JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION); } @AfterMethod(alwaysRun = true) public void tearDown() { if (Desktop.instance != null) Desktop.instance.closeAll_actionPerformed(null); } /* --setprop currently disabled @Test(groups = "Functional") public void setpropsTest() { String MOSTLY_HARMLESS = "MOSTLY_HARMLESS"; String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth"; String[] args = cmdLine.split("\\s+"); Jalview.main(args); Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"), "Earth"); } */ @Test(groups = "Functional", dataProvider = "cmdLines") public void commandsOpenTest(String cmdLine, boolean cmdArgs, int numFrames, String[] sequences) { String[] args = cmdLine.split("\\s+"); Jalview.main(args); Commands cmds = Jalview.getInstance().getCommands(); Assert.assertNotNull(cmds); Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs, "Commands were not provided in the args"); Assert.assertEquals(cmds.argsWereParsed(), cmdArgs, "Overall command parse and operation is false"); Assert.assertEquals(Desktop.getAlignFrames().length, numFrames); if (sequences != null) { Set openedSequenceNames = new HashSet<>(); AlignFrame[] afs = Desktop.getAlignFrames(); for (AlignFrame af : afs) { openedSequenceNames .addAll(af.getViewport().getAlignment().getSequenceNames()); } for (String sequence : sequences) { Assert.assertTrue(openedSequenceNames.contains(sequence), "Sequence '" + sequence + "' was not found in opened alignment files: " + cmdLine + ".\nOpened sequence names are:\n" + String.join("\n", openedSequenceNames)); } } Assert.assertFalse( lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST")); } @Test(groups = "Functional") public void argFilesGlobAndSubstitutionsTest() throws IOException { cleanupArgfilesImages(); String cmdLine = "--argfile=" + testfiles + "/dir*/argfile.txt"; String[] args = cmdLine.split("\\s+"); Jalview.main(args); Commands cmds = Jalview.getInstance().getCommands(); File file1 = new File(png1); File file2 = new File(png2); Assert.assertTrue(file1.exists(), "Did not make file " + png1 + " from argfile"); Assert.assertTrue(file2.exists(), "Did not make file " + png2 + " from argfile"); long size1 = Files.size(file1.toPath()); long size2 = Files.size(file2.toPath()); Assert.assertTrue(file1.isFile() && size1 > 0); Assert.assertTrue(file2.isFile() && size2 > 0); Assert.assertTrue(size2 > size1); // png2 has three sequences, png1 has 2 cleanupArgfilesImages(); } @DataProvider(name = "cmdLines") public Object[][] cmdLines() { String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN", "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" }; String[] t1 = new String[] { "TEST1" }; String[] t2 = new String[] { "TEST2" }; String[] t3 = new String[] { "TEST3" }; return new Object[][] { { "--open=examples/uniref50.fa", true, 1, someUniref50Seqs }, { "--open examples/uniref50.fa", true, 1, someUniref50Seqs }, { "--open=examples/uniref50*.fa", true, 1, someUniref50Seqs }, // NOTE we cannot use shell expansion in tests, so list all files! { "--open examples/uniref50.fa examples/uniref50_mz.fa", true, 1, someUniref50Seqs }, { "--open=[new]examples/uniref50*.fa", true, 2, someUniref50Seqs }, { "--opennew=examples/uniref50*.fa", true, 2, someUniref50Seqs }, { "examples/uniref50.fa", true, 1, someUniref50Seqs }, { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, ArrayUtils.concatArrays(someUniref50Seqs, t1) }, { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 }, { "--argfile=" + testfiles + "/argfile0.txt", true, 1, ArrayUtils.concatArrays(t1, t3) }, { "--argfile=" + testfiles + "/argfile*.txt", true, 4, ArrayUtils.concatArrays(t1, t2, t3) }, { "--argfile=" + testfiles + "/argfile.autocounter", true, 3, ArrayUtils.concatArrays(t1, t2) } }; } public static boolean lookForSequenceName(String sequenceName) { AlignFrame[] afs = Desktop.getAlignFrames(); for (AlignFrame af : afs) { for (String name : af.getViewport().getAlignment().getSequenceNames()) { if (sequenceName.equals(name)) { return true; } } } return false; } public static void cleanupArgfilesImages() { File png1File = new File(png1); File png2File = new File(png2); if (png1File.exists()) { png1File.delete(); } if (png2File.exists()) { png2File.delete(); } } }