001: /*
002: * Copyright 2004 (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/01/18 03:01:07 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.test;
012:
013: public class DateWidget extends Widget implements HasSCOFields {
014: private java.util.Date dateField;
015: private java.sql.Date sqlDateField;
016: private java.sql.Timestamp sqlTimestampField;
017:
018: public DateWidget() {
019: super ();
020: }
021:
022: public java.util.Date getDateField() {
023: return dateField;
024: }
025:
026: public java.sql.Date getSQLDateField() {
027: return sqlDateField;
028: }
029:
030: public java.sql.Timestamp getSQLTimestampField() {
031: return sqlTimestampField;
032: }
033:
034: public Object[] getSCOFieldValues() {
035: return new Object[] { dateField, sqlDateField,
036: sqlTimestampField };
037: }
038:
039: /**
040: * Fills all of the object's fields with random data values. Any non-
041: * primitive fields will also be assigned <code>null</code> on a random
042: * basis.
043: */
044:
045: public void fillRandom() {
046: super .fillRandom();
047:
048: /*
049: * When updating a field (rather than nulling it), roughly half the time
050: * we mutate the existing object if possible. In the remaining cases we
051: * assign a new object.
052: *
053: * All dates below are made to be relative to an even second value
054: * because we don't insist that the database manage fractional seconds
055: * correctly.
056: */
057: if (nextNull())
058: dateField = null;
059: else {
060: long t = r.nextInt() * 1000L;
061:
062: if (dateField != null && r.nextBoolean())
063: dateField.setTime(t);
064: else
065: dateField = new java.util.Date(t);
066: }
067:
068: if (nextNull())
069: sqlDateField = null;
070: else {
071: /*
072: * We have to convert the random date value to String and back in order
073: * to discard the time-of-day portion of the data, otherwise the field
074: * won't compare exactly after it's been transferred to the database and
075: * back.
076: */
077: java.sql.Date rndDate = new java.sql.Date(
078: r.nextInt() * 1000L);
079: long t = java.sql.Date.valueOf(rndDate.toString())
080: .getTime();
081:
082: if (sqlDateField != null && r.nextBoolean())
083: sqlDateField.setTime(t);
084: else
085: sqlDateField = new java.sql.Date(t);
086: }
087:
088: if (nextNull())
089: sqlTimestampField = null;
090: else {
091: long t = r.nextInt() * 1000L;
092:
093: if (sqlTimestampField != null && r.nextBoolean())
094: sqlTimestampField.setTime(t);
095: else
096: sqlTimestampField = new java.sql.Timestamp(t);
097: }
098: }
099:
100: /**
101: * Indicates whether some other object is "equal to" this one. By comparing
102: * against an original copy of the object, <code>compareTo()</code> can be
103: * used to verify that the object has been written to a database and read
104: * back correctly.
105: *
106: * @param obj the reference object with which to compare
107: *
108: * @return <code>true</code> if this object is equal to the obj argument;
109: * <code>false</code> otherwise.
110: */
111:
112: public boolean compareTo(Object obj) {
113: if (this == obj)
114: return true;
115:
116: if (!(obj instanceof DateWidget) || !super .compareTo(obj))
117: return false;
118:
119: DateWidget w = (DateWidget) obj;
120:
121: if (dateField == null) {
122: if (w.dateField != null)
123: return false;
124: } else if (!dateField.equals(w.dateField))
125: return false;
126:
127: if (sqlDateField == null) {
128: if (w.sqlDateField != null)
129: return false;
130: } else if (!sqlDateField.equals(w.sqlDateField))
131: return false;
132:
133: if (sqlTimestampField == null) {
134: if (w.sqlTimestampField != null)
135: return false;
136: } else if (!sqlTimestampField.equals(w.sqlTimestampField))
137: return false;
138:
139: return true;
140: }
141:
142: /**
143: * Returns a string representation for this object. All of the field
144: * values are included in the string for debugging purposes.
145: *
146: * @return a string representation for this object.
147: */
148:
149: public String toString() {
150: StringBuffer s = new StringBuffer(super .toString());
151:
152: s.append(" dateField = ").append(dateField);
153: if (dateField != null)
154: s.append(" (").append(dateField.getTime()).append(')');
155: s.append('\n');
156:
157: s.append(" sqlDateField = ").append(sqlDateField);
158: if (sqlDateField != null)
159: s.append(" (").append(sqlDateField.getTime()).append(')');
160: s.append('\n');
161:
162: s.append(" sqlTimestampField = ").append(sqlTimestampField);
163: if (sqlTimestampField != null)
164: s.append(" (").append(sqlTimestampField.getTime()).append(
165: ')');
166: s.append('\n');
167:
168: return s.toString();
169: }
170: }
|