View Javadoc
1   /*
2    * Copyright (C) 2012-2013, Robin Rosenberg <robin.rosenberg@dewire.com>
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.treewalk;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertFalse;
47  import static org.junit.Assert.assertTrue;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import org.eclipse.jgit.api.Git;
53  import org.eclipse.jgit.api.ResetCommand.ResetType;
54  import org.eclipse.jgit.dircache.DirCache;
55  import org.eclipse.jgit.dircache.DirCacheEditor;
56  import org.eclipse.jgit.dircache.DirCacheEntry;
57  import org.eclipse.jgit.dircache.DirCacheIterator;
58  import org.eclipse.jgit.junit.RepositoryTestCase;
59  import org.eclipse.jgit.lib.Constants;
60  import org.eclipse.jgit.lib.FileMode;
61  import org.eclipse.jgit.lib.ObjectId;
62  import org.eclipse.jgit.lib.ObjectInserter;
63  import org.eclipse.jgit.util.FS;
64  import org.eclipse.jgit.util.FileUtils;
65  import org.junit.Test;
66  
67  public class FileTreeIteratorJava7Test extends RepositoryTestCase {
68  	@Test
69  	public void testFileModeSymLinkIsNotATree() throws IOException {
70  		org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
71  		FS fs = db.getFS();
72  		// mål = target in swedish, just to get som unicode in here
73  		writeTrashFile("mål/data", "targetdata");
74  		fs.createSymLink(new File(trash, "länk"), "mål");
75  		FileTreeIterator fti = new FileTreeIterator(db);
76  		assertFalse(fti.eof());
77  		assertEquals("länk", fti.getEntryPathString());
78  		assertEquals(FileMode.SYMLINK, fti.getEntryFileMode());
79  		fti.next(1);
80  		assertFalse(fti.eof());
81  		assertEquals("mål", fti.getEntryPathString());
82  		assertEquals(FileMode.TREE, fti.getEntryFileMode());
83  		fti.next(1);
84  		assertTrue(fti.eof());
85  	}
86  
87  	@Test
88  	public void testSymlinkNotModifiedThoughNormalized() throws Exception {
89  		DirCache dc = db.lockDirCache();
90  		DirCacheEditor dce = dc.editor();
91  		final String UNNORMALIZED = "target/";
92  		final byte[] UNNORMALIZED_BYTES = Constants.encode(UNNORMALIZED);
93  		try (ObjectInserter oi = db.newObjectInserter()) {
94  			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
95  					UNNORMALIZED_BYTES, 0, UNNORMALIZED_BYTES.length);
96  			dce.add(new DirCacheEditor.PathEdit("link") {
97  				@Override
98  				public void apply(DirCacheEntry ent) {
99  					ent.setFileMode(FileMode.SYMLINK);
100 					ent.setObjectId(linkid);
101 					ent.setLength(UNNORMALIZED_BYTES.length);
102 				}
103 			});
104 			assertTrue(dce.commit());
105 		}
106 		try (Git git = new Git(db)) {
107 			git.commit().setMessage("Adding link").call();
108 			git.reset().setMode(ResetType.HARD).call();
109 			DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
110 			FileTreeIterator fti = new FileTreeIterator(db);
111 
112 			// self-check
113 			assertEquals("link", fti.getEntryPathString());
114 			assertEquals("link", dci.getEntryPathString());
115 
116 			// test
117 			assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
118 					db.newObjectReader()));
119 		}
120 	}
121 
122 	/**
123 	 * Like #testSymlinkNotModifiedThoughNormalized but there is no
124 	 * normalization being done.
125 	 *
126 	 * @throws Exception
127 	 */
128 	@Test
129 	public void testSymlinkModifiedNotNormalized() throws Exception {
130 		DirCache dc = db.lockDirCache();
131 		DirCacheEditor dce = dc.editor();
132 		final String NORMALIZED = "target";
133 		final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
134 		try (ObjectInserter oi = db.newObjectInserter()) {
135 			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
136 					NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
137 			dce.add(new DirCacheEditor.PathEdit("link") {
138 				@Override
139 				public void apply(DirCacheEntry ent) {
140 					ent.setFileMode(FileMode.SYMLINK);
141 					ent.setObjectId(linkid);
142 					ent.setLength(NORMALIZED_BYTES.length);
143 				}
144 			});
145 			assertTrue(dce.commit());
146 		}
147 		try (Git git = new Git(db)) {
148 			git.commit().setMessage("Adding link").call();
149 			git.reset().setMode(ResetType.HARD).call();
150 			DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
151 			FileTreeIterator fti = new FileTreeIterator(db);
152 
153 			// self-check
154 			assertEquals("link", fti.getEntryPathString());
155 			assertEquals("link", dci.getEntryPathString());
156 
157 			// test
158 			assertFalse(fti.isModified(dci.getDirCacheEntry(), true,
159 					db.newObjectReader()));
160 		}
161 	}
162 
163 	/**
164 	 * Like #testSymlinkNotModifiedThoughNormalized but here the link is
165 	 * modified.
166 	 *
167 	 * @throws Exception
168 	 */
169 	@Test
170 	public void testSymlinkActuallyModified() throws Exception {
171 		org.junit.Assume.assumeTrue(FS.DETECTED.supportsSymlinks());
172 		final String NORMALIZED = "target";
173 		final byte[] NORMALIZED_BYTES = Constants.encode(NORMALIZED);
174 		try (ObjectInserter oi = db.newObjectInserter()) {
175 			final ObjectId linkid = oi.insert(Constants.OBJ_BLOB,
176 					NORMALIZED_BYTES, 0, NORMALIZED_BYTES.length);
177 			DirCache dc = db.lockDirCache();
178 			DirCacheEditor dce = dc.editor();
179 			dce.add(new DirCacheEditor.PathEdit("link") {
180 				@Override
181 				public void apply(DirCacheEntry ent) {
182 					ent.setFileMode(FileMode.SYMLINK);
183 					ent.setObjectId(linkid);
184 					ent.setLength(NORMALIZED_BYTES.length);
185 				}
186 			});
187 			assertTrue(dce.commit());
188 		}
189 		try (Git git = new Git(db)) {
190 			git.commit().setMessage("Adding link").call();
191 			git.reset().setMode(ResetType.HARD).call();
192 
193 			FileUtils.delete(new File(trash, "link"), FileUtils.NONE);
194 			FS.DETECTED.createSymLink(new File(trash, "link"), "newtarget");
195 			DirCacheIterator dci = new DirCacheIterator(db.readDirCache());
196 			FileTreeIterator fti = new FileTreeIterator(db);
197 
198 			// self-check
199 			assertEquals("link", fti.getEntryPathString());
200 			assertEquals("link", dci.getEntryPathString());
201 
202 			// test
203 			assertTrue(fti.isModified(dci.getDirCacheEntry(), true,
204 					db.newObjectReader()));
205 		}
206 	}
207 }