1 16 17 package org.springframework.core.style; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import junit.framework.TestCase; 25 26 import org.springframework.core.CollectionFactory; 27 import org.springframework.util.ObjectUtils; 28 29 32 public class ToStringCreatorTests extends TestCase { 33 34 private SomeObject s1, s2, s3; 35 36 protected void setUp() throws Exception { 37 s1 = new SomeObject() { 38 public String toString() { 39 return "A"; 40 } 41 }; 42 s2 = new SomeObject() { 43 public String toString() { 44 return "B"; 45 } 46 }; 47 s3 = new SomeObject() { 48 public String toString() { 49 return "C"; 50 } 51 }; 52 } 53 54 public void testDefaultStyleMap() { 55 final Map map = getMap(); 56 Object stringy = new Object () { 57 public String toString() { 58 return new ToStringCreator(this).append("familyFavoriteSport", map).toString(); 59 } 60 }; 61 assertEquals("[ToStringCreatorTests.4@" + ObjectUtils.getIdentityHexString(stringy) 62 + " familyFavoriteSport = map['Keri' -> 'Softball', 'Scot' -> 'Fishing', 'Keith' -> 'Flag Football']]", 63 stringy.toString()); 64 } 65 66 private Map getMap() { 67 Map map = CollectionFactory.createLinkedMapIfPossible(3); 68 map.put("Keri", "Softball"); 69 map.put("Scot", "Fishing"); 70 map.put("Keith", "Flag Football"); 71 return map; 72 } 73 74 public void testDefaultStyleArray() { 75 SomeObject[] array = new SomeObject[] { s1, s2, s3 }; 76 String str = new ToStringCreator(array).toString(); 77 assertEquals("[@" + ObjectUtils.getIdentityHexString(array) 78 + " array<ToStringCreatorTests.SomeObject>[A, B, C]]", str); 79 } 80 81 public void testPrimitiveArrays() { 82 int[] integers = new int[] { 0, 1, 2, 3, 4 }; 83 String str = new ToStringCreator(integers).toString(); 84 assertEquals("[@" + ObjectUtils.getIdentityHexString(integers) + " array<Integer>[0, 1, 2, 3, 4]]", str); 85 } 86 87 public void testList() { 88 List list = new ArrayList (); 89 list.add(s1); 90 list.add(s2); 91 list.add(s3); 92 String str = new ToStringCreator(this).append("myLetters", list).toString(); 93 assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) + " myLetters = list[A, B, C]]", 94 str); 95 } 96 97 public void testSet() { 98 Set set = CollectionFactory.createLinkedSetIfPossible(3); 99 set.add(s1); 100 set.add(s2); 101 set.add(s3); 102 String str = new ToStringCreator(this).append("myLetters", set).toString(); 103 assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) + " myLetters = set[A, B, C]]", 104 str); 105 } 106 107 public void testClass() { 108 String str = new ToStringCreator(this).append("myClass", this.getClass()).toString(); 109 assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) 110 + " myClass = ToStringCreatorTests]", str); 111 } 112 113 public void testMethod() throws Exception { 114 String str = new ToStringCreator(this).append("myMethod", this.getClass().getMethod("testMethod", null)) 115 .toString(); 116 assertEquals("[ToStringCreatorTests@" + ObjectUtils.getIdentityHexString(this) 117 + " myMethod = testMethod@ToStringCreatorTests]", str); 118 } 119 120 121 public static class SomeObject { 122 123 } 124 125 } 126 | Popular Tags |