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.db4ounit.util.*;
024: import com.db4o.foundation.*;
025:
026: import db4ounit.*;
027:
028: public class CharHandlerUpdateTestCase extends
029: HandlerUpdateTestCaseBase {
030:
031: public static class Item {
032: public char _typedPrimitive;
033:
034: public Character _typedWrapper;
035:
036: public Object _untyped;
037: }
038:
039: public static class ItemArrays {
040: public char[] _typedPrimitiveArray;
041:
042: public Character[] _typedWrapperArray;
043:
044: public Object[] _untypedObjectArray;
045:
046: public Object _primitiveArrayInObject;
047:
048: public Object _wrapperArrayInObject;
049: }
050:
051: private static final char[] data = new char[] {
052: Character.MIN_VALUE, (char) 0x0000, (char) 0x000F,
053: (char) 0x00F0, (char) 0x00FF, (char) 0x0F00, (char) 0x0F0F,
054: (char) 0x0FF0, (char) 0x0FFF, (char) 0xF000, (char) 0xF00F,
055: (char) 0xF0F0, (char) 0xF0FF, (char) 0xFF00, (char) 0xFF0F,
056: (char) 0xFFF0, (char) 0xFFFF, Character.MAX_VALUE, };
057:
058: public static void main(String[] args) {
059: new TestRunner(CharHandlerUpdateTestCase.class).run();
060: }
061:
062: protected void assertArrays(Object obj) {
063: ItemArrays itemArrays = (ItemArrays) obj;
064:
065: assertPrimitiveArray(itemArrays._typedPrimitiveArray);
066: assertPrimitiveArray(castToCharArray(itemArrays._primitiveArrayInObject));
067: assertWrapperArray(itemArrays._typedWrapperArray);
068: assertUntypedObjectArray(itemArrays);
069: assertWrapperArray((Character[]) itemArrays._wrapperArrayInObject);
070: }
071:
072: /**
073: * @sharpen.remove Cannot convert 'object[]' to 'Short[]' in .net
074: */
075: private void assertUntypedObjectArray(ItemArrays itemArrays) {
076: assertWrapperArray((Character[]) itemArrays._untypedObjectArray);
077: }
078:
079: private void assertPrimitiveArray(char[] primitiveArray) {
080: for (int i = 0; i < data.length; i++) {
081: assertAreEqual(data[i], primitiveArray[i]);
082: }
083: }
084:
085: private void assertWrapperArray(Character[] wrapperArray) {
086: for (int i = 0; i < data.length; i++) {
087: assertAreEqual(new Character(data[i]), wrapperArray[i]);
088: }
089: //FIXME: Arrays should also get a null Bitmap to fix.
090: //Assert.isNull(wrapperArray[wrapperArray.length - 1]);
091: }
092:
093: protected void assertValues(Object[] values) {
094: for (int i = 0; i < data.length; i++) {
095: Item item = (Item) values[i];
096: assertAreEqual(data[i], item._typedPrimitive);
097: assertAreEqual(new Character(data[i]), item._typedWrapper);
098: assertAreEqual(new Character(data[i]), item._untyped);
099: }
100:
101: Item nullItem = (Item) values[values.length - 1];
102: assertAreEqual((char) 0, nullItem._typedPrimitive);
103: assertCharWrapperIsNullJavaOnly(nullItem._typedWrapper);
104: Assert.isNull(nullItem._untyped);
105: }
106:
107: protected Object createArrays() {
108: ItemArrays itemArrays = new ItemArrays();
109: itemArrays._typedPrimitiveArray = new char[data.length];
110: System.arraycopy(data, 0, itemArrays._typedPrimitiveArray, 0,
111: data.length);
112:
113: Character[] dataWrapper = new Character[data.length];
114: for (int i = 0; i < data.length; i++) {
115: dataWrapper[i] = new Character(data[i]);
116: }
117:
118: itemArrays._typedWrapperArray = new Character[data.length + 1];
119: System.arraycopy(dataWrapper, 0, itemArrays._typedWrapperArray,
120: 0, dataWrapper.length);
121:
122: initializeUntypedObjectArray(itemArrays, dataWrapper);
123:
124: char[] primitiveArray = new char[data.length];
125: System.arraycopy(data, 0, primitiveArray, 0, data.length);
126: itemArrays._primitiveArrayInObject = primitiveArray;
127:
128: Character[] wrapperArray = new Character[data.length + 1];
129: System.arraycopy(dataWrapper, 0, wrapperArray, 0,
130: dataWrapper.length);
131: itemArrays._wrapperArrayInObject = wrapperArray;
132: return itemArrays;
133: }
134:
135: /**
136: * @sharpen.remove Cannot convert 'Character[]' to 'object[]'
137: */
138: private void initializeUntypedObjectArray(ItemArrays itemArrays,
139: Character[] dataWrapper) {
140: itemArrays._untypedObjectArray = new Character[data.length + 1];
141: System.arraycopy(dataWrapper, 0,
142: itemArrays._untypedObjectArray, 0, dataWrapper.length);
143: }
144:
145: protected Object[] createValues() {
146: Item[] values = new Item[data.length + 1];
147: for (int i = 0; i < data.length; i++) {
148: Item item = new Item();
149: item._typedPrimitive = data[i];
150: item._typedWrapper = new Character(data[i]);
151: item._untyped = new Character(data[i]);
152: values[i] = item;
153: }
154: values[values.length - 1] = new Item();
155: return values;
156: }
157:
158: protected String typeName() {
159: return "char";
160: }
161:
162: private void assertAreEqual(char expected, char actual) {
163: Assert.areEqual(expected, actual);
164: }
165:
166: private void assertAreEqual(Object expected, Object actual) {
167: Assert.areEqual(expected, actual);
168: }
169:
170: /**
171: * @sharpen.remove
172: */
173: private void assertCharWrapperIsNullJavaOnly(Object obj) {
174: if (_handlerVersion == 0) {
175:
176: // Bug when reading old format:
177: // Null wrappers are converted to Character.MAX_VALUE
178:
179: Assert.areEqual(new Character(Character.MAX_VALUE), obj);
180: } else {
181: Assert.isNull(obj);
182: }
183: }
184:
185: private char[] castToCharArray(Object obj) {
186: ObjectByRef byRef = new ObjectByRef(obj);
187: castToCharArrayJavaOnly(byRef);
188: return (char[]) byRef.value;
189: }
190:
191: /**
192: * @sharpen.remove
193: */
194: private void castToCharArrayJavaOnly(ObjectByRef byRef) {
195: if (_db4oHeaderVersion != VersionServices.HEADER_30_40) {
196: return;
197: }
198:
199: // Bug in the oldest format:
200: // It accidentally converted char[] arrays to Character[] arrays.
201:
202: Character[] wrapperArray = (Character[]) byRef.value;
203: char[] res = new char[wrapperArray.length];
204: for (int i = 0; i < wrapperArray.length; i++) {
205: if (wrapperArray[i] != null) {
206: res[i] = wrapperArray[i].charValue();
207: }
208: }
209: byRef.value = res;
210: }
211:
212: }
|