1 10 11 package com.triactive.jdo.model.test.widgets; 12 13 import com.triactive.jdo.model.*; 14 import java.util.Arrays ; 15 import java.util.Random ; 16 import junit.framework.Assert; 17 18 19 26 27 public class BinaryWidget extends Widget 28 { 29 private byte[] fixedLengthBinary; 30 private byte[] varLengthBinary; 31 private byte[] hugeBinary; 32 33 34 37 38 public BinaryWidget() 39 { 40 super(); 41 } 42 43 private static Random r = new Random (0); 44 45 46 51 52 public void fillRandom() 53 { 54 super.fillRandom(); 55 56 fixedLengthBinary = r.nextBoolean() ? null : new byte[20]; 57 varLengthBinary = r.nextBoolean() ? null : new byte[r.nextInt(20) + 1]; 58 hugeBinary = r.nextBoolean() ? null : new byte[r.nextInt(128) + 1]; 59 60 if (fixedLengthBinary != null) 61 r.nextBytes(fixedLengthBinary); 62 63 if (varLengthBinary != null) 64 r.nextBytes(varLengthBinary); 65 66 if (hugeBinary != null) 67 r.nextBytes(hugeBinary); 68 } 69 70 71 82 83 public boolean equals(Object obj) 84 { 85 if (this == obj) 86 return true; 87 88 if (!(obj instanceof BinaryWidget) || !super.equals(obj)) 89 return false; 90 91 BinaryWidget w = (BinaryWidget)obj; 92 93 return Arrays.equals(fixedLengthBinary, w.fixedLengthBinary) 94 && Arrays.equals(varLengthBinary, w.varLengthBinary) 95 && Arrays.equals(hugeBinary, w.hugeBinary); 96 } 97 98 99 104 105 public int hashCode() 106 { 107 return (fixedLengthBinary == null ? 0 : hashCode(fixedLengthBinary)) 108 ^ (varLengthBinary == null ? 0 : hashCode(varLengthBinary)) 109 ^ (hugeBinary == null ? 0 : hashCode(hugeBinary)); 110 } 111 112 113 120 121 private int hashCode(byte[] bytes) 122 { 123 int hc = 0; 124 int leftShift = 0; 125 126 for (int i = 0; i < bytes.length; ++i) 127 { 128 hc ^= bytes[i] << leftShift; 129 130 if ((leftShift += 8) > 24) 131 leftShift = 0; 132 } 133 134 return hc; 135 } 136 137 138 144 145 public String toString() 146 { 147 StringBuffer s = new StringBuffer (super.toString()); 148 149 s.append(" fixedLengthBinary = binary"); 150 151 if (fixedLengthBinary != null) 152 s.append(", hashCode = ").append(hashCode(fixedLengthBinary)) 153 .append(", bytes = ").append(toHexString(fixedLengthBinary)); 154 else 155 s.append(" (null)"); 156 157 s.append("\n varLengthBinary = binary"); 158 159 if (varLengthBinary != null) 160 s.append(", length = ").append(varLengthBinary.length) 161 .append(", hashCode = ").append(hashCode(varLengthBinary)) 162 .append(", bytes = ").append(toHexString(varLengthBinary)); 163 else 164 s.append(" (null)"); 165 166 s.append("\n hugeBinary = binary"); 167 168 if (hugeBinary != null) 169 s.append(", length = ").append(hugeBinary.length) 170 .append(", hashCode = ").append(hashCode(hugeBinary)) 171 .append(", bytes = ").append(toHexString(hugeBinary)); 172 else 173 s.append(" (null)"); 174 175 s.append('\n'); 176 177 return s.toString(); 178 } 179 180 181 188 189 private static String toHexString(byte[] bytes) 190 { 191 StringBuffer s = new StringBuffer (); 192 193 for (int i = 0; i < bytes.length; ++i) 194 s.append(Integer.toHexString((int)bytes[i] & 0xff)); 195 196 return s.toString(); 197 } 198 199 200 206 207 public static void assertValidMetaData(ClassMetaData cmd, Assert test) 208 { 209 test.assertNotNull("Metadata", cmd); 210 test.assertEquals(BinaryWidget.class, cmd.getPCClass()); 211 test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName()); 212 test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("BinaryWidget.jdo") >= 0); 213 test.assertEquals("Superclass", Widget.class, cmd.getPCSuperclass()); 214 test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType()); 215 test.assertNull("Identity class", cmd.getIdentityClass()); 216 217 String [] sortedFieldNames = new String [] 218 { 219 "fixedLengthBinary", 220 "hugeBinary", 221 "varLengthBinary" 222 }; 223 224 String [] lengthExtensions = new String [] 225 { 226 "20", 227 "unlimited", 228 "max 20" 229 }; 230 231 test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount()); 232 233 for (int i = 0; i < sortedFieldNames.length; ++i) 234 { 235 FieldMetaData fmd = cmd.getFieldRelative(i); 236 ArrayMetaData amd = fmd.getArrayMetaData(); 237 String s = sortedFieldNames[i]; 238 239 test.assertEquals(s, fmd.getName()); 240 test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier()); 241 test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart()); 242 test.assertEquals(s + " null value handling", FieldMetaData.NULL_VALUE_NONE, fmd.getNullValueHandling()); 243 test.assertEquals(s + " default fetch group", false, fmd.isInDefaultFetchGroup()); 244 test.assertEquals(s + " array metadata element type", byte.class, amd.getElementType()); 245 test.assertTrue(s + " array metadata embedded flag", amd.isEmbeddedElement()); 246 test.assertNull(s + " collection metadata", fmd.getCollectionMetaData()); 247 test.assertNull(s + " map metadata", fmd.getMapMetaData()); 248 test.assertEquals(s + " length extension", lengthExtensions[i], fmd.getLength()); 249 } 250 } 251 } 252 | Popular Tags |