Skip to content

Commit cd73a1b

Browse files
committed
Merge pull request matyb#54 from godfreyduke/master
Add koans for argument validation using exceptions and Object.requireNotNull.
2 parents e22892b + 249ea18 commit cd73a1b

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

koans/app/config/PathToEnlightenment.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<suite class="AboutStringsInSwitch"/>
4444
<suite class="AboutDiamondOperator"/>
4545
<suite class="AboutTryWithResources"/>
46+
<suite class="AboutRequireNotNull"/>
4647
</package>
4748
<package pkg="java8" name="Java 8">
4849
<suite class="AboutDefaultMethods"/>

koans/src/beginner/AboutExceptions.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,38 @@ public void catchOrder() {
124124
}
125125
assertEquals(s, __);
126126
}
127+
128+
@Koan
129+
public void failArgumentValidationWithAnIllegalArgumentException(){
130+
// This koan demonstrates the use of exceptions in argument validation
131+
String s = "";
132+
try {
133+
s += validateUsingIllegalArgumentException(null);
134+
} catch (IllegalArgumentException ex) {
135+
s = "caught an IllegalArgumentException";
136+
}
137+
assertEquals(s, __);
138+
}
139+
140+
@Koan
141+
public void passArgumentValidationWithAnIllegalArgumentException(){
142+
// This koan demonstrates the use of exceptions in argument validation
143+
String s = "";
144+
try {
145+
s += validateUsingIllegalArgumentException("valid");
146+
} catch (IllegalArgumentException ex) {
147+
s = "caught an IllegalArgumentException";
148+
}
149+
assertEquals(s, __);
150+
}
151+
152+
private int validateUsingIllegalArgumentException(String str) {
153+
// This is effective and both the evaluation and the error message
154+
// can be tailored which can be particularly handy if you're guarding
155+
// against more than null values
156+
if (null == str) {
157+
throw new IllegalArgumentException("str should not be null");
158+
}
159+
return str.length();
160+
}
127161
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package java7;
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 java.util.Objects;
8+
9+
import com.sandwich.koan.Koan;
10+
11+
public class AboutRequireNotNull {
12+
13+
@Koan
14+
public void failArgumentValidationWithRequireNotNull(){
15+
// This koan demonstrates the use of Objects.requireNotNull
16+
// in place of traditional argument validation using exceptions
17+
String s = "";
18+
try {
19+
s += validateUsingRequireNotNull(null);
20+
} catch (NullPointerException ex) {
21+
s = "caught a NullPointerException";
22+
}
23+
assertEquals(s, __);
24+
}
25+
26+
@Koan
27+
public void passArgumentValidationWithRequireNotNull(){
28+
// This koan demonstrates the use of Objects.requireNotNull
29+
// in place of traditional argument validation using exceptions
30+
String s = "";
31+
try {
32+
s += validateUsingRequireNotNull("valid");
33+
} catch (NullPointerException ex) {
34+
s = "caught a NullPointerException";
35+
}
36+
assertEquals(s, __);
37+
}
38+
39+
private int validateUsingRequireNotNull(String str) {
40+
// If you're only concerned with null values requireNotNull
41+
// is concise and the point of the NullPointerException it
42+
// throws is clear, though you can optionally provide a
43+
// description as well
44+
return Objects.requireNonNull(str).length();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)