File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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" />
Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 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 .Arrays ;
8+ import java .util .Objects ;
9+
10+ import com .sandwich .koan .Koan ;
11+
12+ public class AboutRequireNotNull {
13+
14+ @ Koan
15+ public void failArgumentValidationWithRequireNotNull (){
16+ // This koan demonstrates the use of Objects.requireNotNull
17+ // in place of traditional argument validation using exceptions
18+ String s = "" ;
19+ try {
20+ s += validateUsingRequireNotNull (null );
21+ } catch (NullPointerException ex ) {
22+ s = "caught a NullPointerException" ;
23+ }
24+ assertEquals (s , __ );
25+ }
26+
27+ @ Koan
28+ public void passArgumentValidationWithRequireNotNull (){
29+ // This koan demonstrates the use of Objects.requireNotNull
30+ // in place of traditional argument validation using exceptions
31+ String s = "" ;
32+ try {
33+ s += validateUsingRequireNotNull ("valid" );
34+ } catch (NullPointerException ex ) {
35+ s = "caught a NullPointerException" ;
36+ }
37+ assertEquals (s , __ );
38+ }
39+
40+ private int validateUsingRequireNotNull (String str ) {
41+ // If you're only concerned with null values requireNotNull
42+ // is concise and the point of the NullPointerException it
43+ // throws is clear, though you can optionally provide a
44+ // description as well
45+ return Objects .requireNonNull (str ).length ();
46+ }
47+
48+ }
You can’t perform that action at this time.
0 commit comments