01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 04. June 2006 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.io.binary;
13:
14: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
15: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
16: import com.thoughtworks.xstream.io.copy.HierarchicalStreamCopier;
17: import com.thoughtworks.xstream.io.xml.AbstractXMLReaderTest;
18: import com.thoughtworks.xstream.io.xml.XppReader;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.StringReader;
22: import java.io.ByteArrayInputStream;
23:
24: public class BinaryStreamTest extends AbstractXMLReaderTest {
25:
26: private HierarchicalStreamCopier copier = new HierarchicalStreamCopier();
27:
28: protected void setUp() throws Exception {
29: super .setUp();
30: }
31:
32: // factory method
33: protected HierarchicalStreamReader createReader(String xml)
34: throws Exception {
35: // Transmogrify XML input into binary format.
36: HierarchicalStreamReader xmlReader = new XppReader(
37: new StringReader(xml));
38:
39: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
40: HierarchicalStreamWriter binaryWriter = new BinaryStreamWriter(
41: buffer);
42: copier.copy(xmlReader, binaryWriter);
43:
44: return new BinaryStreamReader(new ByteArrayInputStream(buffer
45: .toByteArray()));
46: }
47:
48: public void testHandlesMoreThan256Ids() {
49: int count = 500;
50:
51: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
52: HierarchicalStreamWriter binaryWriter = new BinaryStreamWriter(
53: buffer);
54: binaryWriter.startNode("root");
55: for (int i = 0; i < count; i++) {
56: binaryWriter.startNode("node" + i);
57: binaryWriter.endNode();
58: }
59: for (int i = 0; i < count; i++) {
60: binaryWriter.startNode("node" + i);
61: binaryWriter.endNode();
62: }
63: binaryWriter.endNode();
64:
65: HierarchicalStreamReader binaryReader = new BinaryStreamReader(
66: new ByteArrayInputStream(buffer.toByteArray()));
67: assertEquals("root", binaryReader.getNodeName());
68: for (int i = 0; i < count; i++) {
69: assertTrue("Expected child " + i, binaryReader
70: .hasMoreChildren());
71: binaryReader.moveDown();
72: assertEquals("node" + i, binaryReader.getNodeName());
73: binaryReader.moveUp();
74: }
75: for (int i = 0; i < count; i++) {
76: assertTrue("Expected child " + i, binaryReader
77: .hasMoreChildren());
78: binaryReader.moveDown();
79: assertEquals("node" + i, binaryReader.getNodeName());
80: binaryReader.moveUp();
81: }
82:
83: }
84:
85: }
|