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.

GetTextTest.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (C) 2008, Google Inc. 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.patch;
  11. import static java.nio.charset.StandardCharsets.ISO_8859_1;
  12. import static java.nio.charset.StandardCharsets.UTF_8;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.nio.charset.Charset;
  20. import org.eclipse.jgit.junit.JGitTestUtil;
  21. import org.junit.Test;
  22. public class GetTextTest {
  23. @Test
  24. public void testGetText_BothISO88591() throws IOException {
  25. final Charset cs = ISO_8859_1;
  26. final Patch p = parseTestPatchFile();
  27. assertTrue(p.getErrors().isEmpty());
  28. assertEquals(1, p.getFiles().size());
  29. final FileHeader fh = p.getFiles().get(0);
  30. assertEquals(2, fh.getHunks().size());
  31. assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
  32. }
  33. @Test
  34. public void testGetText_NoBinary() throws IOException {
  35. final Charset cs = ISO_8859_1;
  36. final Patch p = parseTestPatchFile();
  37. assertTrue(p.getErrors().isEmpty());
  38. assertEquals(1, p.getFiles().size());
  39. final FileHeader fh = p.getFiles().get(0);
  40. assertEquals(0, fh.getHunks().size());
  41. assertEquals(readTestPatchFile(cs), fh.getScriptText(cs, cs));
  42. }
  43. @Test
  44. public void testGetText_Convert() throws IOException {
  45. final Charset csOld = ISO_8859_1;
  46. final Charset csNew = UTF_8;
  47. final Patch p = parseTestPatchFile();
  48. assertTrue(p.getErrors().isEmpty());
  49. assertEquals(1, p.getFiles().size());
  50. final FileHeader fh = p.getFiles().get(0);
  51. assertEquals(2, fh.getHunks().size());
  52. // Read the original file as ISO-8859-1 and fix up the one place
  53. // where we changed the character encoding. That makes the exp
  54. // string match what we really expect to get back.
  55. //
  56. String exp = readTestPatchFile(csOld);
  57. exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
  58. assertEquals(exp, fh.getScriptText(csOld, csNew));
  59. }
  60. @Test
  61. public void testGetText_DiffCc() throws IOException {
  62. final Charset csOld = ISO_8859_1;
  63. final Charset csNew = UTF_8;
  64. final Patch p = parseTestPatchFile();
  65. assertTrue(p.getErrors().isEmpty());
  66. assertEquals(1, p.getFiles().size());
  67. final CombinedFileHeader fh = (CombinedFileHeader) p.getFiles().get(0);
  68. assertEquals(1, fh.getHunks().size());
  69. // Read the original file as ISO-8859-1 and fix up the one place
  70. // where we changed the character encoding. That makes the exp
  71. // string match what we really expect to get back.
  72. //
  73. String exp = readTestPatchFile(csOld);
  74. exp = exp.replace("\303\205ngstr\303\266m", "\u00c5ngstr\u00f6m");
  75. assertEquals(exp, fh
  76. .getScriptText(new Charset[] { csNew, csOld, csNew }));
  77. }
  78. private Patch parseTestPatchFile() throws IOException {
  79. final String patchFile = JGitTestUtil.getName() + ".patch";
  80. try (InputStream in = getClass().getResourceAsStream(patchFile)) {
  81. if (in == null) {
  82. fail("No " + patchFile + " test vector");
  83. return null; // Never happens
  84. }
  85. final Patch p = new Patch();
  86. p.parse(in);
  87. return p;
  88. }
  89. }
  90. private String readTestPatchFile(Charset cs) throws IOException {
  91. final String patchFile = JGitTestUtil.getName() + ".patch";
  92. try (InputStream in = getClass().getResourceAsStream(patchFile)) {
  93. if (in == null) {
  94. fail("No " + patchFile + " test vector");
  95. return null; // Never happens
  96. }
  97. final InputStreamReader r = new InputStreamReader(in, cs);
  98. char[] tmp = new char[2048];
  99. final StringBuilder s = new StringBuilder();
  100. int n;
  101. while ((n = r.read(tmp)) > 0)
  102. s.append(tmp, 0, n);
  103. return s.toString();
  104. }
  105. }
  106. }