001: /*
002: * Copyright 2002 (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: Widget.java,v 1.5 2004/03/22 04:58:12 jackknifebarber Exp $
009: */
010:
011: package com.triactive.jdo.model.test.widgets;
012:
013: import com.triactive.jdo.model.*;
014: import java.util.Random;
015: import junit.framework.Assert;
016:
017: /**
018: * An object used to test persistence of primitive and String fields.
019: *
020: * @author <a href="mailto:mmartin5@austin.rr.com">Mike Martin</a>
021: * @version $Revision: 1.5 $
022: */
023:
024: public class Widget {
025: private static Random r = new Random(0);
026:
027: private String fixedLengthString;
028: private String normalString;
029: private String hugeString;
030: private boolean booleanField;
031: private Boolean booleanObjField;
032: private byte byteField;
033: private Byte byteObjField;
034: private short shortField;
035: private Short shortObjField;
036: private int intField;
037: private Integer intObjField;
038: private long longField;
039: private Long longObjField;
040:
041: /**
042: * Constructs an empty test object.
043: */
044:
045: public Widget() {
046: }
047:
048: /**
049: * Fills all of the object's fields with random data values. Any non-
050: * primitive fields (with the exception of <code>id</code>) will also be
051: * assigned <code>null</code> on a random basis.
052: */
053:
054: public void fillRandom() {
055: StringBuffer s = new StringBuffer("Fixed " + r.nextInt());
056:
057: while (s.length() < 20)
058: s.append(' ');
059:
060: fixedLengthString = r.nextBoolean() ? null : s.toString();
061: normalString = r.nextBoolean() ? null : "Normal " + r.nextInt();
062: hugeString = r.nextBoolean() ? null : "Huge " + r.nextInt();
063: booleanField = r.nextBoolean();
064: booleanObjField = r.nextBoolean() ? null : new Boolean(r
065: .nextBoolean());
066: byteField = (byte) r.nextInt(Byte.MAX_VALUE);
067: byteObjField = r.nextBoolean() ? null : new Byte((byte) r
068: .nextInt(Byte.MAX_VALUE));
069: shortField = (short) (r.nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE);
070: shortObjField = r.nextBoolean() ? null : new Short((short) (r
071: .nextInt(Short.MAX_VALUE * 2) - Short.MAX_VALUE));
072: intField = r.nextInt();
073: intObjField = r.nextBoolean() ? null : new Integer(r.nextInt());
074: longField = r.nextLong();
075: longObjField = r.nextBoolean() ? null : new Long(r.nextLong());
076: }
077:
078: /**
079: * Indicates whether some other object is "equal to" this one. By comparing
080: * against an original copy of the object, <code>equals()</code> can be
081: * used to verify that the object has been written to a database and read
082: * back correctly.
083: *
084: * @param obj the reference object with which to compare
085: *
086: * @return <code>true</code> if this object is equal to the obj argument;
087: * <code>false</code> otherwise.
088: */
089:
090: public boolean equals(Object obj) {
091: if (obj == this )
092: return true;
093:
094: if (!(obj instanceof Widget))
095: return false;
096:
097: Widget w = (Widget) obj;
098:
099: if (fixedLengthString == null) {
100: if (w.fixedLengthString != null)
101: return false;
102: } else if (!fixedLengthString.equals(w.fixedLengthString))
103: return false;
104:
105: if (normalString == null) {
106: if (w.normalString != null)
107: return false;
108: } else if (!normalString.equals(w.normalString))
109: return false;
110:
111: if (hugeString == null) {
112: if (w.hugeString != null)
113: return false;
114: } else if (!hugeString.equals(w.hugeString))
115: return false;
116:
117: if (booleanObjField == null) {
118: if (w.booleanObjField != null)
119: return false;
120: } else if (!booleanObjField.equals(w.booleanObjField))
121: return false;
122:
123: if (byteObjField == null) {
124: if (w.byteObjField != null)
125: return false;
126: } else if (!byteObjField.equals(w.byteObjField))
127: return false;
128:
129: if (shortObjField == null) {
130: if (w.shortObjField != null)
131: return false;
132: } else if (!shortObjField.equals(w.shortObjField))
133: return false;
134:
135: if (intObjField == null) {
136: if (w.intObjField != null)
137: return false;
138: } else if (!intObjField.equals(w.intObjField))
139: return false;
140:
141: if (longObjField == null) {
142: if (w.longObjField != null)
143: return false;
144: } else if (!longObjField.equals(w.longObjField))
145: return false;
146:
147: return booleanField == w.booleanField
148: && byteField == w.byteField
149: && shortField == w.shortField && intField == w.intField
150: && longField == w.longField;
151: }
152:
153: /**
154: * Returns a hash code value for this object.
155: *
156: * @return a hash code value for this object.
157: */
158:
159: public int hashCode() {
160: return (fixedLengthString == null ? 0 : fixedLengthString
161: .hashCode())
162: ^ (normalString == null ? 0 : normalString.hashCode())
163: ^ (hugeString == null ? 0 : hugeString.hashCode())
164: ^ (booleanField ? 1 : 0)
165: ^ (int) byteField
166: ^ (int) shortField
167: ^ intField
168: ^ new Long(longField).hashCode()
169: ^ (booleanObjField == null ? 0 : booleanObjField
170: .hashCode())
171: ^ (byteObjField == null ? 0 : byteObjField.hashCode())
172: ^ (shortObjField == null ? 0 : shortObjField.hashCode())
173: ^ (intObjField == null ? 0 : intObjField.hashCode())
174: ^ (longObjField == null ? 0 : longObjField.hashCode());
175: }
176:
177: /**
178: * Returns a string representation for this object. All of the field
179: * values are included in the string for debugging purposes.
180: *
181: * @return a string representation for this object.
182: */
183:
184: public String toString() {
185: StringBuffer s = new StringBuffer("Widget:");
186:
187: s.append("\n fixedLengthString = ").append(fixedLengthString);
188: s.append("\n normalString = ").append(normalString);
189: s.append("\n hugeString = ").append(hugeString);
190: s.append("\n booleanField = ").append(booleanField);
191: s.append("\n booleanObjField = ").append(booleanObjField);
192: s.append("\n byteField = ").append(byteField);
193: s.append("\n byteObjField = ").append(byteObjField);
194: s.append("\n shortField = ").append(shortField);
195: s.append("\n shortObjField = ").append(shortObjField);
196: s.append("\n intField = ").append(intField);
197: s.append("\n intObjField = ").append(intObjField);
198: s.append("\n longField = ").append(longField);
199: s.append("\n longObjField = ").append(longObjField);
200: s.append('\n');
201:
202: return s.toString();
203: }
204:
205: /**
206: * Asserts that the given metadata is correct for an object of this class.
207: *
208: * @param cmd the class metadata to be tested.
209: * @param test the test to be used to call assert methods.
210: */
211:
212: public static void assertValidMetaData(ClassMetaData cmd,
213: Assert test) {
214: test.assertNotNull("Metadata", cmd);
215: test.assertEquals(Widget.class, cmd.getPCClass());
216: test.assertEquals("com.triactive.jdo.model.test.widgets", cmd
217: .getPackageName());
218: test.assertTrue("Source URL", cmd.getSourceURL().toString()
219: .indexOf("package.jdo") >= 0);
220: test.assertNull("Superclass", cmd.getPCSuperclass());
221: test
222: .assertEquals("Identity type",
223: ClassMetaData.DATASTORE_IDENTITY, cmd
224: .getIdentityType());
225: test.assertNull("Identity class", cmd.getIdentityClass());
226:
227: String[] sortedFieldNames = new String[] { "booleanField",
228: "booleanObjField", "byteField", "byteObjField",
229: "fixedLengthString", "hugeString", "intField",
230: "intObjField", "longField", "longObjField",
231: "normalString", "shortField", "shortObjField" };
232:
233: int[] nullValueHandlings = new int[] {
234: FieldMetaData.NULL_VALUE_EXCEPTION,
235: FieldMetaData.NULL_VALUE_NONE,
236: FieldMetaData.NULL_VALUE_EXCEPTION,
237: FieldMetaData.NULL_VALUE_NONE,
238: FieldMetaData.NULL_VALUE_NONE,
239: FieldMetaData.NULL_VALUE_NONE,
240: FieldMetaData.NULL_VALUE_EXCEPTION,
241: FieldMetaData.NULL_VALUE_NONE,
242: FieldMetaData.NULL_VALUE_EXCEPTION,
243: FieldMetaData.NULL_VALUE_NONE,
244: FieldMetaData.NULL_VALUE_NONE,
245: FieldMetaData.NULL_VALUE_EXCEPTION,
246: FieldMetaData.NULL_VALUE_NONE };
247:
248: String[] lengthExtensions = new String[] { null, null, null,
249: null, "20", "unlimited", null, null, null, null,
250: "max 20", null, null };
251:
252: test.assertEquals("Field count", sortedFieldNames.length, cmd
253: .getFieldCount());
254:
255: for (int i = 0; i < sortedFieldNames.length; ++i) {
256: FieldMetaData fmd = cmd.getFieldRelative(i);
257: String s = sortedFieldNames[i];
258:
259: test.assertEquals(s, fmd.getName());
260: test.assertEquals(s + " persistence modifier",
261: FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd
262: .getPersistenceModifier());
263: test.assertEquals(s + " primary key", false, fmd
264: .isPrimaryKeyPart());
265: test.assertEquals(s + " null value handling",
266: nullValueHandlings[i], fmd.getNullValueHandling());
267: test.assertEquals(s + " default fetch group", i != 5, fmd
268: .isInDefaultFetchGroup());
269: test.assertNull(s + " array metadata", fmd
270: .getArrayMetaData());
271: test.assertNull(s + " collection metadata", fmd
272: .getCollectionMetaData());
273: test.assertNull(s + " map metadata", fmd.getMapMetaData());
274: test.assertEquals(s + " length extension",
275: lengthExtensions[i], fmd.getLength());
276: }
277: }
278: }
|