Skip to content

Commit 76eb399

Browse files
matmat
authored andcommitted
reorganized folder structure
1 parent 7d8f479 commit 76eb399

166 files changed

Lines changed: 2465 additions & 53 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎koans-lib/src/com/sandwich/koan/KoanClassLoader.java‎

Lines changed: 0 additions & 20 deletions
This file was deleted.

‎koans/app/config/file_hashes.dat‎

1.33 KB
Binary file not shown.
File renamed without changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package advanced;
2+
3+
import com.sandwich.koan.Koan;
4+
import static com.sandwich.util.Assert.fail;
5+
6+
public class AboutMocks {
7+
8+
static interface Collaborator {
9+
public void doBusinessStuff();
10+
}
11+
12+
static class ExplosiveCollaborator implements Collaborator {
13+
public void doBusinessStuff() {
14+
fail("Default collaborator's behavior is complicating testing.");
15+
}
16+
}
17+
18+
static class ClassUnderTest {
19+
Collaborator c;
20+
public ClassUnderTest(){
21+
// default is to pass a broken Collaborator, test should pass one
22+
// that doesn't throw exception
23+
this(new ExplosiveCollaborator());
24+
}
25+
public ClassUnderTest(Collaborator c){
26+
this.c = c;
27+
}
28+
public boolean doSomething(){
29+
c.doBusinessStuff();
30+
return true;
31+
}
32+
}
33+
34+
@Koan()
35+
public void simpleAnonymousMock(){
36+
// HINT: pass a safe Collaborator implementation to constructor
37+
// new ClassUnderTest(new Collaborator(){... it should not be the
38+
// objective of this test to test that collaborator, so replace it
39+
new ClassUnderTest().doSomething();
40+
}
41+
42+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package beginner;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
6+
import java.util.Arrays;
7+
8+
import com.sandwich.koan.Koan;
9+
10+
public class AboutArrays {
11+
12+
@Koan
13+
public void arraysDoNotConsiderElementsWhenEvaluatingEquality(){
14+
// arrays utilize default object equality (A == {1} B == {1}, though A
15+
// and B contain the same thing, the container is not the same
16+
// referenced array instance...
17+
assertEquals(new int[] { 1 }.equals(new int[] { 1 }), __);
18+
}
19+
20+
@Koan
21+
public void cloneEqualityIsNotRespected(){ //!
22+
int[] original = new int[] { 1 };
23+
assertEquals(original.equals(original.clone()), __);
24+
}
25+
26+
@Koan
27+
public void anArraysHashCodeMethodDoesNotConsiderElements(){
28+
int[] array0 = new int[]{0};
29+
int[] array1 = new int[]{0};
30+
assertEquals(Integer.valueOf(array0.hashCode()).equals(array1.hashCode()), __); // not equal!
31+
// TODO: ponder the consequences when arrays are used in Hash Collection implementations.
32+
}
33+
34+
@Koan
35+
public void arraysHelperClassEqualsMethodConsidersElementsWhenDeterminingEquality(){
36+
int[] array0 = new int[]{0};
37+
int[] array1 = new int[]{0};
38+
assertEquals(Arrays.equals(array0, array1), __); // whew - what most people assume
39+
// about equals in regard to arrays! (logical equality)
40+
}
41+
42+
@Koan
43+
public void arraysHelperClassHashCodeMethodConsidersElementsWhenDeterminingHashCode(){
44+
int[] array0 = new int[]{0};
45+
int[] array1 = new int[]{0};
46+
// whew - what most people assume about hashCode in regard to arrays!
47+
assertEquals(Integer.valueOf(Arrays.hashCode(array0)).equals(Arrays.hashCode(array1)), __);
48+
}
49+
50+
@Koan
51+
public void arraysAreMutable(){
52+
final boolean[] oneBoolean = new boolean[]{false};
53+
oneBoolean[0] = true;
54+
assertEquals(oneBoolean[0], __);
55+
}
56+
57+
@Koan
58+
public void arraysAreIndexedAtZero(){
59+
int[] integers = new int[]{1,2};
60+
assertEquals(integers[0], __);
61+
assertEquals(integers[1], __);
62+
}
63+
64+
@Koan
65+
public void arrayIndexOutOfBounds(){
66+
int[] array = new int[]{1};
67+
@SuppressWarnings("unused")
68+
int meh = array[1]; // remember 0 based indexes, 1 is the 2nd element (which doesn't exist)
69+
}
70+
71+
@Koan
72+
public void arrayLengthCanBeChecked(){
73+
assertEquals(new int[1].length, __);
74+
}
75+
76+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package beginner;
2+
3+
// FYI - usually bad practice to import statically, but can make code cleaner
4+
import static com.sandwich.koan.constant.KoanConstants.__;
5+
import static com.sandwich.util.Assert.assertEquals;
6+
import static com.sandwich.util.Assert.assertFalse;
7+
import static com.sandwich.util.Assert.assertNotNull;
8+
import static com.sandwich.util.Assert.assertNotSame;
9+
import static com.sandwich.util.Assert.assertNull;
10+
import static com.sandwich.util.Assert.assertSame;
11+
import static com.sandwich.util.Assert.assertTrue;
12+
13+
import com.sandwich.koan.Koan;
14+
15+
public class AboutAssertions {
16+
17+
@Koan()
18+
public void assertBooleanTrue() {
19+
assertTrue(__); // should be true really
20+
}
21+
22+
@Koan()
23+
public void assertBooleanFalse() {
24+
assertFalse(__);
25+
}
26+
27+
@Koan()
28+
public void assertNullObject(){
29+
assertNull(__);
30+
}
31+
32+
@Koan()
33+
public void assertNotNullObject(){
34+
assertNotNull(null); // anything other than null should pass here...
35+
}
36+
37+
@Koan()
38+
public void assertEqualsUsingExpression(){
39+
assertTrue("Hello World!".equals(__));
40+
}
41+
42+
@Koan()
43+
public void assertEqualsWithBetterFailureMessage(){
44+
assertEquals(1, __);
45+
}
46+
47+
@Koan()
48+
public void assertEqualsWithDescriptiveMessage() {
49+
// Generally, when using an assertXXX methods, expectation is on the
50+
// left and it is best practice to use a String for the first arg
51+
// indication what has failed
52+
assertEquals("The answer to 'life the universe and everything' should be 42", 42, __);
53+
}
54+
55+
@Koan()
56+
public void assertSameInstance(){
57+
Object same = new Integer(1);
58+
Object sameReference = __;
59+
assertSame(same, sameReference);
60+
}
61+
62+
@Koan()
63+
public void assertNotSameInstance(){
64+
Integer same = new Integer(1);
65+
Integer sameReference = same;
66+
assertNotSame(same, sameReference);
67+
}
68+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package beginner;
2+
3+
import static com.sandwich.koan.constant.KoanConstants.__;
4+
import static com.sandwich.util.Assert.assertEquals;
5+
import static com.sandwich.util.Assert.fail;
6+
7+
import com.sandwich.koan.Koan;
8+
9+
@SuppressWarnings("unused")
10+
public class AboutCasting {
11+
12+
@Koan
13+
public void longPlusInt() {
14+
int a = 6;
15+
long b = 10;
16+
Object c = a + b;
17+
assertEquals(c, __);
18+
assertEquals(c instanceof Integer, __);
19+
assertEquals(c instanceof Long, __);
20+
}
21+
22+
@Koan
23+
public void forceIntTypecast() {
24+
long a = 2147483648L;
25+
// What happens if we force a long value into an int?
26+
int b = (int) a;
27+
assertEquals(b, __);
28+
}
29+
30+
@Koan
31+
public void implicitTypecast() {
32+
int a = 1;
33+
int b = Integer.MAX_VALUE;
34+
long c = a + b; // still overflows int... which is the Integer.MIN_VALUE, the operation occurs prior to assignment to long
35+
assertEquals(c, __);
36+
}
37+
38+
interface Sleepable {
39+
String sleep();
40+
}
41+
42+
class Grandparent implements Sleepable {
43+
public String sleep() {
44+
return "zzzz";
45+
}
46+
}
47+
48+
class Parent extends Grandparent {
49+
public String complain() {
50+
return "TPS reports don't even have a cover letter!";
51+
}
52+
}
53+
54+
class Child extends Parent {
55+
public String complain() {
56+
return "Are we there yet!!";
57+
}
58+
}
59+
60+
@Koan
61+
public void downCastWithInheritance() {
62+
Child child = new Child();
63+
Parent parentReference = child; // Why isn't there an explicit cast?
64+
assertEquals(child instanceof Child, __);
65+
assertEquals(parentReference instanceof Child, __);
66+
assertEquals(parentReference instanceof Parent, __);
67+
assertEquals(parentReference instanceof Grandparent, __);
68+
}
69+
70+
@Koan
71+
public void downCastAndPolymorphism() {
72+
Child child = new Child();
73+
Parent parentReference = child;
74+
// If the result is unexpected, consider the difference between an instance and its reference
75+
assertEquals(parentReference.complain(), __);
76+
}
77+
78+
@Koan
79+
public void upCastWithInheritance() {
80+
Grandparent child = new Child();
81+
Parent parentReference = (Parent) child; // Why do we need an explicit cast here?
82+
Child childReference = (Child) parentReference; // Or here?
83+
assertEquals(childReference instanceof Child, __);
84+
assertEquals(childReference instanceof Parent, __);
85+
assertEquals(childReference instanceof Grandparent, __);
86+
}
87+
88+
@Koan
89+
public void upCastAndPolymorphism() {
90+
Grandparent child = new Child();
91+
Parent parent = (Child) child;
92+
// Think about the result. Did you expect that? Why?
93+
// How is that different from above?
94+
assertEquals(parent.complain(), __);
95+
}
96+
97+
@Koan
98+
public void classCasting() {
99+
try {
100+
Object o = new Object();
101+
((Sleepable) o).sleep(); // would this even compile without the cast?
102+
} catch (ClassCastException x) {
103+
fail("Object does not implement Sleepable, maybe one of the people classes do?");
104+
}
105+
}
106+
107+
@Koan
108+
public void complicatedCast() {
109+
Grandparent parent = new Parent();
110+
// How can we access the parent's ability to "complain" - if the reference is held as a superclass?
111+
assertEquals("TPS reports don't even have a cover letter!", __);
112+
}
113+
114+
}

0 commit comments

Comments
 (0)