KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > model > test > widgets > DateWidget


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: DateWidget.java,v 1.5 2004/03/22 04:58:12 jackknifebarber Exp $
9  */

10
11 package com.triactive.jdo.model.test.widgets;
12
13 import com.triactive.jdo.model.*;
14 import java.sql.Date JavaDoc;
15 import java.sql.Time JavaDoc;
16 import java.sql.Timestamp JavaDoc;
17 import java.util.Random JavaDoc;
18 import java.util.Calendar JavaDoc;
19 import junit.framework.Assert;
20
21
22 /**
23  * An object used to test persistence of date/time fields, in addition to those
24  * in {@link BinaryWidget}.
25  *
26  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
27  * @version $Revision: 1.5 $
28  */

29
30 public class DateWidget extends BinaryWidget
31 {
32     private Date JavaDoc dateField;
33     private Time JavaDoc timeField;
34     private Timestamp JavaDoc timestampField;
35
36
37     /**
38      * Constructs an empty test object.
39      */

40
41     public DateWidget()
42     {
43         super();
44     }
45
46
47     private static Random JavaDoc r = new Random JavaDoc(0);
48
49     /**
50      * Fills all of the object's fields with random data values. Any non-
51      * primitive fields will also be assigned <code>null</code> on a random
52      * basis.
53      */

54
55     public void fillRandom()
56     {
57         super.fillRandom();
58
59         long now = System.currentTimeMillis();
60
61         /*
62          * We have to convert the random date value to String and back in order
63          * to discard the time-of-day portion of the data, otherwise the field
64          * won't compare exactly after it's been transferred to the database
65          * back.
66          */

67         Date JavaDoc rndDate = new Date JavaDoc(now + (long)r.nextInt() * 1000);
68
69         dateField = r.nextBoolean() ? null : Date.valueOf(rndDate.toString());
70
71         /*
72          * It's a similar story with the random time value. We have to use the
73          * Time(hour, minute, second) constructor so that no other date
74          * information creeps into the field.
75          */

76         Calendar JavaDoc cal = Calendar.getInstance();
77         cal.setTime(new java.util.Date JavaDoc(now + (long)r.nextInt() * 1000));
78
79         timeField = r.nextBoolean() ? null : new Time JavaDoc((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 JavaDoc(now + (long)r.nextInt() * 1000);
84     }
85
86
87     /**
88      * Indicates whether some other object is "equal to" this one. By comparing
89      * against an original copy of the object, <code>equals()</code> can be
90      * used to verify that the object has been written to a database and read
91      * back correctly.
92      *
93      * @param obj the reference object with which to compare
94      *
95      * @return <code>true</code> if this object is equal to the obj argument;
96      * <code>false</code> otherwise.
97      */

98
99     public boolean equals(Object JavaDoc 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     /**
123      * Returns a hash code value for this object.
124      *
125      * @return a hash code value for this object.
126      */

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     /**
137      * Returns a string representation for this object. All of the field
138      * values are included in the string for debugging purposes.
139      *
140      * @return a string representation for this object.
141      */

142
143     public String JavaDoc toString()
144     {
145         StringBuffer JavaDoc s = new StringBuffer JavaDoc(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     /**
157      * Asserts that the given metadata is correct for an object of this class.
158      *
159      * @param cmd the class metadata to be tested.
160      * @param test the test to be used to call assert methods.
161      */

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 JavaDoc[] sortedFieldNames = new String JavaDoc[]
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 JavaDoc 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