001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: DateWidget.java,v 1.5 2004/03/22 04:58:12 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.model.test.widgets;
012:
013: import com.triactive.jdo.model.*;
014: import java.sql.Date;
015: import java.sql.Time;
016: import java.sql.Timestamp;
017: import java.util.Random;
018: import java.util.Calendar;
019: import junit.framework.Assert;
020:
021: /**
022: * An object used to test persistence of date/time fields, in addition to those
023: * in {@link BinaryWidget}.
024: *
025: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
026: * @version $Revision: 1.5 $
027: */
028:
029: public class DateWidget extends BinaryWidget {
030: private Date dateField;
031: private Time timeField;
032: private Timestamp timestampField;
033:
034: /**
035: * Constructs an empty test object.
036: */
037:
038: public DateWidget() {
039: super ();
040: }
041:
042: private static Random r = new Random(0);
043:
044: /**
045: * Fills all of the object's fields with random data values. Any non-
046: * primitive fields will also be assigned <code>null</code> on a random
047: * basis.
048: */
049:
050: public void fillRandom() {
051: super .fillRandom();
052:
053: long now = System.currentTimeMillis();
054:
055: /*
056: * We have to convert the random date value to String and back in order
057: * to discard the time-of-day portion of the data, otherwise the field
058: * won't compare exactly after it's been transferred to the database
059: * back.
060: */
061: Date rndDate = new Date(now + (long) r.nextInt() * 1000);
062:
063: dateField = r.nextBoolean() ? null : Date.valueOf(rndDate
064: .toString());
065:
066: /*
067: * It's a similar story with the random time value. We have to use the
068: * Time(hour, minute, second) constructor so that no other date
069: * information creeps into the field.
070: */
071: Calendar cal = Calendar.getInstance();
072: cal
073: .setTime(new java.util.Date(now + (long) r.nextInt()
074: * 1000));
075:
076: timeField = r.nextBoolean() ? null : new Time((cal
077: .get(Calendar.HOUR_OF_DAY)
078: * 86400 + cal.get(Calendar.MINUTE) * 60 + cal
079: .get(Calendar.SECOND)) * 1000);
080:
081: timestampField = r.nextBoolean() ? null : new Timestamp(now
082: + (long) r.nextInt() * 1000);
083: }
084:
085: /**
086: * Indicates whether some other object is "equal to" this one. By comparing
087: * against an original copy of the object, <code>equals()</code> can be
088: * used to verify that the object has been written to a database and read
089: * back correctly.
090: *
091: * @param obj the reference object with which to compare
092: *
093: * @return <code>true</code> if this object is equal to the obj argument;
094: * <code>false</code> otherwise.
095: */
096:
097: public boolean equals(Object obj) {
098: if (this == obj)
099: return true;
100:
101: if (!(obj instanceof DateWidget) || !super .equals(obj))
102: return false;
103:
104: DateWidget w = (DateWidget) obj;
105:
106: if (dateField == null) {
107: if (w.dateField != null)
108: return false;
109: } else if (!dateField.equals(w.dateField))
110: return false;
111:
112: if (timeField == null) {
113: if (w.timeField != null)
114: return false;
115: } else if (!timeField.equals(w.timeField))
116: return false;
117:
118: if (timestampField == null) {
119: if (w.timestampField != null)
120: return false;
121: } else if (!timestampField.equals(w.timestampField))
122: return false;
123:
124: return true;
125: }
126:
127: /**
128: * Returns a hash code value for this object.
129: *
130: * @return a hash code value for this object.
131: */
132:
133: public int hashCode() {
134: return (dateField == null ? 0 : dateField.hashCode())
135: ^ (timeField == null ? 0 : timeField.hashCode())
136: ^ (timestampField == null ? 0 : timestampField
137: .hashCode());
138: }
139:
140: /**
141: * Returns a string representation for this object. All of the field
142: * values are included in the string for debugging purposes.
143: *
144: * @return a string representation for this object.
145: */
146:
147: public String toString() {
148: StringBuffer s = new StringBuffer(super .toString());
149:
150: s.append(" dateField = ").append(dateField);
151: s.append("\n timeField = ").append(timeField);
152: s.append("\n timestampField = ").append(timestampField);
153: s.append('\n');
154:
155: return s.toString();
156: }
157:
158: /**
159: * Asserts that the given metadata is correct for an object of this class.
160: *
161: * @param cmd the class metadata to be tested.
162: * @param test the test to be used to call assert methods.
163: */
164:
165: public static void assertValidMetaData(ClassMetaData cmd,
166: Assert test) {
167: test.assertNotNull("Metadata", cmd);
168: test.assertEquals(DateWidget.class, cmd.getPCClass());
169: test.assertEquals("com.triactive.jdo.model.test.widgets", cmd
170: .getPackageName());
171: test.assertTrue("Source URL", cmd.getSourceURL().toString()
172: .indexOf("DateWidget.jdo") >= 0);
173: test.assertEquals("Superclass", BinaryWidget.class, cmd
174: .getPCSuperclass());
175: test
176: .assertEquals("Identity type",
177: ClassMetaData.DATASTORE_IDENTITY, cmd
178: .getIdentityType());
179: test.assertNull("Identity class", cmd.getIdentityClass());
180:
181: String[] sortedFieldNames = new String[] { "dateField",
182: "timeField", "timestampField" };
183:
184: test.assertEquals("Field count", sortedFieldNames.length, cmd
185: .getFieldCount());
186:
187: for (int i = 0; i < sortedFieldNames.length; ++i) {
188: FieldMetaData fmd = cmd.getFieldRelative(i);
189: String s = sortedFieldNames[i];
190:
191: test.assertEquals(s, fmd.getName());
192: test.assertEquals(s + " persistence modifier",
193: FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd
194: .getPersistenceModifier());
195: test.assertEquals(s + " primary key", false, fmd
196: .isPrimaryKeyPart());
197: test.assertEquals(s + " null value handling",
198: FieldMetaData.NULL_VALUE_NONE, fmd
199: .getNullValueHandling());
200: test.assertEquals(s + " default fetch group", true, fmd
201: .isInDefaultFetchGroup());
202: test.assertNull(s + " array metadata", fmd
203: .getArrayMetaData());
204: test.assertNull(s + " collection metadata", fmd
205: .getCollectionMetaData());
206: test.assertNull(s + " map metadata", fmd.getMapMetaData());
207: }
208: }
209: }
|