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 PacketLineOutTest
extends TestCase
{
53 private ByteArrayOutputStream rawOut
;
55 private PacketLineOut out
;
57 protected void setUp() throws Exception
{
59 rawOut
= new ByteArrayOutputStream();
60 out
= new PacketLineOut(rawOut
);
65 public void testWriteString1() throws IOException
{
67 out
.writeString("bc");
68 assertBuffer("0005a0006bc");
71 public void testWriteString2() throws IOException
{
72 out
.writeString("a\n");
73 out
.writeString("bc\n");
74 assertBuffer("0006a\n0007bc\n");
77 public void testWriteString3() throws IOException
{
84 public void testWriteEnd() throws IOException
{
85 final int[] flushCnt
= new int[1];
86 final OutputStream mockout
= new OutputStream() {
88 public void write(int arg0
) throws IOException
{
93 public void flush() throws IOException
{
98 new PacketLineOut(mockout
).end();
100 assertEquals(1, flushCnt
[0]);
105 public void testWritePacket1() throws IOException
{
106 out
.writePacket(new byte[] { 'a' });
107 assertBuffer("0005a");
110 public void testWritePacket2() throws IOException
{
111 out
.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
112 assertBuffer("0008abcd");
115 public void testWritePacket3() throws IOException
{
116 final int buflen
= SideBandOutputStream
.MAX_BUF
117 - SideBandOutputStream
.HDR_SIZE
;
118 final byte[] buf
= new byte[buflen
];
119 for (int i
= 0; i
< buf
.length
; i
++) {
122 out
.writePacket(buf
);
125 final byte[] act
= rawOut
.toByteArray();
126 final String explen
= Integer
.toString(buf
.length
+ 4, 16);
127 assertEquals(4 + buf
.length
, act
.length
);
128 assertEquals(new String(act
, 0, 4, "UTF-8"), explen
);
129 for (int i
= 0, j
= 4; i
< buf
.length
; i
++, j
++) {
130 assertEquals(buf
[i
], act
[j
]);
134 // writeChannelPacket
136 public void testWriteChannelPacket1() throws IOException
{
137 out
.writeChannelPacket(1, new byte[] { 'a' }, 0, 1);
138 assertBuffer("0006\001a");
141 public void testWriteChannelPacket2() throws IOException
{
142 out
.writeChannelPacket(2, new byte[] { 'b' }, 0, 1);
143 assertBuffer("0006\002b");
146 public void testWriteChannelPacket3() throws IOException
{
147 out
.writeChannelPacket(3, new byte[] { 'c' }, 0, 1);
148 assertBuffer("0006\003c");
153 public void testFlush() throws IOException
{
154 final int[] flushCnt
= new int[1];
155 final OutputStream mockout
= new OutputStream() {
157 public void write(int arg0
) throws IOException
{
158 fail("should not write");
162 public void flush() throws IOException
{
167 new PacketLineOut(mockout
).flush();
168 assertEquals(1, flushCnt
[0]);
171 private void assertBuffer(final String exp
) throws IOException
{
172 assertEquals(exp
, new String(rawOut
.toByteArray(),
173 Constants
.CHARACTER_ENCODING
));