diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-04-02 12:10:09 +0200 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-04-02 12:10:09 +0200 |
commit | 3cc4c6560d6839656bdc80346ab2bdd5e3e7406e (patch) | |
tree | f706551c5ec800e9387e65dc26e5f8a810ed726e | |
parent | e6d39ea35a03bb60abdad7e31b427c6763ab0d9d (diff) | |
parent | 21fd3dd8ad5bb8868edc53c9b53bf30be8b2d748 (diff) | |
download | sonarqube-3cc4c6560d6839656bdc80346ab2bdd5e3e7406e.tar.gz sonarqube-3cc4c6560d6839656bdc80346ab2bdd5e3e7406e.zip |
Merge branch 'current'
6 files changed, 114 insertions, 28 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/dashboards/TimeMachineDashboard.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/dashboards/TimeMachineDashboard.java index e57df8edb6f..257e17abe8f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/dashboards/TimeMachineDashboard.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/dashboards/TimeMachineDashboard.java @@ -31,8 +31,6 @@ import org.sonar.api.web.DashboardTemplate; */ public final class TimeMachineDashboard extends DashboardTemplate { - private static final String TIME_MACHINE_ID = "time_machine"; - private static final String DISPLAY_SPARK_LINE = "displaySparkLine"; private static final String METRIC1 = "metric1"; private static final String METRIC2 = "metric2"; private static final String METRIC3 = "metric3"; @@ -60,9 +58,8 @@ public final class TimeMachineDashboard extends DashboardTemplate { timelineWidget.setProperty(METRIC1, "complexity"); timelineWidget.setProperty(METRIC2, "violations_density"); timelineWidget.setProperty(METRIC3, "coverage"); - - Widget sizeTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 1); - sizeTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + + Widget sizeTimeMachineWidget = addTimeMachineWidgetOnFirstColumn(dashboard); sizeTimeMachineWidget.setProperty(METRIC1, "ncloc"); sizeTimeMachineWidget.setProperty(METRIC2, "lines"); sizeTimeMachineWidget.setProperty(METRIC3, "statements"); @@ -70,16 +67,14 @@ public final class TimeMachineDashboard extends DashboardTemplate { sizeTimeMachineWidget.setProperty(METRIC5, "classes"); sizeTimeMachineWidget.setProperty(METRIC6, "functions"); sizeTimeMachineWidget.setProperty(METRIC7, "accessors"); - - Widget commentsTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 1); - commentsTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + + Widget commentsTimeMachineWidget = addTimeMachineWidgetOnFirstColumn(dashboard); commentsTimeMachineWidget.setProperty(METRIC1, "comment_lines_density"); commentsTimeMachineWidget.setProperty(METRIC2, "comment_lines"); commentsTimeMachineWidget.setProperty(METRIC3, "public_documented_api_density"); commentsTimeMachineWidget.setProperty(METRIC4, "public_undocumented_api"); - - Widget duplicationTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 1); - duplicationTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + + Widget duplicationTimeMachineWidget = addTimeMachineWidgetOnFirstColumn(dashboard); duplicationTimeMachineWidget.setProperty(METRIC1, "duplicated_lines_density"); duplicationTimeMachineWidget.setProperty(METRIC2, "duplicated_lines"); duplicationTimeMachineWidget.setProperty(METRIC3, "duplicated_blocks"); @@ -87,8 +82,7 @@ public final class TimeMachineDashboard extends DashboardTemplate { } private void addSecondColumn(Dashboard dashboard) { - Widget rulesTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 2); - rulesTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + Widget rulesTimeMachineWidget = addTimeMachineWidgetOnSecondColumn(dashboard); rulesTimeMachineWidget.setProperty(METRIC1, "violations"); rulesTimeMachineWidget.setProperty(METRIC2, "violation_density"); rulesTimeMachineWidget.setProperty(METRIC3, "blocker_violations"); @@ -98,15 +92,13 @@ public final class TimeMachineDashboard extends DashboardTemplate { rulesTimeMachineWidget.setProperty(METRIC7, "info_violations"); rulesTimeMachineWidget.setProperty(METRIC7, "weighted_violations"); - Widget complexityTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 2); - complexityTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + Widget complexityTimeMachineWidget = addTimeMachineWidgetOnSecondColumn(dashboard); complexityTimeMachineWidget.setProperty(METRIC1, "complexity"); complexityTimeMachineWidget.setProperty(METRIC2, "function_complexity"); complexityTimeMachineWidget.setProperty(METRIC3, "class_complexity"); complexityTimeMachineWidget.setProperty(METRIC4, "file_complexity"); - Widget testsTimeMachineWidget = dashboard.addWidget(TIME_MACHINE_ID, 2); - testsTimeMachineWidget.setProperty(DISPLAY_SPARK_LINE, "true"); + Widget testsTimeMachineWidget = addTimeMachineWidgetOnSecondColumn(dashboard); testsTimeMachineWidget.setProperty(METRIC1, "coverage"); testsTimeMachineWidget.setProperty(METRIC2, "line_coverage"); testsTimeMachineWidget.setProperty(METRIC3, "branch_coverage"); @@ -117,4 +109,18 @@ public final class TimeMachineDashboard extends DashboardTemplate { testsTimeMachineWidget.setProperty(METRIC7, "test_execution_time"); } -}
\ No newline at end of file + private Widget addTimeMachineWidgetOnFirstColumn(Dashboard dashboard) { + return addTimeMachineWidget(dashboard, 1); + } + + private Widget addTimeMachineWidgetOnSecondColumn(Dashboard dashboard) { + return addTimeMachineWidget(dashboard, 2); + } + + private Widget addTimeMachineWidget(Dashboard dashboard, int columnIndex) { + Widget widget = dashboard.addWidget("time_machine", columnIndex); + widget.setProperty("displaySparkLine", "true"); + return widget; + } + +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/dashboards/TimeMachineDashboardTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/dashboards/TimeMachineDashboardTest.java new file mode 100644 index 00000000000..59463ce21f3 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/dashboards/TimeMachineDashboardTest.java @@ -0,0 +1,47 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.plugins.core.dashboards; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.Collection; + +import org.junit.Test; +import org.sonar.api.web.Dashboard; +import org.sonar.api.web.Dashboard.Widget; +import org.sonar.api.web.DashboardLayout; + +public class TimeMachineDashboardTest { + @Test + public void shouldCreateDashboard() { + TimeMachineDashboard template = new TimeMachineDashboard(); + Dashboard hotspots = template.createDashboard(); + assertThat(template.getName(), is("TimeMachine")); + assertThat(hotspots.getLayout(), is(DashboardLayout.TWO_COLUMNS)); + Collection<Widget> widgets = hotspots.getWidgets(); + assertThat(widgets.size(), is(7)); + for (Widget widget : widgets) { + if (widget.getId().equals("time_machine")) { + assertThat(widget.getProperty("displaySparkLine"), is("true")); + } + } + } +} diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java index 3adb55000fd..2845c2ac94d 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/plugins/squid/JavaSourceImporter.java @@ -79,15 +79,24 @@ public final class JavaSourceImporter implements Sensor { } void importSource(SensorContext context, JavaFile javaFile, InputFile inputFile, Charset sourcesEncoding) { + String source = null; + if (importSources) { + source = loadSourceFromFile(inputFile, sourcesEncoding); + } + try { context.index(javaFile); - - if (importSources) { - String source = FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name()); + if (source != null) { context.saveSource(javaFile, source); } } catch (SonarException e) { - throw e; + throw new SonarException(e.getMessage() + ", on file: " + inputFile.getFile().getAbsolutePath(), e); + } + } + + protected String loadSourceFromFile(InputFile inputFile, Charset sourcesEncoding) { + try { + return FileUtils.readFileToString(inputFile.getFile(), sourcesEncoding.name()); } catch (Exception e) { throw new SonarException("Unable to read and import the source file : '" + inputFile.getFile().getAbsolutePath() + "' with the charset : '" + sourcesEncoding.name() + "'.", e); diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java index 9727e5e525e..0cd45d4e473 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/plugins/squid/JavaSourceImporterTest.java @@ -97,6 +97,8 @@ public class JavaSourceImporterTest { thrown.expect(SonarException.class); thrown.expectMessage("Duplicate source for resource"); + thrown.expectMessage(", on file:"); + thrown.expectMessage("UndocumentedApi.java"); importer.importSource(context, javaFile, inputFile, Charset.defaultCharset()); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java index b3779e84b70..35a10a679f7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/DefaultProfileLoader.java @@ -22,6 +22,7 @@ package org.sonar.batch; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; import org.sonar.api.rules.ActiveRule; +import org.sonar.api.utils.SonarException; import org.sonar.jpa.dao.ProfilesDao; public class DefaultProfileLoader implements ProfileLoader { @@ -40,13 +41,16 @@ public class DefaultProfileLoader implements ProfileLoader { Project root = project.getRoot(); profile = dao.getActiveProfile(root.getLanguageKey(), root.getKey()); if (profile == null) { - throw new RuntimeException("Quality profile not found for " + root.getKey() + ", language " + root.getLanguageKey()); + // This means that the current language is not supported by any installed plugin, otherwise at least a + // "Default <Language Name>" profile would have been created by ActivateDefaultProfiles class. + throw new SonarException("You must intall a Sonar plugin that supports language '" + root.getLanguageKey() + + "' in order to analyse the following project: " + root.getKey()); } } else { profile = dao.getProfile(project.getLanguageKey(), profileName); if (profile == null) { - throw new RuntimeException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey()); + throw new SonarException("Quality profile not found : " + profileName + ", language " + project.getLanguageKey()); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java index 5ddfa9f151b..735c21c7130 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/DefaultProfileLoaderTest.java @@ -28,21 +28,24 @@ import static org.mockito.Mockito.when; import java.util.Collections; import java.util.HashMap; -import org.sonar.batch.DefaultProfileLoader; - import org.apache.commons.configuration.MapConfiguration; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.profiles.Alert; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; import org.sonar.api.rules.ActiveRule; -import org.sonar.batch.ProfileLoader; +import org.sonar.api.utils.SonarException; import org.sonar.jpa.dao.ProfilesDao; public class DefaultProfileLoaderTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private ProfilesDao dao; private ProfileLoader loader; @@ -88,7 +91,7 @@ public class DefaultProfileLoaderTest { verify(dao, never()).getActiveProfile(Java.KEY, "project"); } - @Test(expected = RuntimeException.class) + @Test public void shouldFailIfProfileIsNotFound() { Project project = new Project("project").setLanguageKey(Java.KEY); @@ -98,6 +101,21 @@ public class DefaultProfileLoaderTest { when(dao.getProfile(Java.KEY, "profile1")).thenReturn(null); + thrown.expect(SonarException.class); + thrown.expectMessage("Quality profile not found : unknown, language java"); + loader.load(project); + } + + /** + * SONAR-3125 + */ + @Test + public void shouldGiveExplicitMessageIfNoProfileFound() { + Project project = new Project("foo:project").setLanguageKey("unknown-language"); + when(dao.getProfile(Java.KEY, "profile1")).thenReturn(null); + + thrown.expect(SonarException.class); + thrown.expectMessage("You must intall a Sonar plugin that supports language 'unknown-language' in order to analyse the following project: foo:project"); loader.load(project); } |