View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2014 Andreas Hermann and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    *******************************************************************************/
10  package org.eclipse.jgit.internal.storage.file;
11  
12  import static java.nio.charset.StandardCharsets.UTF_8;
13  import static org.junit.Assert.assertEquals;
14  
15  import java.io.File;
16  import java.io.FileInputStream;
17  import java.io.FileNotFoundException;
18  import java.io.IOException;
19  
20  import org.eclipse.jgit.lib.ObjectId;
21  import org.eclipse.jgit.lib.PersonIdent;
22  import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
23  import org.junit.Test;
24  
25  public class ReflogWriterTest extends SampleDataRepositoryTestCase {
26  
27  	private static String oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c"
28  			+ " John Doe <john@doe.com> 1243028200 +0200\tstash: Add message with line feeds\n";
29  
30  	@Test
31  	public void shouldFilterLineFeedFromMessage() throws Exception {
32  		ReflogWriter writer =
33  				new ReflogWriter((RefDirectory) db.getRefDatabase());
34  		PersonIdent ident = new PersonIdent("John Doe", "john@doe.com",
35  				1243028200000L, 120);
36  		ObjectId oldId = ObjectId
37  				.fromString("da85355dfc525c9f6f3927b876f379f46ccf826e");
38  		ObjectId newId = ObjectId
39  				.fromString("3e7549db262d1e836d9bf0af7e22355468f1717c");
40  
41  		writer.log("refs/heads/master", oldId, newId, ident,
42  				"stash: Add\nmessage\r\nwith line feeds");
43  
44  		byte[] buffer = new byte[oneLine.getBytes(UTF_8).length];
45  		readReflog(buffer);
46  		assertEquals(oneLine, new String(buffer, UTF_8));
47  	}
48  
49  	private void readReflog(byte[] buffer)
50  			throws FileNotFoundException, IOException {
51  		File logfile = new File(db.getDirectory(), "logs/refs/heads/master");
52  		if (!logfile.getParentFile().mkdirs()
53  				&& !logfile.getParentFile().isDirectory()) {
54  			throw new IOException(
55  					"oops, cannot create the directory for the test reflog file"
56  							+ logfile);
57  		}
58  		try (FileInputStream fileInputStream = new FileInputStream(logfile)) {
59  			fileInputStream.read(buffer);
60  		}
61  	}
62  }