Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DiffFormatterReflowTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2009, Johannes E. Schindelin <johannes.schindelin@gmx.de> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.diff;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.fail;
  14. import java.io.ByteArrayOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import org.eclipse.jgit.junit.JGitTestUtil;
  18. import org.eclipse.jgit.patch.FileHeader;
  19. import org.eclipse.jgit.patch.Patch;
  20. import org.eclipse.jgit.util.RawParseUtils;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. public class DiffFormatterReflowTest {
  24. private RawText a;
  25. private RawText b;
  26. private FileHeader file;
  27. private ByteArrayOutputStream out;
  28. private DiffFormatter fmt;
  29. @Before
  30. public void setUp() throws Exception {
  31. out = new ByteArrayOutputStream();
  32. fmt = new DiffFormatter(out);
  33. }
  34. @Test
  35. public void testNegativeContextFails() throws IOException {
  36. init("X");
  37. try {
  38. fmt.setContext(-1);
  39. fail("accepted negative context");
  40. } catch (IllegalArgumentException e) {
  41. // pass
  42. }
  43. }
  44. @Test
  45. public void testContext0() throws IOException {
  46. init("X");
  47. fmt.setContext(0);
  48. assertFormatted();
  49. }
  50. @Test
  51. public void testContext1() throws IOException {
  52. init("X");
  53. fmt.setContext(1);
  54. assertFormatted();
  55. }
  56. @Test
  57. public void testContext3() throws IOException {
  58. init("X");
  59. fmt.setContext(3);
  60. assertFormatted();
  61. }
  62. @Test
  63. public void testContext5() throws IOException {
  64. init("X");
  65. fmt.setContext(5);
  66. assertFormatted();
  67. }
  68. @Test
  69. public void testContext10() throws IOException {
  70. init("X");
  71. fmt.setContext(10);
  72. assertFormatted();
  73. }
  74. @Test
  75. public void testContext100() throws IOException {
  76. init("X");
  77. fmt.setContext(100);
  78. assertFormatted();
  79. }
  80. @Test
  81. public void testEmpty1() throws IOException {
  82. init("E");
  83. assertFormatted("E.patch");
  84. }
  85. @Test
  86. public void testNoNewLine1() throws IOException {
  87. init("Y");
  88. assertFormatted("Y.patch");
  89. }
  90. @Test
  91. public void testNoNewLine2() throws IOException {
  92. init("Z");
  93. assertFormatted("Z.patch");
  94. }
  95. private void init(String name) throws IOException {
  96. a = new RawText(readFile(name + "_PreImage"));
  97. b = new RawText(readFile(name + "_PostImage"));
  98. file = parseTestPatchFile(name + ".patch").getFiles().get(0);
  99. }
  100. private void assertFormatted() throws IOException {
  101. assertFormatted(JGitTestUtil.getName() + ".out");
  102. }
  103. private void assertFormatted(String name) throws IOException {
  104. fmt.format(file, a, b);
  105. final String exp = RawParseUtils.decode(readFile(name));
  106. assertEquals(exp, RawParseUtils.decode(out.toByteArray()));
  107. }
  108. private byte[] readFile(String patchFile) throws IOException {
  109. final InputStream in = getClass().getResourceAsStream(patchFile);
  110. if (in == null) {
  111. fail("No " + patchFile + " test vector");
  112. return null; // Never happens
  113. }
  114. try {
  115. final byte[] buf = new byte[1024];
  116. final ByteArrayOutputStream temp = new ByteArrayOutputStream();
  117. int n;
  118. while ((n = in.read(buf)) > 0)
  119. temp.write(buf, 0, n);
  120. return temp.toByteArray();
  121. } finally {
  122. in.close();
  123. }
  124. }
  125. private Patch parseTestPatchFile(String patchFile) throws IOException {
  126. try (InputStream in = getClass().getResourceAsStream(patchFile)) {
  127. if (in == null) {
  128. fail("No " + patchFile + " test vector");
  129. return null; // Never happens
  130. }
  131. final Patch p = new Patch();
  132. p.parse(in);
  133. return p;
  134. }
  135. }
  136. }