From 75e684f8b1fbbe6112a31d93e14dcab68130014b Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Tue, 1 Jul 2014 18:05:33 +0200 Subject: [PATCH] Fix quality flaws --- .../sonar/core/measure/db/MeasureDtoTest.java | 85 +++++++++++++++++++ .../sonar/core/measure/db/MeasureKeyTest.java | 72 ++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 sonar-core/src/test/java/org/sonar/core/measure/db/MeasureDtoTest.java create mode 100644 sonar-core/src/test/java/org/sonar/core/measure/db/MeasureKeyTest.java diff --git a/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureDtoTest.java b/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureDtoTest.java new file mode 100644 index 00000000000..64171ee337a --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureDtoTest.java @@ -0,0 +1,85 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.core.measure.db; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; + +public class MeasureDtoTest { + + @Test + public void test_getter_and_setter() throws Exception { + MeasureDto measureDto = MeasureDto.createFor(MeasureKey.of("component", "metric")) + .setId(10) + .setValue(2d) + .setTextValue("text value") + .setData(new byte[]{}) + .setVariation(1, 1d) + .setVariation(2, 2d) + .setVariation(3, 3d) + .setVariation(4, 4d) + .setVariation(5, 5d); + + assertThat(measureDto.getId()).isEqualTo(10); + assertThat(measureDto.getValue()).isEqualTo(2d); + assertThat(measureDto.getData()).isNotNull(); + assertThat(measureDto.getVariation(1)).isEqualTo(1d); + assertThat(measureDto.getVariation(2)).isEqualTo(2d); + assertThat(measureDto.getVariation(3)).isEqualTo(3d); + assertThat(measureDto.getVariation(4)).isEqualTo(4d); + assertThat(measureDto.getVariation(5)).isEqualTo(5d); + } + + @Test + public void test_data() throws Exception { + assertThat(MeasureDto.createFor(MeasureKey.of("component", "metric")) + .setTextValue("text value") + .setData(null).getData()).isEqualTo("text value"); + + assertThat(MeasureDto.createFor(MeasureKey.of("component", "metric")) + .setTextValue(null) + .setData(new byte[]{}).getData()).isNotNull(); + } + + @Test + public void fail_to_set_out_of_bounds_variation() throws Exception { + try { + MeasureDto.createFor(MeasureKey.of("component", "metric")) + .setVariation(6, 1d); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IndexOutOfBoundsException.class); + } + } + + @Test + public void fail_to_get_out_of_bounds_variation() throws Exception { + try { + MeasureDto.createFor(MeasureKey.of("component", "metric")) + .getVariation(6); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(IndexOutOfBoundsException.class); + } + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureKeyTest.java b/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureKeyTest.java new file mode 100644 index 00000000000..fcc643b17a6 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/measure/db/MeasureKeyTest.java @@ -0,0 +1,72 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.core.measure.db; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.fest.assertions.Fail.fail; + +public class MeasureKeyTest { + + @Test + public void create_key() throws Exception { + MeasureKey key = MeasureKey.of("sample", "ncloc"); + assertThat(key.componentKey()).isEqualTo("sample"); + assertThat(key.metricKey()).isEqualTo("ncloc"); + } + + @Test + public void component_key_must_not_be_null() throws Exception { + try { + MeasureKey.of(null, "ncloc"); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Component key must be set"); + } + } + + @Test + public void metric_key_must_not_be_null() throws Exception { + try { + MeasureKey.of("sample", null); + fail(); + } catch (IllegalArgumentException e) { + assertThat(e).hasMessage("Metric key must be set"); + } + } + + @Test + public void test_equals_and_hashcode() throws Exception { + MeasureKey key1 = MeasureKey.of("sample", "ncloc"); + MeasureKey key2 = MeasureKey.of("sample", "ncloc"); + MeasureKey key3 = MeasureKey.of("sample", "coverage"); + MeasureKey key4 = MeasureKey.of("sample2", "coverage"); + + assertThat(key1).isEqualTo(key1); + assertThat(key1).isEqualTo(key2); + assertThat(key1).isNotEqualTo(key3); + assertThat(key3).isNotEqualTo(key4); + assertThat(key1).isNotEqualTo(null); + assertThat(key1.hashCode()).isEqualTo(key1.hashCode()); + assertThat(key1.hashCode()).isEqualTo(key2.hashCode()); + } +} -- 2.39.5