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 package org.eclipse.jgit.lib;
44
45 import static java.nio.charset.StandardCharsets.US_ASCII;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertSame;
49 import static org.junit.Assert.fail;
50
51 import java.io.ByteArrayOutputStream;
52 import java.io.IOException;
53 import java.text.MessageFormat;
54
55 import org.eclipse.jgit.internal.JGitText;
56 import org.junit.Test;
57
58 public class CommitBuilderTest {
59
60 private void assertGpgSignatureStringOutcome(String signature,
61 String expectedOutcome) throws IOException {
62 ByteArrayOutputStream out = new ByteArrayOutputStream();
63 CommitBuilder.writeGpgSignatureString(signature, out);
64 String formatted_signature = new String(out.toByteArray(), US_ASCII);
65 assertEquals(expectedOutcome, formatted_signature);
66 }
67
68 @Test
69 public void writeGpgSignatureString_1() throws Exception {
70
71 String signature = "-----BEGIN PGP SIGNATURE-----\n" +
72 "Version: BCPG v1.60\n" +
73 "\n" +
74 "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
75 "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
76 "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
77 "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
78 "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
79 "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
80 "=b9OI\n" +
81 "-----END PGP SIGNATURE-----";
82 String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
83 " Version: BCPG v1.60\n" +
84 " \n" +
85 " iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
86 " opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
87 " gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
88 " uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
89 " 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
90 " IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
91 " =b9OI\n" +
92 " -----END PGP SIGNATURE-----";
93
94 assertGpgSignatureStringOutcome(signature, expectedOutcome);
95 }
96
97 @Test
98 public void writeGpgSignatureString_failsForNonAscii() throws Exception {
99 String signature = "Ü Ä";
100 try {
101 CommitBuilder.writeGpgSignatureString(signature,
102 new ByteArrayOutputStream());
103 fail("Exception expected");
104 } catch (IllegalArgumentException e) {
105
106 String message = MessageFormat.format(JGitText.get().notASCIIString,
107 signature);
108 assertEquals(message, e.getMessage());
109 }
110 }
111
112 @Test
113 public void writeGpgSignatureString_oneLineNotModified() throws Exception {
114 String signature = " A string ";
115 String expectedOutcome = signature;
116 assertGpgSignatureStringOutcome(signature, expectedOutcome);
117 }
118
119 @Test
120 public void writeGpgSignatureString_preservesRandomWhitespace()
121 throws Exception {
122
123 String signature = " String with \n"
124 + "Line 2\n"
125 + " Line 3\n"
126 + "Line 4 \n"
127 + " Line 5 ";
128 String expectedOutcome = " String with \n"
129 + " Line 2\n"
130 + " Line 3\n"
131 + " Line 4 \n"
132 + " Line 5 ";
133
134 assertGpgSignatureStringOutcome(signature, expectedOutcome);
135 }
136
137 @Test
138 public void writeGpgSignatureString_replaceCR() throws Exception {
139
140 String signature = "String with \r"
141 + "Line 2\r"
142 + "Line 3\r"
143 + "Line 4\r"
144 + "Line 5";
145 String expectedOutcome = "String with \n"
146 + " Line 2\n"
147 + " Line 3\n"
148 + " Line 4\n"
149 + " Line 5";
150
151 assertGpgSignatureStringOutcome(signature, expectedOutcome);
152 }
153
154 @Test
155 public void writeGpgSignatureString_replaceCRLF() throws Exception {
156
157 String signature = "String with \r\n"
158 + "Line 2\r\n"
159 + "Line 3\r\n"
160 + "Line 4\r\n"
161 + "Line 5";
162 String expectedOutcome = "String with \n"
163 + " Line 2\n"
164 + " Line 3\n"
165 + " Line 4\n"
166 + " Line 5";
167
168 assertGpgSignatureStringOutcome(signature, expectedOutcome);
169 }
170
171 @Test
172 public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
173
174 String signature = "String with \r"
175 + "Line 2\r\n"
176 + "Line 3\r"
177 + "Line 4\r\n"
178 + "Line 5";
179 String expectedOutcome = "String with \n"
180 + " Line 2\n"
181 + " Line 3\n"
182 + " Line 4\n"
183 + " Line 5";
184
185 assertGpgSignatureStringOutcome(signature, expectedOutcome);
186 }
187
188 @Test
189 public void setGpgSignature() throws Exception {
190 GpgSignature dummy = new GpgSignature(new byte[0]);
191
192 CommitBuilder builder = new CommitBuilder();
193 assertNull(builder.getGpgSignature());
194
195 builder.setGpgSignature(dummy);
196 assertSame(dummy, builder.getGpgSignature());
197
198 builder.setGpgSignature(null);
199 assertNull(builder.getGpgSignature());
200 }
201 }