KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > util > TestToStringBuilder


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

15 package org.apache.hivemind.util;
16
17 import org.apache.hivemind.test.HiveMindTestCase;
18 import org.apache.hivemind.util.ToStringBuilder;
19
20 /**
21  * Tests for the {@link org.apache.hivemind.util.ToStringBuilder} class.
22  *
23  * @author Howard Lewis Ship
24  */

25 public class TestToStringBuilder extends HiveMindTestCase
26 {
27     private int _originalDefaultMode;
28
29     protected void tearDown() throws Exception JavaDoc
30     {
31         super.tearDown();
32
33         ToStringBuilder.setDefaultMode(_originalDefaultMode);
34     }
35
36     protected void setUp() throws Exception JavaDoc
37     {
38         super.setUp();
39         
40         _originalDefaultMode = ToStringBuilder.getDefaultMode();
41     }
42
43     public void testNull()
44     {
45         try
46         {
47             new ToStringBuilder(null);
48             unreachable();
49         }
50         catch (NullPointerException JavaDoc ex)
51         {
52         }
53     }
54
55     public void testSimple()
56     {
57         ToStringBuilder b = new ToStringBuilder(this);
58
59         assertEquals("TestToStringBuilder", b.toString());
60
61         try
62         {
63             b.toString();
64             unreachable();
65         }
66         catch (NullPointerException JavaDoc ex)
67         {
68             // Can't invoke toString() twice!
69
}
70     }
71
72     public void testWithHashCode()
73     {
74         ToStringBuilder b = new ToStringBuilder(this, ToStringBuilder.INCLUDE_HASHCODE);
75
76         assertEquals("TestToStringBuilder@" + Integer.toHexString(hashCode()), b.toString());
77     }
78
79     public void testSetDefault()
80     {
81         int mode = ToStringBuilder.INCLUDE_HASHCODE | ToStringBuilder.INCLUDE_PACKAGE_PREFIX;
82         ToStringBuilder b1 = new ToStringBuilder(this, mode);
83
84         ToStringBuilder.setDefaultMode(mode);
85
86         ToStringBuilder b2 = new ToStringBuilder(this);
87
88         assertEquals(b1.toString(), b2.toString());
89     }
90
91     public void testAppendString()
92     {
93         ToStringBuilder b = new ToStringBuilder(this);
94
95         b.append("fred", "flintstone");
96
97         assertEquals("TestToStringBuilder[fred=flintstone]", b.toString());
98     }
99
100     public void testAppendNullString()
101     {
102         ToStringBuilder b = new ToStringBuilder(this);
103
104         b.append("attr", (String JavaDoc) null);
105
106         assertEquals("TestToStringBuilder[attr=null]", b.toString());
107     }
108
109     public void testAppendTwo()
110     {
111         ToStringBuilder b = new ToStringBuilder(this);
112
113         b.append("fred", "flintstone");
114         b.append("barney", "rubble");
115
116         assertEquals("TestToStringBuilder[fred=flintstone barney=rubble]", b.toString());
117     }
118
119     public void testAppendObject()
120     {
121         ToStringBuilder b = new ToStringBuilder(this);
122
123         b.append("number", new Integer JavaDoc(27));
124
125         assertEquals("TestToStringBuilder[number=27]", b.toString());
126     }
127
128     public void testAppendNullObject()
129     {
130         ToStringBuilder b = new ToStringBuilder(this);
131
132         b.append("object", null);
133
134         assertEquals("TestToStringBuilder[object=null]", b.toString());
135     }
136
137     public void testAppendBoolean()
138     {
139         ToStringBuilder b = new ToStringBuilder(this);
140
141         b.append("yes", true);
142         b.append("no", false);
143
144         assertEquals("TestToStringBuilder[yes=true no=false]", b.toString());
145     }
146
147     public void testAppendByte()
148     {
149         ToStringBuilder b = new ToStringBuilder(this);
150
151         b.append("byte", (byte) 32);
152
153         assertEquals("TestToStringBuilder[byte=32]", b.toString());
154     }
155
156     public void testAppendShort()
157     {
158         ToStringBuilder b = new ToStringBuilder(this);
159
160         b.append("short", (short) - 37);
161         assertEquals("TestToStringBuilder[short=-37]", b.toString());
162     }
163
164     public void testAppendInt()
165     {
166         ToStringBuilder b = new ToStringBuilder(this);
167
168         b.append("int", 217);
169         assertEquals("TestToStringBuilder[int=217]", b.toString());
170     }
171 }
172
Popular Tags