// Question posted at // https://www.facebook.com/groups/Javagroup123/permalink/10156805252566664/ import junit.framework.TestCase; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class RecursionTest extends TestCase { public void test_0_and_0() { assertOutputEquals(new String[]{ // No output at all. }, 0, 0); } public void test_0_and_10() { assertOutputEquals(new String[]{ // No output at all. }, 0, 10); } public void test_1_of_1_only() { assertOutputEquals(new String[]{ "1", }, 1, 1); } public void test_1_of_1_to_2() { assertOutputEquals(new String[]{ "1", "2", }, 1, 2); } public void test_1_of_1_to_3() { assertOutputEquals(new String[]{ "1", "2", "3", }, 1, 3); } public void test_1_of_1_to_5() { assertOutputEquals(new String[]{ "1", "2", "3", "4", "5", }, 1, 5); } public void test_2_of_1_only() { assertOutputEquals(new String[]{ "1 1", }, 2, 1); } public void test_2_of_1_to_2() { assertOutputEquals(new String[]{ "1 1", "1 2", "2 2", }, 2, 2); } public void test_2_of_1_to_3() { assertOutputEquals(new String[]{ "1 1", "1 2", "1 3", "2 2", "2 3", "3 3", }, 2, 3); } public void test_3_of_1_only() { assertOutputEquals(new String[]{ "1 1 1", }, 3, 1); } public void test_5_of_1_only() { assertOutputEquals(new String[]{ "1 1 1 1 1", }, 5, 1); } public void test_15_of_1_only() { assertOutputEquals(new String[]{ "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", }, 15, 1); } public void test_3_of_1_to_2() { assertOutputEquals(new String[]{ "1 1 1", "1 1 2", "1 2 2", "2 2 2", }, 3, 2); } public void test_3_of_1_to_3() { assertOutputEquals(new String[]{ "1 1 1", "1 1 2", "1 1 3", "1 2 2", "1 2 3", "1 3 3", "2 2 2", "2 2 3", "2 3 3", "3 3 3", }, 3, 3); } public void test_10_and_0() { assertOutputEquals(new String[]{ // No output at all. }, 10, 0); } private static final String NL = System.lineSeparator(); private static void assertOutputEquals(final String[] expectedOutputLines, final int n, final int max) { final String actualOutput = callFunctionAndReturnStandardOutput(n, max); final String trimmedActualOutput = trimTrailingSpacesFromLines(actualOutput); final String expectedOutput = buildExpectedOutputString(expectedOutputLines); assertEquals(expectedOutput, trimmedActualOutput); } private static String callFunctionAndReturnStandardOutput(final int n, final int max) { final PrintStream oldSystemOut = System.out; final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final PrintStream printStream = new PrintStream(byteArrayOutputStream); System.setOut(printStream); try { // Function call being tested: Recursion.generateAndPrintValuesWithMax(n, max); } finally { System.setOut(oldSystemOut); printStream.close(); } return byteArrayOutputStream.toString(); } private static String trimTrailingSpacesFromLines(final String actualOutput) { final StringBuilder result = new StringBuilder(); if (!"".equals(actualOutput)) { for (final String line : actualOutput.split(NL)) { result.append(line.replaceFirst(" *$", "")).append(NL); } } return result.toString(); } private static String buildExpectedOutputString(String[] expectedOutputLines) { final StringBuilder expectedOutputStringBuilder = new StringBuilder(); for (String line : expectedOutputLines) { expectedOutputStringBuilder.append(line); expectedOutputStringBuilder.append(NL); } return expectedOutputStringBuilder.toString(); } }