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.

RawTextLoadTest.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright (C) 2017, 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.diff;
  11. import org.eclipse.jgit.errors.BinaryBlobException;
  12. import org.eclipse.jgit.internal.storage.file.FileRepository;
  13. import org.eclipse.jgit.junit.RepositoryTestCase;
  14. import org.eclipse.jgit.lib.Constants;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.eclipse.jgit.lib.ObjectInserter;
  17. import org.eclipse.jgit.lib.ObjectLoader;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import java.io.IOException;
  21. public class RawTextLoadTest extends RepositoryTestCase {
  22. private static byte[] generate(int size, int nullAt) {
  23. byte[] data = new byte[size];
  24. for (int i = 0; i < data.length; i++) {
  25. data[i] = (byte) ((i % 72 == 0) ? '\n' : (i%10) + '0');
  26. }
  27. if (nullAt >= 0) {
  28. data[nullAt] = '\0';
  29. }
  30. return data;
  31. }
  32. private RawText textFor(byte[] data, int limit) throws IOException, BinaryBlobException {
  33. FileRepository repo = createBareRepository();
  34. ObjectId id;
  35. try (ObjectInserter ins = repo.getObjectDatabase().newInserter()) {
  36. id = ins.insert(Constants.OBJ_BLOB, data);
  37. }
  38. ObjectLoader ldr = repo.open(id);
  39. return RawText.load(ldr, limit);
  40. }
  41. @Test
  42. public void testSmallOK() throws Exception {
  43. byte[] data = generate(1000, -1);
  44. RawText result = textFor(data, 1 << 20);
  45. Assert.assertArrayEquals(result.content, data);
  46. }
  47. @Test(expected = BinaryBlobException.class)
  48. public void testSmallNull() throws Exception {
  49. byte[] data = generate(1000, 22);
  50. textFor(data, 1 << 20);
  51. }
  52. @Test
  53. public void testBigOK() throws Exception {
  54. byte[] data = generate(10000, -1);
  55. RawText result = textFor(data, 1 << 20);
  56. Assert.assertArrayEquals(result.content, data);
  57. }
  58. @Test(expected = BinaryBlobException.class)
  59. public void testBigWithNullAtStart() throws Exception {
  60. byte[] data = generate(10000, 22);
  61. textFor(data, 1 << 20);
  62. }
  63. @Test(expected = BinaryBlobException.class)
  64. public void testBinaryThreshold() throws Exception {
  65. byte[] data = generate(2 << 20, -1);
  66. textFor(data, 1 << 20);
  67. }
  68. }