KickJava   Java API By Example, From Geeks To Geeks.

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


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: BinaryWidget.java,v 1.4 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.Arrays JavaDoc;
15 import java.util.Random JavaDoc;
16 import junit.framework.Assert;
17
18
19 /**
20  * An object used to test persistence of binary fields, in addition to those
21  * in {@link Widget}.
22  *
23  * @author <a HREF="mailto:mmartin5@austin.rr.com">Mike Martin</a>
24  * @version $Revision: 1.4 $
25  */

26
27 public class BinaryWidget extends Widget
28 {
29     private byte[] fixedLengthBinary;
30     private byte[] varLengthBinary;
31     private byte[] hugeBinary;
32
33
34     /**
35      * Constructs an empty test object.
36      */

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

51
52     public void fillRandom()
53     {
54         super.fillRandom();
55
56         fixedLengthBinary = r.nextBoolean() ? null : new byte[20];
57         varLengthBinary = r.nextBoolean() ? null : new byte[r.nextInt(20) + 1];
58         hugeBinary = r.nextBoolean() ? null : new byte[r.nextInt(128) + 1];
59
60         if (fixedLengthBinary != null)
61             r.nextBytes(fixedLengthBinary);
62
63         if (varLengthBinary != null)
64             r.nextBytes(varLengthBinary);
65
66         if (hugeBinary != null)
67             r.nextBytes(hugeBinary);
68     }
69
70
71     /**
72      * Indicates whether some other object is "equal to" this one. By comparing
73      * against an original copy of the object, <code>equals()</code> can be
74      * used to verify that the object has been written to a database and read
75      * back correctly.
76      *
77      * @param obj the reference object with which to compare
78      *
79      * @return <code>true</code> if this object is equal to the obj argument;
80      * <code>false</code> otherwise.
81      */

82
83     public boolean equals(Object JavaDoc obj)
84     {
85         if (this == obj)
86             return true;
87
88         if (!(obj instanceof BinaryWidget) || !super.equals(obj))
89             return false;
90
91         BinaryWidget w = (BinaryWidget)obj;
92
93         return Arrays.equals(fixedLengthBinary, w.fixedLengthBinary)
94             && Arrays.equals(varLengthBinary, w.varLengthBinary)
95             && Arrays.equals(hugeBinary, w.hugeBinary);
96     }
97
98
99     /**
100      * Returns a hash code value for this object.
101      *
102      * @return a hash code value for this object.
103      */

104
105     public int hashCode()
106     {
107         return (fixedLengthBinary == null ? 0 : hashCode(fixedLengthBinary))
108              ^ (varLengthBinary == null ? 0 : hashCode(varLengthBinary))
109              ^ (hugeBinary == null ? 0 : hashCode(hugeBinary));
110     }
111
112
113     /**
114      * Computes a suitable hash code from an array of bytes.
115      *
116      * @param bytes The array of bytes on which to compute the hash code.
117      *
118      * @return The computed hash code.
119      */

120
121     private int hashCode(byte[] bytes)
122     {
123         int hc = 0;
124         int leftShift = 0;
125
126         for (int i = 0; i < bytes.length; ++i)
127         {
128             hc ^= bytes[i] << leftShift;
129
130             if ((leftShift += 8) > 24)
131                 leftShift = 0;
132         }
133
134         return hc;
135     }
136
137
138     /**
139      * Returns a string representation for this object. All of the field
140      * values are included in the string for debugging purposes.
141      *
142      * @return a string representation for this object.
143      */

144
145     public String JavaDoc toString()
146     {
147         StringBuffer JavaDoc s = new StringBuffer JavaDoc(super.toString());
148
149         s.append(" fixedLengthBinary = binary");
150
151         if (fixedLengthBinary != null)
152             s.append(", hashCode = ").append(hashCode(fixedLengthBinary))
153              .append(", bytes = ").append(toHexString(fixedLengthBinary));
154         else
155             s.append(" (null)");
156
157         s.append("\n varLengthBinary = binary");
158
159         if (varLengthBinary != null)
160             s.append(", length = ").append(varLengthBinary.length)
161              .append(", hashCode = ").append(hashCode(varLengthBinary))
162              .append(", bytes = ").append(toHexString(varLengthBinary));
163         else
164             s.append(" (null)");
165
166         s.append("\n hugeBinary = binary");
167
168         if (hugeBinary != null)
169             s.append(", length = ").append(hugeBinary.length)
170              .append(", hashCode = ").append(hashCode(hugeBinary))
171              .append(", bytes = ").append(toHexString(hugeBinary));
172         else
173             s.append(" (null)");
174
175         s.append('\n');
176
177         return s.toString();
178     }
179
180
181     /**
182      * Returns a hex string representation of a given byte array.
183      *
184      * @param bytes the byte array to convert to string.
185      *
186      * @return a hex string representation of the given byte array.
187      */

188
189     private static String JavaDoc toHexString(byte[] bytes)
190     {
191         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
192
193         for (int i = 0; i < bytes.length; ++i)
194             s.append(Integer.toHexString((int)bytes[i] & 0xff));
195
196         return s.toString();
197     }
198
199
200     /**
201      * Asserts that the given metadata is correct for an object of this class.
202      *
203      * @param cmd the class metadata to be tested.
204      * @param test the test to be used to call assert methods.
205      */

206
207     public static void assertValidMetaData(ClassMetaData cmd, Assert test)
208     {
209         test.assertNotNull("Metadata", cmd);
210         test.assertEquals(BinaryWidget.class, cmd.getPCClass());
211         test.assertEquals("com.triactive.jdo.model.test.widgets", cmd.getPackageName());
212         test.assertTrue("Source URL", cmd.getSourceURL().toString().indexOf("BinaryWidget.jdo") >= 0);
213         test.assertEquals("Superclass", Widget.class, cmd.getPCSuperclass());
214         test.assertEquals("Identity type", ClassMetaData.DATASTORE_IDENTITY, cmd.getIdentityType());
215         test.assertNull("Identity class", cmd.getIdentityClass());
216
217         String JavaDoc[] sortedFieldNames = new String JavaDoc[]
218             {
219                 "fixedLengthBinary",
220                 "hugeBinary",
221                 "varLengthBinary"
222             };
223
224         String JavaDoc[] lengthExtensions = new String JavaDoc[]
225             {
226                 "20",
227                 "unlimited",
228                 "max 20"
229             };
230
231         test.assertEquals("Field count", sortedFieldNames.length, cmd.getFieldCount());
232
233         for (int i = 0; i < sortedFieldNames.length; ++i)
234         {
235             FieldMetaData fmd = cmd.getFieldRelative(i);
236             ArrayMetaData amd = fmd.getArrayMetaData();
237             String JavaDoc s = sortedFieldNames[i];
238
239             test.assertEquals(s, fmd.getName());
240             test.assertEquals(s + " persistence modifier", FieldMetaData.PERSISTENCE_MODIFIER_PERSISTENT, fmd.getPersistenceModifier());
241             test.assertEquals(s + " primary key", false, fmd.isPrimaryKeyPart());
242             test.assertEquals(s + " null value handling", FieldMetaData.NULL_VALUE_NONE, fmd.getNullValueHandling());
243             test.assertEquals(s + " default fetch group", false, fmd.isInDefaultFetchGroup());
244             test.assertEquals(s + " array metadata element type", byte.class, amd.getElementType());
245             test.assertTrue(s + " array metadata embedded flag", amd.isEmbeddedElement());
246             test.assertNull(s + " collection metadata", fmd.getCollectionMetaData());
247             test.assertNull(s + " map metadata", fmd.getMapMetaData());
248             test.assertEquals(s + " length extension", lengthExtensions[i], fmd.getLength());
249         }
250     }
251 }
252
Popular Tags