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
44 package org.eclipse.jgit.util;
45
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 import java.nio.file.Files;
52 import java.nio.file.Path;
53
54 import org.eclipse.jgit.junit.MockSystemReader;
55 import org.eclipse.jgit.lib.ConfigConstants;
56 import org.eclipse.jgit.storage.file.FileBasedConfig;
57 import org.junit.After;
58 import org.junit.Before;
59 import org.junit.Test;
60
61 public class FS_POSIXTest {
62 private SystemReader originalSystemReaderInstance;
63
64 private FileBasedConfig systemConfig;
65
66 private FileBasedConfig userConfig;
67
68 private Path tmp;
69
70 @Before
71 public void setUp() throws Exception {
72 tmp = Files.createTempDirectory("jgit_test_");
73 MockSystemReader mockSystemReader = new MockSystemReader();
74 SystemReader.setInstance(mockSystemReader);
75
76
77
78
79
80 FS.getFileStoreAttributes(tmp.getParent());
81 systemConfig = new FileBasedConfig(
82 new File(tmp.toFile(), "systemgitconfig"), FS.DETECTED);
83 userConfig = new FileBasedConfig(systemConfig,
84 new File(tmp.toFile(), "usergitconfig"), FS.DETECTED);
85
86
87
88
89 userConfig.setBoolean(ConfigConstants.CONFIG_GC_SECTION, null,
90 ConfigConstants.CONFIG_KEY_AUTODETACH, false);
91 userConfig.save();
92 mockSystemReader.setSystemGitConfig(systemConfig);
93 mockSystemReader.setUserGitConfig(userConfig);
94
95 originalSystemReaderInstance = SystemReader.getInstance();
96 SystemReader.setInstance(mockSystemReader);
97 }
98
99 @After
100 public void tearDown() throws IOException {
101 SystemReader.setInstance(originalSystemReaderInstance);
102 FileUtils.delete(tmp.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
103 }
104
105 @Test
106 public void supportsAtomicCreateNewFile_shouldReturnSupportedAsDefault() {
107 assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
108 }
109
110 @Test
111 public void supportsAtomicCreateNewFile_shouldReturnTrueIfFlagIsSetInUserConfig() {
112 setAtomicCreateCreationFlag(userConfig, "true");
113 assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
114 }
115
116 @Test
117 public void supportsAtomicCreateNewFile_shouldReturnTrueIfFlagIsSetInSystemConfig() {
118 setAtomicCreateCreationFlag(systemConfig, "true");
119 assertTrue(new FS_POSIX().supportsAtomicCreateNewFile());
120 }
121
122 @Test
123 public void supportsAtomicCreateNewFile_shouldReturnFalseIfFlagUnsetInUserConfig() {
124 setAtomicCreateCreationFlag(userConfig, "false");
125 assertFalse(new FS_POSIX().supportsAtomicCreateNewFile());
126 }
127
128 @Test
129 public void supportsAtomicCreateNewFile_shouldReturnFalseIfFlagUnsetInSystemConfig() {
130 setAtomicCreateCreationFlag(systemConfig, "false");
131 assertFalse(new FS_POSIX().supportsAtomicCreateNewFile());
132 }
133
134 private void setAtomicCreateCreationFlag(FileBasedConfig config,
135 String value) {
136 config.setString(ConfigConstants.CONFIG_CORE_SECTION, null,
137 ConfigConstants.CONFIG_KEY_SUPPORTSATOMICFILECREATION, value);
138 }
139 }