]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 22 Sep 2015 09:48:16 +0000 (11:48 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 22 Sep 2015 09:48:16 +0000 (11:48 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/CustomMessageSensor.java
server/sonar-server/src/main/java/org/sonar/server/computation/ReportFiles.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/search/action/RefreshIndex.java
server/sonar-server/src/test/java/org/sonar/server/computation/CeTaskTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/ReportFilesTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/search/action/RefreshIndexTest.java

index 2738c485bbad1e908d75508ef22c064f1bf9d611..df72749dfc85586c826d3b7aaa98ff64f2aa07c6 100644 (file)
@@ -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());
+    }
   }
 }
index 3aae2baac91330d33358a17f577e478e3e69cdb8..13ad4bd9fc545b0133de4997b21cc38490d5afc2 100644 (file)
@@ -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);
+      }
     }
   }
 
index 8442208aad192f3090222211a789338eca19b754..f9f5e0dacb3eea760c7d8c7c2444f231d68959b3 100644 (file)
@@ -206,7 +206,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     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)
index f3b0101663ea70710857fdc2c579ed522539fe68..89c8e5148eebe04966245c9d75ed6301b3668645 100644 (file)
 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<RefreshRequest> {
 
   public RefreshIndex(String indexType) {
@@ -40,7 +39,6 @@ public class RefreshIndex extends IndexAction<RefreshRequest> {
   public List<RefreshRequest> 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 (file)
index 0000000..b950ca7
--- /dev/null
@@ -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 (file)
index 0000000..6bde2ef
--- /dev/null
@@ -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();
+  }
+}
index 32cd52992f9618b06b7445ffc2e916569d4f6335..130f1926a786df63276702b81b807e239b31a2b5 100644 (file)
  */
 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();
   }
 }