Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

RawParseUtilsTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2011, Leonard Broman <leonard.broman@gmail.com>
  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.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotNull;
  46. import static org.junit.Assert.fail;
  47. import java.nio.charset.Charset;
  48. import java.nio.charset.UnsupportedCharsetException;
  49. import org.eclipse.jgit.lib.Constants;
  50. import org.junit.Test;
  51. import static java.nio.charset.StandardCharsets.UTF_8;
  52. public class RawParseUtilsTest {
  53. String commit = "tree e3a1035abd2b319bb01e57d69b0ba6cab289297e\n" +
  54. "parent 54e895b87c0768d2317a2b17062e3ad9f76a8105\n" +
  55. "committer A U Thor <author@xample.com 1528968566 +0200\n" +
  56. "gpgsig -----BEGIN PGP SIGNATURE-----\n" +
  57. " \n" +
  58. " wsBcBAABCAAQBQJbGB4pCRBK7hj4Ov3rIwAAdHIIAENrvz23867ZgqrmyPemBEZP\n" +
  59. " U24B1Tlq/DWvce2buaxmbNQngKZ0pv2s8VMc11916WfTIC9EKvioatmpjduWvhqj\n" +
  60. " znQTFyiMor30pyYsfrqFuQZvqBW01o8GEWqLg8zjf9Rf0R3LlOEw86aT8CdHRlm6\n" +
  61. " wlb22xb8qoX4RB+LYfz7MhK5F+yLOPXZdJnAVbuyoMGRnDpwdzjL5Hj671+XJxN5\n" +
  62. " SasRdhxkkfw/ZnHxaKEc4juMz8Nziz27elRwhOQqlTYoXNJnsV//wy5Losd7aKi1\n" +
  63. " xXXyUpndEOmT0CIcKHrN/kbYoVL28OJaxoBuva3WYQaRrzEe3X02NMxZe9gkSqA=\n" +
  64. " =TClh\n" +
  65. " -----END PGP SIGNATURE-----\n" +
  66. "some other header\n\n" +
  67. "commit message";
  68. @Test
  69. public void testParseEncoding_ISO8859_1_encoding() {
  70. Charset result = RawParseUtils.parseEncoding(Constants
  71. .encodeASCII("encoding ISO-8859-1\n"));
  72. assertNotNull(result);
  73. }
  74. @Test
  75. public void testParseEncoding_Accept_Latin_One_AsISO8859_1() {
  76. Charset result = RawParseUtils.parseEncoding(Constants
  77. .encodeASCII("encoding latin-1\n"));
  78. assertNotNull(result);
  79. assertEquals("ISO-8859-1", result.name());
  80. }
  81. @Test
  82. public void testParseEncoding_badEncoding() {
  83. try {
  84. RawParseUtils.parseEncoding(Constants.encodeASCII("encoding xyz\n"));
  85. fail("should throw an UnsupportedCharsetException: xyz");
  86. } catch (UnsupportedCharsetException e) {
  87. assertEquals("xyz", e.getMessage());
  88. }
  89. }
  90. @Test
  91. public void testHeaderStart() {
  92. byte[] headerName = "some".getBytes(UTF_8);
  93. byte[] commitBytes = commit.getBytes(UTF_8);
  94. assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 0));
  95. assertEquals(625, RawParseUtils.headerStart(headerName, commitBytes, 4));
  96. byte[] missingHeaderName = "missing".getBytes(UTF_8);
  97. assertEquals(-1, RawParseUtils.headerStart(missingHeaderName,
  98. commitBytes, 0));
  99. byte[] fauxHeaderName = "other".getBytes(UTF_8);
  100. assertEquals(-1, RawParseUtils.headerStart(fauxHeaderName, commitBytes, 625 + 4));
  101. }
  102. @Test
  103. public void testHeaderEnd() {
  104. byte[] commitBytes = commit.getBytes(UTF_8);
  105. int[] expected = new int[] {45, 93, 148, 619, 637};
  106. int start = 0;
  107. for (int i = 0; i < expected.length; i++) {
  108. start = RawParseUtils.headerEnd(commitBytes, start);
  109. assertEquals(expected[i], start);
  110. start += 1;
  111. }
  112. }
  113. }