001: /*
002: * Copyright 2003 (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:
009: package com.triactive.jdo.model.test.widgets;
010:
011: import com.triactive.jdo.model.*;
012: import java.util.Random;
013: import junit.framework.Assert;
014:
015: /**
016: * An object used to test persistence of floating-point and decimal fields,
017: * in addition to those in {@link BinaryWidget}.
018: *
019: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
020: * @version $Revision: 1.2 $
021: */
022:
023: public class SelfReferencingWidget extends Widget {
024: private SelfReferencingWidget srWidgetField = null;
025:
026: /**
027: * Constructs an empty test object.
028: */
029:
030: public SelfReferencingWidget() {
031: super ();
032: }
033:
034: private static Random r = new Random(0);
035:
036: /**
037: * Fills all of the object's fields with random data values. Any non-
038: * primitive fields will also be assigned <code>null</code> on a random
039: * basis.
040: */
041:
042: public void fillRandom() {
043: super .fillRandom();
044:
045: srWidgetField = r.nextBoolean() ? null
046: : new SelfReferencingWidget();
047: }
048:
049: /**
050: * Indicates whether some other object is "equal to" this one. By comparing
051: * against an original copy of the object, <code>equals()</code> can be
052: * used to verify that the object has been written to a database and read
053: * back correctly.
054: *
055: * @param obj the reference object with which to compare
056: *
057: * @return <code>true</code> if this object is equal to the obj argument;
058: * <code>false</code> otherwise.
059: */
060:
061: public boolean equals(Object obj) {
062: if (this == obj)
063: return true;
064:
065: if (!(obj instanceof SelfReferencingWidget)
066: || !super .equals(obj))
067: return false;
068:
069: SelfReferencingWidget w = (SelfReferencingWidget) obj;
070:
071: if (srWidgetField == null) {
072: if (w.srWidgetField != null)
073: return false;
074: } else if (!srWidgetField.equals(w.srWidgetField))
075: return false;
076:
077: return true;
078: }
079:
080: /**
081: * Returns a hash code value for this object.
082: *
083: * @return a hash code value for this object.
084: */
085:
086: public int hashCode() {
087: /*
088: * Note that, like equals() above, the technique used below for
089: * computing a hash code on the floating fields only works if the
090: * values in question are in the range 0.0 <= x < 1.0.
091: */
092:
093: return super .hashCode()
094: ^ (srWidgetField == null ? 0 : srWidgetField.hashCode());
095: }
096:
097: /**
098: * Returns a string representation for this object. All of the field
099: * values are included in the string for debugging purposes.
100: *
101: * @return a string representation for this object.
102: */
103:
104: public String toString() {
105: StringBuffer s = new StringBuffer(super .toString());
106:
107: s.append(" srWidgetField = ").append(srWidgetField);
108: s.append('\n');
109:
110: return s.toString();
111: }
112:
113: /**
114: * Asserts that the given metadata is correct for an object of this class.
115: *
116: * @param cmd the class metadata to be tested.
117: * @param test the test to be used to call assert methods.
118: */
119:
120: public static void assertValidMetaData(ClassMetaData cmd,
121: Assert test) {
122: test.assertNotNull("Metadata", cmd);
123: test
124: .assertEquals(SelfReferencingWidget.class, cmd
125: .getPCClass());
126: test.assertEquals("com.triactive.jdo.model.test.widgets", cmd
127: .getPackageName());
128: test.assertTrue("Source URL", cmd.getSourceURL().toString()
129: .indexOf("package.jdo") >= 0);
130: test.assertEquals("Superclass", Widget.class, cmd
131: .getPCSuperclass());
132: test
133: .assertEquals("Identity type",
134: ClassMetaData.DATASTORE_IDENTITY, cmd
135: .getIdentityType());
136: test.assertNull("Identity class", cmd.getIdentityClass());
137:
138: String[] sortedFieldNames = new String[] { "srWidgetField" };
139:
140: int[] nullValueHandlings = new int[] { FieldMetaData.NULL_VALUE_NONE };
141:
142: test.assertEquals("Field count", sortedFieldNames.length, cmd
143: .getFieldCount());
144:
145: for (int i = 0; i < sortedFieldNames.length; ++i) {
146: FieldMetaData fmd = cmd.getFieldRelative(i);
147: String s = sortedFieldNames[i];
148:
149: test.assertEquals(s, fmd.getName());
150: test.assertEquals(s + " persistence modifier",
151: FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd
152: .getPersistenceModifier());
153: test.assertEquals(s + " primary key", false, fmd
154: .isPrimaryKeyPart());
155: test.assertEquals(s + " null value handling",
156: nullValueHandlings[i], fmd.getNullValueHandling());
157: test.assertEquals(s + " default fetch group", false, fmd
158: .isInDefaultFetchGroup());
159: test.assertNull(s + " array metadata", fmd
160: .getArrayMetaData());
161: test.assertNull(s + " collection metadata", fmd
162: .getCollectionMetaData());
163: test.assertNull(s + " map metadata", fmd.getMapMetaData());
164: }
165: }
166: }
|