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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 java.io.IOException;
  45. import java.io.InputStream;
  46. import junit.framework.TestCase;
  47. public class PatchErrorTest extends TestCase {
  48. public void testError_DisconnectedHunk() throws IOException {
  49. final Patch p = parseTestPatchFile();
  50. assertEquals(1, p.getFiles().size());
  51. {
  52. final FileHeader fh = p.getFiles().get(0);
  53. assertEquals(
  54. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  55. fh.getNewPath());
  56. assertEquals(1, fh.getHunks().size());
  57. }
  58. assertEquals(1, p.getErrors().size());
  59. final FormatError e = p.getErrors().get(0);
  60. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  61. assertEquals("Hunk disconnected from file", e.getMessage());
  62. assertEquals(18, e.getOffset());
  63. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  64. }
  65. public void testError_TruncatedOld() throws IOException {
  66. final Patch p = parseTestPatchFile();
  67. assertEquals(1, p.getFiles().size());
  68. assertEquals(1, p.getErrors().size());
  69. final FormatError e = p.getErrors().get(0);
  70. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  71. assertEquals("Truncated hunk, at least 1 old lines is missing", e
  72. .getMessage());
  73. assertEquals(313, e.getOffset());
  74. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  75. }
  76. public void testError_TruncatedNew() throws IOException {
  77. final Patch p = parseTestPatchFile();
  78. assertEquals(1, p.getFiles().size());
  79. assertEquals(1, p.getErrors().size());
  80. final FormatError e = p.getErrors().get(0);
  81. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  82. assertEquals("Truncated hunk, at least 1 new lines is missing", e
  83. .getMessage());
  84. assertEquals(313, e.getOffset());
  85. assertTrue(e.getLineText().startsWith("@@ -236,9 +236,9 @@ protected "));
  86. }
  87. public void testError_BodyTooLong() throws IOException {
  88. final Patch p = parseTestPatchFile();
  89. assertEquals(1, p.getFiles().size());
  90. assertEquals(1, p.getErrors().size());
  91. final FormatError e = p.getErrors().get(0);
  92. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  93. assertEquals("Hunk header 4:11 does not match body line count of 4:12",
  94. e.getMessage());
  95. assertEquals(349, e.getOffset());
  96. assertTrue(e.getLineText().startsWith("@@ -109,4 +109,11 @@ assert"));
  97. }
  98. public void testError_GarbageBetweenFiles() throws IOException {
  99. final Patch p = parseTestPatchFile();
  100. assertEquals(2, p.getFiles().size());
  101. {
  102. final FileHeader fh = p.getFiles().get(0);
  103. assertEquals(
  104. "org.eclipse.jgit.test/tst/org/spearce/jgit/lib/RepositoryConfigTest.java",
  105. fh.getNewPath());
  106. assertEquals(1, fh.getHunks().size());
  107. }
  108. {
  109. final FileHeader fh = p.getFiles().get(1);
  110. assertEquals(
  111. "org.eclipse.jgit/src/org/spearce/jgit/lib/RepositoryConfig.java",
  112. fh.getNewPath());
  113. assertEquals(1, fh.getHunks().size());
  114. }
  115. assertEquals(1, p.getErrors().size());
  116. final FormatError e = p.getErrors().get(0);
  117. assertSame(FormatError.Severity.WARNING, e.getSeverity());
  118. assertEquals("Unexpected hunk trailer", e.getMessage());
  119. assertEquals(926, e.getOffset());
  120. assertEquals("I AM NOT HERE\n", e.getLineText());
  121. }
  122. public void testError_GitBinaryNoForwardHunk() throws IOException {
  123. final Patch p = parseTestPatchFile();
  124. assertEquals(2, p.getFiles().size());
  125. {
  126. final FileHeader fh = p.getFiles().get(0);
  127. assertEquals("org.spearce.egit.ui/icons/toolbar/fetchd.png", fh
  128. .getNewPath());
  129. assertSame(FileHeader.PatchType.GIT_BINARY, fh.getPatchType());
  130. assertTrue(fh.getHunks().isEmpty());
  131. assertNull(fh.getForwardBinaryHunk());
  132. }
  133. {
  134. final FileHeader fh = p.getFiles().get(1);
  135. assertEquals("org.spearce.egit.ui/icons/toolbar/fetche.png", fh
  136. .getNewPath());
  137. assertSame(FileHeader.PatchType.UNIFIED, fh.getPatchType());
  138. assertTrue(fh.getHunks().isEmpty());
  139. assertNull(fh.getForwardBinaryHunk());
  140. }
  141. assertEquals(1, p.getErrors().size());
  142. final FormatError e = p.getErrors().get(0);
  143. assertSame(FormatError.Severity.ERROR, e.getSeverity());
  144. assertEquals("Missing forward-image in GIT binary patch", e
  145. .getMessage());
  146. assertEquals(297, e.getOffset());
  147. assertEquals("\n", e.getLineText());
  148. }
  149. private Patch parseTestPatchFile() throws IOException {
  150. final String patchFile = getName() + ".patch";
  151. final InputStream in = getClass().getResourceAsStream(patchFile);
  152. if (in == null) {
  153. fail("No " + patchFile + " test vector");
  154. return null; // Never happens
  155. }
  156. try {
  157. final Patch p = new Patch();
  158. p.parse(in);
  159. return p;
  160. } finally {
  161. in.close();
  162. }
  163. }
  164. }