2 * Copyright (C) 2009, Google Inc.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.transport
;
40 import java
.io
.ByteArrayOutputStream
;
41 import java
.io
.IOException
;
42 import java
.io
.OutputStream
;
44 import junit
.framework
.TestCase
;
46 import org
.spearce
.jgit
.lib
.Constants
;
48 // Note, test vectors created with:
50 // perl -e 'printf "%4.4x%s\n", 4+length($ARGV[0]),$ARGV[0]'
52 public class SideBandOutputStreamTest
extends TestCase
{
53 private ByteArrayOutputStream rawOut
;
55 private PacketLineOut pckOut
;
57 protected void setUp() throws Exception
{
59 rawOut
= new ByteArrayOutputStream();
60 pckOut
= new PacketLineOut(rawOut
);
63 public void testWrite_CH_DATA() throws IOException
{
64 final SideBandOutputStream out
;
65 out
= new SideBandOutputStream(SideBandOutputStream
.CH_DATA
, pckOut
);
66 out
.write(new byte[] { 'a', 'b', 'c' });
67 assertBuffer("0008\001abc");
70 public void testWrite_CH_PROGRESS() throws IOException
{
71 final SideBandOutputStream out
;
72 out
= new SideBandOutputStream(SideBandOutputStream
.CH_PROGRESS
, pckOut
);
73 out
.write(new byte[] { 'a', 'b', 'c' });
74 assertBuffer("0008\002abc");
77 public void testWrite_CH_ERROR() throws IOException
{
78 final SideBandOutputStream out
;
79 out
= new SideBandOutputStream(SideBandOutputStream
.CH_ERROR
, pckOut
);
80 out
.write(new byte[] { 'a', 'b', 'c' });
81 assertBuffer("0008\003abc");
84 public void testWrite_Small() throws IOException
{
85 final SideBandOutputStream out
;
86 out
= new SideBandOutputStream(SideBandOutputStream
.CH_DATA
, pckOut
);
90 assertBuffer("0006\001a0006\001b0006\001c");
93 public void testWrite_Large() throws IOException
{
94 final int buflen
= SideBandOutputStream
.MAX_BUF
95 - SideBandOutputStream
.HDR_SIZE
;
96 final byte[] buf
= new byte[buflen
];
97 for (int i
= 0; i
< buf
.length
; i
++) {
101 final SideBandOutputStream out
;
102 out
= new SideBandOutputStream(SideBandOutputStream
.CH_DATA
, pckOut
);
105 final byte[] act
= rawOut
.toByteArray();
106 final String explen
= Integer
.toString(buf
.length
+ 5, 16);
107 assertEquals(5 + buf
.length
, act
.length
);
108 assertEquals(new String(act
, 0, 4, "UTF-8"), explen
);
109 assertEquals(1, act
[4]);
110 for (int i
= 0, j
= 5; i
< buf
.length
; i
++, j
++) {
111 assertEquals(buf
[i
], act
[j
]);
115 public void testFlush() throws IOException
{
116 final int[] flushCnt
= new int[1];
117 final OutputStream mockout
= new OutputStream() {
119 public void write(int arg0
) throws IOException
{
120 fail("should not write");
124 public void flush() throws IOException
{
129 new SideBandOutputStream(SideBandOutputStream
.CH_DATA
,
130 new PacketLineOut(mockout
)).flush();
131 assertEquals(0, flushCnt
[0]);
133 new SideBandOutputStream(SideBandOutputStream
.CH_ERROR
,
134 new PacketLineOut(mockout
)).flush();
135 assertEquals(1, flushCnt
[0]);
137 new SideBandOutputStream(SideBandOutputStream
.CH_PROGRESS
,
138 new PacketLineOut(mockout
)).flush();
139 assertEquals(2, flushCnt
[0]);
142 private void assertBuffer(final String exp
) throws IOException
{
143 assertEquals(exp
, new String(rawOut
.toByteArray(),
144 Constants
.CHARACTER_ENCODING
));