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.

AbstractTreeIteratorTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2009, Tor Arne Vestbø <torarnv@gmail.com> and others
  4. *
  5. * This program and the accompanying materials are made available under the
  6. * terms of the Eclipse Distribution License v. 1.0 which is available at
  7. * https://www.eclipse.org/org/documents/edl-v10.php.
  8. *
  9. * SPDX-License-Identifier: BSD-3-Clause
  10. */
  11. package org.eclipse.jgit.treewalk;
  12. import static org.junit.Assert.assertEquals;
  13. import static org.junit.Assert.assertNotNull;
  14. import static org.junit.Assert.assertNotSame;
  15. import static org.junit.Assert.assertSame;
  16. import static org.junit.Assert.assertTrue;
  17. import java.io.IOException;
  18. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  19. import org.eclipse.jgit.lib.Config;
  20. import org.eclipse.jgit.lib.Constants;
  21. import org.eclipse.jgit.lib.FileMode;
  22. import org.eclipse.jgit.lib.ObjectReader;
  23. import org.junit.Test;
  24. public class AbstractTreeIteratorTest {
  25. private static String prefix(String path) {
  26. final int s = path.lastIndexOf('/');
  27. return s > 0 ? path.substring(0, s) : "";
  28. }
  29. public static class FakeTreeIterator extends WorkingTreeIterator {
  30. public FakeTreeIterator(String pathName, FileMode fileMode) {
  31. super(prefix(pathName), new Config().get(WorkingTreeOptions.KEY));
  32. mode = fileMode.getBits();
  33. final int s = pathName.lastIndexOf('/');
  34. final byte[] name = Constants.encode(pathName.substring(s + 1));
  35. ensurePathCapacity(pathOffset + name.length, pathOffset);
  36. System.arraycopy(name, 0, path, pathOffset, name.length);
  37. pathLen = pathOffset + name.length;
  38. }
  39. @Override
  40. public AbstractTreeIterator createSubtreeIterator(ObjectReader reader)
  41. throws IncorrectObjectTypeException, IOException {
  42. return null;
  43. }
  44. }
  45. @Test
  46. public void testPathCompare() throws Exception {
  47. assertTrue(new FakeTreeIterator("a", FileMode.REGULAR_FILE).pathCompare(
  48. new FakeTreeIterator("a", FileMode.TREE)) < 0);
  49. assertTrue(new FakeTreeIterator("a", FileMode.TREE).pathCompare(
  50. new FakeTreeIterator("a", FileMode.REGULAR_FILE)) > 0);
  51. assertTrue(new FakeTreeIterator("a", FileMode.REGULAR_FILE).pathCompare(
  52. new FakeTreeIterator("a", FileMode.REGULAR_FILE)) == 0);
  53. assertTrue(new FakeTreeIterator("a", FileMode.TREE).pathCompare(
  54. new FakeTreeIterator("a", FileMode.TREE)) == 0);
  55. }
  56. @Test
  57. public void testGrowPath() throws Exception {
  58. final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
  59. final byte[] origpath = i.path;
  60. assertEquals(i.path[0], 'a');
  61. assertEquals(i.path[1], 'b');
  62. i.growPath(2);
  63. assertNotSame(origpath, i.path);
  64. assertEquals(origpath.length * 2, i.path.length);
  65. assertEquals(i.path[0], 'a');
  66. assertEquals(i.path[1], 'b');
  67. }
  68. @Test
  69. public void testEnsurePathCapacityFastCase() throws Exception {
  70. final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
  71. final int want = 50;
  72. final byte[] origpath = i.path;
  73. assertEquals(i.path[0], 'a');
  74. assertEquals(i.path[1], 'b');
  75. assertTrue(want < i.path.length);
  76. i.ensurePathCapacity(want, 2);
  77. assertSame(origpath, i.path);
  78. assertEquals(i.path[0], 'a');
  79. assertEquals(i.path[1], 'b');
  80. }
  81. @Test
  82. public void testEnsurePathCapacityGrows() throws Exception {
  83. final FakeTreeIterator i = new FakeTreeIterator("ab", FileMode.TREE);
  84. final int want = 384;
  85. final byte[] origpath = i.path;
  86. assertEquals(i.path[0], 'a');
  87. assertEquals(i.path[1], 'b');
  88. assertTrue(i.path.length < want);
  89. i.ensurePathCapacity(want, 2);
  90. assertNotSame(origpath, i.path);
  91. assertEquals(512, i.path.length);
  92. assertEquals(i.path[0], 'a');
  93. assertEquals(i.path[1], 'b');
  94. }
  95. @Test
  96. public void testEntryFileMode() {
  97. for (FileMode m : new FileMode[] { FileMode.TREE,
  98. FileMode.REGULAR_FILE, FileMode.EXECUTABLE_FILE,
  99. FileMode.GITLINK, FileMode.SYMLINK }) {
  100. final FakeTreeIterator i = new FakeTreeIterator("a", m);
  101. assertEquals(m.getBits(), i.getEntryRawMode());
  102. assertSame(m, i.getEntryFileMode());
  103. }
  104. }
  105. @Test
  106. public void testEntryPath() {
  107. FakeTreeIterator i = new FakeTreeIterator("a/b/cd", FileMode.TREE);
  108. assertEquals("a/b/cd", i.getEntryPathString());
  109. assertEquals(2, i.getNameLength());
  110. byte[] b = new byte[3];
  111. b[0] = 0x0a;
  112. i.getName(b, 1);
  113. assertEquals(0x0a, b[0]);
  114. assertEquals('c', b[1]);
  115. assertEquals('d', b[2]);
  116. }
  117. @Test
  118. public void testCreateEmptyTreeIterator() {
  119. FakeTreeIterator i = new FakeTreeIterator("a/b/cd", FileMode.TREE);
  120. EmptyTreeIterator e = i.createEmptyTreeIterator();
  121. assertNotNull(e);
  122. assertEquals(i.getEntryPathString() + "/", e.getEntryPathString());
  123. }
  124. }