You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReflogWriterTest.java 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.junit.Assert.assertEquals;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import org.eclipse.jgit.lib.ObjectId;
  18. import org.eclipse.jgit.lib.PersonIdent;
  19. import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
  20. import org.junit.Test;
  21. public class ReflogWriterTest extends SampleDataRepositoryTestCase {
  22. private static String oneLine = "da85355dfc525c9f6f3927b876f379f46ccf826e 3e7549db262d1e836d9bf0af7e22355468f1717c"
  23. + " John Doe <john@doe.com> 1243028200 +0200\tstash: Add message with line feeds\n";
  24. @Test
  25. public void shouldFilterLineFeedFromMessage() throws Exception {
  26. ReflogWriter writer =
  27. new ReflogWriter((RefDirectory) db.getRefDatabase());
  28. PersonIdent ident = new PersonIdent("John Doe", "john@doe.com",
  29. 1243028200000L, 120);
  30. ObjectId oldId = ObjectId
  31. .fromString("da85355dfc525c9f6f3927b876f379f46ccf826e");
  32. ObjectId newId = ObjectId
  33. .fromString("3e7549db262d1e836d9bf0af7e22355468f1717c");
  34. writer.log("refs/heads/master", oldId, newId, ident,
  35. "stash: Add\nmessage\r\nwith line feeds");
  36. byte[] buffer = new byte[oneLine.getBytes(UTF_8).length];
  37. readReflog(buffer);
  38. assertEquals(oneLine, new String(buffer, UTF_8));
  39. }
  40. private void readReflog(byte[] buffer)
  41. throws FileNotFoundException, IOException {
  42. File logfile = new File(db.getDirectory(), "logs/refs/heads/master");
  43. if (!logfile.getParentFile().mkdirs()
  44. && !logfile.getParentFile().isDirectory()) {
  45. throw new IOException(
  46. "oops, cannot create the directory for the test reflog file"
  47. + logfile);
  48. }
  49. try (FileInputStream fileInputStream = new FileInputStream(logfile)) {
  50. fileInputStream.read(buffer);
  51. }
  52. }
  53. }