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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/*
* SonarQube
* Copyright (C) 2009-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.application;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Properties;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ShutdownerTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File srcFile;
@Before
public void setUp() throws Exception {
srcFile = new File(Shutdowner.class.getProtectionDomain().getCodeSource().getLocation().toURI());
}
@Test
public void detectHomeDir_assumes_Shutdowner_jar_is_in_lib_dir_root() {
assertThat(Shutdowner.detectHomeDir())
.isEqualTo(srcFile.getParentFile().getParentFile());
}
@Test
public void loadPropertiesFile_fails_with_ISE_if_sonar_properties_not_in_conf_dir() throws IOException {
File homeDir = temporaryFolder.newFolder();
assertThatThrownBy(() -> Shutdowner.loadPropertiesFile(homeDir))
.isInstanceOf(IllegalStateException.class)
.hasMessage("Configuration file not found: " + new File(new File(homeDir, "conf"), "sonar.properties").getAbsolutePath());
}
@Test
public void loadPropertiesFile_reads_sonar_properties_content() throws IOException {
File homeDir = temporaryFolder.newFolder();
File confDir = new File(homeDir, "conf");
confDir.mkdirs();
File sonarProperties = new File(confDir, "sonar.properties");
sonarProperties.createNewFile();
Files.write(sonarProperties.toPath(), Arrays.asList("foo=bar"));
Properties properties = Shutdowner.loadPropertiesFile(homeDir);
assertThat(properties.getProperty("foo")).isEqualTo("bar");
}
@Test
public void resolveTempDir_defaults_to_temp() {
File file = Shutdowner.resolveTempDir(new Properties());
assertThat(file).isEqualTo(new File("temp"));
}
@Test
public void resolveTempDir_reads_relative_temp_dir_location_from_sonar_path_temp() {
String tempDirPath = "blablabl";
Properties properties = new Properties();
properties.put("sonar.path.temp", tempDirPath);
File file = Shutdowner.resolveTempDir(properties);
assertThat(file).isEqualTo(new File(tempDirPath));
}
@Test
public void resolveTempDir_reads_absolute_temp_dir_location_from_sonar_path_temp() throws IOException {
File tempDirLocation = temporaryFolder.newFolder();
Properties properties = new Properties();
properties.put("sonar.path.temp", tempDirLocation.getAbsolutePath());
File file = Shutdowner.resolveTempDir(properties);
assertThat(file).isEqualTo(tempDirLocation);
}
@Test
public void askForHardStop_write_right_bit_with_right_value_in_right_file() throws Exception {
File tempFolder = temporaryFolder.newFolder();
Shutdowner.askForHardStop(tempFolder);
try (RandomAccessFile sharedMemory = new RandomAccessFile(new File(tempFolder, "sharedmemory"), "r")) {
// Using values from org.sonar.process.ProcessCommands
MappedByteBuffer mappedByteBuffer = sharedMemory.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, 50L * 10);
assertThat(mappedByteBuffer.get(1)).isEqualTo((byte) 0xFF);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
|