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 6.8KB

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