Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CommitBuilderTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (C) 2018, Salesforce. 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.lib;
  11. import static java.nio.charset.StandardCharsets.US_ASCII;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertSame;
  15. import static org.junit.Assert.fail;
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.text.MessageFormat;
  19. import org.eclipse.jgit.internal.JGitText;
  20. import org.junit.Test;
  21. public class CommitBuilderTest {
  22. private void assertGpgSignatureStringOutcome(String signature,
  23. String expectedOutcome) throws IOException {
  24. ByteArrayOutputStream out = new ByteArrayOutputStream();
  25. CommitBuilder.writeGpgSignatureString(signature, out);
  26. String formatted_signature = new String(out.toByteArray(), US_ASCII);
  27. assertEquals(expectedOutcome, formatted_signature);
  28. }
  29. @Test
  30. public void writeGpgSignatureString_1() throws Exception {
  31. // @formatter:off
  32. String signature = "-----BEGIN PGP SIGNATURE-----\n" +
  33. "Version: BCPG v1.60\n" +
  34. "\n" +
  35. "iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  36. "opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  37. "gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  38. "uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  39. "3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  40. "IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  41. "=b9OI\n" +
  42. "-----END PGP SIGNATURE-----";
  43. String expectedOutcome = "-----BEGIN PGP SIGNATURE-----\n" +
  44. " Version: BCPG v1.60\n" +
  45. " \n" +
  46. " iQEcBAABCAAGBQJb9cVhAAoJEKX+6Axg/6TZeFsH/0CY0WX/z7U8+7S5giFX4wH4\n" +
  47. " opvBwqyt6OX8lgNwTwBGHFNt8LdmDCCmKoq/XwkNi3ARVjLhe3gBcKXNoavvPk2Z\n" +
  48. " gIg5ChevGkU4afWCOMLVEYnkCBGw2+86XhrK1P7gTHEk1Rd+Yv1ZRDJBY+fFO7yz\n" +
  49. " uSBuF5RpEY2sJiIvp27Gub/rY3B5NTR/feO/z+b9oiP/fMUhpRwG5KuWUsn9NPjw\n" +
  50. " 3tvbgawYpU/2UnS+xnavMY4t2fjRYjsoxndPLb2MUX8X7vC7FgWLBlmI/rquLZVM\n" +
  51. " IQEKkjnA+lhejjK1rv+ulq4kGZJFKGYWYYhRDwFg5PTkzhudhN2SGUq5Wxq1Eg4=\n" +
  52. " =b9OI\n" +
  53. " -----END PGP SIGNATURE-----";
  54. // @formatter:on
  55. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  56. }
  57. @Test
  58. public void writeGpgSignatureString_failsForNonAscii() throws Exception {
  59. String signature = "Ü Ä";
  60. try {
  61. CommitBuilder.writeGpgSignatureString(signature,
  62. new ByteArrayOutputStream());
  63. fail("Exception expected");
  64. } catch (IllegalArgumentException e) {
  65. // good
  66. String message = MessageFormat.format(JGitText.get().notASCIIString,
  67. signature);
  68. assertEquals(message, e.getMessage());
  69. }
  70. }
  71. @Test
  72. public void writeGpgSignatureString_oneLineNotModified() throws Exception {
  73. String signature = " A string ";
  74. String expectedOutcome = signature;
  75. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  76. }
  77. @Test
  78. public void writeGpgSignatureString_preservesRandomWhitespace()
  79. throws Exception {
  80. // @formatter:off
  81. String signature = " String with \n"
  82. + "Line 2\n"
  83. + " Line 3\n"
  84. + "Line 4 \n"
  85. + " Line 5 ";
  86. String expectedOutcome = " String with \n"
  87. + " Line 2\n"
  88. + " Line 3\n"
  89. + " Line 4 \n"
  90. + " Line 5 ";
  91. // @formatter:on
  92. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  93. }
  94. @Test
  95. public void writeGpgSignatureString_replaceCR() throws Exception {
  96. // @formatter:off
  97. String signature = "String with \r"
  98. + "Line 2\r"
  99. + "Line 3\r"
  100. + "Line 4\r"
  101. + "Line 5";
  102. String expectedOutcome = "String with \n"
  103. + " Line 2\n"
  104. + " Line 3\n"
  105. + " Line 4\n"
  106. + " Line 5";
  107. // @formatter:on
  108. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  109. }
  110. @Test
  111. public void writeGpgSignatureString_replaceCRLF() throws Exception {
  112. // @formatter:off
  113. String signature = "String with \r\n"
  114. + "Line 2\r\n"
  115. + "Line 3\r\n"
  116. + "Line 4\r\n"
  117. + "Line 5";
  118. String expectedOutcome = "String with \n"
  119. + " Line 2\n"
  120. + " Line 3\n"
  121. + " Line 4\n"
  122. + " Line 5";
  123. // @formatter:on
  124. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  125. }
  126. @Test
  127. public void writeGpgSignatureString_replaceCRLFMixed() throws Exception {
  128. // @formatter:off
  129. String signature = "String with \r"
  130. + "Line 2\r\n"
  131. + "Line 3\r"
  132. + "Line 4\r\n"
  133. + "Line 5";
  134. String expectedOutcome = "String with \n"
  135. + " Line 2\n"
  136. + " Line 3\n"
  137. + " Line 4\n"
  138. + " Line 5";
  139. // @formatter:on
  140. assertGpgSignatureStringOutcome(signature, expectedOutcome);
  141. }
  142. @Test
  143. public void setGpgSignature() throws Exception {
  144. GpgSignature dummy = new GpgSignature(new byte[0]);
  145. CommitBuilder builder = new CommitBuilder();
  146. assertNull(builder.getGpgSignature());
  147. builder.setGpgSignature(dummy);
  148. assertSame(dummy, builder.getGpgSignature());
  149. builder.setGpgSignature(null);
  150. assertNull(builder.getGpgSignature());
  151. }
  152. }