JAL-629 more tests for stdout
[jalview.git] / test / jalview / bin / CommandLineOperationsNG.java
index 4d05c4e..ef2f78a 100644 (file)
@@ -27,6 +27,7 @@ import java.io.BufferedReader;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
@@ -39,6 +40,8 @@ import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
 import io.github.classgraph.ClassGraph;
+import io.github.classgraph.ModuleRef;
+import io.github.classgraph.ScanResult;
 import jalview.gui.JvOptionPane;
 
 public class CommandLineOperationsNG
@@ -128,41 +131,38 @@ public class CommandLineOperationsNG
 
   public synchronized static String getClassPath()
   {
-    // if (scanner == null)
-    // {
-    // scanner = new ClassGraph();
-    // ScanResult scan = scanner.scan();
-    // classpath = scan.getClasspath();
-    modules = "";
-    // for (ModuleRef mr : scan.getModules())
-    // {
-    // modules.concat(mr.getName());
-    // }
-    java_exe = System.getProperty("java.home") + File.separator + "bin"
-            + File.separator + "java";
-
-    // }
-
-    // while (classpath == null)
-    // {
-    // try
-    // {
-    // Thread.sleep(10);
-    // } catch (InterruptedException x)
-    // {
-    //
-    // }
-    // }
+    if (scanner == null)
+    {
+      scanner = new ClassGraph();
+      ScanResult scan = scanner.scan();
+      classpath = scan.getClasspath();
+      modules = "";
+      for (ModuleRef mr : scan.getModules())
+      {
+        modules.concat(mr.getName());
+      }
+      java_exe = System.getProperty("java.home") + File.separator + "bin"
+              + File.separator + "java";
+
+    }
+
+    while (classpath == null)
+    {
+      try
+      {
+        Thread.sleep(10);
+      } catch (InterruptedException x)
+      {
+
+      }
+    }
     return classpath;
   }
 
   private Worker getJalviewDesktopRunner(boolean withAwt, String cmd,
           int timeout)
   {
-    // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
-    // not expanded by the shell
-    // String classpath = getClassPath();
-    getClassPath();
+    /*
     boolean win = System.getProperty("os.name").indexOf("Win") >= 0;
     String pwd = "";
     try
@@ -178,6 +178,11 @@ public class CommandLineOperationsNG
     String[] classpaths = new String[] { pwd + "/bin/main",
         pwd + "/j11lib/*", pwd + "/resources", pwd + "/help" };
     String classpath = String.join(win ? ";" : ":", classpaths);
+    getClassPath();
+    */
+    // Note: JAL-3065 - don't include quotes for lib/* because the arguments are
+    // not expanded by the shell
+    String classpath = getClassPath();
     String _cmd = java_exe + " "
             + (withAwt ? "-Djava.awt.headless=true" : "") + " -classpath "
             + classpath
@@ -217,7 +222,7 @@ public class CommandLineOperationsNG
     return worker;
   }
 
-  @Test(groups = { "Functional" })
+  @Test(groups = { "Functional", "testTask1" })
   public void reportCurrentWorkingDirectory()
   {
     try
@@ -322,18 +327,20 @@ public class CommandLineOperationsNG
     }
   }
 
-  @Test(groups = { "Functional" }, dataProvider = "allInputOperationsData")
+  @Test(
+    groups =
+    { "Functional", "testTask1" },
+    dataProvider = "allInputOperationsData")
   public void testAllInputOperations(String expectedString,
           String failureMsg)
   {
-    if ("[TESTOUTPUT] arg --nousagestats was set".equals(expectedString))
-      Assert.assertTrue(successfulCMDs.contains(expectedString),
-              failureMsg);
+    Assert.assertTrue(successfulCMDs.contains(expectedString),
+            failureMsg + "; was expecting '" + expectedString + "'");
   }
 
   @Test(
     groups =
-    { "Functional" },
+    { "Functional", "testTask1" },
     dataProvider = "headlessModeOutputOperationsData")
   public void testHeadlessModeOutputOperations(String harg, String type,
           String fileName, boolean withAWT, int expectedMinFileSize,
@@ -345,7 +352,8 @@ public class CommandLineOperationsNG
     file.deleteOnExit();
     Worker worker = getJalviewDesktopRunner(withAWT, cmd, timeout);
     assertNotNull(worker, "worker is null");
-    String msg = "Didn't create an output" + type + " file.[" + cmd + "]";
+    String msg = "Didn't create an output" + type + " file '" + fileName
+            + "'. [" + cmd + "]";
     assertTrue(file.exists(), msg);
     FileAssert.assertFile(file, msg);
     FileAssert.assertMinLength(file, expectedMinFileSize);
@@ -361,6 +369,42 @@ public class CommandLineOperationsNG
     file.delete();
   }
 
+  @Test(
+    groups =
+    { "Functional", "testTask1" },
+    dataProvider = "headlessModeOutputToStdout")
+  public void testHeadlessModeOutputToStdout(String args,
+          String comparisonFile, int timeout)
+  {
+    String cmd = args;
+    File file = new File(comparisonFile);
+    Worker worker = getJalviewDesktopRunner(true, cmd, timeout);
+    int b = -1;
+    StringBuilder sb = new StringBuilder();
+    try
+    {
+      while ((b = worker.getOutputReader().read()) != -1)
+      {
+        sb.append(Character.toChars(b));
+      }
+    } catch (IOException e)
+    {
+      Assert.fail("IOException whilst trying to read from jalview process");
+    }
+
+    String comparisonContent = null;
+    try
+    {
+      comparisonContent = new String(Files.readAllBytes(file.toPath()));
+    } catch (IOException e)
+    {
+      Assert.fail("IOException whilst trying to read comparison file");
+    }
+
+    Assert.assertEquals(sb.toString(), comparisonContent,
+            "STDOUT from jalview command did not match the comparison file");
+  }
+
   @DataProvider(name = "allInputOperationsData")
   public Object[][] getHeadlessModeInputParams()
   {
@@ -452,4 +496,26 @@ public class CommandLineOperationsNG
         //
     };
   }
+
+  @DataProvider(name = "headlessModeOutputToStdout")
+  public static Object[][] getHeadlessModeOutputToStdout()
+  {
+    // JBPNote: I'm not clear why need to specify full path for output file
+    // when running tests on build server, but we will keep this patch for now
+    // since it works.
+    // https://issues.jalview.org/browse/JAL-1889?focusedCommentId=21609&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-21609
+    String workingDir = "test/jalview/bin/";
+    return new Object[][] {
+        //
+        { "--open=examples/uniref50.fa --output=-",
+            workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
+        { "--open examples/uniref50.fa --output -",
+            workingDir + "/uniref50-output.fa", TEST_TIMEOUT },
+        { "--open examples/uniref50.fa --output=[format=blc]-",
+            workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
+        { "--open examples/uniref50.fa --output - --format blc",
+            workingDir + "/uniref50-output.blc", TEST_TIMEOUT },
+        //
+    };
+  }
 }