1 10 11 package com.triactive.jdo.model.test.widgets; 12 13 import com.triactive.jdo.model.*; 14 import java.sql.Date ; 15 import java.sql.Time ; 16 import java.sql.Timestamp ; 17 import java.util.Random ; 18 import java.util.Calendar ; 19 import junit.framework.Assert; 20 21 22 29 30 public class DateWidget extends BinaryWidget 31 { 32 private Date dateField; 33 private Time timeField; 34 private Timestamp timestampField; 35 36 37 40 41 public DateWidget() 42 { 43 super(); 44 } 45 46 47 private static Random r = new Random (0); 48 49 54 55 public void fillRandom() 56 { 57 super.fillRandom(); 58 59 long now = System.currentTimeMillis(); 60 61 67 Date rndDate = new Date (now + (long)r.nextInt() * 1000); 68 69 dateField = r.nextBoolean() ? null : Date.valueOf(rndDate.toString()); 70 71 76 Calendar cal = Calendar.getInstance(); 77 cal.setTime(new java.util.Date (now + (long)r.nextInt() * 1000)); 78 79 timeField = r.nextBoolean() ? null : new Time ((cal.get(Calendar.HOUR_OF_DAY) * 86400 + 80 cal.get(Calendar.MINUTE) * 60 + 81 cal.get(Calendar.SECOND)) * 1000); 82 83 timestampField = r.nextBoolean() ? null : new Timestamp (now + (long)r.nextInt() * 1000); 84 } 85 86 87 98 99 public boolean equals(Object obj) 100 { 101 if (this == obj) 102 return true; 103 104 if (!(obj instanceof DateWidget) || !super.equals(obj)) 105 return false; 106 107 DateWidget w = (DateWidget)obj; 108 109 if (dateField == null) { if (w.dateField != null) return false; } 110 else if (!dateField.equals(w.dateField)) return false; 111 112 if (timeField == null) { if (w.timeField != null) return false; } 113 else if (!timeField.equals(w.timeField)) return false; 114 115 if (timestampField == null) { if (w.timestampField != null) return false; } 116 else if (!timestampField.equals(w.timestampField)) return false; 117 118 return true; 119 } 120 121 122 127 128 public int hashCode() 129 { 130 return (dateField == null ? 0 : dateField.hashCode()) 131 ^ (timeField == null ? 0 : timeField.hashCode()) 132 ^ (timestampField == null ? 0 : timestampField.hashCode()); 133 } 134 135 136 142 143 public String toString() 144 { 145 StringBuffer s = new StringBuffer (super.toString()); 146 147 s.append(" dateField = ").append(dateField); 148 s.append("\n timeField = ").append(timeField); 149 s.append("\n timestampField = ").append(timestampField); 150 s.append('\n'); 151 152 return s.toString(); 153 } 154 155 156 162 163 public static void assertValidMetaData(ClassMetaData cmd, Assert test) 164 { 165 test.assertNotNull("Metadata", cmd); 166 test.assertEquals(DateWidget.class, cmd.getPCClass()); 167 test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName()); 168 test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("DateWidget.jdo") >= 0); 169 test.assertEquals("Superclass", BinaryWidget.class, cmd.getPCSuperclass()); 170 test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType()); 171 test.assertNull("Identity class", cmd.getIdentityClass()); 172 173 String [] sortedFieldNames = new String [] 174 { 175 "dateField", 176 "timeField", 177 "timestampField" 178 }; 179 180 test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount()); 181 182 for (int i = 0; i < sortedFieldNames.length; ++i) 183 { 184 FieldMetaData fmd = cmd.getFieldRelative(i); 185 String s = sortedFieldNames[i]; 186 187 test.assertEquals(s, fmd.getName()); 188 test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier()); 189 test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart()); 190 test.assertEquals(s + " null value handling", FieldMetaData.NULL_VALUE_NONE, fmd.getNullValueHandling()); 191 test.assertEquals(s + " default fetch group", true, fmd.isInDefaultFetchGroup()); 192 test.assertNull(s + " array metadata", fmd.getArrayMetaData()); 193 test.assertNull(s + " collection metadata", fmd.getCollectionMetaData()); 194 test.assertNull(s + " map metadata", fmd.getMapMetaData()); 195 } 196 } 197 } 198 | Popular Tags |