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:
025: import db4ounit.*;
026:
027: public class MarshallingBufferTestCase implements TestCase {
028:
029: private static final int DATA_1 = 111;
030: private static final byte DATA_2 = (byte) 2;
031: private static final int DATA_3 = 333;
032: private static final int DATA_4 = 444;
033: private static final int DATA_5 = 55;
034:
035: public void testWrite() {
036: MarshallingBuffer buffer = new MarshallingBuffer();
037:
038: buffer.writeInt(DATA_1);
039: buffer.writeByte(DATA_2);
040:
041: Buffer content = inspectContent(buffer);
042:
043: Assert.areEqual(DATA_1, content.readInt());
044: Assert.areEqual(DATA_2, content.readByte());
045: }
046:
047: public void testTransferLastWrite() {
048:
049: MarshallingBuffer buffer = new MarshallingBuffer();
050:
051: buffer.writeInt(DATA_1);
052: int lastOffset = offset(buffer);
053: buffer.writeByte(DATA_2);
054:
055: MarshallingBuffer other = new MarshallingBuffer();
056:
057: buffer.transferLastWriteTo(other, true);
058:
059: Assert.areEqual(lastOffset, offset(buffer));
060:
061: Buffer content = inspectContent(other);
062: Assert.areEqual(DATA_2, content.readByte());
063: }
064:
065: private int offset(MarshallingBuffer buffer) {
066: return buffer.testDelegate().offset();
067: }
068:
069: private Buffer inspectContent(MarshallingBuffer buffer) {
070: Buffer bufferDelegate = buffer.testDelegate();
071: bufferDelegate.offset(0);
072: return bufferDelegate;
073: }
074:
075: public void testChildren() {
076: MarshallingBuffer buffer = new MarshallingBuffer();
077: buffer.writeInt(DATA_1);
078: buffer.writeByte(DATA_2);
079:
080: MarshallingBuffer child = buffer.addChild();
081: child.writeInt(DATA_3);
082: child.writeInt(DATA_4);
083:
084: buffer.mergeChildren(null, 0, 0);
085:
086: Buffer content = inspectContent(buffer);
087: Assert.areEqual(DATA_1, content.readInt());
088: Assert.areEqual(DATA_2, content.readByte());
089:
090: int address = content.readInt();
091: content.offset(address);
092:
093: Assert.areEqual(DATA_3, content.readInt());
094: Assert.areEqual(DATA_4, content.readInt());
095: }
096:
097: public void testGrandChildren() {
098: MarshallingBuffer buffer = new MarshallingBuffer();
099: buffer.writeInt(DATA_1);
100: buffer.writeByte(DATA_2);
101:
102: MarshallingBuffer child = buffer.addChild();
103: child.writeInt(DATA_3);
104: child.writeInt(DATA_4);
105:
106: MarshallingBuffer grandChild = child.addChild();
107: grandChild.writeInt(DATA_5);
108:
109: buffer.mergeChildren(null, 0, 0);
110:
111: Buffer content = inspectContent(buffer);
112: Assert.areEqual(DATA_1, content.readInt());
113: Assert.areEqual(DATA_2, content.readByte());
114:
115: int address = content.readInt();
116: content.offset(address);
117:
118: Assert.areEqual(DATA_3, content.readInt());
119: Assert.areEqual(DATA_4, content.readInt());
120:
121: address = content.readInt();
122: content.offset(address);
123: Assert.areEqual(DATA_5, content.readInt());
124:
125: }
126:
127: public void testLinkOffset() {
128:
129: int linkOffset = 7;
130:
131: MarshallingBuffer buffer = new MarshallingBuffer();
132: buffer.writeInt(DATA_1);
133: buffer.writeByte(DATA_2);
134:
135: MarshallingBuffer child = buffer.addChild();
136: child.writeInt(DATA_3);
137: child.writeInt(DATA_4);
138:
139: MarshallingBuffer grandChild = child.addChild();
140: grandChild.writeInt(DATA_5);
141:
142: buffer.mergeChildren(null, 0, linkOffset);
143:
144: Buffer content = inspectContent(buffer);
145:
146: Buffer extendedBuffer = new Buffer(content.length()
147: + linkOffset);
148:
149: content.copyTo(extendedBuffer, 0, linkOffset, content.length());
150:
151: extendedBuffer.offset(linkOffset);
152:
153: Assert.areEqual(DATA_1, extendedBuffer.readInt());
154: Assert.areEqual(DATA_2, extendedBuffer.readByte());
155:
156: int address = extendedBuffer.readInt();
157: extendedBuffer.offset(address);
158:
159: Assert.areEqual(DATA_3, extendedBuffer.readInt());
160: Assert.areEqual(DATA_4, extendedBuffer.readInt());
161:
162: address = extendedBuffer.readInt();
163: extendedBuffer.offset(address);
164: Assert.areEqual(DATA_5, extendedBuffer.readInt());
165:
166: }
167:
168: public void testLateChildrenWrite() {
169: MarshallingBuffer buffer = new MarshallingBuffer();
170: buffer.writeInt(DATA_1);
171: MarshallingBuffer child = buffer.addChild(true, true);
172: child.writeInt(DATA_3);
173: buffer.writeByte(DATA_2);
174: child.writeInt(DATA_4);
175: buffer.mergeChildren(null, 0, 0);
176:
177: Buffer content = inspectContent(buffer);
178: Assert.areEqual(DATA_1, content.readInt());
179:
180: int address = content.readInt();
181: content.readInt(); // length
182:
183: Assert.areEqual(DATA_2, content.readByte());
184:
185: content.seek(address);
186: Assert.areEqual(DATA_3, content.readInt());
187: Assert.areEqual(DATA_4, content.readInt());
188:
189: }
190:
191: }
|