KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > style > ToStringCreatorTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.style;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.springframework.core.CollectionFactory;
27 import org.springframework.util.ObjectUtils;
28
29 /**
30  * @author Keith Donald
31  */

32 public class ToStringCreatorTests extends TestCase {
33
34     private SomeObject s1, s2, s3;
35
36     protected void setUp() throws Exception JavaDoc {
37         s1 = new SomeObject() {
38             public String JavaDoc toString() {
39                 return "A";
40             }
41         };
42         s2 = new SomeObject() {
43             public String JavaDoc toString() {
44                 return "B";
45             }
46         };
47         s3 = new SomeObject() {
48             public String JavaDoc toString() {
49                 return "C";
50             }
51         };
52     }
53
54     public void testDefaultStyleMap() {
55         final Map JavaDoc map = getMap();
56         Object JavaDoc stringy = new Object JavaDoc() {
57             public String JavaDoc 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 JavaDoc getMap() {
67         Map JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc list = new ArrayList JavaDoc();
89         list.add(s1);
90         list.add(s2);
91         list.add(s3);
92         String JavaDoc 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 JavaDoc set = CollectionFactory.createLinkedSetIfPossible(3);
99         set.add(s1);
100         set.add(s2);
101         set.add(s3);
102         String JavaDoc 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 JavaDoc 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 JavaDoc {
114         String JavaDoc 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