aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test/java/org/sonar/db/component/ComponentDtoTest.java
blob: d8e9de2e54b102442d4b1a525a90aee7311fcf15 (plain)
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
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact 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.Test;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.resources.Scopes;

import static org.assertj.core.api.Assertions.assertThat;

public class ComponentDtoTest {

  @Test
  public void setters_and_getters() {
    ComponentDto componentDto = new ComponentDto()
      .setId(1L)
      .setKey("org.struts:struts-core:src/org/struts/RequestContext.java")
      .setDeprecatedKey("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")
      .setRootUuid("uuid_3")
      .setDeveloperUuid("uuid_6")
      .setAuthorizationUpdatedAt(123456789L);

    assertThat(componentDto.getId()).isEqualTo(1L);
    assertThat(componentDto.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
    assertThat(componentDto.deprecatedKey()).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.getRootUuid()).isEqualTo("uuid_3");
    assertThat(componentDto.getCopyResourceUuid()).isEqualTo("uuid_5");
    assertThat(componentDto.getDeveloperUuid()).isEqualTo("uuid_6");
    assertThat(componentDto.getAuthorizationUpdatedAt()).isEqualTo(123456789L);
  }

  @Test
  public 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);
    assertThat(dto).isEqualTo(dtoWithSameUuid);
    assertThat(dto).isNotEqualTo(dtoWithDifferentUuid);

    assertThat(dto.hashCode()).isEqualTo(dto.hashCode());
    assertThat(dto.hashCode()).isEqualTo(dtoWithSameUuid.hashCode());
    assertThat(dto.hashCode()).isNotEqualTo(dtoWithDifferentUuid.hashCode());
  }

  @Test
  public void toString_does_not_fail_if_empty() {
    ComponentDto dto = new ComponentDto();
    assertThat(dto.toString()).isNotEmpty();
  }

  @Test
  public void is_root_project() {
    assertThat(new ComponentDto().setModuleUuid("ABCD").isRootProject()).isFalse();
    assertThat(new ComponentDto().setModuleUuid("ABCD").setScope(Scopes.DIRECTORY).isRootProject()).isFalse();
    assertThat(new ComponentDto().setModuleUuid(null).setScope(Scopes.PROJECT).setQualifier(Qualifiers.PROJECT).isRootProject()).isTrue();
  }

  @Test
  public void test_formatUuidPathFromParent() {
    ComponentDto parent = ComponentTesting.newProjectDto("123").setUuidPath(ComponentDto.UUID_PATH_OF_ROOT);
    assertThat(ComponentDto.formatUuidPathFromParent(parent)).isEqualTo(".123.");
  }

  @Test
  public void test_Name() {
    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");
  }
}