001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.handlers;
022:
023: import db4ounit.*;
024:
025: public class NestedArrayUpdateTestCase extends
026: HandlerUpdateTestCaseBase {
027:
028: private static final Object[] primitiveArrayData = new Object[] {
029: new int[] { 1, 2, 3 }, new int[] { 4, 5 }, new int[] {}, };
030:
031: private static final Object[] stringArrayData = new Object[] {
032: new String[] { "one", null, "" }, new String[] { "two" },
033: new String[] { "" }, new String[] {}, };
034:
035: private static final Object[] nestedArrayData = new Object[] {
036: new Object[] { primitiveArrayData, stringArrayData, },
037: new Object[] { primitiveArrayData, stringArrayData, } };
038:
039: private static final Object[] nestedNestedArrayData = new Object[] {
040: new Object[] { nestedArrayData, nestedArrayData, },
041: new Object[] { nestedArrayData, nestedArrayData, } };
042:
043: public static class ItemArrays {
044:
045: public Object[] _primitiveArray;
046:
047: public Object _primitiveArrayInObject;
048:
049: public Object[] _stringArray;
050:
051: public Object _stringArrayInObject;
052:
053: public Object[] _nestedArray;
054:
055: public Object _nestedArrayInObject;
056:
057: public Object[] _nestedNestedArray;
058:
059: public Object _nestedNestedArrayInObject;
060:
061: }
062:
063: protected Object createArrays() {
064: ItemArrays item = new ItemArrays();
065:
066: item._primitiveArray = primitiveArrayData;
067: item._primitiveArrayInObject = primitiveArrayData;
068: item._stringArray = stringArrayData;
069: item._stringArrayInObject = stringArrayData;
070:
071: item._nestedArray = nestedArrayData;
072: item._nestedArrayInObject = nestedArrayData;
073: item._nestedNestedArray = nestedNestedArrayData;
074: item._nestedNestedArrayInObject = nestedNestedArrayData;
075:
076: return item;
077: }
078:
079: protected void assertArrays(Object obj) {
080: ItemArrays item = (ItemArrays) obj;
081:
082: assertPrimitiveArray(item._primitiveArray);
083: assertPrimitiveArray(item._primitiveArrayInObject);
084: assertStringArray(item._stringArray);
085: assertStringArray(item._stringArrayInObject);
086:
087: assertNestedArray(nestedArrayData, item._nestedArray);
088: assertNestedArray(nestedArrayData, item._nestedArrayInObject);
089: assertNestedArray(nestedNestedArrayData,
090: item._nestedNestedArray);
091: assertNestedArray(nestedNestedArrayData,
092: item._nestedNestedArrayInObject);
093: }
094:
095: private void assertNestedArray(Object expected, Object actual) {
096: Object[] expectedArray = (Object[]) expected;
097: Object[] actualArray = (Object[]) actual;
098: Assert.areEqual(expectedArray.length, actualArray.length);
099: for (int i = 0; i < expectedArray.length; i++) {
100: Object[] expectedSubArray = (Object[]) expectedArray[i];
101: Object actualSubArray = actualArray[i];
102: Object template = expectedSubArray[0];
103: if (template instanceof int[]) {
104: assertPrimitiveArray(actualSubArray);
105: } else if (template instanceof String[]) {
106: assertStringArray(actualSubArray);
107: } else {
108: assertNestedArray(expectedSubArray, actualSubArray);
109: }
110: }
111: }
112:
113: private void assertStringArray(Object array) {
114: Object[] stringArray = (Object[]) array;
115: for (int i = 0; i < stringArray.length; i++) {
116: String[] actual = (String[]) stringArray[i];
117: String[] expected = (String[]) stringArrayData[i];
118: Assert.areEqual(actual.length, expected.length);
119: for (int j = 0; j < expected.length; j++) {
120: Assert.areEqual(expected[j], actual[j]);
121: }
122: }
123: }
124:
125: private void assertPrimitiveArray(Object array) {
126: Object[] primitiveArray = (Object[]) array;
127: for (int i = 0; i < primitiveArray.length; i++) {
128: int[] expected = (int[]) primitiveArrayData[i];
129: int[] actual = castToIntArray(primitiveArray[i]);
130: Assert.areEqual(actual.length, expected.length);
131: for (int j = 0; j < expected.length; j++) {
132: Assert.areEqual(expected[j], actual[j]);
133: }
134: }
135: }
136:
137: protected Object[] createValues() {
138: // not used
139: return null;
140: }
141:
142: protected void assertValues(Object[] values) {
143: // not used
144: }
145:
146: protected String typeName() {
147: return "nested_array";
148: }
149:
150: }
|