View Javadoc
1   /*
2    * Copyright (C) 2010, Robin Rosenberg
3    * Copyright (C) 2009, Google, Inc.
4    * and other copyright owners as documented in the project's IP log.
5    *
6    * This program and the accompanying materials are made available
7    * under the terms of the Eclipse Distribution License v1.0 which
8    * accompanies this distribution, is reproduced below, and is
9    * available at http://www.eclipse.org/org/documents/edl-v10.php
10   *
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or
14   * without modification, are permitted provided that the following
15   * conditions are met:
16   *
17   * - Redistributions of source code must retain the above copyright
18   *   notice, this list of conditions and the following disclaimer.
19   *
20   * - Redistributions in binary form must reproduce the above
21   *   copyright notice, this list of conditions and the following
22   *   disclaimer in the documentation and/or other materials provided
23   *   with the distribution.
24   *
25   * - Neither the name of the Eclipse Foundation, Inc. nor the
26   *   names of its contributors may be used to endorse or promote
27   *   products derived from this software without specific prior
28   *   written permission.
29   *
30   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
31   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
32   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
35   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
41   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
42   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   */
44  package org.eclipse.jgit.util;
45  
46  import static org.junit.Assert.assertEquals;
47  
48  import java.util.concurrent.TimeUnit;
49  
50  import org.eclipse.jgit.junit.MockSystemReader;
51  import org.eclipse.jgit.lib.ObjectId;
52  import org.eclipse.jgit.lib.PersonIdent;
53  import org.junit.Test;
54  
55  /**
56   * Portions of this test is from CommitMsgHookTest in the Android project Gerrit
57   */
58  public class ChangeIdUtilTest {
59  
60  	private final String SOB1 = "Signed-off-by: J Author <ja@example.com>\n";
61  
62  	private final String SOB2 = "Signed-off-by: J Committer <jc@example.com>\n";
63  
64  	final PersonIdent p = RawParseUtils.parsePersonIdent(
65  			"A U Thor <author@example.com> 1142878501 -0500");
66  
67  	final PersonIdent q = RawParseUtils.parsePersonIdent(
68  			"W Riter <writer@example.com> 1142878502 -0500");
69  
70  	ObjectId treeId = ObjectId
71  			.fromString("f51de923607cd51cf872b928a6b523ba823f7f35");
72  
73  	ObjectId treeId1 = ObjectId
74  			.fromString("4b825dc642cb6eb9a060e54bf8d69288fbee4904");
75  
76  	final ObjectId treeId2 = ObjectId
77  			.fromString("617601c79811cbbae338512798318b4e5b70c9ac");
78  
79  	ObjectId parentId = ObjectId
80  			.fromString("91fea719aaf9447feb9580477eb3dd08b62b5eca");
81  
82  	ObjectId parentId1 = null;
83  
84  	final ObjectId parentId2 = ObjectId
85  			.fromString("485c91e0600b165c301c278bfbae3e492413980c");
86  
87  	MockSystemReader mockSystemReader = new MockSystemReader();
88  
89  	final long when = mockSystemReader.getCurrentTime();
90  
91  	final int tz = new MockSystemReader().getTimezone(when);
92  
93  	PersonIdent author = new PersonIdent("J. Author", "ja@example.com");
94  	{
95  		author = new PersonIdent(author, when, tz);
96  	}
97  
98  	PersonIdent committer = new PersonIdent("J. Committer", "jc@example.com");
99  	{
100 		committer = new PersonIdent(committer, when, tz);
101 	}
102 
103 	@Test
104 	public void testClean() {
105 		assertEquals("hej", ChangeIdUtil.clean("hej\n\n"));
106 		assertEquals("hej\n\nsan", ChangeIdUtil.clean("hej\n\nsan\n\n"));
107 		assertEquals("hej\nsan", ChangeIdUtil.clean("hej\n#men\nsan\n\n#men"));
108 		assertEquals("hej\nsan", ChangeIdUtil.clean("hej\nsan\n\n#men"));
109 		assertEquals("hej\nsan", ChangeIdUtil.clean("#no\nhej\nsan\n\n#men"));
110 		assertEquals("hej\nsan", ChangeIdUtil
111 				.clean("#no\nhej\nsan\nSigned-off-by: me \n#men"));
112 	}
113 
114 	@Test
115 	public void testId() {
116 		String msg = "A\nMessage\n";
117 		ObjectId id = ChangeIdUtil.computeChangeId(treeId, parentId, p, q, msg);
118 		assertEquals("73f3751208ac92cbb76f9a26ac4a0d9d472e381b", ObjectId
119 				.toString(id));
120 	}
121 
122 	@Test
123 	public void testHasChangeid() throws Exception {
124 		assertEquals(
125 				"has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
126 						+ "Change-Id: I0123456789012345678901234567890123456789\n",
127 				call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
128 						+ "Change-Id: I0123456789012345678901234567890123456789\n"));
129 	}
130 
131 	@Test
132 	public void testHasChangeidWithReplacement() throws Exception {
133 		assertEquals(
134 				"has changeid\nmore text\n\nSigned-off-by: me@you.too\n"
135 						+ "Change-Id: I2178563fada5edb2c99a8d8c0d619471b050ec24\nBug: 33\n",
136 				call("has changeid\nmore text\n\nSigned-off-by: me@you.too\n"
137 						+ "Change-Id: I0123456789012345678901234567890123456789\nBug: 33\n",
138 						true));
139 	}
140 
141 	@Test
142 	public void testHasChangeidWithReplacementInLastLine() throws Exception {
143 		assertEquals(
144 				"has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
145 						+ "Change-Id: I1d6578f4c96e3db4dd707705fe3d17bf658c4758\n",
146 				call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
147 						+ "Change-Id: I0123456789012345678901234567890123456789\n",
148 						true));
149 	}
150 
151 	@Test
152 	public void testHasChangeidWithReplacementInLastLineNoLineBreak()
153 			throws Exception {
154 		assertEquals(
155 				"has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
156 						+ "Change-Id: I1d6578f4c96e3db4dd707705fe3d17bf658c4758",
157 				call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
158 						+ "Change-Id: I0123456789012345678901234567890123456789",
159 						true));
160 	}
161 
162 	@Test
163 	public void testHasChangeidWithSpacesBeforeId() throws Exception {
164 		assertEquals(
165 				"has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
166 						+ "Change-Id: Ie7575eaf450fdd0002df2e642426faf251de3ad9\n",
167 				call("has changeid\nmore text\n\nBug: 33\nSigned-off-by: me@you.too\n"
168 						+ "Change-Id:    I0123456789012345678901234567890123456789\n",
169 						true));
170 	}
171 
172 	@Test
173 	public void testHasChangeidWithReplacementWithChangeIdInCommitMessage()
174 			throws Exception {
175 		assertEquals(
176 				"has changeid\nmore text\n"
177 						+ "Change-Id: I0123456789012345678901234567890123456789\n\n"
178 						+ "Bug: 33\nSigned-off-by: me@you.too\n"
179 						+ "Change-Id: Ie48d10d59ef67995ca89688ac0171b88f10dd520\n",
180 				call("has changeid\nmore text\n"
181 						+ "Change-Id: I0123456789012345678901234567890123456789\n\n"
182 						+ "Bug: 33\nSigned-off-by: me@you.too\n"
183 						+ "Change-Id: I0123456789012345678901234567890123456789\n",
184 						true));
185 	}
186 
187 	@Test
188 	public void testOneliner() throws Exception {
189 		assertEquals(
190 				"oneliner\n\nChange-Id: I3a98091ce4470de88d52ae317fcd297e2339f063\n",
191 				call("oneliner\n"));
192 	}
193 
194 	@Test
195 	public void testOnelinerFollowedByBlank() throws Exception {
196 		assertEquals(
197 				"oneliner followed by blank\n\nChange-Id: I3a12c21ef342a18498f95c62efbc186cd782b743\n",
198 				call("oneliner followed by blank\n"));
199 	}
200 
201 	@Test
202 	public void testATwoLines() throws Exception {
203 		assertEquals(
204 				"a two lines\nwith text withour break after subject line\n\nChange-Id: I549a0fed3d69b7876c54b4f5a35637135fd43fac\n",
205 				call("a two lines\nwith text withour break after subject line\n"));
206 	}
207 
208 	@Test
209 	public void testRegularCommit() throws Exception {
210 		assertEquals(
211 				"regular commit\n\nwith header and body\n\nChange-Id: I62d8749d3c3a888c11e3fadc3924220a19389766\n",
212 				call("regular commit\n\nwith header and body\n"));
213 	}
214 
215 	@Test
216 	public void testRegularCommitWithSob_ButNoBody() throws Exception {
217 		assertEquals(
218 				"regular commit with sob, but no body\n\nChange-Id: I0f0b4307e9944ecbd5a9f6b9489e25cfaede43c4\nSigned-off-by: me@you.too\n",
219 				call("regular commit with sob, but no body\n\nSigned-off-by: me@you.too\n"));
220 	}
221 
222 	@Test
223 	public void testACommitWithBug_SubButNoBody() throws Exception {
224 		assertEquals(
225 				"a commit with bug, sub but no body\n\nBug: 33\nChange-Id: I337e264868613dab6d1e11a34f394db369487412\nSigned-off-by: me@you.too\n",
226 				call("a commit with bug, sub but no body\n\nBug: 33\nSigned-off-by: me@you.too\n"));
227 	}
228 
229 	@Test
230 	public void testACommitWithSubject_NoBodySobAndBug() throws Exception {
231 		assertEquals(
232 				"a commit with subject, no body sob and bug\n\nChange-Id: Ib3616d4bf77707a3215a6cb0602c004ee119a445\nSigned-off-by: me@you.too\nBug: 33\n",
233 				call("a commit with subject, no body sob and bug\n\nSigned-off-by: me@you.too\nBug: 33\n"));
234 	}
235 
236 	@Test
237 	public void testACommitWithSubjectBug_NonFooterLineAndSob()
238 			throws Exception {
239 		assertEquals(
240 				"a commit with subject bug, non-footer line and sob\n\nBug: 33\nmore text\nSigned-off-by: me@you.too\n\nChange-Id: Ia8500eab2304e6e5eac6ae488ff44d5d850d118a\n",
241 				call("a commit with subject bug, non-footer line and sob\n\nBug: 33\nmore text\nSigned-off-by: me@you.too\n"));
242 	}
243 
244 	@Test
245 	public void testACommitWithSubject_NonFooterAndBugAndSob() throws Exception {
246 		assertEquals(
247 				"a commit with subject, non-footer and bug and sob\n\nmore text (two empty lines after bug)\nBug: 33\n\n\nChange-Id: Idac75ccbad2ab6727b8612e344df5190d87891dd\nSigned-off-by: me@you.too\n",
248 				call("a commit with subject, non-footer and bug and sob\n\nmore text (two empty lines after bug)\nBug: 33\n\n\nSigned-off-by: me@you.too\n"));
249 	}
250 
251 	@Test
252 	public void testACommitWithSubjectBodyBugBrackersAndSob() throws Exception {
253 		assertEquals(
254 				"a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\nChange-Id: I90ecb589bef766302532c3e00915e10114b00f62\n[bracket]\nSigned-off-by: me@you.too\n",
255 				call("a commit with subject body, bug. brackers and sob\n\nText\n\nBug: 33\n[bracket]\nSigned-off-by: me@you.too\n\n"));
256 	}
257 
258 	@Test
259 	public void testACommitWithSubjectBodyBugLineWithASpaceAndSob()
260 			throws Exception {
261 		assertEquals(
262 				"a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\nChange-Id: I864e2218bdee033c8ce9a7f923af9e0d5dc16863\n \nSigned-off-by: me@you.too\n",
263 				call("a commit with subject body, bug. line with a space and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n"));
264 	}
265 
266 	@Test
267 	public void testACommitWithSubjectBodyBugEmptyLineAndSob() throws Exception {
268 		assertEquals(
269 				"a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\nChange-Id: I33f119f533313883e6ada3df600c4f0d4db23a76\n \nSigned-off-by: me@you.too\n",
270 				call("a commit with subject body, bug. empty line and sob\n\nText\n\nBug: 33\n \nSigned-off-by: me@you.too\n\n"));
271 	}
272 
273 	@Test
274 	public void testEmptyMessages() throws Exception {
275 		// Empty input must not produce a change id.
276 		hookDoesNotModify("");
277 		hookDoesNotModify(" ");
278 		hookDoesNotModify("\n");
279 		hookDoesNotModify("\n\n");
280 		hookDoesNotModify("  \n  ");
281 
282 		hookDoesNotModify("#");
283 		hookDoesNotModify("#\n");
284 		hookDoesNotModify("# on branch master\n# Untracked files:\n");
285 		hookDoesNotModify("\n# on branch master\n# Untracked files:\n");
286 		hookDoesNotModify("\n\n# on branch master\n# Untracked files:\n");
287 
288 		hookDoesNotModify("\n# on branch master\ndiff --git a/src b/src\n"
289 				+ "new file mode 100644\nindex 0000000..c78b7f0\n");
290 	}
291 
292 	@Test
293 	public void testChangeIdAlreadySet() throws Exception {
294 		// If a Change-Id is already present in the footer, the hook must
295 		// not modify the message but instead must leave the identity alone.
296 		//
297 		hookDoesNotModify("a\n" + //
298 				"\n" + //
299 				"Change-Id: Iaeac9b4149291060228ef0154db2985a31111335\n");
300 		hookDoesNotModify("fix: this thing\n" + //
301 				"\n" + //
302 				"Change-Id: I388bdaf52ed05b55e62a22d0a20d2c1ae0d33e7e\n");
303 		hookDoesNotModify("fix-a-widget: this thing\n" + //
304 				"\n" + //
305 				"Change-Id: Id3bc5359d768a6400450283e12bdfb6cd135ea4b\n");
306 		hookDoesNotModify("FIX: this thing\n" + //
307 				"\n" + //
308 				"Change-Id: I1b55098b5a2cce0b3f3da783dda50d5f79f873fa\n");
309 		hookDoesNotModify("Fix-A-Widget: this thing\n" + //
310 				"\n" + //
311 				"Change-Id: I4f4e2e1e8568ddc1509baecb8c1270a1fb4b6da7\n");
312 	}
313 
314 	@Test
315 	public void testChangeIdAlreadySetWithReplacement() throws Exception {
316 		// If a Change-Id is already present in the footer, the hook
317 		// replaces the Change-Id with the new value..
318 		//
319 		assertEquals("a\n" + //
320 				"\n" + //
321 				"Change-Id: Ifa324efa85bfb3c8696a46a0f67fa70c35be5f5f\n",
322 				call("a\n" + //
323 						"\n" + //
324 						"Change-Id: Iaeac9b4149291060228ef0154db2985a31111335\n",
325 						true));
326 		assertEquals("fix: this thing\n" + //
327 				"\n" + //
328 				"Change-Id: Ib63e4990a06412a3f24bd93bb160e98ac1bd412b\n",
329 				call("fix: this thing\n" + //
330 						"\n" + //
331 						"Change-Id: I388bdaf52ed05b55e62a22d0a20d2c1ae0d33e7e\n",
332 						true));
333 		assertEquals("fix-a-widget: this thing\n" + //
334 				"\n" + //
335 				"Change-Id: If0444e4d0cabcf41b3d3b46b7e9a7a64a82117af\n",
336 				call("fix-a-widget: this thing\n" + //
337 						"\n" + //
338 						"Change-Id: Id3bc5359d768a6400450283e12bdfb6cd135ea4b\n",
339 						true));
340 		assertEquals("FIX: this thing\n" + //
341 				"\n" + //
342 				"Change-Id: Iba5a3b2d5e5df46448f6daf362b6bfa775c6491d\n",
343 				call("FIX: this thing\n" + //
344 						"\n" + //
345 						"Change-Id: I1b55098b5a2cce0b3f3da783dda50d5f79f873fa\n",
346 						true));
347 		assertEquals("Fix-A-Widget: this thing\n" + //
348 				"\n" + //
349 				"Change-Id: I2573d47c62c42429fbe424d70cfba931f8f87848\n",
350 				call("Fix-A-Widget: this thing\n" + //
351 				"\n" + //
352 				"Change-Id: I4f4e2e1e8568ddc1509baecb8c1270a1fb4b6da7\n",
353 				true));
354 	}
355 
356 	@Test
357 	public void testTimeAltersId() throws Exception {
358 		assertEquals("a\n" + //
359 				"\n" + //
360 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
361 				call("a\n"));
362 
363 		tick();
364 		assertEquals("a\n" + //
365 				"\n" + //
366 				"Change-Id: I3251906b99dda598a58a6346d8126237ee1ea800\n",//
367 				call("a\n"));
368 
369 		tick();
370 		assertEquals("a\n" + //
371 				"\n" + //
372 				"Change-Id: I69adf9208d828f41a3d7e41afbca63aff37c0c5c\n",//
373 				call("a\n"));
374 	}
375 
376 	/** Increment the {@link #author} and {@link #committer} times. */
377 	protected void tick() {
378 		final long delta = TimeUnit.MILLISECONDS.convert(5 * 60,
379 				TimeUnit.SECONDS);
380 		final long now = author.getWhen().getTime() + delta;
381 
382 		author = new PersonIdent(author, now, tz);
383 		committer = new PersonIdent(committer, now, tz);
384 	}
385 
386 	@Test
387 	public void testFirstParentAltersId() throws Exception {
388 		assertEquals("a\n" + //
389 				"\n" + //
390 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
391 				call("a\n"));
392 
393 		parentId1 = parentId2;
394 		assertEquals("a\n" + //
395 				"\n" + //
396 				"Change-Id: I51e86482bde7f92028541aaf724d3a3f996e7ea2\n",//
397 				call("a\n"));
398 	}
399 
400 	@Test
401 	public void testDirCacheAltersId() throws Exception {
402 		assertEquals("a\n" + //
403 				"\n" + //
404 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
405 				call("a\n"));
406 
407 		treeId1 = treeId2;
408 		assertEquals("a\n" + //
409 				"\n" + //
410 				"Change-Id: If56597ea9759f23b070677ea6f064c60c38da631\n",//
411 				call("a\n"));
412 	}
413 
414 	@Test
415 	public void testSingleLineMessages() throws Exception {
416 		assertEquals("a\n" + //
417 				"\n" + //
418 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n",//
419 				call("a\n"));
420 
421 		assertEquals("fix: this thing\n" + //
422 				"\n" + //
423 				"Change-Id: I0f13d0e6c739ca3ae399a05a93792e80feb97f37\n",//
424 				call("fix: this thing\n"));
425 		assertEquals("fix-a-widget: this thing\n" + //
426 				"\n" + //
427 				"Change-Id: I1a1a0c751e4273d532e4046a501a612b9b8a775e\n",//
428 				call("fix-a-widget: this thing\n"));
429 
430 		assertEquals("FIX: this thing\n" + //
431 				"\n" + //
432 				"Change-Id: If816d944c57d3893b60cf10c65931fead1290d97\n",//
433 				call("FIX: this thing\n"));
434 		assertEquals("Fix-A-Widget: this thing\n" + //
435 				"\n" + //
436 				"Change-Id: I3e18d00cbda2ba1f73aeb63ed8c7d57d7fd16c76\n",//
437 				call("Fix-A-Widget: this thing\n"));
438 	}
439 
440 	@Test
441 	public void testMultiLineMessagesWithoutFooter() throws Exception {
442 		assertEquals("a\n" + //
443 				"\n" + //
444 				"b\n" + //
445 				"\n" + //
446 				"Change-Id: Id0b4f42d3d6fc1569595c9b97cb665e738486f5d\n",//
447 				call("a\n" + "\n" + "b\n"));
448 
449 		assertEquals("a\n" + //
450 				"\n" + //
451 				"b\nc\nd\ne\n" + //
452 				"\n" + //
453 				"Change-Id: I7d237b20058a0f46cc3f5fabc4a0476877289d75\n",//
454 				call("a\n" + "\n" + "b\nc\nd\ne\n"));
455 
456 		assertEquals("a\n" + //
457 				"\n" + //
458 				"b\nc\nd\ne\n" + //
459 				"\n" + //
460 				"f\ng\nh\n" + //
461 				"\n" + //
462 				"Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n",//
463 				call("a\n" + "\n" + "b\nc\nd\ne\n" + "\n" + "f\ng\nh\n"));
464 	}
465 
466 	@Test
467 	public void testSingleLineMessagesWithSignedOffBy() throws Exception {
468 		assertEquals("a\n" + //
469 				"\n" + //
470 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n" + //
471 				SOB1,//
472 				call("a\n" + "\n" + SOB1));
473 
474 		assertEquals("a\n" + //
475 				"\n" + //
476 				"Change-Id: I7fc3876fee63c766a2063df97fbe04a2dddd8d7c\n" + //
477 				SOB1 + //
478 				SOB2,//
479 				call("a\n" + "\n" + SOB1 + SOB2));
480 	}
481 
482 	@Test
483 	public void testMultiLineMessagesWithSignedOffBy() throws Exception {
484 		assertEquals("a\n" + //
485 				"\n" + //
486 				"b\nc\nd\ne\n" + //
487 				"\n" + //
488 				"f\ng\nh\n" + //
489 				"\n" + //
490 				"Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n" + //
491 				SOB1,//
492 				call("a\n" + "\n" + "b\nc\nd\ne\n" + "\n" + "f\ng\nh\n" + "\n"
493 						+ SOB1));
494 
495 		assertEquals("a\n" + //
496 				"\n" + //
497 				"b\nc\nd\ne\n" + //
498 				"\n" + //
499 				"f\ng\nh\n" + //
500 				"\n" + //
501 				"Change-Id: I382e662f47bf164d6878b7fe61637873ab7fa4e8\n" + //
502 				SOB1 + //
503 				SOB2,//
504 				call("a\n" + //
505 						"\n" + //
506 						"b\nc\nd\ne\n" + //
507 						"\n" + //
508 						"f\ng\nh\n" + //
509 						"\n" + //
510 						SOB1 + //
511 						SOB2));
512 
513 		assertEquals("a\n" + //
514 				"\n" + //
515 				"b: not a footer\nc\nd\ne\n" + //
516 				"\n" + //
517 				"f\ng\nh\n" + //
518 				"\n" + //
519 				"Change-Id: I8869aabd44b3017cd55d2d7e0d546a03e3931ee2\n" + //
520 				SOB1 + //
521 				SOB2,//
522 				call("a\n" + //
523 						"\n" + //
524 						"b: not a footer\nc\nd\ne\n" + //
525 						"\n" + //
526 						"f\ng\nh\n" + //
527 						"\n" + //
528 						SOB1 + //
529 						SOB2));
530 	}
531 
532 	@Test
533 	public void testNoteInMiddle() throws Exception {
534 		assertEquals("a\n" + //
535 				"\n" + //
536 				"NOTE: This\n" + //
537 				"does not fix it.\n" + //
538 				"\n" + //
539 				"Change-Id: I988a127969a6ee5e58db546aab74fc46e66847f8\n", //
540 				call("a\n" + //
541 						"\n" + //
542 						"NOTE: This\n" + //
543 						"does not fix it.\n"));
544 	}
545 
546 	@Test
547 	public void testKernelStyleFooter() throws Exception {
548 		assertEquals("a\n" + //
549 				"\n" + //
550 				"Change-Id: I1bd787f9e7590a2ac82b02c404c955ffb21877c4\n" + //
551 				SOB1 + //
552 				"[ja: Fixed\n" + //
553 				"     the indentation]\n" + //
554 				SOB2, //
555 				call("a\n" + //
556 						"\n" + //
557 						SOB1 + //
558 						"[ja: Fixed\n" + //
559 						"     the indentation]\n" + //
560 						SOB2));
561 	}
562 
563 	@Test
564 	public void testChangeIdAfterBugOrIssue() throws Exception {
565 		assertEquals("a\n" + //
566 				"\n" + //
567 				"Bug: 42\n" + //
568 				"Change-Id: I8c0321227c4324e670b9ae8cf40eccc87af21b1b\n" + //
569 				SOB1,//
570 				call("a\n" + //
571 						"\n" + //
572 						"Bug: 42\n" + //
573 						SOB1));
574 
575 		assertEquals("a\n" + //
576 				"\n" + //
577 				"Issue: 42\n" + //
578 				"Change-Id: Ie66e07d89ae5b114c0975b49cf326e90331dd822\n" + //
579 				SOB1,//
580 				call("a\n" + //
581 						"\n" + //
582 						"Issue: 42\n" + //
583 						SOB1));
584 	}
585 
586 	@Test
587 	public void testWithEndingURL() throws Exception {
588 		assertEquals("a\n" + //
589 				"\n" + //
590 				"http://example.com/ fixes this\n" + //
591 				"\n" + //
592 				"Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n", //
593 				call("a\n" + //
594 						"\n" + //
595 						"http://example.com/ fixes this\n"));
596 		assertEquals("a\n" + //
597 				"\n" + //
598 				"https://example.com/ fixes this\n" + //
599 				"\n" + //
600 				"Change-Id: I62b9039e2fc0dce274af55e8f99312a8a80a805d\n", //
601 				call("a\n" + //
602 						"\n" + //
603 						"https://example.com/ fixes this\n"));
604 		assertEquals("a\n" + //
605 				"\n" + //
606 				"ftp://example.com/ fixes this\n" + //
607 				"\n" + //
608 				"Change-Id: I71b05dc1f6b9a5540a53a693e64d58b65a8910e8\n", //
609 				call("a\n" + //
610 						"\n" + //
611 						"ftp://example.com/ fixes this\n"));
612 		assertEquals("a\n" + //
613 				"\n" + //
614 				"git://example.com/ fixes this\n" + //
615 				"\n" + //
616 				"Change-Id: Id34e942baa68d790633737d815ddf11bac9183e5\n", //
617 				call("a\n" + //
618 						"\n" + //
619 						"git://example.com/ fixes this\n"));
620 	}
621 
622 	@Test
623 	public void testIndexOfChangeId() {
624 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("", "\n"));
625 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("\n", "\n"));
626 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("\r\n", "\r\n"));
627 
628 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
629 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
630 				"\n"));
631 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
632 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n\n\n",
633 				"\n"));
634 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
635 										+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n \n \n",
636 								"\n"));
637 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
638 				+ "Change-Id:  I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
639 				"\n"));
640 
641 		// leading whitespace is rejected by Gerrit
642 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
643 				+ " Change-Id:  I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
644 				"\n"));
645 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
646 				+ "\t Change-Id:  I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
647 				"\n"));
648 
649 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
650 				+ "Change-Id: \n", "\n"));
651 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
652 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701 \n",
653 				"\n"));
654 		assertEquals(12, ChangeIdUtil.indexOfChangeId("x\n" + "\n"
655 				+ "Bug 4711\n"
656 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
657 				"\n"));
658 		assertEquals(56, ChangeIdUtil.indexOfChangeId("x\n"
659 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
660 				+ "\n"
661 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
662 				"\n"));
663 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n"
664 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
665 				+ "\n" + "x\n", "\n"));
666 		assertEquals(-1, ChangeIdUtil.indexOfChangeId("x\n\n"
667 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n"
668 				+ "\n" + "x\n", "\n"));
669 		assertEquals(5, ChangeIdUtil.indexOfChangeId("x\r\n" + "\r\n"
670 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r\n",
671 				"\r\n"));
672 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\r" + "\r"
673 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r",
674 				"\r"));
675 		assertEquals(3, ChangeIdUtil.indexOfChangeId("x\r" + "\r"
676 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\r",
677 				"\r"));
678 		assertEquals(8, ChangeIdUtil.indexOfChangeId("x\ny\n\nz\n" + "\n"
679 				+ "Change-Id: I3b7e4e16b503ce00f07ba6ad01d97a356dad7701\n",
680 				"\n"));
681 	}
682 
683 	private void hookDoesNotModify(String in) throws Exception {
684 		assertEquals(in, call(in));
685 	}
686 
687 	private String call(String body) throws Exception {
688 		return call(body, false);
689 	}
690 
691 	private String call(String body, boolean replaceExisting) throws Exception {
692 		ObjectId computeChangeId = ChangeIdUtil.computeChangeId(treeId1,
693 				parentId1, author, committer, body);
694 		if (computeChangeId == null)
695 			return body;
696 		return ChangeIdUtil.insertId(body, computeChangeId, replaceExisting);
697 	}
698 
699 }