View Javadoc
1   /*
2    * Copyright (C) 2019, Vishal Devgire <vishaldevgire@gmail.com> 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.util;
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  import java.nio.file.Files;
19  import java.nio.file.Path;
20  
21  import org.eclipse.jgit.junit.MockSystemReader;
22  import org.eclipse.jgit.lib.ConfigConstants;
23  import org.eclipse.jgit.storage.file.FileBasedConfig;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  public class FS_POSIXTest {
29  	private FileBasedConfig jgitConfig;
30  
31  	private FileBasedConfig systemConfig;
32  
33  	private FileBasedConfig userConfig;
34  
35  	private Path tmp;
36  
37  	@Before
38  	public void setUp() throws Exception {
39  		tmp = Files.createTempDirectory("jgit_test_");
40  
41  		MockSystemReader mockSystemReader = new MockSystemReader();
42  		SystemReader.setInstance(mockSystemReader);
43  
44  		// Measure timer resolution before the test to avoid time critical tests
45  		// are affected by time needed for measurement.
46  		// The MockSystemReader must be configured first since we need to use
47  		// the same one here
48  		FS.getFileStoreAttributes(tmp.getParent());
49  
50  		jgitConfig = new FileBasedConfig(new File(tmp.toFile(), "jgitconfig"),
51  				FS.DETECTED);
52  		systemConfig = new FileBasedConfig(jgitConfig,
53  				new File(tmp.toFile(), "systemgitconfig"), FS.DETECTED);
54  		userConfig = new FileBasedConfig(systemConfig,
55  				new File(tmp.toFile(), "usergitconfig"), FS.DETECTED);
56  		// We have to set autoDetach to false for tests, because tests expect to
57  		// be able to clean up by recursively removing the repository, and
58  		// background GC might be in the middle of writing or deleting files,
59  		// which would disrupt this.
60  		userConfig.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
61  				ConfigConstants.CONFIG_KEY_AUTODETACH, false);
62  		userConfig.save();
63  		mockSystemReader.setJGitConfig(jgitConfig);
64  		mockSystemReader.setSystemGitConfig(systemConfig);
65  		mockSystemReader.setUserGitConfig(userConfig);
66  	}
67  
68  	@After
69  	public void tearDown() throws IOException {
70  		SystemReader.setInstance(null);
71  		FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
72  	}
73  
74  	@Test
75  	public void supportsAtomicCreateNewFile_shouldReturnSupportedAsDefault() {
76  		assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
77  	}
78  
79  	@Test
80  	public void supportsAtomicCreateNewFile_shouldReturnTrueIfFlagIsSetInUserConfig() {
81  		setAtomicCreateCreationFlag(userConfig, "true");
82  		assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
83  	}
84  
85  	@Test
86  	public void supportsAtomicCreateNewFile_shouldReturnTrueIfFlagIsSetInSystemConfig() {
87  		setAtomicCreateCreationFlag(systemConfig, "true");
88  		assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
89  	}
90  
91  	@Test
92  	public void supportsAtomicCreateNewFile_shouldReturnFalseIfFlagUnsetInUserConfig() {
93  		setAtomicCreateCreationFlag(userConfig, "false");
94  		assertFalse(new FS_POSIX().supportsAtomicCreateNewFile());
95  	}
96  
97  	@Test
98  	public void supportsAtomicCreateNewFile_shouldReturnFalseIfFlagUnsetInSystemConfig() {
99  		setAtomicCreateCreationFlag(systemConfig, "false");
100 		assertFalse(new FS_POSIX().supportsAtomicCreateNewFile());
101 	}
102 
103 	private void setAtomicCreateCreationFlag(FileBasedConfig config,
104 			String value) {
105 		config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
106 				ConfigConstants.CONFIG_KEY_SUPPORTSATOMICFILECREATION, value);
107 	}
108 }