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