From 6641643ee94e8c7393af31bbaa3f29bda4abefa9 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 16 Dec 2013 16:39:57 +0100 Subject: [PATCH] Fix quality flaws --- .../exceptions/BadRequestException.java | 2 +- .../org/sonar/server/platform/Platform.java | 2 - .../qualityprofile/QProfileOperations.java | 14 ++-- .../qualityprofile/RubyQProfilesService.java | 50 --------------- .../RubyQProfilesServiceTest.java | 64 ------------------- 5 files changed, 9 insertions(+), 123 deletions(-) delete mode 100644 sonar-server/src/main/java/org/sonar/server/qualityprofile/RubyQProfilesService.java delete mode 100644 sonar-server/src/test/java/org/sonar/server/qualityprofile/RubyQProfilesServiceTest.java diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java index 0487470eb6f..ee792637e5c 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java @@ -101,7 +101,7 @@ public class BadRequestException extends ServerException { @CheckForNull public Object[] l10nParams() { - return l10nParams; + return Arrays.copyOf(l10nParams, l10nParams.length); } @Override diff --git a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java index e5d27bd2efd..64d996599a6 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/Platform.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/Platform.java @@ -93,7 +93,6 @@ import org.sonar.server.plugins.*; import org.sonar.server.qualityprofile.QProfileOperations; import org.sonar.server.qualityprofile.QProfileSearch; import org.sonar.server.qualityprofile.QProfiles; -import org.sonar.server.qualityprofile.RubyQProfilesService; import org.sonar.server.rule.RubyRuleService; import org.sonar.server.rule.RuleRegistry; import org.sonar.server.rules.ProfilesConsole; @@ -273,7 +272,6 @@ public final class Platform { servicesContainer.addSingleton(QProfiles.class); servicesContainer.addSingleton(QProfileSearch.class); servicesContainer.addSingleton(QProfileOperations.class); - servicesContainer.addSingleton(RubyQProfilesService.class); // users servicesContainer.addSingleton(HibernateUserFinder.class); diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java index c7f6af0355b..b790aa172cb 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileOperations.java @@ -58,6 +58,7 @@ import static com.google.common.collect.Lists.newArrayList; public class QProfileOperations implements ServerComponent { private static final String PROPERTY_PREFIX = "sonar.profile."; + private static final String QUALITY_PROFILES_ALREADY_EXISTS = "quality_profiles.already_exists"; private final MyBatis myBatis; private final QualityProfileDao dao; @@ -200,7 +201,7 @@ public class QProfileOperations implements ServerComponent { validateName(name); Validation.checkMandatoryParameter(language, "language"); if (find(name, language) != null) { - throw BadRequestException.ofL10n("quality_profiles.already_exists"); + throw BadRequestException.ofL10n(QUALITY_PROFILES_ALREADY_EXISTS); } } @@ -209,7 +210,7 @@ public class QProfileOperations implements ServerComponent { validateName(newName); QualityProfileDto profileDto = findNeverNull(profileId); if (!profileDto.getName().equals(newName) && find(newName, profileDto.getLanguage()) != null) { - throw BadRequestException.ofL10n("quality_profiles.already_exists"); + throw BadRequestException.ofL10n(QUALITY_PROFILES_ALREADY_EXISTS); } return profileDto; } @@ -221,14 +222,15 @@ public class QProfileOperations implements ServerComponent { private QualityProfileDto findNeverNull(Integer id) { QualityProfileDto qualityProfile = find(id); - if (qualityProfile == null) { - throw new NotFoundException("This quality profile does not exists."); - } - return qualityProfile; + return checkNotNull((qualityProfile)); } private QualityProfileDto findNeverNull(String name, String language) { QualityProfileDto qualityProfile = find(name, language); + return checkNotNull(qualityProfile); + } + + private QualityProfileDto checkNotNull(QualityProfileDto qualityProfile) { if (qualityProfile == null) { throw new NotFoundException("This quality profile does not exists."); } diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RubyQProfilesService.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RubyQProfilesService.java deleted file mode 100644 index 37ec46b2cf3..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RubyQProfilesService.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.server.qualityprofile; - -import org.sonar.api.ServerComponent; -import org.sonar.server.util.Validation; - -import java.util.List; -import java.util.Map; - -public class RubyQProfilesService implements ServerComponent { - - private final QProfiles qProfiles; - - public RubyQProfilesService(QProfiles qProfiles) { - this.qProfiles = qProfiles; - } - - public List searchProfiles() { - return qProfiles.searchProfiles(); - } - - public void newProfile(Map params) { - String name = (String) params.get("name"); - String language = (String) params.get("language"); -// RubyUtils.toStrings() - - Validation.checkMandatoryParameter(name, "name"); - Validation.checkMandatoryParameter(language, "language"); -// qProfiles.newProfile(name, language, UserSession.get()); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RubyQProfilesServiceTest.java b/sonar-server/src/test/java/org/sonar/server/qualityprofile/RubyQProfilesServiceTest.java deleted file mode 100644 index 0593ebadff3..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/qualityprofile/RubyQProfilesServiceTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 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.server.qualityprofile; - -import com.google.common.collect.ImmutableMap; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.runners.MockitoJUnitRunner; - -import static com.google.common.collect.Lists.newArrayList; -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.when; - -@RunWith(MockitoJUnitRunner.class) -public class RubyQProfilesServiceTest { - - @Mock - QProfiles qProfiles; - - RubyQProfilesService service; - - @Before - public void setUp() throws Exception { - service = new RubyQProfilesService(qProfiles); - } - - @Test - public void search_profiles() throws Exception { - when(qProfiles.searchProfiles()).thenReturn(newArrayList( - new QProfile().setId(1).setName("Sonar Way with Findbugs").setLanguage("java").setParent("Sonar Way").setVersion(1).setUsed(false) - )); - - assertThat(service.searchProfiles()).hasSize(1); - } - - @Test - public void new_profile() throws Exception { - service.newProfile(ImmutableMap.of( - "name", "Default", - "language", "java") - ); -// verify(qProfiles).newProfile(eq("Default"), eq("java"), any(UserSession.class)); - } -} -- 2.39.5