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 com.db4o.*;
024: import com.db4o.db4ounit.util.*;
025:
026: import db4ounit.*;
027:
028: public class BooleanHandlerUpdateTestCase extends
029: HandlerUpdateTestCaseBase {
030:
031: public static class Item {
032: public boolean _typedPrimitive;
033:
034: public Boolean _typedWrapper;
035:
036: public Object _untyped;
037: }
038:
039: public static class ItemArrays {
040: public boolean[] _typedPrimitiveArray;
041:
042: public Boolean[] _typedWrapperArray;
043:
044: public Object[] _untypedObjectArray;
045:
046: public Object _primitiveArrayInObject;
047:
048: public Object _wrapperArrayInObject;
049: }
050:
051: private static final boolean[] data = new boolean[] { true, false, };
052:
053: public static void main(String[] args) {
054: new TestRunner(BooleanHandlerUpdateTestCase.class).run();
055: }
056:
057: protected void assertArrays(Object obj) {
058: ItemArrays itemArrays = (ItemArrays) obj;
059:
060: assertPrimitiveArray(itemArrays._typedPrimitiveArray);
061: if (_db4oHeaderVersion == VersionServices.HEADER_30_40) {
062: // Bug in the oldest format: It accidentally boolean[] arrays to
063: // Boolean[] arrays.
064: assertWrapperArray((Boolean[]) itemArrays._primitiveArrayInObject);
065: } else {
066: assertPrimitiveArray((boolean[]) itemArrays._primitiveArrayInObject);
067: }
068: assertWrapperArray(itemArrays._typedWrapperArray);
069: assertUntypedObjectArray(itemArrays);
070: assertWrapperArray((Boolean[]) itemArrays._wrapperArrayInObject);
071: }
072:
073: /**
074: * @sharpen.remove Cannot convert 'object[]' to 'Boolean[]' in .net
075: */
076: private void assertUntypedObjectArray(ItemArrays itemArrays) {
077: assertWrapperArray((Boolean[]) itemArrays._untypedObjectArray);
078: }
079:
080: private void assertPrimitiveArray(boolean[] primitiveArray) {
081: for (int i = 0; i < data.length; i++) {
082: assertAreEqual(data[i], primitiveArray[i]);
083: }
084: }
085:
086: private void assertWrapperArray(Boolean[] wrapperArray) {
087: for (int i = 0; i < data.length; i++) {
088: assertAreEqual(new Boolean(data[i]), wrapperArray[i]);
089: }
090: // FIXME: Arrays should also get a null Bitmap to fix.
091: // Assert.isNull(wrapperArray[wrapperArray.length - 1]);
092: }
093:
094: protected void assertValues(Object[] values) {
095: for (int i = 0; i < data.length; i++) {
096: Item item = (Item) values[i];
097: assertAreEqual(data[i], item._typedPrimitive);
098: assertAreEqual(new Boolean(data[i]), item._typedWrapper);
099: assertAreEqual(new Boolean(data[i]), item._untyped);
100: }
101:
102: Item nullItem = (Item) values[values.length - 1];
103: assertAreEqual(false, nullItem._typedPrimitive);
104: assertPrimitiveWrapperIsNullJavaOnly(nullItem._typedWrapper);
105: Assert.isNull(nullItem._untyped);
106: }
107:
108: private void assertAreEqual(boolean expected, boolean actual) {
109: Assert.areEqual(expected, actual);
110: }
111:
112: private void assertAreEqual(Object expected, Object actual) {
113: Assert.areEqual(expected, actual);
114: }
115:
116: protected Object createArrays() {
117: ItemArrays itemArrays = new ItemArrays();
118: itemArrays._typedPrimitiveArray = new boolean[data.length];
119: System.arraycopy(data, 0, itemArrays._typedPrimitiveArray, 0,
120: data.length);
121:
122: Boolean[] dataWrapper = new Boolean[data.length];
123: for (int i = 0; i < data.length; i++) {
124: dataWrapper[i] = new Boolean(data[i]);
125: }
126:
127: itemArrays._typedWrapperArray = new Boolean[data.length + 1];
128: System.arraycopy(dataWrapper, 0, itemArrays._typedWrapperArray,
129: 0, dataWrapper.length);
130:
131: initializeUntypedObjectArray(itemArrays, dataWrapper);
132:
133: boolean[] primitiveArray = new boolean[data.length];
134: System.arraycopy(data, 0, primitiveArray, 0, data.length);
135: itemArrays._primitiveArrayInObject = primitiveArray;
136:
137: Boolean[] wrapperArray = new Boolean[data.length + 1];
138: System.arraycopy(dataWrapper, 0, wrapperArray, 0,
139: dataWrapper.length);
140: itemArrays._wrapperArrayInObject = wrapperArray;
141: return itemArrays;
142: }
143:
144: /**
145: * @sharpen.remove Cannot convert 'Boolean[]' to 'object[]'
146: */
147: private void initializeUntypedObjectArray(ItemArrays itemArrays,
148: Boolean[] dataWrapper) {
149: itemArrays._untypedObjectArray = new Boolean[data.length + 1];
150: System.arraycopy(dataWrapper, 0,
151: itemArrays._untypedObjectArray, 0, dataWrapper.length);
152: }
153:
154: protected Object[] createValues() {
155: Item[] values = new Item[data.length + 1];
156: for (int i = 0; i < data.length; i++) {
157: Item item = new Item();
158: item._typedPrimitive = data[i];
159: item._typedWrapper = new Boolean(data[i]);
160: item._untyped = new Boolean(data[i]);
161: values[i] = item;
162: }
163: values[values.length - 1] = new Item();
164: return values;
165: }
166:
167: protected String typeName() {
168: return "boolean";
169: }
170:
171: }
|