From 164d6d931c3b015877cbfa936f03ea4503349ddb Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 22 Sep 2015 11:48:16 +0200 Subject: [PATCH] Fix quality flaws --- .../sonar/xoo/rule/CustomMessageSensor.java | 10 +- .../sonar/server/computation/ReportFiles.java | 10 +- .../sonar/server/rule/index/RuleIndex.java | 2 +- .../server/search/action/RefreshIndex.java | 4 +- .../sonar/server/computation/CeTaskTest.java | 60 ++++++++++++ .../server/computation/ReportFilesTest.java | 96 +++++++++++++++++++ .../search/action/RefreshIndexTest.java | 4 +- 7 files changed, 171 insertions(+), 15 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/CeTaskTest.java create mode 100644 server/sonar-server/src/test/java/org/sonar/server/computation/ReportFilesTest.java diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/CustomMessageSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/CustomMessageSensor.java index 2738c485bba..df72749dfc8 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/CustomMessageSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/CustomMessageSensor.java @@ -51,9 +51,11 @@ public class CustomMessageSensor extends AbstractDeprecatedXooRuleSensor { @Override protected void processFile(InputFile inputFile, org.sonar.api.resources.File sonarFile, SensorContext context, RuleKey ruleKey, String languageKey) { Issuable issuable = perspectives.as(Issuable.class, sonarFile); - issuable.addIssue(issuable.newIssueBuilder() - .ruleKey(ruleKey) - .message(settings.getString(MESSAGE_PROPERTY)) - .build()); + if (issuable != null) { + issuable.addIssue(issuable.newIssueBuilder() + .ruleKey(ruleKey) + .message(settings.getString(MESSAGE_PROPERTY)) + .build()); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/ReportFiles.java b/server/sonar-server/src/main/java/org/sonar/server/computation/ReportFiles.java index 3aae2baac91..13ad4bd9fc5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/ReportFiles.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/ReportFiles.java @@ -61,10 +61,12 @@ public class ReportFiles { public void deleteAll() { File dir = reportDir(); - try { - FileUtils.deleteDirectory(dir); - } catch (Exception e) { - throw new IllegalStateException(format("Fail to delete directory: %s", dir.getAbsolutePath()), e); + if (dir.exists()) { + try { + FileUtils.cleanDirectory(dir); + } catch (Exception e) { + throw new IllegalStateException(format("Fail to clean directory: %s", dir.getAbsolutePath()), e); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index 8442208aad1..f9f5e0dacb3 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -206,7 +206,7 @@ public class RuleIndex extends BaseIndex { String queryString = query.getQueryText(); // Human readable type of querying - qb.should(QueryBuilders.simpleQueryString(query.getQueryText()) + qb.should(QueryBuilders.simpleQueryStringQuery(query.getQueryText()) .field(RuleNormalizer.RuleField.NAME.field() + "." + IndexField.SEARCH_WORDS_SUFFIX, 20f) .field(RuleNormalizer.RuleField.HTML_DESCRIPTION.field() + "." + IndexField.SEARCH_WORDS_SUFFIX, 3f) .defaultOperator(SimpleQueryStringBuilder.Operator.AND) diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/action/RefreshIndex.java b/server/sonar-server/src/main/java/org/sonar/server/search/action/RefreshIndex.java index f3b0101663e..89c8e5148ee 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/action/RefreshIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/action/RefreshIndex.java @@ -20,11 +20,10 @@ package org.sonar.server.search.action; import com.google.common.collect.ImmutableList; +import java.util.List; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.sonar.server.search.Index; -import java.util.List; - public class RefreshIndex extends IndexAction { public RefreshIndex(String indexType) { @@ -40,7 +39,6 @@ public class RefreshIndex extends IndexAction { public List doCall(Index index) { return ImmutableList.of( new RefreshRequest() - .force(false) .indices(index.getIndexName())); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/CeTaskTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/CeTaskTest.java new file mode 100644 index 00000000000..b950ca753f9 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/CeTaskTest.java @@ -0,0 +1,60 @@ +/* + * 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.server.computation; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CeTaskTest { + + @Test + public void build() { + CeTask.Builder builder = new CeTask.Builder(); + builder.setType("TYPE_1"); + builder.setUuid("UUID_1"); + builder.setSubmitterLogin("LOGIN_1"); + builder.setComponentKey("COMPONENT_KEY_1"); + builder.setComponentUuid("COMPONENT_UUID_1"); + builder.setComponentName("The component"); + CeTask task = builder.build(); + + assertThat(task.getType()).isEqualTo("TYPE_1"); + assertThat(task.getUuid()).isEqualTo("UUID_1"); + assertThat(task.getSubmitterLogin()).isEqualTo("LOGIN_1"); + assertThat(task.getComponentKey()).isEqualTo("COMPONENT_KEY_1"); + assertThat(task.getComponentUuid()).isEqualTo("COMPONENT_UUID_1"); + assertThat(task.getComponentName()).isEqualTo("The component"); + } + + @Test + public void equals_and_hashCode_on_uuid() { + CeTask.Builder builder1 = new CeTask.Builder().setType("TYPE_1").setUuid("UUID_1"); + CeTask task1 = builder1.build(); + CeTask task1bis = builder1.build(); + CeTask task2 = new CeTask.Builder().setType("TYPE_1").setUuid("UUID_2").build(); + + assertThat(task1.equals(task1)).isTrue(); + assertThat(task1.equals(task1bis)).isTrue(); + assertThat(task1.equals(task2)).isFalse(); + assertThat(task1.hashCode()).isEqualTo(task1.hashCode()); + assertThat(task1.hashCode()).isEqualTo(task1bis.hashCode()); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/ReportFilesTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/ReportFilesTest.java new file mode 100644 index 00000000000..6bde2ef3279 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/ReportFilesTest.java @@ -0,0 +1,96 @@ +package org.sonar.server.computation; + +import java.io.File; +import java.io.IOException; +import org.apache.commons.io.FileUtils; +import org.h2.util.IOUtils; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.config.Settings; +import org.sonar.process.ProcessProperties; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ReportFilesTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + File reportDir; + Settings settings = new Settings(); + ReportFiles underTest = new ReportFiles(settings); + + @Before + public void setUp() throws IOException { + File dataDir = temp.newFolder(); + reportDir = new File(dataDir, "ce/reports"); + settings.setProperty(ProcessProperties.PATH_DATA, dataDir.getCanonicalPath()); + } + + @Test + public void save_report() throws IOException { + underTest.save("TASK_1", IOUtils.getInputStreamFromString("{binary}")); + + assertThat(FileUtils.readFileToString(new File(reportDir, "TASK_1.zip"))).isEqualTo("{binary}"); + + } + + @Test + public void deleteIfExists_uuid_does_not_exist() { + // do not fail, does nothing + underTest.deleteIfExists("TASK_1"); + } + + @Test + public void deleteIfExists() throws IOException { + File report = new File(reportDir, "TASK_1.zip"); + FileUtils.touch(report); + assertThat(report).exists(); + + underTest.deleteIfExists("TASK_1"); + assertThat(report).doesNotExist(); + } + + /** + * List the zip files contained in the report directory + */ + @Test + public void listUuids() throws IOException { + FileUtils.touch(new File(reportDir, "TASK_1.zip")); + FileUtils.touch(new File(reportDir, "TASK_2.zip")); + FileUtils.touch(new File(reportDir, "something.else")); + + assertThat(underTest.listUuids()).containsOnly("TASK_1", "TASK_2"); + } + + @Test + public void listUuids_dir_does_not_exist_yet() throws IOException { + FileUtils.deleteQuietly(reportDir); + + assertThat(underTest.listUuids()).isEmpty(); + } + + @Test + public void deleteAll() throws IOException { + FileUtils.touch(new File(reportDir, "TASK_1.zip")); + FileUtils.touch(new File(reportDir, "TASK_2.zip")); + FileUtils.touch(new File(reportDir, "something.else")); + + underTest.deleteAll(); + + // directory still exists but is empty + assertThat(reportDir).exists().isDirectory(); + assertThat(reportDir.listFiles()).isEmpty(); + } + + @Test + public void deleteAll_dir_does_not_exist_yet() throws IOException { + FileUtils.deleteQuietly(reportDir); + + underTest.deleteAll(); + + assertThat(reportDir).doesNotExist(); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/action/RefreshIndexTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/action/RefreshIndexTest.java index 32cd52992f9..130f1926a78 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/search/action/RefreshIndexTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/search/action/RefreshIndexTest.java @@ -19,14 +19,13 @@ */ package org.sonar.server.search.action; +import java.util.List; import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; import org.junit.Before; import org.junit.Test; import org.sonar.server.search.Index; import org.sonar.server.search.IndexDefinition; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -69,6 +68,5 @@ public class RefreshIndexTest { RefreshRequest request = requests.get(0); assertThat(request.indices()).containsOnly(TEST_INDEX.getIndexName()); - assertThat(request.force()).isFalse(); } }