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 junit.framework.TestCase;
15:
16: import java.io.ByteArrayInputStream;
17: import java.io.ByteArrayOutputStream;
18: import java.io.DataInputStream;
19: import java.io.DataOutput;
20: import java.io.DataOutputStream;
21: import java.io.IOException;
22:
23: public class TokenTest extends TestCase {
24:
25: private Token.Formatter tokenFormatter;
26: private ByteArrayOutputStream buffer;
27: private DataOutput out;
28:
29: protected void setUp() throws Exception {
30: super .setUp();
31: tokenFormatter = new Token.Formatter();
32: buffer = new ByteArrayOutputStream();
33: out = new DataOutputStream(buffer);
34: }
35:
36: public void testDoesNotSupportNegativeIds() throws IOException {
37: Token.StartNode token = new Token.StartNode(-5);
38: try {
39: writeOneToken(token);
40: fail("Expected exception");
41: } catch (IOException expectedException) {
42: // expected exception
43: }
44: }
45:
46: public void testUsesOneExtraByteForIdsThatCanBeRepresentedAsByte()
47: throws IOException {
48: Token.StartNode token = new Token.StartNode(255);
49: writeOneToken(token);
50: assertEquals(2, buffer.size()); // One byte already written for token type.
51: assertEquals(token, readOneToken());
52: }
53:
54: public void testUsesTwoExtraBytesForIdsThatCanBeRepresentedAsShort()
55: throws IOException {
56: Token.StartNode token = new Token.StartNode(30000);
57: writeOneToken(token);
58: assertEquals(3, buffer.size()); // One byte already written for token type.
59: assertEquals(token, readOneToken());
60: }
61:
62: public void testUsesFourExtraBytesForIdsThatCanBeRepresentedAsShort()
63: throws IOException {
64: Token.StartNode token = new Token.StartNode(Integer.MAX_VALUE);
65: writeOneToken(token);
66: assertEquals(5, buffer.size()); // One byte already written for token type.
67: assertEquals(token, readOneToken());
68: }
69:
70: public void testUsesEightExtraBytesForIdsThatCanBeRepresentedAsLong()
71: throws IOException {
72: Token.StartNode token = new Token.StartNode(324234325543L);
73: writeOneToken(token);
74: assertEquals(9, buffer.size()); // One byte already written for token type.
75: assertEquals(token, readOneToken());
76: }
77:
78: private Token readOneToken() throws IOException {
79: return tokenFormatter.read(new DataInputStream(
80: new ByteArrayInputStream(buffer.toByteArray())));
81: }
82:
83: private void writeOneToken(Token.StartNode token)
84: throws IOException {
85: tokenFormatter.write(out, token);
86: }
87:
88: }
|