11package beginner ;
22
3- import com .sandwich .koan .Koan ;
3+ import static com .sandwich .koan .constant .KoanConstants .__ ;
4+ import static com .sandwich .util .Assert .assertEquals ;
45
56import java .text .MessageFormat ;
7+ import java .util .ArrayList ;
8+ import java .util .List ;
69
7- import static com .sandwich .koan .constant .KoanConstants .*;
8- import static com .sandwich .util .Assert .*;
9- import static com .sandwich .util .TypeUtils .*;
10+ import com .sandwich .koan .Koan ;
1011
1112public class AboutObjects {
1213
13- @ Koan ()
14+ @ Koan
1415 public void newObjectInstancesCanBeCreatedDirectly () {
1516 assertEquals (new Object () instanceof Object , __ );
1617 }
1718
18- @ Koan ()
19+ @ Koan
1920 public void allClassesInheritFromObject () {
2021 class Foo {}
2122
22- Class [] ancestors = ancestors (new Foo ());
23+ Class <?> [] ancestors = getAncestors (new Foo ());
2324 assertEquals (ancestors [0 ], __ );
2425 assertEquals (ancestors [1 ], __ );
2526 }
2627
27- @ Koan ()
28+ @ Koan
2829 public void objectToString () {
2930 Object object = new Object ();
3031 // TODO: Why is it best practice to ALWAYS override toString?
3132 String expectedToString = MessageFormat .format ("{0}@{1}" , Object .class .getName (), Integer .toHexString (object .hashCode ()));
3233 assertEquals (expectedToString , __ ); // hint: object.toString()
3334 }
3435
35- @ Koan ()
36+ @ Koan
3637 public void toStringConcatenates (){
3738 final String string = "ha" ;
3839 Object object = new Object (){
@@ -43,10 +44,20 @@ public void toStringConcatenates(){
4344 assertEquals (string + object , __ );
4445 }
4546
46- @ Koan ()
47+ @ Koan
4748 public void toStringIsTestedForNullWhenInvokedImplicitly (){
4849 String string = "string" ;
4950 assertEquals (string +null , __ );
5051 }
5152
53+ private Class <?>[] getAncestors (Object object ) {
54+ List <Class <?>> ancestors = new ArrayList <Class <?>>();
55+ Class <?> clazz = object .getClass ();
56+ while (clazz != null ) {
57+ ancestors .add (clazz );
58+ clazz = clazz .getSuperclass ();
59+ }
60+ return ancestors .toArray (new Class []{});
61+ }
62+
5263}
0 commit comments