JAL-629 added setprop. More filename based substitutions enabled. Test for these.
[jalview.git] / test / jalview / bin / CommandsTest.java
1 package jalview.bin;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.util.Date;
7 import java.util.HashSet;
8 import java.util.Set;
9
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;
16
17 import jalview.gui.AlignFrame;
18 import jalview.gui.Desktop;
19 import jalview.gui.JvOptionPane;
20 import jalview.util.ArrayUtils;
21
22 @Test(singleThreaded = true)
23 public class CommandsTest
24 {
25   private static final String testfiles = "test/jalview/bin/argparser/testfiles";
26
27   private static final String png1 = testfiles + "/dir1/test2.png";
28
29   private static final String png2 = testfiles + "/dir2/test2.png";
30
31   @BeforeClass(alwaysRun = true)
32   public static void setUpBeforeClass() throws Exception
33   {
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);
38   }
39
40   @AfterClass(alwaysRun = true)
41   public static void resetProps()
42   {
43     Cache.loadProperties("test/jalview/testProps.jvprops");
44   }
45
46   @BeforeClass(alwaysRun = true)
47   public void setUpJvOptionPane()
48   {
49     JvOptionPane.setInteractiveMode(false);
50     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
51   }
52
53   @AfterMethod(alwaysRun = true)
54   public void tearDown()
55   {
56     if (Desktop.instance != null)
57       Desktop.instance.closeAll_actionPerformed(null);
58   }
59
60   /* --setprop currently disabled
61   @Test(groups = "Functional")
62   public void setpropsTest()
63   {
64     String MOSTLY_HARMLESS = "MOSTLY_HARMLESS";
65     String cmdLine = "--setprop=" + MOSTLY_HARMLESS + "=Earth";
66     String[] args = cmdLine.split("\\s+");
67     Jalview.main(args);
68     Assert.assertEquals(Cache.getDefault(MOSTLY_HARMLESS, "Magrathea"),
69             "Earth");
70   }
71   */
72
73   @Test(groups = "Functional", dataProvider = "cmdLines")
74   public void commandsOpenTest(String cmdLine, boolean cmdArgs,
75           int numFrames, String[] sequences)
76   {
77     String[] args = cmdLine.split("\\s+");
78     Jalview.main(args);
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");
85
86     Assert.assertEquals(Desktop.getAlignFrames().length, numFrames);
87
88     if (sequences != null)
89     {
90       Set<String> openedSequenceNames = new HashSet<>();
91       AlignFrame[] afs = Desktop.getAlignFrames();
92       for (AlignFrame af : afs)
93       {
94         openedSequenceNames
95                 .addAll(af.getViewport().getAlignment().getSequenceNames());
96       }
97       for (String sequence : sequences)
98       {
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));
104       }
105     }
106
107     Assert.assertFalse(
108             lookForSequenceName("THIS_SEQUENCE_ID_DOESN'T_EXIST"));
109   }
110
111   @Test(groups = "Functional")
112   public void argFilesGlobAndSubstitutionsTest() throws IOException
113   {
114     cleanupArgfilesImages();
115     String cmdLine = "--headless --argfile=" + testfiles
116             + "/dir*/argfile.txt";
117     String[] args = cmdLine.split("\\s+");
118     Jalview.main(args);
119     Commands cmds = Jalview.getInstance().getCommands();
120     File file1 = new File(png1);
121     File file2 = new File(png2);
122     Assert.assertTrue(file1.exists(),
123             "Did not make file " + png1 + " from argfile");
124     Assert.assertTrue(file2.exists(),
125             "Did not make file " + png2 + " from argfile");
126     long size1 = Files.size(file1.toPath());
127     long size2 = Files.size(file2.toPath());
128     Assert.assertTrue(file1.isFile() && size1 > 0);
129     Assert.assertTrue(file2.isFile() && size2 > 0);
130     Assert.assertTrue(size2 > size1); // png2 has three sequences, png1 has 2
131     cleanupArgfilesImages();
132   }
133
134   @DataProvider(name = "cmdLines")
135   public Object[][] cmdLines()
136   {
137     String[] someUniref50Seqs = new String[] { "FER_CAPAA", "FER_CAPAN",
138         "FER1_MAIZE", "FER1_SPIOL", "O80429_MAIZE" };
139     String[] t1 = new String[] { "TEST1" };
140     String[] t2 = new String[] { "TEST2" };
141     String[] t3 = new String[] { "TEST3" };
142     return new Object[][] {
143         { "--open=examples/uniref50.fa", true, 1, someUniref50Seqs },
144         { "--open examples/uniref50.fa", true, 1, someUniref50Seqs },
145         { "--open=examples/uniref50*.fa", true, 1, someUniref50Seqs },
146         // NOTE we cannot use shell expansion in tests, so list all files!
147         { "--open examples/uniref50.fa examples/uniref50_mz.fa", true, 1,
148             someUniref50Seqs },
149         { "--open=[new]examples/uniref50*.fa", true, 2, someUniref50Seqs },
150         { "--opennew=examples/uniref50*.fa", true, 2, someUniref50Seqs },
151         { "examples/uniref50.fa", true, 1, someUniref50Seqs },
152         { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2,
153             ArrayUtils.concatArrays(someUniref50Seqs, t1) },
154         { "examples/uniref50.fa " + testfiles + "/test1.fa", true, 2, t1 },
155         { "--argfile=" + testfiles + "/argfile0.txt", true, 1,
156             ArrayUtils.concatArrays(t1, t3) },
157         { "--argfile=" + testfiles + "/argfile*.txt", true, 4,
158             ArrayUtils.concatArrays(t1, t2, t3) },
159         { "--argfile=" + testfiles + "/argfile.autocounter", true, 3,
160             ArrayUtils.concatArrays(t1, t2) } };
161   }
162
163   public static boolean lookForSequenceName(String sequenceName)
164   {
165     AlignFrame[] afs = Desktop.getAlignFrames();
166     for (AlignFrame af : afs)
167     {
168       for (String name : af.getViewport().getAlignment().getSequenceNames())
169       {
170         if (sequenceName.equals(name))
171         {
172           return true;
173         }
174       }
175     }
176     return false;
177   }
178
179   public static void cleanupArgfilesImages()
180   {
181     File png1File = new File(png1);
182     File png2File = new File(png2);
183     if (png1File.exists())
184     {
185       png1File.delete();
186     }
187     if (png2File.exists())
188     {
189       png2File.delete();
190     }
191   }
192
193 }