KickJava   Java API By Example, From Geeks To Geeks.

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


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: Widget.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.util.Random JavaDoc;
15 import junit.framework.Assert;
16
17
18 /**
19  * An object used to test persistence of primitive and String fields.
20  *
21  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
22  * @version $Revision: 1.5 $
23  */

24
25 public class Widget
26 {
27     private static Random JavaDoc r = new Random JavaDoc(0);
28
29     private String JavaDoc fixedLengthString;
30     private String JavaDoc normalString;
31     private String JavaDoc hugeString;
32     private boolean booleanField;
33     private Boolean JavaDoc booleanObjField;
34     private byte byteField;
35     private Byte JavaDoc byteObjField;
36     private short shortField;
37     private Short JavaDoc shortObjField;
38     private int intField;
39     private Integer JavaDoc intObjField;
40     private long longField;
41     private Long JavaDoc longObjField;
42
43
44     /**
45      * Constructs an empty test object.
46      */

47
48     public Widget()
49     {
50     }
51
52
53     /**
54      * Fills all of the object's fields with random data values. Any non-
55      * primitive fields (with the exception of <code>id</code>) will also be
56      * assigned <code>null</code> on a random basis.
57      */

58
59     public void fillRandom()
60     {
61         StringBuffer JavaDoc s = new StringBuffer JavaDoc("Fixed " + r.nextInt());
62
63         while (s.length() < 20)
64             s.append(' ');
65
66         fixedLengthString = r.nextBoolean() ? null : s.toString();
67         normalString = r.nextBoolean() ? null : "Normal " + r.nextInt();
68         hugeString = r.nextBoolean() ? null : "Huge " + r.nextInt();
69         booleanField = r.nextBoolean();
70         booleanObjField = r.nextBoolean() ? null : new Boolean JavaDoc(r.nextBoolean());
71         byteField = (byte)r.nextInt(Byte.MAX_VALUE);
72         byteObjField = r.nextBoolean() ? null : new Byte JavaDoc((byte)r.nextInt(Byte.MAX_VALUE));
73         shortField = (short)(r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE);
74         shortObjField = r.nextBoolean() ? null : new Short JavaDoc((short)(r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE));
75         intField = r.nextInt();
76         intObjField = r.nextBoolean() ? null : new Integer JavaDoc(r.nextInt());
77         longField = r.nextLong();
78         longObjField = r.nextBoolean() ? null : new Long JavaDoc(r.nextLong());
79     }
80
81
82     /**
83      * Indicates whether some other object is "equal to" this one. By comparing
84      * against an original copy of the object, <code>equals()</code> can be
85      * used to verify that the object has been written to a database and read
86      * back correctly.
87      *
88      * @param obj the reference object with which to compare
89      *
90      * @return <code>true</code> if this object is equal to the obj argument;
91      * <code>false</code> otherwise.
92      */

93
94     public boolean equals(Object JavaDoc obj)
95     {
96         if (obj == this)
97             return true;
98
99         if (!(obj instanceof Widget))
100             return false;
101
102         Widget w = (Widget)obj;
103
104         if (fixedLengthString == null) { if (w.fixedLengthString != null) return false; }
105         else if (!fixedLengthString.equals(w.fixedLengthString)) return false;
106
107         if (normalString == null) { if (w.normalString != null) return false; }
108         else if (!normalString.equals(w.normalString)) return false;
109
110         if (hugeString == null) { if (w.hugeString != null) return false; }
111         else if (!hugeString.equals(w.hugeString)) return false;
112
113         if (booleanObjField == null) { if (w.booleanObjField != null) return false; }
114         else if (!booleanObjField.equals(w.booleanObjField)) return false;
115
116         if (byteObjField == null) { if (w.byteObjField != null) return false; }
117         else if (!byteObjField.equals(w.byteObjField)) return false;
118
119         if (shortObjField == null) { if (w.shortObjField != null) return false; }
120         else if (!shortObjField.equals(w.shortObjField)) return false;
121
122         if (intObjField == null) { if (w.intObjField != null) return false; }
123         else if (!intObjField.equals(w.intObjField)) return false;
124
125         if (longObjField == null) { if (w.longObjField != null) return false; }
126         else if (!longObjField.equals(w.longObjField)) return false;
127
128         return booleanField == w.booleanField
129             && byteField == w.byteField
130             && shortField == w.shortField
131             && intField == w.intField
132             && longField == w.longField;
133     }
134
135
136     /**
137      * Returns a hash code value for this object.
138      *
139      * @return a hash code value for this object.
140      */

141
142     public int hashCode()
143     {
144         return (fixedLengthString == null ? 0 : fixedLengthString.hashCode())
145              ^ (normalString == null ? 0 : normalString.hashCode())
146              ^ (hugeString == null ? 0 : hugeString.hashCode())
147              ^ (booleanField ? 1 : 0)
148              ^ (int)byteField
149              ^ (int)shortField
150              ^ intField
151              ^ new Long JavaDoc(longField).hashCode()
152              ^ (booleanObjField == null ? 0 : booleanObjField.hashCode())
153              ^ (byteObjField == null ? 0 : byteObjField.hashCode())
154              ^ (shortObjField == null ? 0 : shortObjField.hashCode())
155              ^ (intObjField == null ? 0 : intObjField.hashCode())
156              ^ (longObjField == null ? 0 : longObjField.hashCode());
157     }
158
159
160     /**
161      * Returns a string representation for this object. All of the field
162      * values are included in the string for debugging purposes.
163      *
164      * @return a string representation for this object.
165      */

166
167     public String JavaDoc toString()
168     {
169         StringBuffer JavaDoc s = new StringBuffer JavaDoc("Widget:");
170
171         s.append("\n fixedLengthString = ").append(fixedLengthString);
172         s.append("\n normalString = ").append(normalString);
173         s.append("\n hugeString = ").append(hugeString);
174         s.append("\n booleanField = ").append(booleanField);
175         s.append("\n booleanObjField = ").append(booleanObjField);
176         s.append("\n byteField = ").append(byteField);
177         s.append("\n byteObjField = ").append(byteObjField);
178         s.append("\n shortField = ").append(shortField);
179         s.append("\n shortObjField = ").append(shortObjField);
180         s.append("\n intField = ").append(intField);
181         s.append("\n intObjField = ").append(intObjField);
182         s.append("\n longField = ").append(longField);
183         s.append("\n longObjField = ").append(longObjField);
184         s.append('\n');
185
186         return s.toString();
187     }
188
189
190     /**
191      * Asserts that the given metadata is correct for an object of this class.
192      *
193      * @param cmd the class metadata to be tested.
194      * @param test the test to be used to call assert methods.
195      */

196
197     public static void assertValidMetaData(ClassMetaData cmd, Assert test)
198     {
199         test.assertNotNull("Metadata", cmd);
200         test.assertEquals(Widget.class, cmd.getPCClass());
201         test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName());
202         test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("package.jdo") >= 0);
203         test.assertNull("Superclass", cmd.getPCSuperclass());
204         test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType());
205         test.assertNull("Identity class", cmd.getIdentityClass());
206
207         String JavaDoc[] sortedFieldNames = new String JavaDoc[]
208             {
209                 "booleanField",
210                 "booleanObjField",
211                 "byteField",
212                 "byteObjField",
213                 "fixedLengthString",
214                 "hugeString",
215                 "intField",
216                 "intObjField",
217                 "longField",
218                 "longObjField",
219                 "normalString",
220                 "shortField",
221                 "shortObjField"
222             };
223
224         int[] nullValueHandlings = new int[]
225             {
226                 FieldMetaData.NULL_VALUE_EXCEPTION,
227                 FieldMetaData.NULL_VALUE_NONE,
228                 FieldMetaData.NULL_VALUE_EXCEPTION,
229                 FieldMetaData.NULL_VALUE_NONE,
230                 FieldMetaData.NULL_VALUE_NONE,
231                 FieldMetaData.NULL_VALUE_NONE,
232                 FieldMetaData.NULL_VALUE_EXCEPTION,
233                 FieldMetaData.NULL_VALUE_NONE,
234                 FieldMetaData.NULL_VALUE_EXCEPTION,
235                 FieldMetaData.NULL_VALUE_NONE,
236                 FieldMetaData.NULL_VALUE_NONE,
237                 FieldMetaData.NULL_VALUE_EXCEPTION,
238                 FieldMetaData.NULL_VALUE_NONE
239             };
240
241         String JavaDoc[] lengthExtensions = new String JavaDoc[]
242             {
243                 null,
244                 null,
245                 null,
246                 null,
247                 "20",
248                 "unlimited",
249                 null,
250                 null,
251                 null,
252                 null,
253                 "max 20",
254                 null,
255                 null
256             };
257
258         test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount());
259
260         for (int i = 0; i < sortedFieldNames.length; ++i)
261         {
262             FieldMetaData fmd = cmd.getFieldRelative(i);
263             String JavaDoc s = sortedFieldNames[i];
264
265             test.assertEquals(s, fmd.getName());
266             test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier());
267             test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart());
268             test.assertEquals(s + " null value handling", nullValueHandlings[i], fmd.getNullValueHandling());
269             test.assertEquals(s + " default fetch group", i != 5, fmd.isInDefaultFetchGroup());
270             test.assertNull(s + " array metadata", fmd.getArrayMetaData());
271             test.assertNull(s + " collection metadata", fmd.getCollectionMetaData());
272             test.assertNull(s + " map metadata", fmd.getMapMetaData());
273             test.assertEquals(s + " length extension", lengthExtensions[i], fmd.getLength());
274         }
275     }
276 }
277
Popular Tags