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.assertFalse;
48 import static org.junit.Assert.assertSame;
49
50 import java.io.ByteArrayInputStream;
51 import java.io.ByteArrayOutputStream;
52 import java.io.IOException;
53
54 import org.eclipse.jgit.lib.Constants;
55 import org.eclipse.jgit.lib.ObjectId;
56 import org.eclipse.jgit.lib.ObjectInserter;
57 import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
58 import org.eclipse.jgit.util.NB;
59 import org.junit.Test;
60
61 public class RefAdvertiserTest {
62 @Test
63 public void advertiser() throws IOException {
64 ByteArrayOutputStream buf = new ByteArrayOutputStream();
65 PacketLineOut pckOut = new PacketLineOut(buf);
66 PacketLineOutRefAdvertiser adv = new PacketLineOutRefAdvertiser(pckOut);
67
68
69
70
71
72
73 adv.advertiseCapability("test-1");
74 adv.advertiseCapability("sideband");
75 adv.advertiseId(id(1), "refs/heads/master");
76 adv.advertiseId(id(4), "refs/" + padStart("F", 987));
77 adv.advertiseId(id(2), "refs/heads/next");
78 adv.advertiseId(id(3), "refs/Iñtërnâtiônàlizætiøn☃💩");
79 adv.end();
80 assertFalse(adv.isEmpty());
81
82 PacketLineIn pckIn = new PacketLineIn(
83 new ByteArrayInputStream(buf.toByteArray()));
84 String s = pckIn.readStringRaw();
85 assertEquals(id(1).name() + " refs/heads/master\0 test-1 sideband\n",
86 s);
87
88 s = pckIn.readStringRaw();
89 assertEquals(id(4).name() + " refs/" + padStart("F", 987) + '\n', s);
90
91 s = pckIn.readStringRaw();
92 assertEquals(id(2).name() + " refs/heads/next\n", s);
93
94 s = pckIn.readStringRaw();
95 assertEquals(id(3).name() + " refs/Iñtërnâtiônàlizætiøn☃💩\n", s);
96
97 s = pckIn.readStringRaw();
98 assertSame(PacketLineIn.END, s);
99 }
100
101 private static ObjectId id(int i) {
102 try (ObjectInserter.Formatter f = new ObjectInserter.Formatter()) {
103 byte[] tmp = new byte[4];
104 NB.encodeInt32(tmp, 0, i);
105 return f.idFor(Constants.OBJ_BLOB, tmp);
106 }
107 }
108
109 private static String padStart(String s, int len) {
110 StringBuilder b = new StringBuilder(len);
111 for (int i = s.length(); i < len; i++) {
112 b.append((char) ('a' + (i % 26)));
113 }
114 return b.append(s).toString();
115 }
116 }