001: /*
002: * TestFieldRange.java
003: *
004: * Created on October 12, 2006, 10:14 AM
005: *
006: * To change this template, choose Tools | Template Manager
007: * and open the template in the editor.
008: */
009: /*
010: * Licensed to the Apache Software Foundation (ASF) under one
011: * or more contributor license agreements. See the NOTICE file
012: * distributed with this work for additional information
013: * regarding copyright ownership. The ASF licenses this file
014: * to you under the Apache License, Version 2.0 (the
015: * "License"); you may not use this file except in compliance
016: * with the License. You may obtain a copy of the License at
017: *
018: * http://www.apache.org/licenses/LICENSE-2.0
019: *
020: * Unless required by applicable law or agreed to in writing,
021: * software distributed under the License is distributed on an
022: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
023: * KIND, either express or implied. See the License for the
024: * specific language governing permissions and limitations
025: * under the License.
026: */
027: package org.apache.openjpa.persistence.kernel;
028:
029: import java.io.Serializable;
030: import java.math.BigDecimal;
031: import java.math.BigInteger;
032: import java.util.Date;
033:
034: import org.apache.openjpa.persistence.kernel.common.apps.AllFieldTypesTest;
035: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
036: import junit.framework.Assert;
037: import junit.framework.AssertionFailedError;
038:
039: import org.apache.openjpa.persistence.OpenJPAEntityManager;
040:
041: public class TestFieldRange extends BaseKernelTest {
042:
043: protected static String RANDOM_STRING = "This is my test String with all "
044: + "kinds of wierd characters: "
045: + "!@@#$\\%^&\"*()-=\\|\"\"\"\"\"+_/?.>,<~`"
046: + "'''''''''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
047: + "''''''''''''\\\\\\\\\\\\\\\\\\\\\\\\\\\\"
048: + "!@@#$\\%^&\"*()-=\\|+_/?.>,<~`";
049:
050: /**
051: * Creates a new instance of TestFieldRange
052: */
053: public TestFieldRange() {
054: }
055:
056: public TestFieldRange(String name) {
057: super (name);
058: }
059:
060: public void setUp() throws Exception {
061: super .setUp();
062: deleteAll(AllFieldTypesTest.class);
063: }
064:
065: public void testSaveState() throws Exception {
066: allFieldSaveState((int) 259645, (short) 50849,
067: (long) 2349847982L, (float) 43273423.0234723F,
068: (double) 34678.02384723D, (byte) -120, (boolean) true,
069: (char) '%', (Date) new Date(),
070: (Serializable) new StringBuffer(5000),
071: (String) RANDOM_STRING, randomBigInteger(),
072: randomBigDecimal());
073: }
074:
075: /**
076: * Test to make sure all the numeric fields can accept the maximum
077: * values for their data size. Note that we subtract one from
078: * each of the values because some databases (like InstantDB)
079: * may consider them to be equivalent to NULL.
080: * FixMe: Hangs for some mysterious reason. aokeke
081: */
082:
083: // public void testLargeNumbers()
084: // throws Exception {
085: // try {
086: // allFieldSaveState(
087: // (int) (Integer.MAX_VALUE - 1),
088: // (short) (Short.MAX_VALUE - 1),
089: // (long) (Long.MAX_VALUE - 1l),
090: // (float) (Float.MAX_VALUE - 1.0f),
091: // (double) (Double.MAX_VALUE - 1.0d),
092: // (byte) (Byte.MAX_VALUE),
093: // (boolean) true,
094: // (char) 'q',
095: // (Date) new Date(),
096: // (Serializable) new StringBuffer(5000),
097: // (String) RANDOM_STRING,
098: // randomBigInteger(),
099: // randomBigDecimal());
100: // } catch (Throwable e) {
101: // bug(3, e, "doubles and floats");
102: // }
103: // }
104: /**
105: * Test to make sure all the numeric fields can be set to
106: * very low values. We add one to the minimim value because
107: * some databases (such as InstantDB) consider the MIN_VALUE
108: * to be equivalent to null. This is arguably a bug, but
109: * not a killer one.
110: */
111: public void testLargeNumbersNegative() throws Exception {
112: allFieldSaveState((int) (Integer.MIN_VALUE + 1),
113: (short) (Short.MIN_VALUE + 1),
114: (long) (Long.MIN_VALUE + 1l),
115: (float) (Float.MIN_VALUE + 1.0f),
116: (double) (Double.MIN_VALUE + 1.0d),
117: (byte) (Byte.MIN_VALUE + 1), (boolean) true,
118: (char) 'q', (Date) new Date(),
119: (Serializable) new StringBuffer(5000),
120: (String) RANDOM_STRING, randomBigInteger(),
121: randomBigDecimal());
122: }
123:
124: public void testDoubleAndFloatPrecision() throws Exception {
125: allFieldSaveState((int) (0), (short) (0), (long) (0l),
126: (float) (10.0f / 3.0f), (double) (100.0d / 3.0d),
127: (byte) (0), (boolean) true, (char) 'q',
128: (Date) new Date(),
129: (Serializable) new StringBuffer(5000),
130: (String) RANDOM_STRING, randomBigInteger(),
131: randomBigDecimal());
132: }
133:
134: public void testZeroNumbers() throws Exception {
135: allFieldSaveState((int) (0), (short) (0), (long) (0l),
136: (float) (0.0f), (double) (0.0d), (byte) (0),
137: (boolean) true, (char) 'q', (Date) new Date(),
138: (Serializable) new StringBuffer(5000),
139: (String) RANDOM_STRING, new BigInteger("0"),
140: new BigDecimal("0.0"));
141: }
142:
143: public void testLowDate() throws Exception {
144: dateTest(0);
145: }
146:
147: public void testCurDate() throws Exception {
148: dateTest(System.currentTimeMillis());
149: }
150:
151: public void testHighDate() throws Exception {
152: try {
153: // postgres will sometimes store the String "invalid" if the
154: // date is too high, which will prevent us from even reading
155: // the records that contain this corrupt value (thus breaking
156: // any subsequent attempts to read instances of AllFieldTypesTest).
157: // An Example of a date like this is:
158: // (Timestamp) 2038-02-08 22:20:07.65
159: if (getCurrentPlatform() == AbstractTestCase.Platform.POSTGRESQL)
160: fail("Postgres can't even try to store a high date");
161:
162: dateTest(System.currentTimeMillis() * 2);
163: } catch (AssertionFailedError e) {
164: bug(6, e, "Some data stores cannot deal "
165: + "with very high dates");
166: }
167: }
168:
169: /**
170: * Some date instances that have been known to have problems.
171: */
172: public void testProblematicDates() throws Exception {
173: dateTest(1047744639); // pointbase had probs with this
174: }
175:
176: private void dateTest(long l) throws Exception {
177: Date d = new Date(l);
178:
179: allFieldSaveState((int) 10, (short) 10, (long) 10, (float) 0,
180: (double) 0, (byte) 10, (boolean) true, (char) 'x',
181: (Date) d, (Serializable) new StringBuffer(10),
182: (String) RANDOM_STRING, new BigInteger("0"),
183: new BigDecimal("0"));
184: }
185:
186: public void allFieldSaveState(int testint, short testshort,
187: long testlong, float testfloat, double testdouble,
188: byte testbyte, boolean testboolean, char testchar,
189: Date testDate, Serializable testObject, String testString,
190: BigInteger testBigInteger, BigDecimal testBigDecimal)
191: throws Exception {
192: try {
193: allFieldSaveStateInternal(testint, testshort, testlong,
194: testfloat, testdouble, testbyte, testboolean,
195: testchar, testDate, testObject, testString,
196: testBigInteger, testBigDecimal);
197: } finally {
198: try {
199: // make *sure* we do not leave a transaction open
200: rollbackTx(getPM(true, false));
201: } catch (Throwable t) {
202:
203: }
204: }
205: }
206:
207: public void allFieldSaveStateInternal(int testint, short testshort,
208: long testlong, float testfloat, double testdouble,
209: byte testbyte, boolean testboolean, char testchar,
210: Date testDate, Serializable testObject, String testString,
211: BigInteger testBigInteger, BigDecimal testBigDecimal)
212: throws Exception {
213: OpenJPAEntityManager pm = getPM(true, false);
214: startTx(pm);
215:
216: AllFieldTypesTest test = new AllFieldTypesTest();
217: pm.persist(test);
218: Object testID = pm.getObjectId(test);
219:
220: test.setTestint(testint);
221: test.setTestlong(testlong);
222: test.setTestdouble(testdouble);
223: test.setTestshort(testshort);
224: test.setTestfloat(testfloat);
225: test.setTestbyte(testbyte);
226: test.setTestboolean(testboolean);
227: test.setTestchar(testchar);
228: test.setTestString(testString);
229: test.setTestDate(testDate);
230: test.setTestObject(testObject);
231: test.setTestBigInteger(testBigInteger);
232: test.setTestBigDecimal(testBigDecimal);
233:
234: try {
235: endTx(pm);
236: } catch (Exception e) {
237: if (e instanceof Exception
238: && ((Exception) e).getMessage().indexOf(
239: "Maximum length is 8000") != -1) {
240: bug(
241: AbstractTestCase.Platform.SQLSERVER,
242: 5,
243: e,
244: "SQLServer cannot deal"
245: + " with numbers with more than 8000 digits");
246: } else {
247: throw e;
248: }
249: }
250:
251: endEm(pm);
252:
253: //assertPersistent (test, true, false, false, false);
254:
255: pm = getPM(true, false);
256: startTx(pm);
257:
258: AllFieldTypesTest retrievedObject = (AllFieldTypesTest) pm
259: .find(AllFieldTypesTest.class, testID);
260:
261: assertEquals("Field type int", testint, retrievedObject
262: .getTestint());
263: assertEquals("Field type short", testshort, retrievedObject
264: .getTestshort());
265: assertEquals("Field type boolean", testboolean, retrievedObject
266: .getTestboolean());
267: assertEquals("Field type char", testchar, retrievedObject
268: .getTestchar());
269: assertEquals("Field type long", testlong, retrievedObject
270: .getTestlong());
271:
272: assertEquals("Field type byte", testbyte, retrievedObject
273: .getTestbyte());
274: assertEquals("Field type String", testString, retrievedObject
275: .getTestString());
276:
277: int i1 = (int) (testDate.getTime() / 1000);
278: int i2 = (int) (retrievedObject.getTestDate().getTime() / 1000);
279:
280: int testDateDay = testDate.getDay();
281: int testDateMonth = testDate.getMonth();
282: int testDateYear = testDate.getYear();
283:
284: int retrievedObjectDay = retrievedObject.getTestDate().getDay();
285: int retrievedObjectMonth = retrievedObject.getTestDate()
286: .getMonth();
287: int retrievedObjectYear = retrievedObject.getTestDate()
288: .getYear();
289:
290: System.out.println("i1 : " + i1 + "\ni2 : " + i2);
291:
292: //CR346162. In this CR, it was stated that @Temporal(DATE) fields will be equal for year, month, day but not for hours,
293: //minutes, seconds. So, we removed the time check and checked only for the equality of day, month and year
294:
295: /* assertEquals("Field type Date: "
296: + testDate.getTime() + "!="
297: + retrievedObject.getTestDate().getTime()
298: + "[" + new Date(testDate.getTime()) + " != "
299: + new Date(retrievedObject.getTestDate().getTime()) + "]",
300: (int) (testDate.getTime() / 1000),
301: (int) (retrievedObject.getTestDate().getTime() / 1000));*/
302:
303: if ((testDateDay != retrievedObjectDay)
304: || (testDateMonth != retrievedObjectMonth)
305: || (testDateYear != retrievedObjectYear)) {
306: Assert
307: .fail("Field type Date not stored properly. One or more of the components of the date (day, month or year) do not match. \n"
308: + " Value that should be stored : "
309: + testDate.toGMTString()
310: + ". \nValue that is actually"
311: + "stored : "
312: + retrievedObject.getTestDate()
313: .toGMTString());
314: }
315: //assertEquals ("Field type Object", testObject,
316: //retrievedObject.getTestObject ());
317: assertEquals("Field type BigInteger", testBigInteger,
318: retrievedObject.getTestBigInteger());
319:
320: try {
321: assertEquals("Field type BigDecimal (BigInteger part)",
322: testBigDecimal.toBigInteger(), retrievedObject
323: .getTestBigDecimal().toBigInteger());
324:
325: assertEquals("Field type BigDecimal", testBigDecimal,
326: retrievedObject.getTestBigDecimal());
327:
328: assertEquals("Field type float", testfloat, retrievedObject
329: .getTestfloat(), 0.01f);
330: assertEquals("Field type double", testdouble,
331: retrievedObject.getTestdouble(), 0.01d);
332: } catch (AssertionFailedError afe) {
333: bug(3, afe,
334: "Doubles and Floats lose precision in some data stores");
335: }
336:
337: rollbackTx(pm);
338: }
339: }
|