1 43 44 package org.jfree.util.junit; 45 46 import java.awt.Point ; 47 import java.util.ArrayList ; 48 import java.util.Collection ; 49 50 import junit.framework.Test; 51 import junit.framework.TestCase; 52 import junit.framework.TestSuite; 53 54 import org.jfree.util.ObjectUtilities; 55 56 59 public class ObjectUtilitiesTests extends TestCase { 60 61 66 public static Test suite() { 67 return new TestSuite(ObjectUtilitiesTests.class); 68 } 69 70 75 public ObjectUtilitiesTests(String name) { 76 super(name); 77 } 78 79 89 public void testCloneObject() throws CloneNotSupportedException { 90 Object obj; 91 Object clone; 92 93 boolean pass = false; 95 obj = "Hello World"; 96 try { 97 clone = ObjectUtilities.clone(obj); 98 pass = false; 99 } 100 catch (CloneNotSupportedException e) { 101 pass = true; 102 } 103 assertTrue(pass); 104 105 pass = false; 107 obj = new Integer (123); 108 try { 109 clone = ObjectUtilities.clone(obj); 110 pass = false; 111 } 112 catch (CloneNotSupportedException e) { 113 pass = true; 114 } 115 assertTrue(pass); 116 117 obj = new Point (1, 2); 119 clone = ObjectUtilities.clone(obj); 120 assertEquals(obj, clone); 121 122 obj = null; 124 try { 125 clone = ObjectUtilities.clone(obj); 126 pass = false; 127 } 128 catch (IllegalArgumentException e) { 129 pass = true; 130 } 131 assertTrue(pass); 132 } 133 134 137 public void testDeepClone() { 138 Collection c1 = new ArrayList (); 139 Collection c2 = null; 140 141 try { 143 c2 = ObjectUtilities.deepClone(c1); 144 assertTrue(c2.isEmpty()); 145 } 146 catch (CloneNotSupportedException e) { 147 assertTrue(false); 148 } 149 150 c1 = new ArrayList (); 152 c1.add(new Point (1, 2)); 153 c1.add(new Point (3, 4)); 154 try { 155 c2 = ObjectUtilities.deepClone(c1); 156 assertEquals(2, c2.size()); 157 assertTrue(c2.contains(new Point (1, 2))); 158 assertTrue(c2.contains(new Point (3, 4))); 159 } 160 catch (CloneNotSupportedException e) { 161 assertTrue(false); 162 } 163 164 c1 = new ArrayList (); 166 c1.add(new Point (1, 2)); 167 c1.add(null); 168 c1.add(new Point (3, 4)); 169 try { 170 c2 = ObjectUtilities.deepClone(c1); 171 assertEquals(3, c2.size()); 172 assertTrue(c2.contains(new Point (1, 2))); 173 assertTrue(c2.contains(new Point (3, 4))); 174 } 175 catch (CloneNotSupportedException e) { 176 assertTrue(false); 177 } 178 179 c1.clear(); 181 c1.add("S1"); 182 c1.add("S2"); 183 try { 184 c2 = ObjectUtilities.deepClone(c1); 185 assertTrue(false); } 187 catch (CloneNotSupportedException e) { 188 assertTrue(true); 189 } 190 191 try { 193 c2 = ObjectUtilities.deepClone(null); 194 assertTrue(false); } 196 catch (IllegalArgumentException e) { 197 assertTrue(true); 198 } 199 catch (CloneNotSupportedException e) { 200 assertTrue(false); 201 } 202 203 } 204 205 } 206 | Popular Tags |