View Javadoc
1   /*
2    * Copyright (C) 2017 Ericsson and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.file;
12  
13  import static org.junit.Assert.assertFalse;
14  import static org.junit.Assert.assertTrue;
15  
16  import java.io.File;
17  import java.io.IOException;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  public class GcOrphanFilesTest extends GcTestCase {
23  	private static final String PACK = "pack";
24  
25  	private static final String BITMAP_File_1 = PACK + "-1.bitmap";
26  
27  	private static final String BITMAP_File_2 = PACK + "-2.bitmap";
28  
29  	private static final String IDX_File_2 = PACK + "-2.idx";
30  
31  	private static final String IDX_File_malformed = PACK + "-1234idx";
32  
33  	private static final String KEEP_File_2 = PACK + "-2.keep";
34  
35  	private static final String PACK_File_2 = PACK + "-2.pack";
36  
37  	private static final String PACK_File_3 = PACK + "-3.pack";
38  
39  	private File packDir;
40  
41  	@Override
42  	@Before
43  	public void setUp() throws Exception {
44  		super.setUp();
45  		packDir = repo.getObjectDatabase().getPackDirectory();
46  	}
47  
48  	@Test
49  	public void bitmapAndIdxDeletedButPackNot() throws Exception {
50  		createFileInPackFolder(BITMAP_File_1);
51  		createFileInPackFolder(IDX_File_2);
52  		createFileInPackFolder(PACK_File_3);
53  		gc.gc().get();
54  		assertFalse(new File(packDir, BITMAP_File_1).exists());
55  		assertFalse(new File(packDir, IDX_File_2).exists());
56  		assertTrue(new File(packDir, PACK_File_3).exists());
57  	}
58  
59  	@Test
60  	public void bitmapDeletedButIdxAndPackNot() throws Exception {
61  		createFileInPackFolder(BITMAP_File_1);
62  		createFileInPackFolder(IDX_File_2);
63  		createFileInPackFolder(PACK_File_2);
64  		createFileInPackFolder(PACK_File_3);
65  		gc.gc().get();
66  		assertFalse(new File(packDir, BITMAP_File_1).exists());
67  		assertTrue(new File(packDir, IDX_File_2).exists());
68  		assertTrue(new File(packDir, PACK_File_2).exists());
69  		assertTrue(new File(packDir, PACK_File_3).exists());
70  	}
71  
72  	@Test
73  	public void malformedIdxNotDeleted() throws Exception {
74  		createFileInPackFolder(IDX_File_malformed);
75  		gc.gc().get();
76  		assertTrue(new File(packDir, IDX_File_malformed).exists());
77  	}
78  
79  	@Test
80  	public void keepPreventsDeletionOfIndexFilesForMissingPackFile()
81  			throws Exception {
82  		createFileInPackFolder(BITMAP_File_1);
83  		createFileInPackFolder(IDX_File_2);
84  		createFileInPackFolder(BITMAP_File_2);
85  		createFileInPackFolder(KEEP_File_2);
86  		createFileInPackFolder(PACK_File_3);
87  		gc.gc().get();
88  		assertFalse(new File(packDir, BITMAP_File_1).exists());
89  		assertTrue(new File(packDir, BITMAP_File_2).exists());
90  		assertTrue(new File(packDir, IDX_File_2).exists());
91  		assertTrue(new File(packDir, KEEP_File_2).exists());
92  		assertTrue(new File(packDir, PACK_File_3).exists());
93  	}
94  
95  	private void createFileInPackFolder(String fileName) throws IOException {
96  		if (!packDir.exists() || !packDir.isDirectory()) {
97  			assertTrue(packDir.mkdirs());
98  		}
99  		assertTrue(new File(packDir, fileName).createNewFile());
100 	}
101 
102 	@Test
103 	public void noSuchPackFolder() throws Exception {
104 		assertTrue(packDir.delete());
105 		gc.gc().get();
106 	}
107 }