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