1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  package org.eclipse.jgit.internal.storage.file;
44  
45  import static org.junit.Assert.assertFalse;
46  import static org.junit.Assert.assertTrue;
47  
48  import java.io.File;
49  import java.io.FilenameFilter;
50  import java.io.IOException;
51  import java.io.PrintWriter;
52  import java.text.MessageFormat;
53  import java.util.Collection;
54  import java.util.Collections;
55  import java.util.Set;
56  import java.util.concurrent.Callable;
57  import java.util.concurrent.ExecutorService;
58  import java.util.concurrent.Executors;
59  import java.util.concurrent.Future;
60  
61  import org.eclipse.jgit.internal.JGitText;
62  import org.eclipse.jgit.junit.RepositoryTestCase;
63  import org.eclipse.jgit.lib.ConfigConstants;
64  import org.eclipse.jgit.lib.Constants;
65  import org.eclipse.jgit.lib.ObjectId;
66  import org.eclipse.jgit.revwalk.RevCommit;
67  import org.eclipse.jgit.storage.file.FileBasedConfig;
68  import org.junit.Assume;
69  import org.junit.Rule;
70  import org.junit.Test;
71  import org.junit.rules.ExpectedException;
72  
73  public class ObjectDirectoryTest extends RepositoryTestCase {
74  
75  	@Rule
76  	public ExpectedException expectedEx = ExpectedException.none();
77  
78  	@Test
79  	public void testConcurrentInsertionOfBlobsToTheSameNewFanOutDirectory()
80  			throws Exception {
81  		ExecutorService e = Executors.newCachedThreadPool();
82  		for (int i=0; i < 100; ++i) {
83  			ObjectDirectory dir = createBareRepository().getObjectDatabase();
84  			for (Future f : e.invokeAll(blobInsertersForTheSameFanOutDir(dir))) {
85  				f.get();
86  			}
87  		}
88  	}
89  
90  	
91  
92  
93  
94  
95  
96  
97  
98  
99  
100 
101 
102 
103 
104 
105 	@Test
106 	public void testScanningForPackfiles() throws Exception {
107 		ObjectId unknownID = ObjectId
108 				.fromString("c0ffee09d0b63d694bf49bc1e6847473f42d4a8c");
109 		GC gc = new GC(db);
110 		gc.setExpireAgeMillis(0);
111 		gc.setPackExpireAgeMillis(0);
112 
113 		
114 		
115 		try (FileRepository receivingDB = new FileRepository(
116 				db.getDirectory())) {
117 			
118 			
119 			FileBasedConfig cfg = receivingDB.getConfig();
120 			cfg.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
121 					ConfigConstants.CONFIG_KEY_TRUSTFOLDERSTAT, true);
122 			cfg.save();
123 
124 			
125 			
126 			ObjectId id = commitFile("file.txt", "test", "master").getId();
127 			gc.gc();
128 			assertFalse(receivingDB.hasObject(unknownID));
129 			assertTrue(receivingDB.getObjectDatabase().hasPackedObject(id));
130 
131 			
132 			File packsFolder = new File(receivingDB.getObjectsDirectory(),
133 					"pack");
134 			
135 			
136 			
137 			File tmpFile = new File(packsFolder, "1.tmp");
138 			RevCommit id2 = commitFile("file.txt", "test2", "master");
139 			
140 			
141 			
142 			fsTick(null);
143 
144 			
145 			
146 			
147 			
148 			
149 			
150 			
151 			assertTrue(tmpFile.createNewFile());
152 			assertFalse(receivingDB.hasObject(unknownID));
153 
154 			
155 			
156 			gc.gc();
157 
158 			
159 			
160 			
161 			
162 			
163 			
164 			
165 			
166 			
167 			Thread.sleep(2600);
168 
169 			File[] ret = packsFolder.listFiles(new FilenameFilter() {
170 				@Override
171 				public boolean accept(File dir, String name) {
172 					return name.endsWith(".pack");
173 				}
174 			});
175 			assertTrue(ret != null && ret.length == 1);
176 			Assume.assumeTrue(tmpFile.lastModified() == ret[0].lastModified());
177 
178 			
179 			assertFalse(receivingDB.hasObject(unknownID));
180 			assertTrue(receivingDB.hasObject(id2));
181 		}
182 	}
183 
184 	@Test
185 	public void testShallowFile()
186 			throws Exception {
187 		FileRepository repository = createBareRepository();
188 		ObjectDirectory dir = repository.getObjectDatabase();
189 
190 		String commit = "d3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
191 		try (PrintWriter writer = new PrintWriter(
192 				new File(repository.getDirectory(), Constants.SHALLOW))) {
193 			writer.println(commit);
194 		}
195 		Set<ObjectId> shallowCommits = dir.getShallowCommits();
196 		assertTrue(shallowCommits.remove(ObjectId.fromString(commit)));
197 		assertTrue(shallowCommits.isEmpty());
198 	}
199 
200 	@Test
201 	public void testShallowFileCorrupt()
202 			throws Exception {
203 		FileRepository repository = createBareRepository();
204 		ObjectDirectory dir = repository.getObjectDatabase();
205 
206 		String commit = "X3148f9410b071edd4a4c85d2a43d1fa2574b0d2";
207 		try (PrintWriter writer = new PrintWriter(
208 				new File(repository.getDirectory(), Constants.SHALLOW))) {
209 			writer.println(commit);
210 		}
211 
212 		expectedEx.expect(IOException.class);
213 		expectedEx.expectMessage(MessageFormat
214 				.format(JGitText.get().badShallowLine, commit));
215 		dir.getShallowCommits();
216 	}
217 
218 	private Collection<Callable<ObjectId>> blobInsertersForTheSameFanOutDir(
219 			final ObjectDirectory dir) {
220 		Callable<ObjectId> callable = new Callable<ObjectId>() {
221 			@Override
222 			public ObjectId call() throws Exception {
223 				return dir.newInserter().insert(Constants.OBJ_BLOB, new byte[0]);
224 			}
225 		};
226 		return Collections.nCopies(4, callable);
227 	}
228 
229 }