KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2003 (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
9 package com.triactive.jdo.model.test.widgets;
10
11 import com.triactive.jdo.model.*;
12 import java.util.Random JavaDoc;
13 import junit.framework.Assert;
14
15
16 /**
17  * An object used to test persistence of floating-point and decimal fields,
18  * in addition to those in {@link BinaryWidget}.
19  *
20  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
21  * @version $Revision: 1.2 $
22  */

23
24 public class SelfReferencingWidget extends Widget
25 {
26     private SelfReferencingWidget srWidgetField = null;
27
28
29     /**
30      * Constructs an empty test object.
31      */

32
33     public SelfReferencingWidget()
34     {
35         super();
36     }
37
38     private static Random JavaDoc r = new Random JavaDoc(0);
39
40
41     /**
42      * Fills all of the object's fields with random data values. Any non-
43      * primitive fields will also be assigned <code>null</code> on a random
44      * basis.
45      */

46
47     public void fillRandom()
48     {
49         super.fillRandom();
50
51         srWidgetField = r.nextBoolean() ? null : new SelfReferencingWidget();
52     }
53
54
55     /**
56      * Indicates whether some other object is "equal to" this one. By comparing
57      * against an original copy of the object, <code>equals()</code> can be
58      * used to verify that the object has been written to a database and read
59      * back correctly.
60      *
61      * @param obj the reference object with which to compare
62      *
63      * @return <code>true</code> if this object is equal to the obj argument;
64      * <code>false</code> otherwise.
65      */

66
67     public boolean equals(Object JavaDoc obj)
68     {
69         if (this == obj)
70             return true;
71
72         if (!(obj instanceof SelfReferencingWidget) || !super.equals(obj))
73             return false;
74
75         SelfReferencingWidget w = (SelfReferencingWidget)obj;
76
77         if (srWidgetField == null) { if (w.srWidgetField != null) return false; }
78         else if (!srWidgetField.equals(w.srWidgetField)) return false;
79
80         return true;
81     }
82
83
84     /**
85      * Returns a hash code value for this object.
86      *
87      * @return a hash code value for this object.
88      */

89
90     public int hashCode()
91     {
92         /*
93          * Note that, like equals() above, the technique used below for
94          * computing a hash code on the floating fields only works if the
95          * values in question are in the range 0.0 <= x < 1.0.
96          */

97
98         return super.hashCode()
99              ^ (srWidgetField == null ? 0 : srWidgetField.hashCode());
100     }
101
102
103     /**
104      * Returns a string representation for this object. All of the field
105      * values are included in the string for debugging purposes.
106      *
107      * @return a string representation for this object.
108      */

109
110     public String JavaDoc toString()
111     {
112         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
113
114         s.append(" srWidgetField = ").append(srWidgetField);
115         s.append('\n');
116
117         return s.toString();
118     }
119
120
121     /**
122      * Asserts that the given metadata is correct for an object of this class.
123      *
124      * @param cmd the class metadata to be tested.
125      * @param test the test to be used to call assert methods.
126      */

127
128     public static void assertValidMetaData(ClassMetaData cmd, Assert test)
129     {
130         test.assertNotNull("Metadata", cmd);
131         test.assertEquals(SelfReferencingWidget.class, cmd.getPCClass());
132         test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName());
133         test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("package.jdo") >= 0);
134         test.assertEquals("Superclass", Widget.class, cmd.getPCSuperclass());
135         test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType());
136         test.assertNull("Identity class", cmd.getIdentityClass());
137
138         String JavaDoc[] sortedFieldNames = new String JavaDoc[]
139             {
140                 "srWidgetField"
141             };
142
143         int[] nullValueHandlings = new int[]
144             {
145                 FieldMetaData.NULL_VALUE_NONE
146             };
147
148         test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount());
149
150         for (int i = 0; i < sortedFieldNames.length; ++i)
151         {
152             FieldMetaData fmd = cmd.getFieldRelative(i);
153             String JavaDoc s = sortedFieldNames[i];
154
155             test.assertEquals(s, fmd.getName());
156             test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier());
157             test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart());
158             test.assertEquals(s + " null value handling", nullValueHandlings[i], fmd.getNullValueHandling());
159             test.assertEquals(s + " default fetch group", false, fmd.isInDefaultFetchGroup());
160             test.assertNull(s + " array metadata", fmd.getArrayMetaData());
161             test.assertNull(s + " collection metadata", fmd.getCollectionMetaData());
162             test.assertNull(s + " map metadata", fmd.getMapMetaData());
163         }
164     }
165 }
166
Popular Tags