1 10 11 package com.triactive.jdo.test; 12 13 import com.triactive.jdo.store.OID; 14 import java.io.Serializable ; 15 import java.util.ArrayList ; 16 import java.util.Collection ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Map.Entry; 21 import java.util.Random ; 22 import javax.jdo.JDOHelper; 23 24 25 public abstract class TestObject implements Cloneable , Serializable 26 { 27 protected static Random r = new Random (0); 28 29 public static boolean allowNegativeByteValues = true; 30 31 32 protected byte nextByte() 33 { 34 if (allowNegativeByteValues) 35 return (byte)(r.nextInt((int)Byte.MAX_VALUE * 2) - Byte.MAX_VALUE); 36 else 37 return (byte)r.nextInt((int)Byte.MAX_VALUE + 1); 38 } 39 40 41 protected char nextCharacter() 42 { 43 return (char)('!' + r.nextInt(93)); 44 } 45 46 47 protected String nextString(int length) 48 { 49 StringBuffer s = new StringBuffer (); 50 51 while (length-- > 0) 52 s.append(nextCharacter()); 53 54 return s.toString(); 55 } 56 57 58 protected byte[] nextBinary(int length) 59 { 60 byte[] ba = new byte[length]; 61 62 for (int i = 0; i < length; ++i) 63 ba[i] = (byte)(r.nextInt((int)Byte.MAX_VALUE * 2) - Byte.MAX_VALUE); 64 65 return ba; 66 } 67 68 69 73 protected boolean nextNull() 74 { 75 return r.nextInt(5) < 1; 76 } 77 78 79 public Object clone() 80 { 81 Object obj = null; 82 83 try { obj = super.clone(); } catch (CloneNotSupportedException e) {} 84 85 return obj; 86 } 87 88 89 public abstract void fillRandom(); 90 91 92 103 104 public abstract boolean compareTo(Object obj); 105 106 107 public boolean equals(Object obj) 108 { 109 if (obj == this) 110 return true; 111 112 Object id = JDOHelper.getObjectId(this); 113 114 return id == null ? super.equals(obj) : id.equals(JDOHelper.getObjectId(obj)); 115 } 116 117 118 public int hashCode() 119 { 120 Object id = JDOHelper.getObjectId(this); 121 122 return id == null ? super.hashCode() : id.hashCode(); 123 } 124 125 public String toString() 126 { 127 StringBuffer s = new StringBuffer (getClass().getName() + ":"); 128 129 s.append(" JVM id = ").append(System.identityHashCode(this)); 130 s.append('\n'); 131 132 Object id = JDOHelper.getObjectId(this); 133 s.append(" JDO id = ").append(id); 134 if (id instanceof OID) 135 s.append(" (").append(((OID)id).longValue()).append(')'); 136 s.append('\n'); 137 138 return s.toString(); 139 } 140 141 142 public static Object clone(Object o) 143 { 144 if (o instanceof Number || o instanceof String || o instanceof Boolean ) 145 return o; 146 147 try 148 { 149 return o.getClass().getMethod("clone", null).invoke(o, null); 150 } 151 catch (RuntimeException e) 152 { 153 throw e; 154 } 155 catch (Exception e) 156 { 157 throw new RuntimeException ("Clone failed", e); 158 } 159 } 160 161 162 173 174 public static boolean compareObject(Object o1, Object o2) 175 { 176 if (o1 == null) 177 return o2 == null; 178 else if (o1 instanceof TestObject) 179 return ((TestObject)o1).compareTo(o2); 180 else 181 return o1.equals(o2); 182 } 183 184 185 195 196 public static boolean compareCollection(Collection c1, Collection c2) 197 { 198 if (c1 == null) 199 return c2 == null; 200 else if (c2 == null) 201 return false; 202 203 if (c1.size() != c2.size()) 204 return false; 205 206 c2 = new ArrayList (c2); 207 208 Iterator i = c1.iterator(); 209 210 while (i.hasNext()) 211 { 212 Object obj = i.next(); 213 214 boolean found = false; 215 Iterator j = c2.iterator(); 216 217 while (j.hasNext()) 218 { 219 if (compareObject(obj, j.next())) 220 { 221 j.remove(); 222 found = true; 223 break; 224 } 225 } 226 227 if (!found) 228 return false; 229 } 230 231 return c2.isEmpty(); 232 } 233 234 235 245 246 public static boolean compareMap(Map m1, Map m2) 247 { 248 if (m1 == null) 249 return m2 == null; 250 else if (m2 == null) 251 return false; 252 253 if (m1.size() != m2.size()) 254 return false; 255 256 m2 = new HashMap (m2); 257 258 Iterator i = m1.entrySet().iterator(); 259 260 while (i.hasNext()) 261 { 262 Entry e1 = (Entry)i.next(); 263 264 boolean found = false; 265 Iterator j = m2.entrySet().iterator(); 266 267 while (j.hasNext()) 268 { 269 Entry e2 = (Entry)j.next(); 270 271 if (compareObject(e1.getKey(), e2.getKey()) && 272 compareObject(e1.getValue(), e2.getValue())) 273 { 274 j.remove(); 275 found = true; 276 break; 277 } 278 } 279 280 if (!found) 281 return false; 282 } 283 284 return m2.isEmpty(); 285 } 286 } 287 | Popular Tags |