1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @summary Additional tests for the OutputAnalyzer utility class when
  27  * the output comes from a file.
  28  * @library /testlibrary
  29  */
  30 
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.nio.file.Files;
  34 import java.nio.file.StandardOpenOption;
  35 import com.oracle.java.testlibrary.OutputAnalyzer;
  36 
  37 public class FileOutputAnalyzerTest {
  38 
  39     public static void main(String args[]) throws Exception {
  40         new FileOutputAnalyzerTest().testBasics();
  41         new FileOutputAnalyzerTest().testNullPointerException();
  42         new FileOutputAnalyzerTest().testIOException();
  43     }
  44 
  45     public void testBasics() throws IOException {
  46         String expected =
  47             "line1" + System.lineSeparator()
  48             + "line2" + System.lineSeparator() 
  49             + "line3";
  50         File testFile = new File("test.txt");
  51         testFile.deleteOnExit();
  52         Files.write(testFile.toPath(), expected.getBytes(),
  53         StandardOpenOption.CREATE_NEW);
  54 
  55         OutputAnalyzer analyzer  = new OutputAnalyzer(testFile);
  56         String actual = analyzer.getStdout();
  57         if (!expected.equals(actual)) {
  58             throw new RuntimeException(String.format(
  59                     "The expected stdout  '%s' doesn't equal to '%s'",
  60                     expected, actual));
  61         }
  62         actual = analyzer.getStderr();
  63         if (!actual.isEmpty()) {
  64             throw new RuntimeException(String.format(
  65                     "The expected stderr should be empty, '%s'", actual));
  66         }
  67         actual = analyzer.getOutput();
  68         if (!expected.equals(actual)) {
  69             throw new RuntimeException(String.format(
  70                     "The expected output '%s' doesn't equal to '%s'",
  71                     expected, actual));
  72         }
  73     }
  74 
  75     public void testNullPointerException() throws IOException {
  76         try {
  77             new OutputAnalyzer((File) null);
  78         } catch (NullPointerException e) {
  79             return;
  80         }
  81         throw new RuntimeException("NPE is hidden by OutputAnalyzer's constructor");
  82     }
  83 
  84     public void testIOException() {
  85         try {
  86             // Try to read from a not existing file, it should raise
  87             // FileNotFoundException. Another cases are not covered
  88             // due to their complexity and small benefit.
  89             new OutputAnalyzer(new File("non-existing.file"));
  90         } catch (IOException e) {
  91             return;
  92         }
  93         throw new RuntimeException("IOException is hidden by OutputAnalyzer's constructor");
  94     }
  95 
  96 }