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.

DirCacheEntryTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 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.dircache;
  44. import static java.time.Instant.EPOCH;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertFalse;
  47. import static org.junit.Assert.assertSame;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import org.eclipse.jgit.lib.FileMode;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.junit.Test;
  53. public class DirCacheEntryTest {
  54. @Test
  55. public void testIsValidPath() {
  56. assertTrue(isValidPath("a"));
  57. assertTrue(isValidPath("a/b"));
  58. assertTrue(isValidPath("ab/cd/ef"));
  59. assertFalse(isValidPath(""));
  60. assertFalse(isValidPath("/a"));
  61. assertFalse(isValidPath("a//b"));
  62. assertFalse(isValidPath("ab/cd//ef"));
  63. assertFalse(isValidPath("a/"));
  64. assertFalse(isValidPath("ab/cd/ef/"));
  65. assertFalse(isValidPath("a\u0000b"));
  66. }
  67. @SuppressWarnings("unused")
  68. private static boolean isValidPath(String path) {
  69. try {
  70. new DirCacheEntry(path);
  71. return true;
  72. } catch (InvalidPathException e) {
  73. return false;
  74. }
  75. }
  76. @SuppressWarnings("unused")
  77. @Test
  78. public void testCreate_ByStringPath() {
  79. assertEquals("a", new DirCacheEntry("a").getPathString());
  80. assertEquals("a/b", new DirCacheEntry("a/b").getPathString());
  81. try {
  82. new DirCacheEntry("/a");
  83. fail("Incorrectly created DirCacheEntry");
  84. } catch (IllegalArgumentException err) {
  85. assertEquals("Invalid path: /a", err.getMessage());
  86. }
  87. }
  88. @SuppressWarnings("unused")
  89. @Test
  90. public void testCreate_ByStringPathAndStage() {
  91. DirCacheEntry e;
  92. e = new DirCacheEntry("a", 0);
  93. assertEquals("a", e.getPathString());
  94. assertEquals(0, e.getStage());
  95. e = new DirCacheEntry("a/b", 1);
  96. assertEquals("a/b", e.getPathString());
  97. assertEquals(1, e.getStage());
  98. e = new DirCacheEntry("a/c", 2);
  99. assertEquals("a/c", e.getPathString());
  100. assertEquals(2, e.getStage());
  101. e = new DirCacheEntry("a/d", 3);
  102. assertEquals("a/d", e.getPathString());
  103. assertEquals(3, e.getStage());
  104. try {
  105. new DirCacheEntry("/a", 1);
  106. fail("Incorrectly created DirCacheEntry");
  107. } catch (IllegalArgumentException err) {
  108. assertEquals("Invalid path: /a", err.getMessage());
  109. }
  110. try {
  111. new DirCacheEntry("a", -11);
  112. fail("Incorrectly created DirCacheEntry");
  113. } catch (IllegalArgumentException err) {
  114. assertEquals("Invalid stage -11 for path a", err.getMessage());
  115. }
  116. try {
  117. new DirCacheEntry("a", 4);
  118. fail("Incorrectly created DirCacheEntry");
  119. } catch (IllegalArgumentException err) {
  120. assertEquals("Invalid stage 4 for path a", err.getMessage());
  121. }
  122. }
  123. @Test
  124. public void testSetFileMode() {
  125. final DirCacheEntry e = new DirCacheEntry("a");
  126. assertEquals(0, e.getRawMode());
  127. e.setFileMode(FileMode.REGULAR_FILE);
  128. assertSame(FileMode.REGULAR_FILE, e.getFileMode());
  129. assertEquals(FileMode.REGULAR_FILE.getBits(), e.getRawMode());
  130. e.setFileMode(FileMode.EXECUTABLE_FILE);
  131. assertSame(FileMode.EXECUTABLE_FILE, e.getFileMode());
  132. assertEquals(FileMode.EXECUTABLE_FILE.getBits(), e.getRawMode());
  133. e.setFileMode(FileMode.SYMLINK);
  134. assertSame(FileMode.SYMLINK, e.getFileMode());
  135. assertEquals(FileMode.SYMLINK.getBits(), e.getRawMode());
  136. e.setFileMode(FileMode.GITLINK);
  137. assertSame(FileMode.GITLINK, e.getFileMode());
  138. assertEquals(FileMode.GITLINK.getBits(), e.getRawMode());
  139. try {
  140. e.setFileMode(FileMode.MISSING);
  141. fail("incorrectly accepted FileMode.MISSING");
  142. } catch (IllegalArgumentException err) {
  143. assertEquals("Invalid mode 0 for path a", err.getMessage());
  144. }
  145. try {
  146. e.setFileMode(FileMode.TREE);
  147. fail("incorrectly accepted FileMode.TREE");
  148. } catch (IllegalArgumentException err) {
  149. assertEquals("Invalid mode 40000 for path a", err.getMessage());
  150. }
  151. }
  152. @Test
  153. public void testCopyMetaDataWithStage() {
  154. copyMetaDataHelper(false);
  155. }
  156. @Test
  157. public void testCopyMetaDataWithoutStage() {
  158. copyMetaDataHelper(true);
  159. }
  160. private static void copyMetaDataHelper(boolean keepStage) {
  161. DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_2);
  162. e.setAssumeValid(false);
  163. e.setCreationTime(2L);
  164. e.setFileMode(FileMode.EXECUTABLE_FILE);
  165. e.setLastModified(EPOCH.plusMillis(3L));
  166. e.setLength(100L);
  167. e.setObjectId(ObjectId
  168. .fromString("0123456789012345678901234567890123456789"));
  169. e.setUpdateNeeded(true);
  170. DirCacheEntry f = new DirCacheEntry("someother/path",
  171. DirCacheEntry.STAGE_1);
  172. f.setAssumeValid(true);
  173. f.setCreationTime(10L);
  174. f.setFileMode(FileMode.SYMLINK);
  175. f.setLastModified(EPOCH.plusMillis(20L));
  176. f.setLength(100000000L);
  177. f.setObjectId(ObjectId
  178. .fromString("1234567890123456789012345678901234567890"));
  179. f.setUpdateNeeded(true);
  180. e.copyMetaData(f, keepStage);
  181. assertTrue(e.isAssumeValid());
  182. assertEquals(10L, e.getCreationTime());
  183. assertEquals(
  184. ObjectId.fromString("1234567890123456789012345678901234567890"),
  185. e.getObjectId());
  186. assertEquals(FileMode.SYMLINK, e.getFileMode());
  187. assertEquals(EPOCH.plusMillis(20L), e.getLastModifiedInstant());
  188. assertEquals(100000000L, e.getLength());
  189. if (keepStage)
  190. assertEquals(DirCacheEntry.STAGE_2, e.getStage());
  191. else
  192. assertEquals(DirCacheEntry.STAGE_1, e.getStage());
  193. assertTrue(e.isUpdateNeeded());
  194. assertEquals("some/path", e.getPathString());
  195. }
  196. }