001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang;
018:
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Modifier;
021: import java.util.Calendar;
022: import java.util.Date;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import junit.textui.TestRunner;
028:
029: /**
030: * Unit tests {@link org.apache.commons.lang.ObjectUtils}.
031: *
032: * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
033: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
034: * @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
035: * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
036: * @version $Id: ObjectUtilsTest.java 475113 2006-11-15 04:14:42Z bayard $
037: */
038: public class ObjectUtilsTest extends TestCase {
039: private static final String FOO = "foo";
040: private static final String BAR = "bar";
041:
042: public ObjectUtilsTest(String name) {
043: super (name);
044: }
045:
046: public static void main(String[] args) {
047: TestRunner.run(suite());
048: }
049:
050: public static Test suite() {
051: TestSuite suite = new TestSuite(ObjectUtilsTest.class);
052: suite.setName("ObjectUtils Tests");
053: return suite;
054: }
055:
056: protected void setUp() throws Exception {
057: super .setUp();
058: }
059:
060: protected void tearDown() throws Exception {
061: super .tearDown();
062: }
063:
064: //-----------------------------------------------------------------------
065: public void testConstructor() {
066: assertNotNull(new ObjectUtils());
067: Constructor[] cons = ObjectUtils.class
068: .getDeclaredConstructors();
069: assertEquals(1, cons.length);
070: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
071: assertEquals(true, Modifier.isPublic(ObjectUtils.class
072: .getModifiers()));
073: assertEquals(false, Modifier.isFinal(ObjectUtils.class
074: .getModifiers()));
075: }
076:
077: //-----------------------------------------------------------------------
078: public void testIsNull() {
079: Object o = FOO;
080: Object dflt = BAR;
081: assertSame("dflt was not returned when o was null", dflt,
082: ObjectUtils.defaultIfNull(null, dflt));
083: assertSame("dflt was returned when o was not null", o,
084: ObjectUtils.defaultIfNull(o, dflt));
085: }
086:
087: public void testEquals() {
088: assertTrue("ObjectUtils.equals(null, null) returned false",
089: ObjectUtils.equals(null, null));
090: assertTrue("ObjectUtils.equals(\"foo\", null) returned true",
091: !ObjectUtils.equals(FOO, null));
092: assertTrue("ObjectUtils.equals(null, \"bar\") returned true",
093: !ObjectUtils.equals(null, BAR));
094: assertTrue(
095: "ObjectUtils.equals(\"foo\", \"bar\") returned true",
096: !ObjectUtils.equals(FOO, BAR));
097: assertTrue(
098: "ObjectUtils.equals(\"foo\", \"foo\") returned false",
099: ObjectUtils.equals(FOO, FOO));
100: }
101:
102: public void testHashCode() {
103: assertEquals(0, ObjectUtils.hashCode(null));
104: assertEquals("a".hashCode(), ObjectUtils.hashCode("a"));
105: }
106:
107: // /**
108: // * Show that java.util.Date and java.sql.Timestamp are apples and oranges.
109: // * Prompted by an email discussion.
110: // *
111: // * The behavior is different b/w Sun Java 1.3.1_10 and 1.4.2_03.
112: // */
113: // public void testDateEqualsJava() {
114: // long now = 1076957313284L; // Feb 16, 2004 10:49... PST
115: // java.util.Date date = new java.util.Date(now);
116: // java.sql.Timestamp realTimestamp = new java.sql.Timestamp(now);
117: // java.util.Date timestamp = realTimestamp;
118: // // sanity check 1:
119: // assertEquals(284000000, realTimestamp.getNanos());
120: // assertEquals(1076957313284L, date.getTime());
121: // //
122: // // On Sun 1.3.1_10:
123: // //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
124: // //
125: // //assertEquals(1076957313284L, timestamp.getTime());
126: // //
127: // //junit.framework.AssertionFailedError: expected:<1076957313284> but was:<1076957313000>
128: // //
129: // //assertEquals(1076957313284L, realTimestamp.getTime());
130: // // sanity check 2:
131: // assertEquals(date.getDay(), realTimestamp.getDay());
132: // assertEquals(date.getHours(), realTimestamp.getHours());
133: // assertEquals(date.getMinutes(), realTimestamp.getMinutes());
134: // assertEquals(date.getMonth(), realTimestamp.getMonth());
135: // assertEquals(date.getSeconds(), realTimestamp.getSeconds());
136: // assertEquals(date.getTimezoneOffset(), realTimestamp.getTimezoneOffset());
137: // assertEquals(date.getYear(), realTimestamp.getYear());
138: // //
139: // // Time values are == and equals() on Sun 1.4.2_03 but NOT on Sun 1.3.1_10:
140: // //
141: // //assertFalse("Sanity check failed: date.getTime() == timestamp.getTime()", date.getTime() == timestamp.getTime());
142: // //assertFalse("Sanity check failed: timestamp.equals(date)", timestamp.equals(date));
143: // //assertFalse("Sanity check failed: date.equals(timestamp)", date.equals(timestamp));
144: // // real test:
145: // //assertFalse("java.util.Date and java.sql.Timestamp should be equal", ObjectUtils.equals(date, timestamp));
146: // }
147:
148: public void testIdentityToString() {
149: assertEquals(null, ObjectUtils.identityToString(null));
150: assertEquals("java.lang.String@"
151: + Integer.toHexString(System.identityHashCode(FOO)),
152: ObjectUtils.identityToString(FOO));
153: Integer i = new Integer(90);
154: assertEquals("java.lang.Integer@"
155: + Integer.toHexString(System.identityHashCode(i)),
156: ObjectUtils.identityToString(i));
157: }
158:
159: public void testAppendIdentityToString() {
160: assertEquals(null, ObjectUtils.appendIdentityToString(null,
161: null));
162: assertEquals(null, ObjectUtils.appendIdentityToString(
163: new StringBuffer(), null));
164: assertEquals("java.lang.String@"
165: + Integer.toHexString(System.identityHashCode(FOO)),
166: ObjectUtils.appendIdentityToString(null, FOO)
167: .toString());
168: assertEquals("java.lang.String@"
169: + Integer.toHexString(System.identityHashCode(FOO)),
170: ObjectUtils.appendIdentityToString(new StringBuffer(),
171: FOO).toString());
172: Integer val = new Integer(90);
173: assertEquals("java.lang.Integer@"
174: + Integer.toHexString(System.identityHashCode(val)),
175: ObjectUtils.appendIdentityToString(null, val)
176: .toString());
177: assertEquals("java.lang.Integer@"
178: + Integer.toHexString(System.identityHashCode(val)),
179: ObjectUtils.appendIdentityToString(new StringBuffer(),
180: val).toString());
181: }
182:
183: public void testToString_Object() {
184: assertEquals("", ObjectUtils.toString((Object) null));
185: assertEquals(Boolean.TRUE.toString(), ObjectUtils
186: .toString(Boolean.TRUE));
187: }
188:
189: public void testToString_ObjectString() {
190: assertEquals(BAR, ObjectUtils.toString((Object) null, BAR));
191: assertEquals(Boolean.TRUE.toString(), ObjectUtils.toString(
192: Boolean.TRUE, BAR));
193: }
194:
195: public void testNull() {
196: assertTrue(ObjectUtils.NULL != null);
197: assertTrue(ObjectUtils.NULL instanceof ObjectUtils.Null);
198: assertSame(ObjectUtils.NULL, SerializationUtils
199: .clone(ObjectUtils.NULL));
200: }
201:
202: public void testMax() {
203: Calendar calendar = Calendar.getInstance();
204: Comparable nonNullComparable1 = calendar.getTime();
205: Comparable nonNullComparable2 = calendar.getTime();
206:
207: calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
208: Comparable minComparable = calendar.getTime();
209:
210: assertNotSame(nonNullComparable1, nonNullComparable2);
211:
212: assertSame(nonNullComparable1, ObjectUtils.max(null,
213: nonNullComparable1));
214: assertSame(nonNullComparable1, ObjectUtils.max(
215: nonNullComparable1, null));
216: assertSame(nonNullComparable1, ObjectUtils.max(
217: nonNullComparable1, nonNullComparable2));
218: assertSame(nonNullComparable1, ObjectUtils.max(
219: nonNullComparable1, minComparable));
220: assertSame(nonNullComparable1, ObjectUtils.max(minComparable,
221: nonNullComparable1));
222: }
223:
224: public void testMin() {
225: Calendar calendar = Calendar.getInstance();
226: Comparable nonNullComparable1 = calendar.getTime();
227: Comparable nonNullComparable2 = calendar.getTime();
228:
229: calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
230: Comparable minComparable = calendar.getTime();
231:
232: assertNotSame(nonNullComparable1, nonNullComparable2);
233:
234: assertSame(nonNullComparable1, ObjectUtils.min(null,
235: nonNullComparable1));
236: assertSame(nonNullComparable1, ObjectUtils.min(
237: nonNullComparable1, null));
238: assertSame(nonNullComparable1, ObjectUtils.min(
239: nonNullComparable1, nonNullComparable2));
240: assertSame(minComparable, ObjectUtils.min(nonNullComparable1,
241: minComparable));
242: assertSame(minComparable, ObjectUtils.min(minComparable,
243: nonNullComparable1));
244: }
245: }
|