|
| 1 | +package com.sandwich.koans; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotSame; |
| 5 | +import static org.junit.Assert.assertSame; |
| 6 | +import static org.junit.Assert.fail; |
| 7 | + |
| 8 | +import java.io.PrintStream; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.junit.After; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +import com.sandwich.koans.CommandLineTestCase.AssertionFailedException; |
| 17 | + |
| 18 | +public class CommandLineTestCaseTest { |
| 19 | + |
| 20 | + CommandLineTestCase testCase; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp(){ |
| 24 | + testCase = new CommandLineTestCase(){}; |
| 25 | + } |
| 26 | + |
| 27 | + @After |
| 28 | + public void tearDown(){ |
| 29 | + testCase.tearDown(); // really important - will remove regular System.out if commented! |
| 30 | + testCase = null; |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void testThatConsoleIsInitializedBySetUp() throws Exception { |
| 35 | + PrintStream originalOut = System.out; |
| 36 | + testCase.setUp(); |
| 37 | + assertNotSame(originalOut, System.out); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testThatConsoleIsCleanAfterSetUp() throws Exception { |
| 42 | + testCase.setUp(); |
| 43 | + testCase.assertSystemOutLineEquals(0, ""); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testThatConsoleIsAttachedToSystem() throws Exception { |
| 48 | + testCase.setUp(); |
| 49 | + System.out.print("hello \n world!"); |
| 50 | + testCase.assertSystemOutLineEquals(0, "hello "); |
| 51 | + testCase.assertSystemOutLineEquals(1, " world!"); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testThatConsoleIsAttachedToSystemWithTrimStringArg() throws Exception { |
| 56 | + testCase.setUp(); |
| 57 | + System.out.println("hello \n world!"); |
| 58 | + testCase.assertSystemOutLineEquals(0, "hello", true); |
| 59 | + testCase.assertSystemOutLineEquals(1, "world!", true); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testThatTearDownDetachesDummiedConsoleFromSystem(){ |
| 64 | + PrintStream originalConsole = System.out; |
| 65 | + testCase.setUp(); |
| 66 | + PrintStream fakeConsole = System.out; |
| 67 | + assertNotSame(fakeConsole, originalConsole); |
| 68 | + testCase.tearDown(); |
| 69 | + assertSame(originalConsole, System.out); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testThatStubAllKoansStubsAllKoansReference() throws Exception { |
| 74 | + List<?> oldKoans = AllKoans.getKoans(); |
| 75 | + List<Class<?>> newKoans = Collections.emptyList(); |
| 76 | + testCase.stubAllKoans(newKoans); |
| 77 | + assertNotSame(oldKoans, AllKoans.getKoans()); |
| 78 | + assertSame(newKoans, AllKoans.getKoans()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void testTestCaseRestoresAllKoansReference() throws Exception { |
| 83 | + List<?> oldKoans = AllKoans.getKoans(); |
| 84 | + List<Class<?>> newKoans = Collections.emptyList(); |
| 85 | + testCase.stubAllKoans(newKoans); |
| 86 | + assertNotSame(oldKoans, AllKoans.getKoans()); |
| 87 | + assertSame(newKoans, AllKoans.getKoans()); |
| 88 | + testCase.tearDown(); |
| 89 | + // creates all new instance |
| 90 | + assertNotSame(oldKoans, AllKoans.getKoans()); |
| 91 | + assertEquals(oldKoans, AllKoans.getKoans()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testClearSysout() throws Exception { |
| 96 | + testCase.setUp(); |
| 97 | + System.out.print("!"); |
| 98 | + testCase.assertSystemOutLineEquals(0, "!"); |
| 99 | + testCase.clearSysout(); |
| 100 | + testCase.assertSystemOutLineEquals(0, ""); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testSystemOutEquals_freshStart(){ |
| 105 | + testCase.setUp(); |
| 106 | + testCase.assertSystemOutEquals(""); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testSystemOutEquals_aString() throws Exception { |
| 111 | + String helloWorld = "Hello World!"; |
| 112 | + testCase.setUp(); |
| 113 | + testCase.assertSystemOutEquals(""); |
| 114 | + System.out.print(helloWorld); |
| 115 | + testCase.assertSystemOutEquals(helloWorld); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testSystemOutContains_happyPath() throws Exception { |
| 120 | + testCase.setUp(); |
| 121 | + String zeroThruOne = "01"; |
| 122 | + System.out.print(zeroThruOne); |
| 123 | + for(char c : zeroThruOne.toCharArray()){ |
| 124 | + testCase.assertSystemOutContains(String.valueOf(c)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testSystemOutContains_failurePath() throws Exception { |
| 130 | + testCase.setUp(); |
| 131 | + String zeroThruOne = "01"; |
| 132 | + System.out.print(zeroThruOne); |
| 133 | + try{ |
| 134 | + testCase.assertSystemOutContains(String.valueOf("a")); |
| 135 | + fail(); |
| 136 | + }catch(AssertionFailedException ex){ |
| 137 | + assertEquals("expected: <a> but found: <01>", ex.getMessage()); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments