001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestObjectUtils.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import java.util.*;
011:
012: import com.uwyn.rife.tools.ExceptionUtils;
013: import junit.framework.TestCase;
014:
015: public class TestObjectUtils extends TestCase {
016: public TestObjectUtils(String name) {
017: super (name);
018: }
019:
020: public void testGenericClone() {
021: assertNull(ObjectUtils.genericClone(null));
022:
023: Object object_orig = new Object();
024: Object object_copy = ObjectUtils.genericClone(object_orig);
025: assertSame(object_orig, object_copy);
026:
027: String string_orig = new String("the string");
028: String string_copy = ObjectUtils.genericClone(string_orig);
029: assertSame(string_orig, string_copy);
030:
031: Exception exception_orig = new Exception("the string");
032: Exception exception_copy = ObjectUtils
033: .genericClone(exception_orig);
034: assertSame(exception_orig, exception_copy);
035:
036: StringBuffer stringbuffer_orig = new StringBuffer("the string");
037: StringBuffer stringbuffer_copy = ObjectUtils
038: .genericClone(stringbuffer_orig);
039: assertNotSame(stringbuffer_orig, stringbuffer_copy);
040: assertEquals(stringbuffer_orig.toString(), stringbuffer_copy
041: .toString());
042:
043: Byte byte_orig = new Byte((byte) 1);
044: Byte byte_copy = ObjectUtils.genericClone(byte_orig);
045: assertSame(byte_orig, byte_copy);
046:
047: Short short_orig = new Short((short) 1);
048: Short short_copy = ObjectUtils.genericClone(short_orig);
049: assertSame(short_orig, short_copy);
050:
051: Integer integer_orig = new Integer(1);
052: Integer integer_copy = ObjectUtils.genericClone(integer_orig);
053: assertSame(integer_orig, integer_copy);
054:
055: Long long_orig = new Long(1);
056: Long long_copy = ObjectUtils.genericClone(long_orig);
057: assertSame(long_orig, long_copy);
058:
059: Float float_orig = new Float(1);
060: Float float_copy = ObjectUtils.genericClone(float_orig);
061: assertSame(float_orig, float_copy);
062:
063: Double double_orig = new Double(1);
064: Double double_copy = ObjectUtils.genericClone(double_orig);
065: assertSame(double_orig, double_copy);
066:
067: Boolean boolean_orig = new Boolean(false);
068: Boolean boolean_copy = ObjectUtils.genericClone(boolean_orig);
069: assertSame(boolean_orig, boolean_copy);
070:
071: Character character_orig = new Character('a');
072: Character character_copy = ObjectUtils
073: .genericClone(character_orig);
074: assertSame(character_orig, character_copy);
075:
076: Uncloneable uncloneable_orig = new Uncloneable(89);
077: assertNull(ObjectUtils.genericClone(uncloneable_orig));
078:
079: ProtectedCloneable protected_cloneable_orig = new ProtectedCloneable(
080: 89);
081: assertNull(ObjectUtils.genericClone(protected_cloneable_orig));
082:
083: ArrayList<String> string_collection_orig = new ArrayList<String>();
084: string_collection_orig.add("one");
085: string_collection_orig.add("two");
086: ArrayList<String> string_collection_copy = ObjectUtils
087: .genericClone(string_collection_orig);
088: assertNotSame(string_collection_orig, string_collection_copy);
089: assertEquals(string_collection_orig.size(),
090: string_collection_copy.size());
091: assertSame(string_collection_orig.get(0),
092: string_collection_copy.get(0));
093: assertSame(string_collection_orig.get(1),
094: string_collection_copy.get(1));
095:
096: Date cloneable_orig = new Date();
097: Date cloneable_copy = ObjectUtils.genericClone(cloneable_orig);
098: assertNotSame(cloneable_orig, cloneable_copy);
099: assertEquals(cloneable_orig.getDate(), cloneable_copy.getDate());
100:
101: ArrayList<Date> date_collection_orig = new ArrayList<Date>();
102: date_collection_orig.add(new Date());
103: date_collection_orig.add(new Date());
104: ArrayList<Date> date_collection_copy = ObjectUtils
105: .genericClone(date_collection_orig);
106: assertNotSame(date_collection_orig, date_collection_copy);
107: assertEquals(date_collection_orig.size(), date_collection_copy
108: .size());
109: assertSame(date_collection_orig.get(0), date_collection_copy
110: .get(0));
111: assertSame(date_collection_orig.get(1), date_collection_copy
112: .get(1));
113: }
114:
115: public void testDeepClone() {
116: try {
117: assertNull(ObjectUtils.deepClone(null));
118:
119: Object object_orig = new Object();
120: Object object_copy = ObjectUtils.deepClone(object_orig);
121: assertSame(object_orig, object_copy);
122:
123: String string_orig = new String("the string");
124: String string_copy = ObjectUtils.deepClone(string_orig);
125: assertSame(string_orig, string_copy);
126:
127: Date cloneable_orig = new Date();
128: Date cloneable_copy = ObjectUtils.deepClone(cloneable_orig);
129: assertNotSame(cloneable_orig, cloneable_copy);
130: assertEquals(cloneable_orig.getDate(), cloneable_copy
131: .getDate());
132:
133: Uncloneable uncloneable_orig = new Uncloneable(89);
134: try {
135: ObjectUtils.deepClone(uncloneable_orig);
136: fail();
137: } catch (CloneNotSupportedException e) {
138: assertTrue(true);
139: }
140:
141: ProtectedCloneable protected_cloneable_orig = new ProtectedCloneable(
142: 89);
143: try {
144: ObjectUtils.deepClone(protected_cloneable_orig);
145: fail();
146: } catch (CloneNotSupportedException e) {
147: assertTrue(true);
148: }
149:
150: boolean[] boolean_array_orig = new boolean[] { true, false,
151: true };
152: boolean[] boolean_array_copy = ObjectUtils
153: .deepClone(boolean_array_orig);
154: assertNotSame(boolean_array_orig, boolean_array_copy);
155: assertEquals(boolean_array_orig.length,
156: boolean_array_copy.length);
157: assertEquals(boolean_array_orig[0], boolean_array_orig[0]);
158: assertEquals(boolean_array_orig[1], boolean_array_orig[1]);
159: assertEquals(boolean_array_orig[2], boolean_array_orig[2]);
160:
161: byte[] byte_array_orig = new byte[] { (byte) 7, (byte) 23 };
162: byte[] byte_array_copy = ObjectUtils
163: .deepClone(byte_array_orig);
164: assertNotSame(byte_array_orig, byte_array_copy);
165: assertEquals(byte_array_orig.length, byte_array_copy.length);
166: assertEquals(byte_array_orig[0], byte_array_copy[0]);
167: assertEquals(byte_array_orig[1], byte_array_copy[1]);
168:
169: char[] char_array_orig = new char[] { 'k', 'I' };
170: char[] char_array_copy = ObjectUtils
171: .deepClone(char_array_orig);
172: assertNotSame(char_array_orig, char_array_copy);
173: assertEquals(char_array_orig.length, char_array_copy.length);
174: assertEquals(char_array_orig[0], char_array_copy[0]);
175: assertEquals(char_array_orig[1], char_array_copy[1]);
176:
177: short[] short_array_orig = new short[] { (short) 77,
178: (short) 9 };
179: short[] short_array_copy = ObjectUtils
180: .deepClone(short_array_orig);
181: assertNotSame(short_array_orig, short_array_copy);
182: assertEquals(short_array_orig.length,
183: short_array_copy.length);
184: assertEquals(short_array_orig[0], short_array_copy[0]);
185: assertEquals(short_array_orig[1], short_array_copy[1]);
186:
187: int[] int_array_orig = new int[] { 879, 86, 13, 89 };
188: int[] int_array_copy = ObjectUtils
189: .deepClone(int_array_orig);
190: assertNotSame(int_array_orig, int_array_copy);
191: assertEquals(int_array_orig.length, int_array_copy.length);
192: assertEquals(int_array_orig[0], int_array_copy[0]);
193: assertEquals(int_array_orig[1], int_array_copy[1]);
194: assertEquals(int_array_orig[2], int_array_copy[2]);
195: assertEquals(int_array_orig[3], int_array_copy[3]);
196:
197: long[] long_array_orig = new long[] { 869523L, 913437L };
198: long[] long_array_copy = ObjectUtils
199: .deepClone(long_array_orig);
200: assertNotSame(long_array_orig, long_array_copy);
201: assertEquals(long_array_orig.length, long_array_copy.length);
202: assertEquals(long_array_orig[0], long_array_copy[0]);
203: assertEquals(long_array_orig[1], long_array_copy[1]);
204:
205: float[] float_array_orig = new float[] { 89.4f, 1123.9f,
206: 1.1f };
207: float[] float_array_copy = ObjectUtils
208: .deepClone(float_array_orig);
209: assertNotSame(float_array_orig, float_array_copy);
210: assertEquals(float_array_orig.length,
211: float_array_copy.length);
212: assertEquals(float_array_orig[0], float_array_copy[0]);
213: assertEquals(float_array_orig[1], float_array_copy[1]);
214: assertEquals(float_array_orig[2], float_array_copy[2]);
215:
216: double[] double_array_orig = new double[] { 87986.121d,
217: 979121.d };
218: double[] double_array_copy = ObjectUtils
219: .deepClone(double_array_orig);
220: assertNotSame(double_array_orig, double_array_copy);
221: assertEquals(double_array_orig.length,
222: double_array_copy.length);
223: assertEquals(double_array_orig[0], double_array_copy[0]);
224: assertEquals(double_array_orig[1], double_array_copy[1]);
225:
226: String[] string_array_orig = new String[] { "one", "two" };
227: String[] string_array_copy = ObjectUtils
228: .deepClone(string_array_orig);
229: assertNotSame(string_array_orig, string_array_copy);
230: assertEquals(string_array_orig.length,
231: string_array_copy.length);
232: assertSame(string_array_orig[0], string_array_copy[0]);
233: assertSame(string_array_orig[1], string_array_copy[1]);
234:
235: Date[] date_array_orig = new Date[] { new Date(),
236: new Date() };
237: Date[] date_array_copy = ObjectUtils
238: .deepClone(date_array_orig);
239: assertNotSame(date_array_orig, date_array_copy);
240: assertEquals(date_array_orig.length, date_array_copy.length);
241: assertNotSame(date_array_orig[0], date_array_copy[0]);
242: assertNotSame(date_array_orig[1], date_array_copy[1]);
243: assertEquals(date_array_orig[0], date_array_copy[0]);
244: assertEquals(date_array_orig[1], date_array_copy[1]);
245:
246: long[][] long_multiarray_orig = new long[][] { { 869523L },
247: { 2323L } };
248: long[][] long_multiarray_copy = ObjectUtils
249: .deepClone(long_multiarray_orig);
250: assertNotSame(long_multiarray_orig, long_multiarray_copy);
251: assertEquals(long_multiarray_orig.length,
252: long_multiarray_copy.length);
253: assertNotSame(long_multiarray_orig[0],
254: long_multiarray_copy[0]);
255: assertNotSame(long_multiarray_orig[1],
256: long_multiarray_copy[1]);
257: assertEquals(long_multiarray_orig[0].length,
258: long_multiarray_copy[0].length);
259: assertEquals(long_multiarray_orig[1].length,
260: long_multiarray_copy[1].length);
261: assertNotSame(long_multiarray_orig[0][0],
262: long_multiarray_copy[0][0]);
263: assertNotSame(long_multiarray_orig[1][0],
264: long_multiarray_copy[1][0]);
265: assertEquals(long_multiarray_orig[0][0],
266: long_multiarray_copy[0][0]);
267: assertEquals(long_multiarray_orig[1][0],
268: long_multiarray_copy[1][0]);
269:
270: String[][][] string_multiarray_orig = new String[][][] {
271: { { "000", "001" }, { "010", "011" },
272: { "020", "021" } },
273: { { "100", "101" }, { "110", "111" },
274: { "120", "121" } } };
275: String[][][] string_multiarray_copy = ObjectUtils
276: .deepClone(string_multiarray_orig);
277: assertNotSame(string_multiarray_orig,
278: string_multiarray_copy);
279: assertEquals(string_multiarray_orig.length,
280: string_multiarray_copy.length);
281: assertNotSame(string_multiarray_orig[0],
282: string_multiarray_copy[0]);
283: assertNotSame(string_multiarray_orig[1],
284: string_multiarray_copy[1]);
285: assertEquals(string_multiarray_orig[0].length,
286: string_multiarray_copy[0].length);
287: assertNotSame(string_multiarray_orig[0][0],
288: string_multiarray_copy[0][0]);
289: assertNotSame(string_multiarray_orig[0][1],
290: string_multiarray_copy[0][1]);
291: assertEquals(string_multiarray_orig[1].length,
292: string_multiarray_copy[1].length);
293: assertNotSame(string_multiarray_orig[1][0],
294: string_multiarray_copy[1][0]);
295: assertNotSame(string_multiarray_orig[1][1],
296: string_multiarray_copy[1][1]);
297: assertSame(string_multiarray_copy[0][0][0],
298: string_multiarray_copy[0][0][0]);
299: assertSame(string_multiarray_copy[0][0][1],
300: string_multiarray_copy[0][0][1]);
301: assertSame(string_multiarray_copy[0][1][0],
302: string_multiarray_copy[0][1][0]);
303: assertSame(string_multiarray_copy[0][1][1],
304: string_multiarray_copy[0][1][1]);
305: assertSame(string_multiarray_copy[0][2][0],
306: string_multiarray_copy[0][2][0]);
307: assertSame(string_multiarray_copy[0][2][1],
308: string_multiarray_copy[0][2][1]);
309: assertSame(string_multiarray_copy[1][0][0],
310: string_multiarray_copy[1][0][0]);
311: assertSame(string_multiarray_copy[1][0][1],
312: string_multiarray_copy[1][0][1]);
313: assertSame(string_multiarray_copy[1][1][0],
314: string_multiarray_copy[1][1][0]);
315: assertSame(string_multiarray_copy[1][1][1],
316: string_multiarray_copy[1][1][1]);
317: assertSame(string_multiarray_copy[1][2][0],
318: string_multiarray_copy[1][2][0]);
319: assertSame(string_multiarray_copy[1][2][1],
320: string_multiarray_copy[1][2][1]);
321: assertEquals(string_multiarray_copy[0][0][0],
322: string_multiarray_copy[0][0][0]);
323: assertEquals(string_multiarray_copy[0][0][1],
324: string_multiarray_copy[0][0][1]);
325: assertEquals(string_multiarray_copy[0][1][0],
326: string_multiarray_copy[0][1][0]);
327: assertEquals(string_multiarray_copy[0][1][1],
328: string_multiarray_copy[0][1][1]);
329: assertEquals(string_multiarray_copy[0][2][0],
330: string_multiarray_copy[0][2][0]);
331: assertEquals(string_multiarray_copy[0][2][1],
332: string_multiarray_copy[0][2][1]);
333: assertEquals(string_multiarray_copy[1][0][0],
334: string_multiarray_copy[1][0][0]);
335: assertEquals(string_multiarray_copy[1][0][1],
336: string_multiarray_copy[1][0][1]);
337: assertEquals(string_multiarray_copy[1][1][0],
338: string_multiarray_copy[1][1][0]);
339: assertEquals(string_multiarray_copy[1][1][1],
340: string_multiarray_copy[1][1][1]);
341: assertEquals(string_multiarray_copy[1][2][0],
342: string_multiarray_copy[1][2][0]);
343: assertEquals(string_multiarray_copy[1][2][1],
344: string_multiarray_copy[1][2][1]);
345:
346: Date[][] date_multiarray_orig = new Date[][] {
347: { new Date() }, { new Date() } };
348: Date[][] date_multiarray_copy = ObjectUtils
349: .deepClone(date_multiarray_orig);
350: assertNotSame(date_multiarray_orig, date_multiarray_copy);
351: assertEquals(date_multiarray_orig.length,
352: date_multiarray_copy.length);
353: assertNotSame(date_multiarray_orig[0],
354: date_multiarray_copy[0]);
355: assertNotSame(date_multiarray_orig[1],
356: date_multiarray_copy[1]);
357: assertNotSame(date_multiarray_orig[0][0],
358: date_multiarray_copy[0][0]);
359: assertNotSame(date_multiarray_orig[1][0],
360: date_multiarray_copy[1][0]);
361: assertEquals(date_multiarray_orig[0][0],
362: date_multiarray_copy[0][0]);
363: assertEquals(date_multiarray_orig[1][0],
364: date_multiarray_copy[1][0]);
365:
366: ArrayList<String> string_collection_orig = new ArrayList<String>();
367: string_collection_orig.add("one");
368: string_collection_orig.add("two");
369: ArrayList<String> string_collection_copy = ObjectUtils
370: .deepClone(string_collection_orig);
371: assertNotSame(string_collection_orig,
372: string_collection_copy);
373: assertEquals(string_collection_orig.size(),
374: string_collection_copy.size());
375: assertSame(string_collection_orig.get(0),
376: string_collection_copy.get(0));
377: assertSame(string_collection_orig.get(1),
378: string_collection_copy.get(1));
379:
380: ArrayList<Date> date_collection_orig = new ArrayList<Date>();
381: date_collection_orig.add(new Date());
382: date_collection_orig.add(new Date());
383: ArrayList<Date> date_collection_copy = ObjectUtils
384: .deepClone(date_collection_orig);
385: assertNotSame(date_collection_orig, date_collection_copy);
386: assertEquals(date_collection_orig.size(),
387: date_collection_copy.size());
388: assertNotSame(date_collection_orig.get(0),
389: date_collection_copy.get(0));
390: assertNotSame(date_collection_orig.get(1),
391: date_collection_copy.get(1));
392: assertEquals(date_collection_orig.get(0),
393: date_collection_copy.get(0));
394: assertEquals(date_collection_orig.get(1),
395: date_collection_copy.get(1));
396:
397: LinkedHashMap<String, String> string_map_orig = new LinkedHashMap<String, String>();
398: string_map_orig.put("k1", "v1");
399: string_map_orig.put("k2", "v2");
400: string_map_orig.put("k3", "v3");
401: LinkedHashMap<String, String> string_map_copy = ObjectUtils
402: .deepClone(string_map_orig);
403: assertNotSame(string_map_orig, string_map_copy);
404: assertEquals(string_map_orig.size(), string_map_copy.size());
405: Set<Map.Entry<String, String>> string_map_orig_entries = string_map_orig
406: .entrySet();
407: Iterator<Map.Entry<String, String>> string_map_orig_entries_it = string_map_orig_entries
408: .iterator();
409: Set<Map.Entry<String, String>> string_map_copy_entries = string_map_copy
410: .entrySet();
411: Iterator<Map.Entry<String, String>> string_map_copy_entries_it = string_map_copy_entries
412: .iterator();
413: Map.Entry<String, String> string_map_orig_entry = null;
414: Map.Entry<String, String> string_map_copy_entry = null;
415: while (string_map_orig_entries_it.hasNext()) {
416: string_map_orig_entry = string_map_orig_entries_it
417: .next();
418: string_map_copy_entry = string_map_copy_entries_it
419: .next();
420:
421: assertSame(string_map_orig_entry.getKey(),
422: string_map_copy_entry.getKey());
423: assertSame(string_map_orig_entry.getValue(),
424: string_map_copy_entry.getValue());
425: }
426:
427: LinkedHashMap<Date, Date> date_map_orig = new LinkedHashMap<Date, Date>();
428: date_map_orig.put(new Date(), new Date());
429: date_map_orig.put(new Date(), new Date());
430: date_map_orig.put(new Date(), new Date());
431: LinkedHashMap<Date, Date> date_map_copy = ObjectUtils
432: .deepClone(date_map_orig);
433: assertNotSame(date_map_orig, date_map_copy);
434: assertEquals(date_map_orig.size(), date_map_copy.size());
435: Set<Map.Entry<Date, Date>> date_map_orig_entries = date_map_orig
436: .entrySet();
437: Iterator<Map.Entry<Date, Date>> date_map_orig_entries_it = date_map_orig_entries
438: .iterator();
439: Set<Map.Entry<Date, Date>> date_map_copy_entries = date_map_copy
440: .entrySet();
441: Iterator<Map.Entry<Date, Date>> date_map_copy_entries_it = date_map_copy_entries
442: .iterator();
443: Map.Entry<Date, Date> date_map_orig_entry = null;
444: Map.Entry<Date, Date> date_map_copy_entry = null;
445: while (date_map_orig_entries_it.hasNext()) {
446: date_map_orig_entry = date_map_orig_entries_it.next();
447: date_map_copy_entry = date_map_copy_entries_it.next();
448:
449: assertNotSame(date_map_orig_entry.getKey(),
450: date_map_copy_entry.getKey());
451: assertEquals(date_map_orig_entry.getKey(),
452: date_map_copy_entry.getKey());
453: assertNotSame(date_map_orig_entry.getValue(),
454: date_map_copy_entry.getValue());
455: assertEquals(date_map_orig_entry.getValue(),
456: date_map_copy_entry.getValue());
457: }
458: } catch (CloneNotSupportedException e) {
459: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
460: }
461: }
462:
463: public void testGetBaseClass() {
464: assertSame(Void.TYPE, ObjectUtils.getBaseClass(null));
465: assertSame(Object.class, ObjectUtils.getBaseClass(new Object()));
466: assertSame(String.class, ObjectUtils.getBaseClass(new String()));
467: assertSame(short.class, ObjectUtils.getBaseClass(new short[0]));
468: assertSame(int.class, ObjectUtils.getBaseClass(new int[0]));
469: assertSame(long.class, ObjectUtils.getBaseClass(new long[0]));
470: assertSame(float.class, ObjectUtils.getBaseClass(new float[0]));
471: assertSame(double.class, ObjectUtils
472: .getBaseClass(new double[0]));
473: assertSame(boolean.class, ObjectUtils
474: .getBaseClass(new boolean[0]));
475: assertSame(char.class, ObjectUtils.getBaseClass(new char[0]));
476: assertSame(byte.class, ObjectUtils.getBaseClass(new byte[0]));
477: assertSame(String.class, ObjectUtils
478: .getBaseClass(new String[0]));
479: assertSame(short.class, ObjectUtils
480: .getBaseClass(new short[0][1]));
481: assertSame(Integer.class, ObjectUtils
482: .getBaseClass(new Integer[7][8]));
483: }
484: }
485:
486: class ProtectedCloneable extends Uncloneable implements Cloneable {
487: public ProtectedCloneable(int value) {
488: super (value);
489: }
490:
491: protected ProtectedCloneable clone()
492: throws CloneNotSupportedException {
493: return (ProtectedCloneable) super .clone();
494: }
495: }
496:
497: class Uncloneable {
498: private int mValue = 0;
499:
500: public Uncloneable(int value) {
501: mValue = value;
502: }
503:
504: public void setValue(int value) {
505: mValue = value;
506: }
507:
508: public int getValue() {
509: return mValue;
510: }
511: }
|