1 10 11 package com.triactive.jdo.model.test.widgets; 12 13 import com.triactive.jdo.model.*; 14 import java.util.Random ; 15 import junit.framework.Assert; 16 17 18 24 25 public class Widget 26 { 27 private static Random r = new Random (0); 28 29 private String fixedLengthString; 30 private String normalString; 31 private String hugeString; 32 private boolean booleanField; 33 private Boolean booleanObjField; 34 private byte byteField; 35 private Byte byteObjField; 36 private short shortField; 37 private Short shortObjField; 38 private int intField; 39 private Integer intObjField; 40 private long longField; 41 private Long longObjField; 42 43 44 47 48 public Widget() 49 { 50 } 51 52 53 58 59 public void fillRandom() 60 { 61 StringBuffer s = new StringBuffer ("Fixed " + r.nextInt()); 62 63 while (s.length() < 20) 64 s.append(' '); 65 66 fixedLengthString = r.nextBoolean() ? null : s.toString(); 67 normalString = r.nextBoolean() ? null : "Normal " + r.nextInt(); 68 hugeString = r.nextBoolean() ? null : "Huge " + r.nextInt(); 69 booleanField = r.nextBoolean(); 70 booleanObjField = r.nextBoolean() ? null : new Boolean (r.nextBoolean()); 71 byteField = (byte)r.nextInt(Byte.MAX_VALUE); 72 byteObjField = r.nextBoolean() ? null : new Byte ((byte)r.nextInt(Byte.MAX_VALUE)); 73 shortField = (short)(r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE); 74 shortObjField = r.nextBoolean() ? null : new Short ((short)(r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE)); 75 intField = r.nextInt(); 76 intObjField = r.nextBoolean() ? null : new Integer (r.nextInt()); 77 longField = r.nextLong(); 78 longObjField = r.nextBoolean() ? null : new Long (r.nextLong()); 79 } 80 81 82 93 94 public boolean equals(Object obj) 95 { 96 if (obj == this) 97 return true; 98 99 if (!(obj instanceof Widget)) 100 return false; 101 102 Widget w = (Widget)obj; 103 104 if (fixedLengthString == null) { if (w.fixedLengthString != null) return false; } 105 else if (!fixedLengthString.equals(w.fixedLengthString)) return false; 106 107 if (normalString == null) { if (w.normalString != null) return false; } 108 else if (!normalString.equals(w.normalString)) return false; 109 110 if (hugeString == null) { if (w.hugeString != null) return false; } 111 else if (!hugeString.equals(w.hugeString)) return false; 112 113 if (booleanObjField == null) { if (w.booleanObjField != null) return false; } 114 else if (!booleanObjField.equals(w.booleanObjField)) return false; 115 116 if (byteObjField == null) { if (w.byteObjField != null) return false; } 117 else if (!byteObjField.equals(w.byteObjField)) return false; 118 119 if (shortObjField == null) { if (w.shortObjField != null) return false; } 120 else if (!shortObjField.equals(w.shortObjField)) return false; 121 122 if (intObjField == null) { if (w.intObjField != null) return false; } 123 else if (!intObjField.equals(w.intObjField)) return false; 124 125 if (longObjField == null) { if (w.longObjField != null) return false; } 126 else if (!longObjField.equals(w.longObjField)) return false; 127 128 return booleanField == w.booleanField 129 && byteField == w.byteField 130 && shortField == w.shortField 131 && intField == w.intField 132 && longField == w.longField; 133 } 134 135 136 141 142 public int hashCode() 143 { 144 return (fixedLengthString == null ? 0 : fixedLengthString.hashCode()) 145 ^ (normalString == null ? 0 : normalString.hashCode()) 146 ^ (hugeString == null ? 0 : hugeString.hashCode()) 147 ^ (booleanField ? 1 : 0) 148 ^ (int)byteField 149 ^ (int)shortField 150 ^ intField 151 ^ new Long (longField).hashCode() 152 ^ (booleanObjField == null ? 0 : booleanObjField.hashCode()) 153 ^ (byteObjField == null ? 0 : byteObjField.hashCode()) 154 ^ (shortObjField == null ? 0 : shortObjField.hashCode()) 155 ^ (intObjField == null ? 0 : intObjField.hashCode()) 156 ^ (longObjField == null ? 0 : longObjField.hashCode()); 157 } 158 159 160 166 167 public String toString() 168 { 169 StringBuffer s = new StringBuffer ("Widget:"); 170 171 s.append("\n fixedLengthString = ").append(fixedLengthString); 172 s.append("\n normalString = ").append(normalString); 173 s.append("\n hugeString = ").append(hugeString); 174 s.append("\n booleanField = ").append(booleanField); 175 s.append("\n booleanObjField = ").append(booleanObjField); 176 s.append("\n byteField = ").append(byteField); 177 s.append("\n byteObjField = ").append(byteObjField); 178 s.append("\n shortField = ").append(shortField); 179 s.append("\n shortObjField = ").append(shortObjField); 180 s.append("\n intField = ").append(intField); 181 s.append("\n intObjField = ").append(intObjField); 182 s.append("\n longField = ").append(longField); 183 s.append("\n longObjField = ").append(longObjField); 184 s.append('\n'); 185 186 return s.toString(); 187 } 188 189 190 196 197 public static void assertValidMetaData(ClassMetaData cmd, Assert test) 198 { 199 test.assertNotNull("Metadata", cmd); 200 test.assertEquals(Widget.class, cmd.getPCClass()); 201 test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName()); 202 test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("package.jdo") >= 0); 203 test.assertNull("Superclass", cmd.getPCSuperclass()); 204 test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType()); 205 test.assertNull("Identity class", cmd.getIdentityClass()); 206 207 String [] sortedFieldNames = new String [] 208 { 209 "booleanField", 210 "booleanObjField", 211 "byteField", 212 "byteObjField", 213 "fixedLengthString", 214 "hugeString", 215 "intField", 216 "intObjField", 217 "longField", 218 "longObjField", 219 "normalString", 220 "shortField", 221 "shortObjField" 222 }; 223 224 int[] nullValueHandlings = new int[] 225 { 226 FieldMetaData.NULL_VALUE_EXCEPTION, 227 FieldMetaData.NULL_VALUE_NONE, 228 FieldMetaData.NULL_VALUE_EXCEPTION, 229 FieldMetaData.NULL_VALUE_NONE, 230 FieldMetaData.NULL_VALUE_NONE, 231 FieldMetaData.NULL_VALUE_NONE, 232 FieldMetaData.NULL_VALUE_EXCEPTION, 233 FieldMetaData.NULL_VALUE_NONE, 234 FieldMetaData.NULL_VALUE_EXCEPTION, 235 FieldMetaData.NULL_VALUE_NONE, 236 FieldMetaData.NULL_VALUE_NONE, 237 FieldMetaData.NULL_VALUE_EXCEPTION, 238 FieldMetaData.NULL_VALUE_NONE 239 }; 240 241 String [] lengthExtensions = new String [] 242 { 243 null, 244 null, 245 null, 246 null, 247 "20", 248 "unlimited", 249 null, 250 null, 251 null, 252 null, 253 "max 20", 254 null, 255 null 256 }; 257 258 test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount()); 259 260 for (int i = 0; i < sortedFieldNames.length; ++i) 261 { 262 FieldMetaData fmd = cmd.getFieldRelative(i); 263 String s = sortedFieldNames[i]; 264 265 test.assertEquals(s, fmd.getName()); 266 test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier()); 267 test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart()); 268 test.assertEquals(s + " null value handling", nullValueHandlings[i], fmd.getNullValueHandling()); 269 test.assertEquals(s + " default fetch group", i != 5, fmd.isInDefaultFetchGroup()); 270 test.assertNull(s + " array metadata", fmd.getArrayMetaData()); 271 test.assertNull(s + " collection metadata", fmd.getCollectionMetaData()); 272 test.assertNull(s + " map metadata", fmd.getMapMetaData()); 273 test.assertEquals(s + " length extension", lengthExtensions[i], fmd.getLength()); 274 } 275 } 276 } 277 | Popular Tags |