KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > ObjectUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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 package org.apache.commons.lang;
17
18 import java.lang.reflect.Constructor JavaDoc;
19 import java.lang.reflect.Modifier JavaDoc;
20
21 import junit.framework.Test;
22 import junit.framework.TestCase;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25
26 /**
27  * Unit tests {@link org.apache.commons.lang.ObjectUtils}.
28  *
29  * @author <a HREF="mailto:jmcnally@collab.net">John McNally</a>
30  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
31  * @author <a HREF="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
32  * @author <a HREF="mailto:ggregory@seagullsw.com">Gary Gregory</a>
33  * @version $Id: ObjectUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35 public class ObjectUtilsTest extends TestCase {
36     private static final String JavaDoc FOO = "foo";
37     private static final String JavaDoc BAR = "bar";
38
39     public ObjectUtilsTest(String JavaDoc name) {
40         super(name);
41     }
42
43     public static void main(String JavaDoc[] args) {
44         TestRunner.run(suite());
45     }
46
47     public static Test suite() {
48         TestSuite suite = new TestSuite(ObjectUtilsTest.class);
49         suite.setName("ObjectUtils Tests");
50         return suite;
51     }
52
53     protected void setUp() throws Exception JavaDoc {
54         super.setUp();
55     }
56
57     protected void tearDown() throws Exception JavaDoc {
58         super.tearDown();
59     }
60
61     //-----------------------------------------------------------------------
62
public void testConstructor() {
63         assertNotNull(new ObjectUtils());
64         Constructor JavaDoc[] cons = ObjectUtils.class.getDeclaredConstructors();
65         assertEquals(1, cons.length);
66         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
67         assertEquals(true, Modifier.isPublic(ObjectUtils.class.getModifiers()));
68         assertEquals(false, Modifier.isFinal(ObjectUtils.class.getModifiers()));
69     }
70     
71     //-----------------------------------------------------------------------
72
public void testIsNull() {
73         Object JavaDoc o = FOO;
74         Object JavaDoc dflt = BAR;
75         assertSame("dflt was not returned when o was null", dflt, ObjectUtils.defaultIfNull(null, dflt));
76         assertSame("dflt was returned when o was not null", o, ObjectUtils.defaultIfNull(o, dflt));
77     }
78
79     public void testEquals() {
80         assertTrue("ObjectUtils.equals(null, null) returned false", ObjectUtils.equals(null, null));
81         assertTrue("ObjectUtils.equals(\"foo\", null) returned true", !ObjectUtils.equals(FOO, null));
82         assertTrue("ObjectUtils.equals(null, \"bar\") returned true", !ObjectUtils.equals(null, BAR));
83         assertTrue("ObjectUtils.equals(\"foo\", \"bar\") returned true", !ObjectUtils.equals(FOO, BAR));
84         assertTrue("ObjectUtils.equals(\"foo\", \"foo\") returned false", ObjectUtils.equals(FOO, FOO));
85     }
86
87     public void testHashCode() {
88         assertEquals(0, ObjectUtils.hashCode(null));
89         assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
90     }
91
92 // /**
93
// * Show that java.util.Date and java.sql.Timestamp are apples and oranges.
94
// * Prompted by an email discussion.
95
// *
96
// * The behavior is different b/w Sun Java 1.3.1_10 and 1.4.2_03.
97
// */
98
// public void testDateEqualsJava() {
99
// long now = 1076957313284L; // Feb 16, 2004 10:49... PST
100
// java.util.Date date = new java.util.Date(now);
101
// java.sql.Timestamp realTimestamp = new java.sql.Timestamp(now);
102
// java.util.Date timestamp = realTimestamp;
103
// // sanity check 1:
104
// assertEquals(284000000, realTimestamp.getNanos());
105
// assertEquals(1076957313284L, date.getTime());
106
// //
107
// // On Sun 1.3.1_10:
108
// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
109
// //
110
// //assertEquals(1076957313284L, timestamp.getTime());
111
// //
112
// //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
113
// //
114
// //assertEquals(1076957313284L, realTimestamp.getTime());
115
// // sanity check 2:
116
// assertEquals(date.getDay(), realTimestamp.getDay());
117
// assertEquals(date.getHours(), realTimestamp.getHours());
118
// assertEquals(date.getMinutes(), realTimestamp.getMinutes());
119
// assertEquals(date.getMonth(), realTimestamp.getMonth());
120
// assertEquals(date.getSeconds(), realTimestamp.getSeconds());
121
// assertEquals(date.getTimezoneOffset(), realTimestamp.getTimezoneOffset());
122
// assertEquals(date.getYear(), realTimestamp.getYear());
123
// //
124
// // Time values are == and equals() on Sun 1.4.2_03 but NOT on Sun 1.3.1_10:
125
// //
126
// //assertFalse("Sanity check failed: date.getTime() == timestamp.getTime()", date.getTime() == timestamp.getTime());
127
// //assertFalse("Sanity check failed: timestamp.equals(date)", timestamp.equals(date));
128
// //assertFalse("Sanity check failed: date.equals(timestamp)", date.equals(timestamp));
129
// // real test:
130
// //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp));
131
// }
132

133     public void testIdentityToString() {
134         assertEquals(null, ObjectUtils.identityToString(null));
135         assertEquals(
136             "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
137             ObjectUtils.identityToString(FOO));
138         Integer JavaDoc i = new Integer JavaDoc(90);
139         assertEquals(
140             "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(i)),
141             ObjectUtils.identityToString(i));
142     }
143
144     public void testAppendIdentityToString() {
145         assertEquals(null, ObjectUtils.appendIdentityToString(null, null));
146         assertEquals(null, ObjectUtils.appendIdentityToString(new StringBuffer JavaDoc(), null));
147         assertEquals(
148             "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
149             ObjectUtils.appendIdentityToString(null, FOO).toString());
150         assertEquals(
151             "java.lang.String@" + Integer.toHexString(System.identityHashCode(FOO)),
152             ObjectUtils.appendIdentityToString(new StringBuffer JavaDoc(), FOO).toString());
153         Integer JavaDoc val = new Integer JavaDoc(90);
154         assertEquals(
155             "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)),
156             ObjectUtils.appendIdentityToString(null, val).toString());
157         assertEquals(
158             "java.lang.Integer@" + Integer.toHexString(System.identityHashCode(val)),
159             ObjectUtils.appendIdentityToString(new StringBuffer JavaDoc(), val).toString());
160     }
161
162     public void testToString_Object() {
163         assertEquals("", ObjectUtils.toString((Object JavaDoc) null) );
164         assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE) );
165     }
166             
167     public void testToString_ObjectString() {
168         assertEquals(BAR, ObjectUtils.toString((Object JavaDoc) null, BAR) );
169         assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(Boolean.TRUE, BAR) );
170     }
171
172     public void testNull() {
173         assertTrue(ObjectUtils.NULL != null);
174         assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
175         assertSame(ObjectUtils.NULL, SerializationUtils.clone(ObjectUtils.NULL));
176     }
177
178 }
179
Popular Tags