1 10 11 package com.triactive.jdo.model.test.widgets; 12 13 import com.triactive.jdo.model.*; 14 import java.math.BigDecimal ; 15 import java.math.BigInteger ; 16 import java.util.Random ; 17 import junit.framework.Assert; 18 19 20 27 28 public class FloatWidget extends BinaryWidget 29 { 30 private float floatField; 31 private double doubleField; 32 private Float floatObjField; 33 private Double doubleObjField; 34 private BigDecimal bigDecimalField; 35 private BigInteger bigIntegerField; 36 37 38 41 42 public FloatWidget() 43 { 44 super(); 45 } 46 47 private static Random r = new Random (0); 48 49 50 55 56 public void fillRandom() 57 { 58 super.fillRandom(); 59 60 floatField = r.nextFloat(); 61 doubleField = r.nextDouble(); 62 floatObjField = r.nextBoolean() ? null : new Float (r.nextFloat()); 63 doubleObjField = r.nextBoolean() ? null : new Double (r.nextDouble()); 64 bigDecimalField = r.nextBoolean() ? null : BigDecimal.valueOf((long)r.nextInt(), 2); 65 bigIntegerField = r.nextBoolean() ? null : BigInteger.valueOf((long)r.nextInt()); 66 } 67 68 69 80 81 public boolean equals(Object obj) 82 { 83 if (this == obj) 84 return true; 85 86 if (!(obj instanceof FloatWidget) || !super.equals(obj)) 87 return false; 88 89 FloatWidget w = (FloatWidget)obj; 90 91 100 101 if (Math.abs(floatField - w.floatField) >= 1e-6) return false; 102 if (Math.abs(doubleField - w.doubleField) >= 1e-15) return false; 103 104 if (floatObjField == null) { if (w.floatObjField != null) return false; } 105 else if (Math.abs(floatObjField.floatValue() - w.floatObjField.floatValue()) >= 1e-6) return false; 106 107 if (doubleObjField == null) { if (w.doubleObjField != null) return false; } 108 else if (Math.abs(doubleObjField.doubleValue() - w.doubleObjField.doubleValue()) >= 1e-15) return false; 109 110 114 115 if (bigDecimalField == null) { if (w.bigDecimalField != null) return false; } 116 else if (!bigDecimalField.equals(w.bigDecimalField)) return false; 117 118 if (bigIntegerField == null) { if (w.bigIntegerField != null) return false; } 119 else if (!bigIntegerField.equals(w.bigIntegerField)) return false; 120 121 return true; 122 } 123 124 125 130 131 public int hashCode() 132 { 133 138 139 return (int)(floatField * 1e6) 140 ^ (int)(doubleField * 1e9) 141 ^ (floatObjField == null ? 0 : (int)(floatObjField.floatValue() * 1e6)) 142 ^ (doubleObjField == null ? 0 : (int)(doubleObjField.doubleValue() * 1e9)) 143 ^ (bigDecimalField == null ? 0 : bigDecimalField.hashCode()) 144 ^ (bigIntegerField == null ? 0 : bigIntegerField.hashCode()); 145 } 146 147 148 154 155 public String toString() 156 { 157 StringBuffer s = new StringBuffer (super.toString()); 158 159 s.append(" floatField = ").append(floatField); 160 s.append("\n doubleField = ").append(doubleField); 161 s.append("\n bigDecimalField = ").append(bigDecimalField); 162 s.append("\n bigIntegerField = ").append(bigIntegerField); 163 s.append('\n'); 164 165 return s.toString(); 166 } 167 168 169 175 176 public static void assertValidMetaData(ClassMetaData cmd, Assert test) 177 { 178 test.assertNotNull("Metadata", cmd); 179 test.assertEquals(FloatWidget.class, cmd.getPCClass()); 180 test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName()); 181 test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("package.jdo") >= 0); 182 test.assertEquals("Superclass", BinaryWidget.class, cmd.getPCSuperclass()); 183 test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType()); 184 test.assertNull("Identity class", cmd.getIdentityClass()); 185 186 String [] sortedFieldNames = new String [] 187 { 188 "bigDecimalField", 189 "bigIntegerField", 190 "doubleField", 191 "doubleObjField", 192 "floatField", 193 "floatObjField" 194 }; 195 196 int[] nullValueHandlings = new int[] 197 { 198 FieldMetaData.NULL_VALUE_NONE, 199 FieldMetaData.NULL_VALUE_NONE, 200 FieldMetaData.NULL_VALUE_EXCEPTION, 201 FieldMetaData.NULL_VALUE_NONE, 202 FieldMetaData.NULL_VALUE_EXCEPTION, 203 FieldMetaData.NULL_VALUE_NONE 204 }; 205 206 test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount()); 207 208 for (int i = 0; i < sortedFieldNames.length; ++i) 209 { 210 FieldMetaData fmd = cmd.getFieldRelative(i); 211 String s = sortedFieldNames[i]; 212 213 test.assertEquals(s, fmd.getName()); 214 test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier()); 215 test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart()); 216 test.assertEquals(s + " null value handling", nullValueHandlings[i], fmd.getNullValueHandling()); 217 test.assertEquals(s + " default fetch group", true, fmd.isInDefaultFetchGroup()); 218 test.assertNull(s + " array metadata", fmd.getArrayMetaData()); 219 test.assertNull(s + " collection metadata", fmd.getCollectionMetaData()); 220 test.assertNull(s + " map metadata", fmd.getMapMetaData()); 221 } 222 } 223 } 224 | Popular Tags |