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.

MetricImplTest.java 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.metric;
  21. import org.junit.Rule;
  22. import org.junit.Test;
  23. import org.junit.rules.ExpectedException;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. public class MetricImplTest {
  26. @Rule
  27. public ExpectedException expectedException = ExpectedException.none();
  28. private static final int SOME_ID = 42;
  29. private static final String SOME_KEY = "key";
  30. private static final String SOME_NAME = "name";
  31. @Test(expected = NullPointerException.class)
  32. public void constructor_throws_NPE_if_key_arg_is_null() {
  33. new MetricImpl(SOME_ID, null, SOME_NAME, Metric.MetricType.BOOL);
  34. }
  35. @Test(expected = NullPointerException.class)
  36. public void constructor_throws_NPE_if_name_arg_is_null() {
  37. new MetricImpl(SOME_ID, SOME_KEY, null, Metric.MetricType.BOOL);
  38. }
  39. @Test(expected = NullPointerException.class)
  40. public void constructor_throws_NPE_if_valueType_arg_is_null() {
  41. new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, null);
  42. }
  43. @Test
  44. public void constructor_throws_IAE_if_bestValueOptimized_is_true_but_bestValue_is_null() {
  45. expectedException.expect(IllegalArgumentException.class);
  46. expectedException.expectMessage("A BestValue must be specified if Metric is bestValueOptimized");
  47. new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.INT, 1, null, true, false);
  48. }
  49. @Test
  50. public void verify_getters() {
  51. MetricImpl metric = new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.FLOAT);
  52. assertThat(metric.getId()).isEqualTo(SOME_ID);
  53. assertThat(metric.getKey()).isEqualTo(SOME_KEY);
  54. assertThat(metric.getName()).isEqualTo(SOME_NAME);
  55. assertThat(metric.getType()).isEqualTo(Metric.MetricType.FLOAT);
  56. }
  57. @Test
  58. public void equals_uses_only_key() {
  59. MetricImpl expected = new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.FLOAT);
  60. assertThat(new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.FLOAT)).isEqualTo(expected);
  61. assertThat(new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.STRING)).isEqualTo(expected);
  62. assertThat(new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.STRING, null, 0d, true, true)).isEqualTo(expected);
  63. assertThat(new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.STRING, null, null, false, false)).isEqualTo(expected);
  64. assertThat(new MetricImpl(SOME_ID, "some other key", SOME_NAME, Metric.MetricType.FLOAT)).isNotEqualTo(expected);
  65. }
  66. @Test
  67. public void hashcode_uses_only_key() {
  68. int expected = new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.FLOAT).hashCode();
  69. assertThat(new MetricImpl(SOME_ID, SOME_KEY, "some other name", Metric.MetricType.FLOAT).hashCode()).isEqualTo(expected);
  70. assertThat(new MetricImpl(SOME_ID, SOME_KEY, "some other name", Metric.MetricType.BOOL).hashCode()).isEqualTo(expected);
  71. }
  72. @Test
  73. public void all_fields_are_displayed_in_toString() {
  74. assertThat(new MetricImpl(SOME_ID, SOME_KEY, SOME_NAME, Metric.MetricType.FLOAT, 1, 951d, true, false).toString())
  75. .isEqualTo("MetricImpl{id=42, key=key, name=name, type=FLOAT, bestValue=951.0, bestValueOptimized=true, deleteHistoricalData=false}");
  76. }
  77. }