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
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.db.component;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ComponentDtoTest {
@Test
void setters_and_getters() {
ComponentDto componentDto = new ComponentDto()
.setKey("org.struts:struts-core:src/org/struts/RequestContext.java")
.setName("RequestContext.java")
.setLongName("org.struts.RequestContext")
.setQualifier("FIL")
.setScope("FIL")
.setLanguage("java")
.setDescription("desc")
.setPath("src/org/struts/RequestContext.java")
.setCopyComponentUuid("uuid_5");
assertThat(componentDto.getKey()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(componentDto.name()).isEqualTo("RequestContext.java");
assertThat(componentDto.longName()).isEqualTo("org.struts.RequestContext");
assertThat(componentDto.qualifier()).isEqualTo("FIL");
assertThat(componentDto.scope()).isEqualTo("FIL");
assertThat(componentDto.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(componentDto.language()).isEqualTo("java");
assertThat(componentDto.description()).isEqualTo("desc");
assertThat(componentDto.getCopyComponentUuid()).isEqualTo("uuid_5");
assertThat(componentDto.isPrivate()).isFalse();
}
@Test
void equals_and_hashcode() {
ComponentDto dto = new ComponentDto().setUuid("u1");
ComponentDto dtoWithSameUuid = new ComponentDto().setUuid("u1");
ComponentDto dtoWithDifferentUuid = new ComponentDto().setUuid("u2");
assertThat(dto)
.isEqualTo(dto)
.isEqualTo(dtoWithSameUuid)
.isNotEqualTo(dtoWithDifferentUuid)
.hasSameHashCodeAs(dto)
.hasSameHashCodeAs(dtoWithSameUuid);
assertThat(dto.hashCode()).isNotEqualTo(dtoWithDifferentUuid.hashCode());
}
@Test
void toString_does_not_fail_if_empty() {
ComponentDto dto = new ComponentDto();
assertThat(dto.toString()).isNotEmpty();
}
@Test
void is_root_project() {
assertThat(new ComponentDto().setUuid("uuid").setBranchUuid("branch").isRootProject()).isFalse();
assertThat(new ComponentDto().setScope(ComponentScopes.DIRECTORY).setUuid("uuid").setBranchUuid("uuid").isRootProject()).isFalse();
assertThat(new ComponentDto().setScope(ComponentScopes.PROJECT).setUuid("uuid").setBranchUuid("uuid").isRootProject()).isTrue();
}
@Test
void formatUuidPathFromParent() {
ComponentDto parent = ComponentTesting.newPrivateProjectDto("123").setUuidPath(ComponentDto.UUID_PATH_OF_ROOT);
assertThat(ComponentDto.formatUuidPathFromParent(parent)).isEqualTo(".123.");
}
@Test
void getUuidPathLikeIncludingSelf() {
ComponentDto project = ComponentTesting.newPrivateProjectDto().setUuidPath(ComponentDto.UUID_PATH_OF_ROOT);
assertThat(project.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + ".%");
ComponentDto dir = ComponentTesting.newDirectory(project, "path");
assertThat(dir.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + "." + dir.uuid() + ".%");
ComponentDto file = ComponentTesting.newFileDto(project, dir);
assertThat(file.getUuidPathLikeIncludingSelf()).isEqualTo("." + project.uuid() + "." + dir.uuid() + "." + file.uuid() + ".%");
}
@Test
void getUuidPathAsList() {
ComponentDto root = new ComponentDto().setUuidPath(ComponentDto.UUID_PATH_OF_ROOT);
assertThat(root.getUuidPathAsList()).isEmpty();
ComponentDto nonRoot = new ComponentDto().setUuidPath(".12.34.56.");
assertThat(nonRoot.getUuidPathAsList()).containsExactly("12", "34", "56");
}
}
|