diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-01 13:54:09 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-09-02 14:28:19 +0200 |
commit | 65a791b7eab29403268ea9b3f9c9eea2850949be (patch) | |
tree | f1f32ff4b0c47f295d93d5462dd8890543b32de3 /sonar-plugin-api/src/test | |
parent | b707f2c2b6a5820496d5ba83ad6e69432512f908 (diff) | |
download | sonarqube-65a791b7eab29403268ea9b3f9c9eea2850949be.tar.gz sonarqube-65a791b7eab29403268ea9b3f9c9eea2850949be.zip |
SONAR-6730 Fix test implementation
Diffstat (limited to 'sonar-plugin-api/src/test')
6 files changed, 776 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java new file mode 100644 index 00000000000..139962b392b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestComponentTest.java @@ -0,0 +1,124 @@ +/* + * 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.api.ce.measure.test; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.ce.measure.Component; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestComponentTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_project() throws Exception { + TestComponent component = new TestComponent("Project", Component.Type.PROJECT, null); + + assertThat(component.getKey()).isEqualTo("Project"); + assertThat(component.getType()).isEqualTo(Component.Type.PROJECT); + } + + @Test + public void create_source_file() throws Exception { + TestComponent component = new TestComponent("File", Component.Type.FILE, new TestComponent.FileAttributesImpl("xoo", false)); + + assertThat(component.getType()).isEqualTo(Component.Type.FILE); + assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo("xoo"); + assertThat(component.getFileAttributes().isUnitTest()).isFalse(); + } + + @Test + public void create_test_file() throws Exception { + TestComponent component = new TestComponent("File", Component.Type.FILE, new TestComponent.FileAttributesImpl(null, true)); + + assertThat(component.getType()).isEqualTo(Component.Type.FILE); + assertThat(component.getFileAttributes().isUnitTest()).isTrue(); + assertThat(component.getFileAttributes().getLanguageKey()).isNull(); + } + + @Test + public void fail_with_ISE_when_calling_get_file_attributes_on_not_file() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Only component of type FILE have a FileAttributes object"); + + TestComponent component = new TestComponent("Project", Component.Type.PROJECT, null); + component.getFileAttributes(); + } + + @Test + public void fail_with_IAE_when_trying_to_create_a_file_without_file_attributes() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("omponent of type FILE must have a FileAttributes object"); + + new TestComponent("File", Component.Type.FILE, null); + } + + @Test + public void fail_with_IAE_when_trying_to_create_not_a_file_with_file_attributes() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Only component of type FILE have a FileAttributes object"); + + new TestComponent("Project", Component.Type.PROJECT, new TestComponent.FileAttributesImpl(null, true)); + } + + @Test + public void fail_with_NPE_when_creating_component_without_key() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Key cannot be null"); + + new TestComponent(null, Component.Type.PROJECT, null); + } + + @Test + public void fail_with_NPE_when_creating_component_without_type() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Type cannot be null"); + + new TestComponent("Project", null, null); + } + + @Test + public void test_equals_and_hashcode() throws Exception { + TestComponent component = new TestComponent("Project1", Component.Type.PROJECT, null); + TestComponent sameComponent = new TestComponent("Project1", Component.Type.PROJECT, null); + TestComponent anotherComponent = new TestComponent("Project2", Component.Type.PROJECT, null); + + assertThat(component).isEqualTo(component); + assertThat(component).isEqualTo(sameComponent); + assertThat(component).isNotEqualTo(anotherComponent); + assertThat(component).isNotEqualTo(null); + + assertThat(component.hashCode()).isEqualTo(component.hashCode()); + assertThat(component.hashCode()).isEqualTo(sameComponent.hashCode()); + assertThat(component.hashCode()).isNotEqualTo(anotherComponent.hashCode()); + } + + @Test + public void test_to_string() throws Exception { + assertThat(new TestComponent("File", Component.Type.FILE, new TestComponent.FileAttributesImpl("xoo", true)).toString()) + .isEqualTo("ComponentImpl{key=File, type='FILE', fileAttributes=FileAttributesImpl{languageKey='xoo', unitTest=true}}"); + } + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java new file mode 100644 index 00000000000..46966669c2b --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestIssueTest.java @@ -0,0 +1,195 @@ +/* + * 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.api.ce.measure.test; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.ce.measure.Issue; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.Severity; +import org.sonar.api.utils.Duration; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestIssueTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_issue() throws Exception { + Issue issue = new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .setDebt(Duration.create(10L)) + .build(); + + assertThat(issue.key()).isEqualTo("ABCD"); + assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("xoo", "S01")); + assertThat(issue.severity()).isEqualTo(Severity.BLOCKER); + assertThat(issue.status()).isEqualTo(org.sonar.api.issue.Issue.STATUS_RESOLVED); + assertThat(issue.resolution()).isEqualTo(org.sonar.api.issue.Issue.RESOLUTION_FIXED); + assertThat(issue.debt()).isEqualTo(Duration.create(10L)); + } + + @Test + public void create_issue_with_minimal_fields() throws Exception { + Issue issue = new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .build(); + + assertThat(issue.resolution()).isNull(); + assertThat(issue.debt()).isNull(); + } + + @Test + public void fail_with_NPE_when_building_issue_without_key() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("key cannot be null"); + + new TestIssue.Builder() + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } + + @Test + public void fail_with_NPE_when_creating_issue_with_null_key() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("key cannot be null"); + + new TestIssue.Builder().setKey(null); + } + + @Test + public void fail_with_NPE_when_building_issue_without_rule_key() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("ruleKey cannot be null"); + + new TestIssue.Builder() + .setKey("ABCD") + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } + + @Test + public void fail_with_NPE_when_creating_issue_with_null_rule_key() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("ruleKey cannot be null"); + + new TestIssue.Builder().setRuleKey(null); + } + + @Test + public void fail_with_IAE_when_building_issue_with_invalid_resolution() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("resolution 'unknown' is invalid"); + + new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution("unknown") + .build(); + } + + @Test + public void fail_with_NPE_when_building_issue_without_severity() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("severity cannot be null"); + + new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } + + @Test + public void fail_with_NPE_when_creating_issue_with_null_severity() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("severity cannot be null"); + + new TestIssue.Builder().setSeverity(null); + } + + @Test + public void fail_with_IAE_when_building_issue_with_invalid_severity() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("severity 'unknown' is invalid"); + + new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity("unknown") + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } + + @Test + public void fail_with_NPE_when_building_issue_without_status() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("status cannot be null"); + + new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } + + @Test + public void fail_with_NPE_when_creating_issue_with_null_status() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("status cannot be null"); + + new TestIssue.Builder().setStatus(null); + } + + @Test + public void fail_with_IAE_when_building_issue_with_invalid_status() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("status 'unknown' is invalid"); + + new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus("unknown") + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .build(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java new file mode 100644 index 00000000000..8ad7b6730b0 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerContextTest.java @@ -0,0 +1,205 @@ +/* + * 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.api.ce.measure.test; + +import java.util.Arrays; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.ce.measure.Component; +import org.sonar.api.ce.measure.Issue; +import org.sonar.api.ce.measure.Settings; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.Severity; +import org.sonar.api.utils.Duration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition; + +public class TestMeasureComputerContextTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + static final String INPUT_METRIC = "INPUT_METRIC"; + static final String OUTPUT_METRIC = "OUTPUT_METRIC"; + + final static Component PROJECT = new TestComponent("Project", Component.Type.PROJECT, null); + + final static MeasureComputerDefinition DEFINITION = new TestMeasureComputerDefinition.MeasureComputerDefinitionBuilderImpl() + .setInputMetrics(INPUT_METRIC) + .setOutputMetrics(OUTPUT_METRIC) + .build(); + + Settings settings = new TestSettings(); + + TestMeasureComputerContext underTest = new TestMeasureComputerContext(PROJECT, settings, DEFINITION); + + @Test + public void get_component() throws Exception { + assertThat(underTest.getComponent()).isEqualTo(PROJECT); + } + + @Test + public void get_settings() throws Exception { + assertThat(underTest.getSettings()).isEqualTo(settings); + } + + @Test + public void get_int_measure() throws Exception { + underTest.addInputMeasure(INPUT_METRIC, 10); + + assertThat(underTest.getMeasure(INPUT_METRIC).getIntValue()).isEqualTo(10); + } + + @Test + public void get_double_measure() throws Exception { + underTest.addInputMeasure(INPUT_METRIC, 10d); + + assertThat(underTest.getMeasure(INPUT_METRIC).getDoubleValue()).isEqualTo(10d); + } + + @Test + public void get_long_measure() throws Exception { + underTest.addInputMeasure(INPUT_METRIC, 10L); + + assertThat(underTest.getMeasure(INPUT_METRIC).getLongValue()).isEqualTo(10L); + } + + @Test + public void get_string_measure() throws Exception { + underTest.addInputMeasure(INPUT_METRIC, "text"); + + assertThat(underTest.getMeasure(INPUT_METRIC).getStringValue()).isEqualTo("text"); + } + + @Test + public void fail_with_IAE_when_trying_to_get_measure_on_unknown_metric() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Only metrics in [INPUT_METRIC] can be used to load measures"); + + underTest.getMeasure("unknown"); + } + + @Test + public void get_int_children_measures() throws Exception { + underTest.addChildrenMeasures(INPUT_METRIC, 10, 20); + + assertThat(underTest.getChildrenMeasures(INPUT_METRIC)).hasSize(2); + } + + @Test + public void get_doublet_children_measures() throws Exception { + underTest.addChildrenMeasures(INPUT_METRIC, 10d, 20d); + + assertThat(underTest.getChildrenMeasures(INPUT_METRIC)).hasSize(2); + } + + @Test + public void get_long_children_measures() throws Exception { + underTest.addChildrenMeasures(INPUT_METRIC, 10L, 20L); + + assertThat(underTest.getChildrenMeasures(INPUT_METRIC)).hasSize(2); + } + + @Test + public void get_string_children_measures() throws Exception { + underTest.addChildrenMeasures(INPUT_METRIC, "value1", "value2"); + + assertThat(underTest.getChildrenMeasures(INPUT_METRIC)).hasSize(2); + } + + @Test + public void fail_with_IAE_when_trying_to_get_children_measures_on_unknown_metric() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Only metrics in [INPUT_METRIC] can be used to load measures"); + + underTest.getChildrenMeasures("unknown"); + } + + @Test + public void add_int_measure() throws Exception { + underTest.addMeasure(OUTPUT_METRIC, 10); + + assertThat(underTest.getMeasure(OUTPUT_METRIC).getIntValue()).isEqualTo(10); + } + + @Test + public void add_double_measure() throws Exception { + underTest.addMeasure(OUTPUT_METRIC, 10d); + + assertThat(underTest.getMeasure(OUTPUT_METRIC).getDoubleValue()).isEqualTo(10d); + } + + @Test + public void add_long_measure() throws Exception { + underTest.addMeasure(OUTPUT_METRIC, 10L); + + assertThat(underTest.getMeasure(OUTPUT_METRIC).getLongValue()).isEqualTo(10L); + } + + @Test + public void add_string_measure() throws Exception { + underTest.addMeasure(OUTPUT_METRIC, "text"); + + assertThat(underTest.getMeasure(OUTPUT_METRIC).getStringValue()).isEqualTo("text"); + } + + @Test + public void fail_with_IAE_when_trying_to_add_measure_on_unknown_metric() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'unknown' is not allowed"); + + underTest.addMeasure("unknown", 10); + } + + @Test + public void fail_with_IAE_when_trying_to_add_measure_on_input_metric() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("Only metrics in [OUTPUT_METRIC] can be used to add measures. Metric 'INPUT_METRIC' is not allowed"); + + underTest.addMeasure(INPUT_METRIC, 10); + } + + @Test + public void fail_with_UOE_when_trying_to_add_same_measures_twice() throws Exception { + thrown.expect(UnsupportedOperationException.class); + thrown.expectMessage("A measure on metric 'OUTPUT_METRIC' already exists"); + + underTest.addMeasure(OUTPUT_METRIC, 10); + underTest.addMeasure(OUTPUT_METRIC, 20); + } + + @Test + public void get_issues() throws Exception { + Issue issue = new TestIssue.Builder() + .setKey("ABCD") + .setRuleKey(RuleKey.of("xoo", "S01")) + .setSeverity(Severity.BLOCKER) + .setStatus(org.sonar.api.issue.Issue.STATUS_RESOLVED) + .setResolution(org.sonar.api.issue.Issue.RESOLUTION_FIXED) + .setDebt(Duration.create(10L)) + .build(); + underTest.setIssues(Arrays.asList(issue)); + + assertThat(underTest.getIssues()).hasSize(1); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java new file mode 100644 index 00000000000..6a34b47c6f1 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureComputerDefinitionTest.java @@ -0,0 +1,121 @@ +/* + * 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.api.ce.measure.test; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.ce.measure.MeasureComputer.MeasureComputerDefinition; +import org.sonar.api.ce.measure.test.TestMeasureComputerDefinition.MeasureComputerDefinitionBuilderImpl; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestMeasureComputerDefinitionTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void build_definition() throws Exception { + MeasureComputerDefinition definition = new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", "INPUT_2") + .setOutputMetrics("OUTPUT_1", "OUTPUT_2") + .build(); + + assertThat(definition.getInputMetrics()).containsOnly("INPUT_1", "INPUT_2"); + assertThat(definition.getOutputMetrics()).containsOnly("OUTPUT_1", "OUTPUT_2"); + } + + @Test + public void build_definition_without_input_metric() throws Exception { + MeasureComputerDefinition definition = new MeasureComputerDefinitionBuilderImpl() + .setOutputMetrics("OUTPUT_1", "OUTPUT_2") + .build(); + + assertThat(definition.getInputMetrics()).isEmpty(); + assertThat(definition.getOutputMetrics()).containsOnly("OUTPUT_1", "OUTPUT_2"); + } + + @Test + public void fail_with_NPE_when_building_definition_with_null_input_metrics() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Input metrics cannot be null"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics(null) + .setOutputMetrics("OUTPUT_1", "OUTPUT_2") + .build(); + } + + @Test + public void fail_with_NPE_when_building_definition_with_on_null_input_metric() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Null metric is not allowed"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", null) + .setOutputMetrics("OUTPUT_1", "OUTPUT_2") + .build(); + } + + @Test + public void fail_with_NPE_when_building_definition_with_null_output_metrics() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Output metrics cannot be null"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", "INPUT_2") + .setOutputMetrics(null) + .build(); + } + + @Test + public void fail_with_NPE_when_building_definition_without_output_metrics() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Output metrics cannot be null"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", "INPUT_2") + .build(); + } + + @Test + public void fail_with_NPE_when_building_definition_with_on_null_ouput_metric() throws Exception { + thrown.expect(NullPointerException.class); + thrown.expectMessage("Null metric is not allowed"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", "INPUT_2") + .setOutputMetrics("OUTPUT_1", null) + .build(); + } + + @Test + public void fail_with_IAE_when_building_definition_with_empty_output_metrics() throws Exception { + thrown.expect(IllegalArgumentException.class); + thrown.expectMessage("At least one output metric must be defined"); + + new MeasureComputerDefinitionBuilderImpl() + .setInputMetrics("INPUT_1", "INPUT_2") + .setOutputMetrics() + .build(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.java new file mode 100644 index 00000000000..fb54b4ba8f1 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestMeasureTest.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.api.ce.measure.test; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestMeasureTest { + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void create_double_measure() throws Exception { + assertThat(TestMeasure.createMeasure(10d).getDoubleValue()).isEqualTo(10d); + } + + @Test + public void create_int_measure() throws Exception { + assertThat(TestMeasure.createMeasure(10).getIntValue()).isEqualTo(10); + } + + @Test + public void create_long_measure() throws Exception { + assertThat(TestMeasure.createMeasure(10L).getLongValue()).isEqualTo(10L); + } + + @Test + public void create_string_measure() throws Exception { + assertThat(TestMeasure.createMeasure("value").getStringValue()).isEqualTo("value"); + } + + @Test + public void getDoubleValue_fails_with_ISE_when_not_a_double() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Not a double measure"); + + TestMeasure.createMeasure(10).getDoubleValue(); + } + + @Test + public void getIntValue_fails_with_ISE_when_not_an_int() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Not an integer measure"); + + TestMeasure.createMeasure(10L).getIntValue(); + } + + @Test + public void getLongValue_fails_with_ISE_when_not_a_long() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Not a long measure"); + + TestMeasure.createMeasure(10).getLongValue(); + } + + @Test + public void getStringValue_fails_with_ISE_when_not_a_string() throws Exception { + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Not a string measure"); + + TestMeasure.createMeasure(10).getStringValue(); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestSettingsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestSettingsTest.java new file mode 100644 index 00000000000..911be29777f --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/ce/measure/test/TestSettingsTest.java @@ -0,0 +1,46 @@ +/* + * 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.api.ce.measure.test; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestSettingsTest { + + TestSettings underTest = new TestSettings(); + + @Test + public void get_string_value() throws Exception { + underTest.setValue("key", "value"); + + assertThat(underTest.getString("key")).isEqualTo("value"); + assertThat(underTest.getString("unknown")).isNull(); + } + + @Test + public void get_string_array_value() throws Exception { + underTest.setValue("key", "value1,value2"); + + assertThat(underTest.getStringArray("key")).containsOnly("value1", "value2"); + assertThat(underTest.getStringArray("unknown")).isEmpty(); + } +} |