1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.transport;
45
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.fail;
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.IOException;
51 import java.io.OutputStream;
52
53 import org.eclipse.jgit.lib.Constants;
54 import org.junit.Before;
55 import org.junit.Test;
56
57
58
59
60
61 public class PacketLineOutTest {
62 private ByteArrayOutputStream rawOut;
63
64 private PacketLineOut out;
65
66 @Before
67 public void setUp() throws Exception {
68 rawOut = new ByteArrayOutputStream();
69 out = new PacketLineOut(rawOut);
70 }
71
72
73
74 @Test
75 public void testWriteString1() throws IOException {
76 out.writeString("a");
77 out.writeString("bc");
78 assertBuffer("0005a0006bc");
79 }
80
81 @Test
82 public void testWriteString2() throws IOException {
83 out.writeString("a\n");
84 out.writeString("bc\n");
85 assertBuffer("0006a\n0007bc\n");
86 }
87
88 @Test
89 public void testWriteString3() throws IOException {
90 out.writeString("");
91 assertBuffer("0004");
92 }
93
94
95
96 @Test
97 public void testWriteEnd() throws IOException {
98 final int[] flushCnt = new int[1];
99 final OutputStream mockout = new OutputStream() {
100 @Override
101 public void write(int arg0) throws IOException {
102 rawOut.write(arg0);
103 }
104
105 @Override
106 public void flush() throws IOException {
107 flushCnt[0]++;
108 }
109 };
110
111 new PacketLineOut(mockout).end();
112 assertBuffer("0000");
113 assertEquals(1, flushCnt[0]);
114 }
115
116
117
118 @Test
119 public void testWritePacket1() throws IOException {
120 out.writePacket(new byte[] { 'a' });
121 assertBuffer("0005a");
122 }
123
124 @Test
125 public void testWritePacket2() throws IOException {
126 out.writePacket(new byte[] { 'a', 'b', 'c', 'd' });
127 assertBuffer("0008abcd");
128 }
129
130 @Test
131 public void testWritePacket3() throws IOException {
132 final int buflen = SideBandOutputStream.MAX_BUF - 5;
133 final byte[] buf = new byte[buflen];
134 for (int i = 0; i < buf.length; i++) {
135 buf[i] = (byte) i;
136 }
137 out.writePacket(buf);
138 out.flush();
139
140 final byte[] act = rawOut.toByteArray();
141 final String explen = Integer.toString(buf.length + 4, 16);
142 assertEquals(4 + buf.length, act.length);
143 assertEquals(new String(act, 0, 4, "UTF-8"), explen);
144 for (int i = 0, j = 4; i < buf.length; i++, j++) {
145 assertEquals(buf[i], act[j]);
146 }
147 }
148
149
150
151 @Test
152 public void testFlush() throws IOException {
153 final int[] flushCnt = new int[1];
154 final OutputStream mockout = new OutputStream() {
155 @Override
156 public void write(int arg0) throws IOException {
157 fail("should not write");
158 }
159
160 @Override
161 public void flush() throws IOException {
162 flushCnt[0]++;
163 }
164 };
165
166 new PacketLineOut(mockout).flush();
167 assertEquals(1, flushCnt[0]);
168 }
169
170 private void assertBuffer(final String exp) throws IOException {
171 assertEquals(exp, new String(rawOut.toByteArray(),
172 Constants.CHARACTER_ENCODING));
173 }
174 }