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;
020:
021: import java.lang.reflect.Array;
022: import java.text.Collator;
023: import java.util.Arrays;
024: import java.util.Comparator;
025: import java.util.Date;
026:
027: import org.apache.openjpa.persistence.kernel.common.apps.ArraysTest;
028: import org.apache.openjpa.persistence.common.utils.AbstractTestCase;
029: import junit.framework.AssertionFailedError;
030: import org.apache.openjpa.persistence.OpenJPAEntityManager;
031:
032: /**
033: * Test varying kinds of array values.
034: *
035: * @author Marc Prud'hommeaux
036: * @author Abe White
037: */
038: public class TestArrayValues2 extends BaseKernelTest {
039:
040: private static double DOUBLE_PRECISION = 0.01D;
041: public static float FLOAT_PRECISION = 0.01F;
042:
043: public void testStringArray() throws Exception {
044: int max = ((int) (Math.random() * 20)) + 5;
045: String[] array = new String[max];
046: for (int i = 0; i < max; i++)
047: array[i] = randomString();
048: saveArray(array, false, false);
049: }
050:
051: public void testLongArray() throws Exception {
052: int max = ((int) (Math.random() * 20)) + 5;
053: Long[] array = new Long[max];
054: for (int i = 0; i < max; i++)
055: array[i] = randomLong();
056: try {
057: saveArray(array, false, false);
058: } catch (AssertionFailedError afe) {
059: }
060: }
061:
062: public void testLongPrimitiveArray() throws Exception {
063: int max = ((int) (Math.random() * 20)) + 5;
064: long[] array = new long[max];
065: for (int i = 0; i < max; i++)
066: array[i] = randomLong().longValue();
067: try {
068: saveArray(array, true, false);
069: } catch (AssertionFailedError afe) {
070: }
071: }
072:
073: public void testShortArray() throws Exception {
074: int max = ((int) (Math.random() * 20)) + 5;
075: Short[] array = new Short[max];
076: for (int i = 0; i < max; i++)
077: array[i] = randomShort();
078: saveArray(array, false, false);
079: }
080:
081: public void testShortPrimitiveArray() throws Exception {
082: int max = ((int) (Math.random() * 20)) + 5;
083: short[] array = new short[max];
084: for (int i = 0; i < max; i++)
085: array[i] = randomShort().shortValue();
086: saveArray(array, true, false);
087: }
088:
089: public void testIntArray() throws Exception {
090: int max = ((int) (Math.random() * 20)) + 5;
091: Integer[] array = new Integer[max];
092: for (int i = 0; i < max; i++)
093: array[i] = randomInt();
094: saveArray(array, false, false);
095: }
096:
097: public void testIntPrimitiveArray() throws Exception {
098: int max = ((int) (Math.random() * 20)) + 5;
099: int[] array = new int[max];
100: for (int i = 0; i < max; i++)
101: array[i] = randomInt().intValue();
102: saveArray(array, true, false);
103: }
104:
105: // FixMe: Both tests hangs on Mysql
106: /*public void testByteArray()
107: throws Exception {
108: int max = ((int) (Math.random() * 20)) + 5;
109: Byte[] array = new Byte[max];
110: for (int i = 0; i < max; i++)
111: array[i] = randomByte();
112: saveArray(array, false, false);
113: }
114:
115: public void testBytePrimitiveArray()
116: throws Exception {
117: int max = ((int) (Math.random() * 20)) + 5;
118: byte[] array = new byte[max];
119: for (int i = 0; i < max; i++)
120: array[i] = randomByte().byteValue();
121: saveArray(array, true, false);
122: }*/
123:
124: public void testBooleanArray() throws Exception {
125: int max = ((int) (Math.random() * 20)) + 5;
126: Boolean[] array = new Boolean[max];
127: for (int i = 0; i < max; i++)
128: array[i] = randomBoolean();
129: saveArray(array, false, false);
130: }
131:
132: public void testCharacterArray() throws Exception {
133: int max = ((int) (Math.random() * 20)) + 5;
134: Character[] array = new Character[max];
135: array[0] = new Character((char) 1);
136: for (int i = 1; i < max; i++)
137: array[i] = randomChar();
138: saveArray(array, false, false);
139: }
140:
141: public void testCharacterPrimitiveArray() throws Exception {
142: int max = ((int) (Math.random() * 20)) + 5;
143: char[] array = new char[max];
144: array[0] = 1;
145: for (int i = 1; i < max; i++)
146: array[i] = randomChar().charValue();
147: saveArray(array, true, false);
148: }
149:
150: public void testCharacterPrimitiveClobArray() throws Exception {
151: int max = ((int) (Math.random() * 20)) + 5;
152: char[] array = new char[max];
153: array[0] = 1;
154: for (int i = 1; i < max; i++)
155: array[i] = randomChar().charValue();
156: saveArray(array, true, true);
157: }
158:
159: public void testBooleanPrimitiveArray() throws Exception {
160: int max = ((int) (Math.random() * 20)) + 5;
161: boolean[] array = new boolean[max];
162: for (int i = 0; i < max; i++)
163: array[i] = randomBoolean().booleanValue();
164: saveArray(array, true, false);
165: }
166:
167: public void testFloatArray() throws Exception {
168: int max = ((int) (Math.random() * 20)) + 5;
169: Float[] array = new Float[max];
170: for (int i = 0; i < max; i++)
171: array[i] = randomFloat();
172: saveArray(array, false, false);
173: }
174:
175: public void testFloatPrimitiveArray() throws Exception {
176: int max = ((int) (Math.random() * 20)) + 5;
177: float[] array = new float[max];
178: for (int i = 0; i < max; i++)
179: array[i] = randomFloat().floatValue();
180: saveArray(array, true, false);
181: }
182:
183: public void testDoubleArray() throws Exception {
184: int max = ((int) (Math.random() * 20)) + 5;
185: Double[] array = new Double[max];
186: for (int i = 0; i < max; i++)
187: array[i] = randomDouble();
188: saveArray(array, false, false);
189: }
190:
191: public void testDoublePrimitiveArray() throws Exception {
192: int max = ((int) (Math.random() * 20)) + 5;
193: double[] array = new double[max];
194: for (int i = 0; i < max; i++)
195: array[i] = randomDouble().doubleValue();
196: saveArray(array, true, false);
197: }
198:
199: public void testDateArray() throws Exception {
200: int max = ((int) (Math.random() * 20)) + 5;
201: Date[] array = new Date[max];
202: for (int i = 0; i < max; i++)
203: array[i] = randomDate();
204: saveArray(array, false, false);
205: }
206:
207: /*Fix Me aokeke - Takes a lot of time to run */
208: // public void testFirstClassObjectArray()throws Exception
209: // {
210: // int max = ((int) (Math.random() * 20)) + 5;
211: // ArraysTest[] array = new ArraysTest[max];
212: // for (int i = 0; i < max; i++)
213: // array[i] = new ArraysTest();
214: // saveArray(array, false, false);
215: // }
216: private void saveArray(Object array, boolean primitive, boolean lob)
217: throws Exception {
218: try {
219: saveArrayInternal(array, primitive, lob);
220: } catch (Exception e) {
221: throw e;
222: } catch (Error error) {
223: throw error;
224: } finally {
225: //
226: }
227: }
228:
229: private void saveArrayInternal(Object vals, boolean primitive,
230: boolean lob) throws Exception {
231: Object[] array;
232: if (primitive)
233: array = convertPrimitiveArray(vals);
234: else
235: array = (Object[]) vals;
236: Arrays.sort(array, new TestArraySorter());
237:
238: OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
239: startTx(pm);
240:
241: ArraysTest test = new ArraysTest();
242: pm.persist(test);
243: int testID = test.getId();
244:
245: setGetTestArray(test, vals, primitive, lob, true);
246: endTx(pm);
247:
248: pm = (OpenJPAEntityManager) currentEntityManager();
249: startTx(pm);
250: ArraysTest retrievedObject = pm.find(ArraysTest.class, testID);
251:
252: Object retrievedVals = setGetTestArray(retrievedObject, vals,
253: primitive, lob, false);
254: Object[] retrievedArray;
255: if (primitive)
256: retrievedArray = convertPrimitiveArray(retrievedVals);
257: else
258: retrievedArray = (Object[]) retrievedVals;
259:
260: assertNotNull(retrievedArray);
261: assertTrue(array.length != 0);
262: assertEquals(array.length, retrievedArray.length);
263: assertNotNull(array[0]);
264: assertNotNull(retrievedArray[0]);
265:
266: // make sure the classes of the keys are the same.
267: assertEquals(array[0].getClass(), retrievedArray[0].getClass());
268: Arrays.sort(retrievedArray, new TestArraySorter());
269: for (int i = 0; i < array.length; i++)
270: assertClassAndValueEquals(array[i], retrievedArray[i]);
271:
272: pm.remove(retrievedObject);
273: endTx(pm);
274: }
275:
276: private Object[] convertPrimitiveArray(Object array)
277: throws Exception {
278: int length = Array.getLength(array);
279: Class type = Array.get(array, 0).getClass();
280:
281: Object[] copy = (Object[]) Array.newInstance(type, length);
282: for (int i = 0; i < length; i++)
283: copy[i] = Array.get(array, i);
284:
285: return copy;
286: }
287:
288: private void assertClassAndValueEquals(Object o1, Object o2) {
289: assertTrue("First object was null", o1 != null);
290: assertTrue("Second object was null", o2 != null);
291:
292: assertTrue("Types did not match (class1="
293: + o1.getClass().getName() + ", class2="
294: + o2.getClass().getName() + ")", o1.getClass()
295: .isAssignableFrom(o2.getClass()));
296:
297: // floats and doubles are a little special: we only
298: // compare them to a certain precision, after which
299: // we give up.
300: if (o1 instanceof Double)
301: assertEquals(((Double) o1).doubleValue(), ((Double) o2)
302: .doubleValue(), DOUBLE_PRECISION);
303: else if (o1 instanceof Float)
304: assertEquals(((Float) o1).floatValue(), ((Float) o2)
305: .floatValue(), FLOAT_PRECISION);
306: else
307: assertEquals("Object did not match (class1="
308: + o1.getClass().getName() + ", class2="
309: + o2.getClass().getName() + ")", o1, o2);
310: }
311:
312: /**
313: * Generic setter/getter for setting the array.
314: */
315: private Object setGetTestArray(ArraysTest test, Object array,
316: boolean primitive, boolean lob, boolean doSet)
317: throws Exception {
318: if (array == null)
319: return null;
320:
321: Object first = Array.get(array, 0);
322: if (first instanceof Date) {
323: if (doSet)
324: test.setDate((Date[]) array);
325: return test.getDate();
326: } else if (first instanceof String) {
327: if (doSet)
328: test.setString((String[]) array);
329: return test.getString();
330: } else if (first instanceof Character) {
331: if (doSet && !primitive)
332: test.setCharacter((Character[]) array);
333: else if (doSet && !lob)
334: test.setCharacterP((char[]) array);
335: else if (doSet)
336: test.setCharacterPClob((char[]) array);
337: else if (!primitive)
338: return test.getCharacter();
339: else if (!lob)
340: return test.getCharacterP();
341: else
342: return test.getCharacterPClob();
343: return null;
344: } else if (first instanceof Double) {
345: if (doSet && !primitive)
346: test.setDouble((Double[]) array);
347: else if (doSet)
348: test.setDoubleP((double[]) array);
349: else if (!primitive)
350: return test.getDouble();
351: else
352: return test.getDoubleP();
353: return null;
354: } else if (first instanceof Byte) {
355: if (doSet && !primitive)
356: test.setByte((Byte[]) array);
357: else if (doSet)
358: test.setByteP((byte[]) array);
359: else if (!primitive)
360: return test.getByte();
361: else
362: return test.getByteP();
363: return null;
364: } else if (first instanceof Float) {
365: if (doSet && !primitive)
366: test.setFloat((Float[]) array);
367: else if (doSet)
368: test.setFloatP((float[]) array);
369: else if (!primitive)
370: return test.getFloat();
371: else
372: return test.getFloatP();
373: return null;
374: } else if (first instanceof Long) {
375: if (doSet && !primitive)
376: test.setLong((Long[]) array);
377: else if (doSet)
378: test.setLongP((long[]) array);
379: else if (!primitive)
380: return test.getLong();
381: else
382: return test.getLongP();
383: return null;
384: } else if (first instanceof Integer) {
385: if (doSet && !primitive)
386: test.setInt((Integer[]) array);
387: else if (doSet)
388: test.setIntP((int[]) array);
389: else if (!primitive)
390: return test.getInt();
391: else
392: return test.getIntP();
393: return null;
394: } else if (first instanceof Short) {
395: if (doSet && !primitive)
396: test.setShort((Short[]) array);
397: else if (doSet)
398: test.setShortP((short[]) array);
399: else if (!primitive)
400: return test.getShort();
401: else
402: return test.getShortP();
403: return null;
404: } else if (first instanceof Boolean) {
405: if (doSet && !primitive)
406: test.setBoolean((Boolean[]) array);
407: else if (doSet)
408: test.setBooleanP((boolean[]) array);
409: else if (!primitive)
410: return test.getBoolean();
411: else
412: return test.getBooleanP();
413: return null;
414: } else if (first instanceof ArraysTest) {
415: if (doSet)
416: test.setArraysTest((ArraysTest[]) array);
417: return test.getArraysTest();
418: }
419:
420: fail("Unknown array type");
421: return null;
422: }
423:
424: private static class TestArraySorter implements Comparator {
425:
426: private Collator collator = Collator.getInstance();
427:
428: public int compare(Object o1, Object o2) {
429: if (o1.equals(o2))
430: return 0;
431:
432: if (o1 instanceof Number) {
433: return ((Number) o1).doubleValue() > ((Number) o2)
434: .doubleValue() ? 1 : -1;
435: } else if (o1 instanceof Date) {
436: return ((Date) o1).before((Date) o2) ? 1 : -1;
437: } else if (o1 instanceof ArraysTest) {
438: return ((ArraysTest) o1).compareTo(o2);
439: }
440: return collator.compare(o1.toString(), o2.toString());
441: }
442: }
443: }
|