View Javadoc
1   /*
2    * Copyright (C) 2019, Matthias Sohn <matthias.sohn@sap.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  package org.eclipse.jgit.util;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.mockito.Mockito.when;
14  
15  import java.nio.charset.StandardCharsets;
16  import java.nio.file.Files;
17  import java.nio.file.Path;
18  
19  import org.eclipse.jgit.storage.file.FileBasedConfig;
20  import org.junit.After;
21  import org.junit.Before;
22  import org.junit.Test;
23  import org.junit.runner.RunWith;
24  import org.mockito.Mock;
25  import org.mockito.junit.MockitoJUnitRunner;
26  
27  @RunWith(MockitoJUnitRunner.class)
28  public class SystemReaderTest {
29  	private Path trash;
30  
31  	private Path mockSystemConfig;
32  
33  	private Path mockUserConfig;
34  
35  	@Mock
36  	private FS fs;
37  
38  	@Before
39  	public void setup() throws Exception {
40  		trash = Files.createTempDirectory("jgit_test");
41  		mockSystemConfig = trash.resolve("systemgitconfig");
42  		Files.write(mockSystemConfig, "[core]\n  trustFolderStat = false\n"
43  				.getBytes(StandardCharsets.UTF_8));
44  		mockUserConfig = trash.resolve(".gitconfig");
45  		Files.write(mockUserConfig,
46  				"[core]\n  bare = false\n".getBytes(StandardCharsets.UTF_8));
47  		when(fs.getGitSystemConfig()).thenReturn(mockSystemConfig.toFile());
48  		when(fs.userHome()).thenReturn(trash.toFile());
49  		SystemReader.setInstance(null);
50  	}
51  
52  	@After
53  	public void teardown() throws Exception {
54  		FileUtils.delete(trash.toFile(), FileUtils.RECURSIVE | FileUtils.RETRY);
55  	}
56  
57  	@Test
58  	public void openSystemConfigReturnsDifferentInstances() throws Exception {
59  		FileBasedConfig system1 = SystemReader.getInstance()
60  				.openSystemConfig(null, fs);
61  		system1.load();
62  		assertEquals("false",
63  				system1.getString("core", null, "trustFolderStat"));
64  
65  		FileBasedConfig system2 = SystemReader.getInstance()
66  				.openSystemConfig(null, fs);
67  		system2.load();
68  		assertEquals("false",
69  				system2.getString("core", null, "trustFolderStat"));
70  
71  		system1.setString("core", null, "trustFolderStat", "true");
72  		assertEquals("true",
73  				system1.getString("core", null, "trustFolderStat"));
74  		assertEquals("false",
75  				system2.getString("core", null, "trustFolderStat"));
76  	}
77  
78  	@Test
79  	public void openUserConfigReturnsDifferentInstances() throws Exception {
80  		FileBasedConfig user1 = SystemReader.getInstance().openUserConfig(null,
81  				fs);
82  		user1.load();
83  		assertEquals("false", user1.getString("core", null, "bare"));
84  
85  		FileBasedConfig user2 = SystemReader.getInstance().openUserConfig(null,
86  				fs);
87  		user2.load();
88  		assertEquals("false", user2.getString("core", null, "bare"));
89  
90  		user1.setString("core", null, "bare", "true");
91  		assertEquals("true", user1.getString("core", null, "bare"));
92  		assertEquals("false", user2.getString("core", null, "bare"));
93  	}
94  }