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.

MeasureImplTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.ce.task.projectanalysis.measure.Measure;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  25. public class MeasureImplTest {
  26. @Test
  27. public void get_int_value() {
  28. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1));
  29. assertThat(measure.getIntValue()).isEqualTo(1);
  30. }
  31. @Test
  32. public void fail_with_ISE_when_not_int_value() {
  33. assertThatThrownBy(() -> {
  34. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1));
  35. measure.getIntValue();
  36. })
  37. .isInstanceOf(IllegalStateException.class)
  38. .hasMessage("Value can not be converted to int because current value type is a DOUBLE");
  39. }
  40. @Test
  41. public void get_double_value() {
  42. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1));
  43. assertThat(measure.getDoubleValue()).isEqualTo(1d);
  44. }
  45. @Test
  46. public void fail_with_ISE_when_not_double_value() {
  47. assertThatThrownBy(() -> {
  48. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1));
  49. measure.getDoubleValue();
  50. })
  51. .isInstanceOf(IllegalStateException.class)
  52. .hasMessage("Value can not be converted to double because current value type is a INT");
  53. }
  54. @Test
  55. public void get_long_value() {
  56. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1L));
  57. assertThat(measure.getLongValue()).isEqualTo(1L);
  58. }
  59. @Test
  60. public void fail_with_ISE_when_not_long_value() {
  61. assertThatThrownBy(() -> {
  62. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create("value"));
  63. measure.getLongValue();
  64. })
  65. .isInstanceOf(IllegalStateException.class)
  66. .hasMessage("Value can not be converted to long because current value type is a STRING");
  67. }
  68. @Test
  69. public void get_string_value() {
  70. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create("value"));
  71. assertThat(measure.getStringValue()).isEqualTo("value");
  72. }
  73. @Test
  74. public void fail_with_ISE_when_not_string_value() {
  75. assertThatThrownBy(() -> {
  76. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1L));
  77. measure.getStringValue();
  78. })
  79. .isInstanceOf(IllegalStateException.class)
  80. .hasMessage("Value can not be converted to string because current value type is a LONG");
  81. }
  82. @Test
  83. public void get_boolean_value() {
  84. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(true));
  85. assertThat(measure.getBooleanValue()).isTrue();
  86. }
  87. @Test
  88. public void fail_with_ISE_when_not_boolean_value() {
  89. assertThatThrownBy(() -> {
  90. MeasureImpl measure = new MeasureImpl(Measure.newMeasureBuilder().create(1d, 1));
  91. measure.getBooleanValue();
  92. })
  93. .isInstanceOf(IllegalStateException.class)
  94. .hasMessage("Value can not be converted to boolean because current value type is a DOUBLE");
  95. }
  96. @Test
  97. public void fail_with_ISE_when_creating_measure_with_no_value() {
  98. assertThatThrownBy(() -> new MeasureImpl(Measure.newMeasureBuilder().createNoValue()))
  99. .isInstanceOf(IllegalStateException.class)
  100. .hasMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]");
  101. }
  102. @Test
  103. public void fail_with_ISE_when_creating_measure_with_not_allowed_value() {
  104. assertThatThrownBy(() -> new MeasureImpl(Measure.newMeasureBuilder().create(Measure.Level.ERROR)))
  105. .isInstanceOf(IllegalStateException.class)
  106. .hasMessage("Only following types are allowed [BOOLEAN, INT, LONG, DOUBLE, STRING]");
  107. }
  108. @Test
  109. public void fail_with_NPE_when_creating_measure_with_null_measure() {
  110. assertThatThrownBy(() -> new MeasureImpl(null))
  111. .isInstanceOf(NullPointerException.class)
  112. .hasMessage("Measure couldn't be null");
  113. }
  114. }