Skip to content

Commit 68f7ad5

Browse files
committed
tests
0 parents  commit 68f7ad5

8 files changed

Lines changed: 1244 additions & 0 deletions

‎lib/cglib-nodep-2.2.jar‎

315 KB
Binary file not shown.

‎lib/easymock-3.0.jar‎

109 KB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.sandwich.koans;
2+
import static org.junit.Assert.assertEquals;
3+
4+
import java.io.ByteArrayOutputStream;
5+
import java.io.PrintStream;
6+
import java.util.List;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
11+
import com.sandwich.koans.TestUtils.ArgRunner;
12+
13+
public abstract class CommandLineTestCase {
14+
15+
private PrintStream console;
16+
private ByteArrayOutputStream bytes;
17+
18+
@Before
19+
public void setUp() {
20+
bytes = new ByteArrayOutputStream();
21+
console = System.out;
22+
AllKoans.koans = AllKoans.createKoansList();
23+
System.setOut(new PrintStream(bytes));
24+
}
25+
26+
@After
27+
public void tearDown() {
28+
AllKoans.koans = AllKoans.createKoansList();
29+
System.setOut(console);
30+
}
31+
32+
protected List<Class<?>> stubAllKoans(List<Class<?>> koans){
33+
List<Class<?>> oldKoans = AllKoans.getKoans();
34+
AllKoans.koans = koans;
35+
return oldKoans;
36+
}
37+
38+
protected void clearSysout(){
39+
bytes = new ByteArrayOutputStream();
40+
System.setOut(new PrintStream(bytes));
41+
}
42+
43+
protected void assertSystemOutEquals(String expectation){
44+
expectation = expectation == null ? "" : expectation;
45+
if(!expectation.equals(bytes.toString())){
46+
throw new AssertionFailedException(expectation, bytes.toString());
47+
}
48+
}
49+
50+
protected void assertSystemOutContains(String expectation){
51+
if(!bytes.toString().contains(expectation)){
52+
throw new AssertionFailedException(expectation, bytes.toString());
53+
}
54+
}
55+
56+
protected void assertSystemOutLineEquals(final int lineNumber, final String lineText){
57+
assertSystemOutLineEquals(lineNumber, lineText, false);
58+
}
59+
60+
protected void assertSystemOutLineEquals(final int lineNumber, final String lineText,
61+
final boolean trimLinesString) {
62+
final int[] onLine = new int[]{0};
63+
final boolean[] found = new boolean[]{false};
64+
TestUtils.forEachLine(bytes.toString(), new ArgRunner<String>(){
65+
public void run(String s){
66+
if(onLine[0] == lineNumber){
67+
if(trimLinesString){
68+
s = s.trim();
69+
}
70+
assertEquals(lineText, s);
71+
found[0] = true;
72+
}
73+
onLine[0]++;
74+
}
75+
});
76+
if(!found[0]){
77+
throw new AssertionFailedException(lineText+" was expected, but not found in: "+bytes.toString());
78+
}
79+
}
80+
81+
static class AssertionFailedException extends AssertionError {
82+
private static final long serialVersionUID = -752171658596389715L;
83+
AssertionFailedException(Object o0, Object o1){
84+
super("expected: <"+o0+"> but found: <"+o1+">");
85+
}
86+
AssertionFailedException(Object o0){
87+
super(o0);
88+
}
89+
}
90+
91+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.sandwich.koans;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.fail;
6+
7+
import java.lang.reflect.Method;
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
import org.junit.Test;
14+
15+
public class KoanAnnotationTest {
16+
17+
@Test
18+
public void testThatKoanAnnotationIndicatesMethodToRun() throws Exception {
19+
final boolean[] invoked = {false};
20+
Map<Object, List<Method>> koans = new HashMap<Object, List<Method>>();
21+
Object object = new Object() {
22+
@SuppressWarnings("unused")
23+
@Koan
24+
public void koan() {
25+
invoked[0] = true;
26+
}
27+
@SuppressWarnings("unused")
28+
public void koanKnockOff(){
29+
fail();
30+
}
31+
};
32+
koans.put(object, Arrays.asList(object.getClass().getDeclaredMethod("koan")));
33+
new KoanSuiteRunner().runKoans(koans);
34+
assertTrue(invoked[0]);
35+
}
36+
37+
@SuppressWarnings("unused")
38+
@Test
39+
public void testThatKoanAnnotationDescriptionReferencedAtRuntime() throws Exception {
40+
final boolean[] invoked = {false};
41+
final String expected = "koanString";
42+
Map<Object, List<Method>> koans = new HashMap<Object, List<Method>>();
43+
Object object = new Object() {
44+
@Koan(expected)
45+
public void koan() {
46+
invoked[0] = true;
47+
}
48+
};
49+
assertEquals(expected, object.getClass().getDeclaredMethod("koan").getAnnotation(Koan.class).value());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)