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