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.nio.file.Paths;
18  import java.time.Instant;
19  
20  import org.junit.Before;
21  import org.junit.Test;
22  
23  public class GcTemporaryFilesTest extends GcTestCase {
24  	private static final String TEMP_IDX = "gc_1234567890.idx_tmp";
25  
26  	private static final String TEMP_PACK = "gc_1234567890.pack_tmp";
27  
28  	private File packDir;
29  
30  	@Override
31  	@Before
32  	public void setUp() throws Exception {
33  		super.setUp();
34  		packDir = Paths.get(repo.getObjectsDirectory().getAbsolutePath(),
35  				"pack").toFile(); //$NON-NLS-1$
36  	}
37  
38  	@Test
39  	public void oldTempPacksAndIdxAreDeleted() throws Exception {
40  		File tempIndex = new File(packDir, TEMP_IDX);
41  		File tempPack = new File(packDir, TEMP_PACK);
42  		if (!packDir.exists() || !packDir.isDirectory()) {
43  			assertTrue(packDir.mkdirs());
44  		}
45  		assertTrue(tempPack.createNewFile());
46  		assertTrue(tempIndex.createNewFile());
47  		assertTrue(tempIndex.exists());
48  		assertTrue(tempPack.exists());
49  		long _24HoursBefore = Instant.now().toEpochMilli()
50  				- 24 * 60 * 62 * 1000;
51  		tempIndex.setLastModified(_24HoursBefore);
52  		tempPack.setLastModified(_24HoursBefore);
53  		gc.gc().get();
54  		assertFalse(tempIndex.exists());
55  		assertFalse(tempPack.exists());
56  	}
57  
58  	@Test
59  	public void recentTempPacksAndIdxAreNotDeleted() throws Exception {
60  		File tempIndex = new File(packDir, TEMP_IDX);
61  		File tempPack = new File(packDir, TEMP_PACK);
62  		if (!packDir.exists() || !packDir.isDirectory()) {
63  			assertTrue(packDir.mkdirs());
64  		}
65  		assertTrue(tempPack.createNewFile());
66  		assertTrue(tempIndex.createNewFile());
67  		assertTrue(tempIndex.exists());
68  		assertTrue(tempPack.exists());
69  		gc.gc().get();
70  		assertTrue(tempIndex.exists());
71  		assertTrue(tempPack.exists());
72  	}
73  }