You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentImplTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.task.projectanalysis.api.measurecomputer;
  21. import org.junit.Test;
  22. import org.sonar.api.ce.measure.Component;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  25. public class ComponentImplTest {
  26. @Test
  27. public void create_project() {
  28. ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null);
  29. assertThat(component.getKey()).isEqualTo("Project");
  30. assertThat(component.getType()).isEqualTo(Component.Type.PROJECT);
  31. }
  32. @Test
  33. public void create_source_file() {
  34. ComponentImpl component = new ComponentImpl("File", Component.Type.FILE, new ComponentImpl.FileAttributesImpl("xoo", false));
  35. assertThat(component.getType()).isEqualTo(Component.Type.FILE);
  36. assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo("xoo");
  37. assertThat(component.getFileAttributes().isUnitTest()).isFalse();
  38. }
  39. @Test
  40. public void create_test_file() {
  41. ComponentImpl component = new ComponentImpl("File", Component.Type.FILE, new ComponentImpl.FileAttributesImpl(null, true));
  42. assertThat(component.getType()).isEqualTo(Component.Type.FILE);
  43. assertThat(component.getFileAttributes().isUnitTest()).isTrue();
  44. assertThat(component.getFileAttributes().getLanguageKey()).isNull();
  45. }
  46. @Test
  47. public void fail_with_ISE_when_calling_get_file_attributes_on_not_file() {
  48. assertThatThrownBy(() -> {
  49. ComponentImpl component = new ComponentImpl("Project", Component.Type.PROJECT, null);
  50. component.getFileAttributes();
  51. })
  52. .isInstanceOf(IllegalStateException.class)
  53. .hasMessage("Only component of type FILE have a FileAttributes object");
  54. }
  55. @Test
  56. public void fail_with_IAE_when_trying_to_create_a_file_without_file_attributes() {
  57. assertThatThrownBy(() -> new ComponentImpl("File", Component.Type.FILE, null))
  58. .isInstanceOf(IllegalArgumentException.class)
  59. .hasMessageContaining("omponent of type FILE must have a FileAttributes object");
  60. }
  61. @Test
  62. public void fail_with_IAE_when_trying_to_create_not_a_file_with_file_attributes() {
  63. assertThatThrownBy(() -> new ComponentImpl("Project", Component.Type.PROJECT, new ComponentImpl.FileAttributesImpl(null, true)))
  64. .isInstanceOf(IllegalArgumentException.class)
  65. .hasMessage("Only component of type FILE have a FileAttributes object");
  66. }
  67. @Test
  68. public void fail_with_NPE_when_creating_component_without_key() {
  69. assertThatThrownBy(() -> new ComponentImpl(null, Component.Type.PROJECT, null))
  70. .isInstanceOf(NullPointerException.class)
  71. .hasMessage("Key cannot be null");
  72. }
  73. @Test
  74. public void fail_with_NPE_when_creating_component_without_type() {
  75. assertThatThrownBy(() -> new ComponentImpl("Project", null, null))
  76. .isInstanceOf(NullPointerException.class)
  77. .hasMessage("Type cannot be null");
  78. }
  79. @Test
  80. public void test_equals_and_hashcode() {
  81. ComponentImpl component = new ComponentImpl("Project1", Component.Type.PROJECT, null);
  82. ComponentImpl sameComponent = new ComponentImpl("Project1", Component.Type.PROJECT, null);
  83. ComponentImpl anotherComponent = new ComponentImpl("Project2", Component.Type.PROJECT, null);
  84. assertThat(component).isEqualTo(component);
  85. assertThat(component).isEqualTo(sameComponent);
  86. assertThat(component).isNotEqualTo(anotherComponent);
  87. assertThat(component).isNotEqualTo(null);
  88. assertThat(component.hashCode()).isEqualTo(component.hashCode());
  89. assertThat(component.hashCode()).isEqualTo(sameComponent.hashCode());
  90. assertThat(component.hashCode()).isNotEqualTo(anotherComponent.hashCode());
  91. }
  92. @Test
  93. public void test_to_string() {
  94. assertThat(new ComponentImpl("File", Component.Type.FILE, new ComponentImpl.FileAttributesImpl("xoo", true)).toString())
  95. .isEqualTo("ComponentImpl{key=File, type='FILE', fileAttributes=FileAttributesImpl{languageKey='xoo', unitTest=true}}");
  96. }
  97. }