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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 junit.framework.TestCase;
  45. import org.eclipse.jgit.lib.Constants;
  46. import org.eclipse.jgit.lib.FileMode;
  47. public class DirCacheEntryTest extends TestCase {
  48. public void testIsValidPath() {
  49. assertTrue(isValidPath("a"));
  50. assertTrue(isValidPath("a/b"));
  51. assertTrue(isValidPath("ab/cd/ef"));
  52. assertFalse(isValidPath(""));
  53. assertFalse(isValidPath("/a"));
  54. assertFalse(isValidPath("a//b"));
  55. assertFalse(isValidPath("ab/cd//ef"));
  56. assertFalse(isValidPath("a/"));
  57. assertFalse(isValidPath("ab/cd/ef/"));
  58. assertFalse(isValidPath("a\u0000b"));
  59. }
  60. private static boolean isValidPath(final String path) {
  61. return DirCacheEntry.isValidPath(Constants.encode(path));
  62. }
  63. public void testCreate_ByStringPath() {
  64. assertEquals("a", new DirCacheEntry("a").getPathString());
  65. assertEquals("a/b", new DirCacheEntry("a/b").getPathString());
  66. try {
  67. new DirCacheEntry("/a");
  68. fail("Incorrectly created DirCacheEntry");
  69. } catch (IllegalArgumentException err) {
  70. assertEquals("Invalid path: /a", err.getMessage());
  71. }
  72. }
  73. public void testCreate_ByStringPathAndStage() {
  74. DirCacheEntry e;
  75. e = new DirCacheEntry("a", 0);
  76. assertEquals("a", e.getPathString());
  77. assertEquals(0, e.getStage());
  78. e = new DirCacheEntry("a/b", 1);
  79. assertEquals("a/b", e.getPathString());
  80. assertEquals(1, e.getStage());
  81. e = new DirCacheEntry("a/c", 2);
  82. assertEquals("a/c", e.getPathString());
  83. assertEquals(2, e.getStage());
  84. e = new DirCacheEntry("a/d", 3);
  85. assertEquals("a/d", e.getPathString());
  86. assertEquals(3, e.getStage());
  87. try {
  88. new DirCacheEntry("/a", 1);
  89. fail("Incorrectly created DirCacheEntry");
  90. } catch (IllegalArgumentException err) {
  91. assertEquals("Invalid path: /a", err.getMessage());
  92. }
  93. try {
  94. new DirCacheEntry("a", -11);
  95. fail("Incorrectly created DirCacheEntry");
  96. } catch (IllegalArgumentException err) {
  97. assertEquals("Invalid stage -11 for path a", err.getMessage());
  98. }
  99. try {
  100. new DirCacheEntry("a", 4);
  101. fail("Incorrectly created DirCacheEntry");
  102. } catch (IllegalArgumentException err) {
  103. assertEquals("Invalid stage 4 for path a", err.getMessage());
  104. }
  105. }
  106. public void testSetFileMode() {
  107. final DirCacheEntry e = new DirCacheEntry("a");
  108. assertEquals(0, e.getRawMode());
  109. e.setFileMode(FileMode.REGULAR_FILE);
  110. assertSame(FileMode.REGULAR_FILE, e.getFileMode());
  111. assertEquals(FileMode.REGULAR_FILE.getBits(), e.getRawMode());
  112. e.setFileMode(FileMode.EXECUTABLE_FILE);
  113. assertSame(FileMode.EXECUTABLE_FILE, e.getFileMode());
  114. assertEquals(FileMode.EXECUTABLE_FILE.getBits(), e.getRawMode());
  115. e.setFileMode(FileMode.SYMLINK);
  116. assertSame(FileMode.SYMLINK, e.getFileMode());
  117. assertEquals(FileMode.SYMLINK.getBits(), e.getRawMode());
  118. e.setFileMode(FileMode.GITLINK);
  119. assertSame(FileMode.GITLINK, e.getFileMode());
  120. assertEquals(FileMode.GITLINK.getBits(), e.getRawMode());
  121. try {
  122. e.setFileMode(FileMode.MISSING);
  123. fail("incorrectly accepted FileMode.MISSING");
  124. } catch (IllegalArgumentException err) {
  125. assertEquals("Invalid mode 0 for path a", err.getMessage());
  126. }
  127. try {
  128. e.setFileMode(FileMode.TREE);
  129. fail("incorrectly accepted FileMode.TREE");
  130. } catch (IllegalArgumentException err) {
  131. assertEquals("Invalid mode 40000 for path a", err.getMessage());
  132. }
  133. }
  134. }