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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.Constants;
  50. import org.eclipse.jgit.lib.FileMode;
  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. return DirCacheEntry.isValidPath(Constants.encode(path));
  68. }
  69. @Test
  70. public void testCreate_ByStringPath() {
  71. assertEquals("a", new DirCacheEntry("a").getPathString());
  72. assertEquals("a/b", new DirCacheEntry("a/b").getPathString());
  73. try {
  74. new DirCacheEntry("/a");
  75. fail("Incorrectly created DirCacheEntry");
  76. } catch (IllegalArgumentException err) {
  77. assertEquals("Invalid path: /a", err.getMessage());
  78. }
  79. }
  80. @Test
  81. public void testCreate_ByStringPathAndStage() {
  82. DirCacheEntry e;
  83. e = new DirCacheEntry("a", 0);
  84. assertEquals("a", e.getPathString());
  85. assertEquals(0, e.getStage());
  86. e = new DirCacheEntry("a/b", 1);
  87. assertEquals("a/b", e.getPathString());
  88. assertEquals(1, e.getStage());
  89. e = new DirCacheEntry("a/c", 2);
  90. assertEquals("a/c", e.getPathString());
  91. assertEquals(2, e.getStage());
  92. e = new DirCacheEntry("a/d", 3);
  93. assertEquals("a/d", e.getPathString());
  94. assertEquals(3, e.getStage());
  95. try {
  96. new DirCacheEntry("/a", 1);
  97. fail("Incorrectly created DirCacheEntry");
  98. } catch (IllegalArgumentException err) {
  99. assertEquals("Invalid path: /a", err.getMessage());
  100. }
  101. try {
  102. new DirCacheEntry("a", -11);
  103. fail("Incorrectly created DirCacheEntry");
  104. } catch (IllegalArgumentException err) {
  105. assertEquals("Invalid stage -11 for path a", err.getMessage());
  106. }
  107. try {
  108. new DirCacheEntry("a", 4);
  109. fail("Incorrectly created DirCacheEntry");
  110. } catch (IllegalArgumentException err) {
  111. assertEquals("Invalid stage 4 for path a", err.getMessage());
  112. }
  113. }
  114. @Test
  115. public void testSetFileMode() {
  116. final DirCacheEntry e = new DirCacheEntry("a");
  117. assertEquals(0, e.getRawMode());
  118. e.setFileMode(FileMode.REGULAR_FILE);
  119. assertSame(FileMode.REGULAR_FILE, e.getFileMode());
  120. assertEquals(FileMode.REGULAR_FILE.getBits(), e.getRawMode());
  121. e.setFileMode(FileMode.EXECUTABLE_FILE);
  122. assertSame(FileMode.EXECUTABLE_FILE, e.getFileMode());
  123. assertEquals(FileMode.EXECUTABLE_FILE.getBits(), e.getRawMode());
  124. e.setFileMode(FileMode.SYMLINK);
  125. assertSame(FileMode.SYMLINK, e.getFileMode());
  126. assertEquals(FileMode.SYMLINK.getBits(), e.getRawMode());
  127. e.setFileMode(FileMode.GITLINK);
  128. assertSame(FileMode.GITLINK, e.getFileMode());
  129. assertEquals(FileMode.GITLINK.getBits(), e.getRawMode());
  130. try {
  131. e.setFileMode(FileMode.MISSING);
  132. fail("incorrectly accepted FileMode.MISSING");
  133. } catch (IllegalArgumentException err) {
  134. assertEquals("Invalid mode 0 for path a", err.getMessage());
  135. }
  136. try {
  137. e.setFileMode(FileMode.TREE);
  138. fail("incorrectly accepted FileMode.TREE");
  139. } catch (IllegalArgumentException err) {
  140. assertEquals("Invalid mode 40000 for path a", err.getMessage());
  141. }
  142. }
  143. }