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.

PatchErrorTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.patch;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.assertSame;
  47. import static org.junit.Assert.assertTrue;
  48. import static org.junit.Assert.fail;
  49. import java.io.IOException;
  50. import java.io.InputStream;
  51. import org.eclipse.jgit.junit.JGitTestUtil;
  52. import org.junit.Test;
  53. public class PatchErrorTest {
  54. @Test
  55. public void testError_DisconnectedHunk() throws IOException {
  56. final Patch p = parseTestPatchFile();
  57. assertEquals(1, p.getFiles().size());
  58. {
  59. final FileHeader fh = p.getFiles().get(0);
  60. assertEquals(
  61. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  62. fh.getNewPath());
  63. assertEquals(1, fh.getHunks().size());
  64. }
  65. assertEquals(1, p.getErrors().size());
  66. final FormatError e = p.getErrors().get(0);
  67. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  68. assertEquals("Hunk disconnected from file", e.getMessage());
  69. assertEquals(18, e.getOffset());
  70. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  71. }
  72. @Test
  73. public void testError_TruncatedOld() throws IOException {
  74. final Patch p = parseTestPatchFile();
  75. assertEquals(1, p.getFiles().size());
  76. assertEquals(1, p.getErrors().size());
  77. final FormatError e = p.getErrors().get(0);
  78. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  79. assertEquals("Truncated hunk, at least 1 old lines is missing", e
  80. .getMessage());
  81. assertEquals(313, e.getOffset());
  82. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  83. }
  84. @Test
  85. public void testError_TruncatedNew() throws IOException {
  86. final Patch p = parseTestPatchFile();
  87. assertEquals(1, p.getFiles().size());
  88. assertEquals(1, p.getErrors().size());
  89. final FormatError e = p.getErrors().get(0);
  90. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  91. assertEquals("Truncated hunk, at least 1 new lines is missing", e
  92. .getMessage());
  93. assertEquals(313, e.getOffset());
  94. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  95. }
  96. @Test
  97. public void testError_BodyTooLong() throws IOException {
  98. final Patch p = parseTestPatchFile();
  99. assertEquals(1, p.getFiles().size());
  100. assertEquals(1, p.getErrors().size());
  101. final FormatError e = p.getErrors().get(0);
  102. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  103. assertEquals("Hunk header 4:11 does not match body line count of 4:12",
  104. e.getMessage());
  105. assertEquals(349, e.getOffset());
  106. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  107. }
  108. @Test
  109. public void testError_GarbageBetweenFiles() throws IOException {
  110. final Patch p = parseTestPatchFile();
  111. assertEquals(2, p.getFiles().size());
  112. {
  113. final FileHeader fh = p.getFiles().get(0);
  114. assertEquals(
  115. "org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
  116. fh.getNewPath());
  117. assertEquals(1, fh.getHunks().size());
  118. }
  119. {
  120. final FileHeader fh = p.getFiles().get(1);
  121. assertEquals(
  122. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  123. fh.getNewPath());
  124. assertEquals(1, fh.getHunks().size());
  125. }
  126. assertEquals(1, p.getErrors().size());
  127. final FormatError e = p.getErrors().get(0);
  128. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  129. assertEquals("Unexpected hunk trailer", e.getMessage());
  130. assertEquals(926, e.getOffset());
  131. assertEquals("I AM NOT HERE\n", e.getLineText());
  132. }
  133. @Test
  134. public void testError_GitBinaryNoForwardHunk() throws IOException {
  135. final Patch p = parseTestPatchFile();
  136. assertEquals(2, p.getFiles().size());
  137. {
  138. final FileHeader fh = p.getFiles().get(0);
  139. assertEquals("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
  140. .getNewPath());
  141. assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
  142. assertTrue(fh.getHunks().isEmpty());
  143. assertNull(fh.getForwardBinaryHunk());
  144. }
  145. {
  146. final FileHeader fh = p.getFiles().get(1);
  147. assertEquals("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
  148. .getNewPath());
  149. assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  150. assertTrue(fh.getHunks().isEmpty());
  151. assertNull(fh.getForwardBinaryHunk());
  152. }
  153. assertEquals(1, p.getErrors().size());
  154. final FormatError e = p.getErrors().get(0);
  155. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  156. assertEquals("Missing forward-image in GIT binary patch", e
  157. .getMessage());
  158. assertEquals(297, e.getOffset());
  159. assertEquals("\n", e.getLineText());
  160. }
  161. private Patch parseTestPatchFile() throws IOException {
  162. final String patchFile = JGitTestUtil.getName() + ".patch";
  163. final InputStream in = getClass().getResourceAsStream(patchFile);
  164. if (in == null) {
  165. fail("No " + patchFile + " test vector");
  166. return null; // Never happens
  167. }
  168. try {
  169. final Patch p = new Patch();
  170. p.parse(in);
  171. return p;
  172. } finally {
  173. in.close();
  174. }
  175. }
  176. }