001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence.kernel.common.apps;
020:
021: import java.math.BigDecimal;
022: import java.math.BigInteger;
023: import java.util.Arrays;
024: import java.util.Calendar;
025: import java.util.Date;
026: import javax.persistence.Entity;
027: import javax.persistence.GeneratedValue;
028: import javax.persistence.GenerationType;
029: import javax.persistence.Id;
030: import javax.persistence.Table;
031: import javax.persistence.Temporal;
032: import javax.persistence.TemporalType;
033:
034: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
035:
036: /**
037: * Used in testing; should be enhanced.
038: */
039: @Entity
040: @Table(name="ALL_FLDTYPETEST")
041: public class AllFieldTypesTest {
042:
043: @Id
044: @GeneratedValue(strategy=GenerationType.AUTO)
045: private int id;
046:
047: private int testint;
048: private long testlong;
049: private short testshort;
050: private float testfloat;
051: private double testdouble;
052: private byte testbyte;
053: private boolean testboolean;
054: private char testchar;
055: private String testString;
056: private String testBigString;
057: @Temporal(TemporalType.DATE)
058: private Date testDate;
059: @Temporal(TemporalType.DATE)
060: private Calendar testCalendar;
061: private Object testObject;
062: private BigInteger testBigInteger;
063: private BigDecimal testBigDecimal;
064:
065: public AllFieldTypesTest() {
066: testfloat = 1f;
067: testdouble = 1.0;
068: }
069:
070: public Date getTestDate() {
071: return this .testDate;
072: }
073:
074: public void setTestDate(Date testDate) {
075: this .testDate = testDate;
076: }
077:
078: public void setTestCalendar(Calendar testCalendar) {
079: this .testCalendar = testCalendar;
080: }
081:
082: public Calendar getTestCalendar() {
083: return this .testCalendar;
084: }
085:
086: public Object getTestObject() {
087: return this .testObject;
088: }
089:
090: public void setTestObject(Object testObject) {
091: this .testObject = testObject;
092: }
093:
094: public char getTestchar() {
095: return this .testchar;
096: }
097:
098: public void setTestchar(char testchar) {
099: this .testchar = testchar;
100: }
101:
102: public int getTestint() {
103: return this .testint;
104: }
105:
106: public void setTestint(int testint) {
107: this .testint = testint;
108: }
109:
110: public short getTestshort() {
111: return this .testshort;
112: }
113:
114: public void setTestshort(short testshort) {
115: this .testshort = testshort;
116: }
117:
118: public long getTestlong() {
119: return this .testlong;
120: }
121:
122: public void setTestlong(long testlong) {
123: this .testlong = testlong;
124: }
125:
126: public boolean getTestboolean() {
127: return this .testboolean;
128: }
129:
130: public void setTestboolean(boolean testboolean) {
131: this .testboolean = testboolean;
132: }
133:
134: public float getTestfloat() {
135: return this .testfloat;
136: }
137:
138: public void setTestfloat(float testfloat) {
139: this .testfloat = testfloat;
140: }
141:
142: public double getTestdouble() {
143: return this .testdouble;
144: }
145:
146: public void setTestdouble(double testdouble) {
147: this .testdouble = testdouble;
148: }
149:
150: public String getTestString() {
151: return this .testString;
152: }
153:
154: public void setTestString(String testString) {
155: this .testString = testString;
156: }
157:
158: public void setTestBigString(String testBigString) {
159: this .testBigString = testBigString;
160: }
161:
162: public String getTestBigString() {
163: return this .testBigString;
164: }
165:
166: public byte getTestbyte() {
167: return this .testbyte;
168: }
169:
170: public void setTestbyte(byte testbyte) {
171: this .testbyte = testbyte;
172: }
173:
174: public BigInteger getTestBigInteger() {
175: return this .testBigInteger;
176: }
177:
178: public void setTestBigInteger(BigInteger testBigInteger) {
179: this .testBigInteger = testBigInteger;
180: }
181:
182: public BigDecimal getTestBigDecimal() {
183: return this .testBigDecimal;
184: }
185:
186: public void setTestBigDecimal(BigDecimal testBigDecimal) {
187: this .testBigDecimal = testBigDecimal;
188: }
189:
190: public void randomize(boolean objects, boolean blobs) {
191: testint = AbstractTestCase.randomInt().intValue();
192: testlong = AbstractTestCase.randomLong().longValue();
193: testshort = AbstractTestCase.randomShort().shortValue();
194: testfloat = AbstractTestCase.randomFloat().floatValue();
195: testdouble = AbstractTestCase.randomDouble().doubleValue();
196: testbyte = AbstractTestCase.randomByte().byteValue();
197: testboolean = AbstractTestCase.randomBoolean().booleanValue();
198: testchar = AbstractTestCase.randomChar().charValue();
199:
200: if (objects) {
201: testString = AbstractTestCase.randomString();
202: testDate = AbstractTestCase.randomDate();
203:
204: testCalendar = Calendar.getInstance();
205: testCalendar.setTime(AbstractTestCase.randomDate());
206:
207: testBigInteger = AbstractTestCase.randomBigInteger();
208: testBigDecimal = AbstractTestCase.randomBigDecimal();
209: }
210:
211: if (blobs && objects) {
212: testObject = AbstractTestCase.randomBlob();
213: testBigString = AbstractTestCase.randomClob();
214: }
215: }
216:
217: public String toString() {
218: return "\n{" + "testint="
219: + testint
220: + ";"
221: + "testlong="
222: + testlong
223: + ";"
224: + "testshort="
225: + testshort
226: + ";"
227: + "testfloat="
228: + testfloat
229: + ";"
230: + "testdouble="
231: + testdouble
232: + ";"
233: + "testbyte="
234: + testbyte
235: + ";"
236: + "testboolean="
237: + testboolean
238: + ";"
239: + "testchar="
240: + testchar
241: + ";"
242: + "testString="
243: + testString
244: + ";"
245: + "testBigString="
246: + testBigString
247: + ";"
248: + "testDate="
249: + testDate
250: + ";"
251: + "testCalendar="
252: + testCalendar
253: + ";"
254: + "testObject="
255: + testObject
256: + "("
257: + ((testObject instanceof byte[]) ? "(byte[])"
258: + ((byte[]) testObject).length + ""
259: : (testObject == null ? "null" : testObject
260: .getClass().getName())) + ")" + ";"
261: + "testBigInteger=" + testBigInteger + ";"
262: + "testBigDecimal=" + testBigDecimal + ";" + "}";
263: }
264:
265: public int hashCode() {
266: return (int) ((testint
267: + testlong
268: + testshort
269: + testfloat
270: + testdouble
271: + testbyte
272: + (testboolean ? 1 : 0)
273: + testchar
274: + (testString == null ? 0 : testString.hashCode())
275: + (testBigString == null ? 0 : testBigString.hashCode())
276: + (testDate == null ? 0 : testDate.hashCode())
277: + (testCalendar == null ? 0 : testCalendar.hashCode())
278: + (testObject == null ? 0 : testObject.hashCode())
279: + (testBigInteger == null ? 0 : testBigInteger
280: .hashCode()) + (testBigDecimal == null ? 0
281: : testBigDecimal.hashCode())))
282: % Integer.MAX_VALUE;
283: }
284:
285: public boolean equals(Object other) {
286: if (other == null && !(other instanceof AllFieldTypesTest))
287: return false;
288:
289: AllFieldTypesTest o = (AllFieldTypesTest) other;
290:
291: return o.testint == testint && o.testlong == testlong
292: && o.testshort == testshort && o.testfloat == testfloat
293: && o.testdouble == testdouble && o.testbyte == testbyte
294: && o.testboolean == testboolean
295: && o.testchar == testchar
296: && eq(o.testString, testString)
297: && eq(o.testBigString, testBigString)
298: && eq(o.testDate, testDate)
299: && eq(o.testCalendar, testCalendar)
300: && eq(o.testObject, testObject)
301: && eq(o.testBigInteger, o.testBigInteger)
302: && eq(o.testBigDecimal, o.testBigDecimal);
303: }
304:
305: private boolean eq(Object a, Object b) {
306: if (a == b)
307: return true;
308: if (a == null && b != null)
309: return false;
310: if (a != null && b == null)
311: return false;
312:
313: // OK, this is stupid, but we want to special-case blobs
314: // thay are byte arrays, since new byte [] { 1, 2} does not
315: // equals inew byte [] { 1, 2}
316: if (a instanceof byte[] && b instanceof byte[])
317: return Arrays.equals((byte[]) a, (byte[]) b);
318:
319: return a.equals(b);
320: }
321: }
|