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.internal;
022:
023: import com.db4o.internal.*;
024: import com.db4o.internal.marshall.*;
025: import com.db4o.internal.slots.*;
026:
027: import db4ounit.*;
028: import db4ounit.extensions.*;
029:
030: public class MarshallingContextTestCase extends AbstractDb4oTestCase {
031:
032: public static void main(String[] arguments) {
033: new MarshallingContextTestCase().runSolo();
034: }
035:
036: public static class StringItem {
037: public String _name;
038:
039: public StringItem(String name) {
040: _name = name;
041: }
042: }
043:
044: public static class StringIntItem {
045: public String _name;
046: public int _int;
047:
048: public StringIntItem(String name, int i) {
049: _name = name;
050: _int = i;
051: }
052: }
053:
054: public static class StringIntBooleanItem {
055: public String _name;
056: public int _int;
057: public boolean _bool;
058:
059: public StringIntBooleanItem(String name, int i, boolean bool) {
060: _name = name;
061: _int = i;
062: _bool = bool;
063: }
064: }
065:
066: public void testStringItem() {
067: StringItem writtenItem = new StringItem("one");
068: StringItem readItem = (StringItem) writeRead(writtenItem);
069: Assert.areEqual(writtenItem._name, readItem._name);
070: }
071:
072: public void testStringIntItem() {
073: StringIntItem writtenItem = new StringIntItem("one", 777);
074: StringIntItem readItem = (StringIntItem) writeRead(writtenItem);
075: Assert.areEqual(writtenItem._name, readItem._name);
076: Assert.areEqual(writtenItem._int, readItem._int);
077: }
078:
079: public void testStringIntBooleanItem() {
080: StringIntBooleanItem writtenItem = new StringIntBooleanItem(
081: "one", 777, true);
082: StringIntBooleanItem readItem = (StringIntBooleanItem) writeRead(writtenItem);
083: Assert.areEqual(writtenItem._name, readItem._name);
084: Assert.areEqual(writtenItem._int, readItem._int);
085: Assert.areEqual(writtenItem._bool, readItem._bool);
086: }
087:
088: private Object writeRead(Object obj) {
089: int imaginativeID = 500;
090: ObjectReference ref = new ObjectReference(
091: classMetadataForObject(obj), imaginativeID);
092: ref.setObject(obj);
093: ObjectMarshaller marshaller = MarshallerFamily.current()._object;
094: MarshallingContext marshallingContext = new MarshallingContext(
095: trans(), ref, Integer.MAX_VALUE, true);
096: marshaller.marshall(ref.getObject(), marshallingContext);
097: Pointer4 pointer = marshallingContext.allocateSlot();
098: Buffer buffer = marshallingContext.ToWriteBuffer(pointer);
099:
100: buffer.offset(0);
101:
102: // String str = new String(buffer._buffer);
103: // System.out.println(str);
104:
105: UnmarshallingContext unmarshallingContext = new UnmarshallingContext(
106: trans(), ref, Const4.ADD_TO_ID_TREE, false);
107: unmarshallingContext.buffer(buffer);
108: unmarshallingContext.activationDepth(5);
109: return unmarshallingContext.read();
110: }
111:
112: private ClassMetadata classMetadataForObject(Object obj) {
113: return stream()
114: .produceClassMetadata(reflector().forObject(obj));
115: }
116:
117: }
|