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.gui.AlignFrame;
18 import jalview.gui.Desktop;
19 import jalview.gui.JvOptionPane;
20 import jalview.util.ArrayUtils;
23 public class CommandsTest
25 private static final String testfiles = "test/jalview/bin/argparser/testfiles";
27 private static final String png1 = testfiles + "/dir1/test1.png";
29 private static final String png2 = testfiles + "/dir2/test1.png";
31 @BeforeClass(alwaysRun = true)
32 public static void setUpBeforeClass() throws Exception
34 Cache.loadProperties("test/jalview/gui/quitProps.jvprops");
35 Date oneHourFromNow = new Date(
36 System.currentTimeMillis() + 3600 * 1000);
37 Cache.setDateProperty("JALVIEW_NEWS_RSS_LASTMODIFIED", oneHourFromNow);
40 @AfterClass(alwaysRun = true)
41 public static void resetProps()
43 Cache.loadProperties("test/jalview/testProps.jvprops");
46 @BeforeClass(alwaysRun = true)
47 public void setUpJvOptionPane()
49 JvOptionPane.setInteractiveMode(false);
50 JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
53 @AfterMethod(alwaysRun = true)
54 public void tearDown()
56 if (Desktop.instance != null)
57 Desktop.instance.closeAll_actionPerformed(null);
60 /* --setprop currently disabled
61 @Test(groups = "Functional")
62 public void setpropsTest()
64 String MOSTLY_HARMLESS = "MOSTLY_HARMLESS";
65 String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth";
66 String[] args = cmdLine.split("\\s+");
68 Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"),
73 @Test(groups = "Functional", dataProvider = "cmdLines")
74 public void commandsOpenTest(String cmdLine, boolean cmdArgs,
75 int numFrames, String[] sequences)
77 String[] args = cmdLine.split("\\s+");
79 Commands cmds = Jalview.getInstance().getCommands();
80 Assert.assertNotNull(cmds);
81 Assert.assertEquals(cmds.commandArgsProvided(), cmdArgs,
82 "Commands were not provided in the args");
83 Assert.assertEquals(cmds.argsWereParsed(), cmdArgs,
84 "Overall command parse and operation is false");
86 Assert.assertEquals(Desktop.getAlignFrames().length, numFrames);
88 if (sequences != null)
90 Set<String> openedSequenceNames = new HashSet<>();
91 AlignFrame[] afs = Desktop.getAlignFrames();
92 for (AlignFrame af : afs)
95 .addAll(af.getViewport().getAlignment().getSequenceNames());
97 for (String sequence : sequences)
99 Assert.assertTrue(openedSequenceNames.contains(sequence),
100 "Sequence '" + sequence
101 + "' was not found in opened alignment files: "
102 + cmdLine + ".\nOpened sequence names are:\n"
103 + String.join("\n", openedSequenceNames));
108 lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
111 @Test(groups = "Functional", dataProvider = "argfileOutputFiles")
112 public void argFilesGlobAndSubstitutionsTest(String cmdLine,
113 String[] filenames) throws IOException
115 cleanupFiles(filenames);
116 String[] args = cmdLine.split("\\s+");
118 Commands cmds = Jalview.getInstance().getCommands();
119 Assert.assertNotNull(cmds);
120 File lastFile = null;
121 for (String filename : filenames)
123 File file = new File(filename);
124 Assert.assertTrue(file.exists(), "File '" + filename
125 + "' was not created by '" + cmdLine + "'");
126 Assert.assertTrue(file.isFile(), "File '" + filename
127 + "' is not a file from '" + cmdLine + "'");
128 Assert.assertTrue(Files.size(file.toPath()) > 0, "File '" + filename
129 + "' has no content from '" + cmdLine + "'");
130 // make sure the successive output files get bigger!
131 if (lastFile != null)
133 Files.size(file.toPath()) > Files.size(lastFile.toPath()));
135 cleanupFiles(filenames);
139 @DataProvider(name = "argfileOutputFiles")
140 public Object[][] argfileOutputFiles()
142 return new Object[][] {
143 { "--argfile=" + testfiles + "/**/*.txt", new String[]
144 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png",
145 testfiles + "/dir3/subdir/test0.png" } },
146 { "--argfile=" + testfiles + "/**/argfile.txt", new String[]
147 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
148 { "--argfile=" + testfiles + "/dir*/argfile.txt", new String[]
149 { testfiles + "/dir1/test1.png", testfiles + "/dir2/test1.png" } },
150 { "--initsubstitutions --open examples/uniref50.fa --image "
151 + testfiles + "/{basename}.png",
153 { testfiles + "/uniref50.png" } },
154 { "--open examples/uniref50.fa --image " + testfiles
157 { testfiles + "/{basename}.png" } } };
160 @DataProvider(name = "cmdLines")
161 public Object[][] cmdLines()
163 String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
164 "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
165 String[] t1 = new String[] { "TEST1" };
166 String[] t2 = new String[] { "TEST2" };
167 String[] t3 = new String[] { "TEST3" };
168 return new Object[][] {
169 { "--open=examples/uniref50.fa", true, 1, someUniref50Seqs },
170 { "--open examples/uniref50.fa", true, 1, someUniref50Seqs },
171 { "--open=examples/uniref50*.fa", true, 1, someUniref50Seqs },
172 // NOTE we cannot use shell expansion in tests, so list all files!
173 { "--open examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
175 { "--open=[new]examples/uniref50*.fa", true, 2, someUniref50Seqs },
176 { "--opennew=examples/uniref50*.fa", true, 2, someUniref50Seqs },
177 { "examples/uniref50.fa", true, 1, someUniref50Seqs },
178 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
179 ArrayUtils.concatArrays(someUniref50Seqs, t1) },
180 { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
181 { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
182 ArrayUtils.concatArrays(t1, t3) },
183 { "--argfile=" + testfiles + "/argfile*.txt", true, 4,
184 ArrayUtils.concatArrays(t1, t2, t3) },
185 { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
186 ArrayUtils.concatArrays(t1, t2) } };
189 public static boolean lookForSequenceName(String sequenceName)
191 AlignFrame[] afs = Desktop.getAlignFrames();
192 for (AlignFrame af : afs)
194 for (String name : af.getViewport().getAlignment().getSequenceNames())
196 if (sequenceName.equals(name))
205 public static void cleanupFiles(String[] filenames)
207 for (String filename : filenames)
209 File file = new File(filename);