/
TeamscalePropertiesUtilsTest.java
90 lines (75 loc) · 3.21 KB
/
TeamscalePropertiesUtilsTest.java
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
package com.teamscale.jacoco.agent.options;
import okhttp3.HttpUrl;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class TeamscalePropertiesUtilsTest {
private Path teamscalePropertiesPath;
@BeforeEach
void createTempPath(@TempDir Path tempDir) {
teamscalePropertiesPath = tempDir.resolve("teamscale.properties");
}
@Test
void pathDoesNotExist() throws AgentOptionParseException {
assertThat(
TeamscalePropertiesUtils.parseCredentials(Paths.get("/does/not/exist/teamscale.properties"))).isNull();
}
@Test
void successfulParsing() throws AgentOptionParseException, IOException {
Files.write(teamscalePropertiesPath,
"url=http://test\nusername=user\naccesskey=key".getBytes(StandardCharsets.UTF_8));
TeamscaleCredentials credentials = TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath);
assertThat(credentials).isNotNull();
assertThat(credentials.url).isEqualTo(HttpUrl.get("http://test"));
assertThat(credentials.userName).isEqualTo("user");
assertThat(credentials.accessKey).isEqualTo("key");
}
@Test
void missingUsername() throws IOException {
Files.write(teamscalePropertiesPath, "url=http://test\naccesskey=key".getBytes(StandardCharsets.UTF_8));
assertThatThrownBy(
() -> TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath)).hasMessageContaining(
"missing the username");
}
@Test
void missingAccessKey() throws IOException {
Files.write(teamscalePropertiesPath, "url=http://test\nusername=user".getBytes(StandardCharsets.UTF_8));
assertThatThrownBy(
() -> TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath)).hasMessageContaining(
"missing the accesskey");
}
@Test
void missingUrl() throws IOException {
Files.write(teamscalePropertiesPath, "username=user\nusername=user".getBytes(StandardCharsets.UTF_8));
assertThatThrownBy(
() -> TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath)).hasMessageContaining(
"missing the url");
}
@Test
void malformedUrl() throws IOException {
Files.write(teamscalePropertiesPath, "url=$$**\nusername=user\nusername=user".getBytes(StandardCharsets.UTF_8));
assertThatThrownBy(
() -> TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath)).hasMessageContaining(
"malformed URL");
}
/** This test doesn't work on Windows since {@link java.io.File#setReadable(boolean)} does not work there. */
@DisabledOnOs(OS.WINDOWS)
@Test
void fileNotReadable() throws IOException {
Files.write(teamscalePropertiesPath,
"url=http://test\nusername=user\nusername=user".getBytes(StandardCharsets.UTF_8));
assertThat(teamscalePropertiesPath.toFile().setReadable(false)).isTrue();
assertThatThrownBy(
() -> TeamscalePropertiesUtils.parseCredentials(teamscalePropertiesPath)).hasMessageContaining(
"Failed to read");
}
}