| 1 | /* |
| 2 | * Copyright (C) 2008 Steve Ratcliffe |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * |
| 14 | * Author: Steve Ratcliffe |
| 15 | * Create date: 11-Jan-2009 |
| 16 | */ |
| 17 | package func.lib; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | |
| 21 | import java.util.Arrays; |
| 22 | import java.util.stream.Collectors; |
| 23 | |
| 24 | /** |
| 25 | * Standard output and error as produced during a run. |
| 26 | * |
| 27 | * @author Steve Ratcliffe |
| 28 | */ |
| 29 | public class Outputs { |
| 30 | private final String out; |
| 31 | private final String err; |
| 32 | |
| 33 | public Outputs(String out, String err) { |
| 34 | this.out = out; |
| 35 | this.err = err; |
| 36 | } |
| 37 | |
| 38 | protected String getOut() { |
| 39 | return out; |
| 40 | } |
| 41 | |
| 42 | protected String getErr() { |
| 43 | // filter possible lines added by set JRE option _JAVA_OPTIONS=... which cases |
| 44 | // a line "Picked up _JAVA_OPTIONS:" in stderr |
| 45 | return Arrays.stream(err.split("\n")) |
| 46 | .filter(line -> !line.contains("_JAVA_OPTIONS")) |
| 47 | .collect(Collectors.joining("\n")); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Check that the standard error is empty. |
| 52 | */ |
| 53 | public void checkNoError() { |
| 54 | assertEquals("no error output", "", getErr()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check that the output contains the given strings. You can specify |
| 59 | * any number of strings. |
| 60 | * @param strings The list of strings to check. |
| 61 | */ |
| 62 | public void checkOutput(String... strings) { |
| 63 | String out = getOut(); |
| 64 | for (String s : strings) { |
| 65 | if (!out.contains(s)) { |
| 66 | // Test has failed. Construct an assertion that will print |
| 67 | // something that is useful to show the problem. |
| 68 | assertEquals("contains '" + s + "'", |
| 69 | "..." + s + "...", |
| 70 | out); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check that the output contains the given strings. You can specify |
| 77 | * any number of strings. |
| 78 | * @param strings The list of strings to check. |
| 79 | */ |
| 80 | public void checkError(String... strings) { |
| 81 | String err = getErr(); |
| 82 | for (String s : strings) { |
| 83 | if (!err.contains(s)) { |
| 84 | // Test has failed. Construct an assertion that will print |
| 85 | // something that is useful to show the problem. |
| 86 | assertEquals("contains '" + s + "'", |
| 87 | "..." + s + "...", |
| 88 | err); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |