/* * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @summary Additional tests for the OutputAnalyzer utility class when * the output comes from a file. * @library /testlibrary */ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardOpenOption; import com.oracle.java.testlibrary.OutputAnalyzer; public class FileOutputAnalyzerTest { public static void main(String args[]) throws Exception { new FileOutputAnalyzerTest().testBasics(); new FileOutputAnalyzerTest().testNullPointerException(); new FileOutputAnalyzerTest().testIOException(); } public void testBasics() throws IOException { String expected = "line1" + System.lineSeparator() + "line2" + System.lineSeparator() + "line3"; File testFile = new File("test.txt"); testFile.deleteOnExit(); Files.write(testFile.toPath(), expected.getBytes(), StandardOpenOption.CREATE_NEW); OutputAnalyzer analyzer = new OutputAnalyzer(testFile); String actual = analyzer.getStdout(); if (!expected.equals(actual)) { throw new RuntimeException(String.format( "The expected stdout '%s' doesn't equal to '%s'", expected, actual)); } actual = analyzer.getStderr(); if (!actual.isEmpty()) { throw new RuntimeException(String.format( "The expected stderr should be empty, '%s'", actual)); } actual = analyzer.getOutput(); if (!expected.equals(actual)) { throw new RuntimeException(String.format( "The expected output '%s' doesn't equal to '%s'", expected, actual)); } } public void testNullPointerException() throws IOException { try { new OutputAnalyzer((File) null); } catch (NullPointerException e) { return; } throw new RuntimeException("NPE is hidden by OutputAnalyzer's constructor"); } public void testIOException() { try { // Try to read from a not existing file, it should raise // FileNotFoundException. Another cases are not covered // due to their complexity and small benefit. new OutputAnalyzer(new File("non-existing.file")); } catch (IOException e) { return; } throw new RuntimeException("IOException is hidden by OutputAnalyzer's constructor"); } }