]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3755 fix NPE when file does not exist anymore
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 3 May 2013 08:10:25 +0000 (10:10 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 3 May 2013 08:10:33 +0000 (10:10 +0200)
sonar-batch/src/main/java/org/sonar/batch/issue/ScanIssueStorage.java
sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_fail_if_unknown_component.xml [new file with mode: 0644]
sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_load_component_id_from_db.xml [new file with mode: 0644]

index d5c00ab12d907f47478b69d6adf51f487f8edd83..124d5f2137b5d49bcb110c65f3bbb29522908ae7 100644 (file)
@@ -26,22 +26,33 @@ import org.sonar.batch.index.SnapshotCache;
 import org.sonar.core.issue.DefaultIssue;
 import org.sonar.core.issue.db.IssueStorage;
 import org.sonar.core.persistence.MyBatis;
+import org.sonar.core.resource.ResourceDao;
+import org.sonar.core.resource.ResourceDto;
+import org.sonar.core.resource.ResourceQuery;
 
 public class ScanIssueStorage extends IssueStorage implements BatchComponent {
 
   private final SnapshotCache snapshotCache;
+  private final ResourceDao resourceDao;
 
-  public ScanIssueStorage(MyBatis mybatis, RuleFinder ruleFinder, SnapshotCache snapshotCache) {
+  public ScanIssueStorage(MyBatis mybatis, RuleFinder ruleFinder, SnapshotCache snapshotCache, ResourceDao resourceDao) {
     super(mybatis, ruleFinder);
     this.snapshotCache = snapshotCache;
+    this.resourceDao = resourceDao;
   }
 
   @Override
   protected int componentId(DefaultIssue issue) {
     Snapshot snapshot = snapshotCache.get(issue.componentKey());
-    if (snapshot == null) {
-      throw new IllegalStateException("Component does not exist: " + issue.componentKey());
+    if (snapshot != null) {
+      return snapshot.getResourceId();
     }
-    return snapshot.getResourceId();
+
+    // Load from db when component does not exist in cache (deleted file for example)
+    ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(issue.componentKey()));
+    if (resourceDto == null) {
+      throw new IllegalStateException("Unknown component: " + issue.componentKey());
+    }
+    return resourceDto.getId().intValue();
   }
 }
diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ScanIssueStorageTest.java
new file mode 100644 (file)
index 0000000..e805a06
--- /dev/null
@@ -0,0 +1,108 @@
+/*
+ * 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.batch.issue;
+
+import org.junit.Test;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.RuleQuery;
+import org.sonar.batch.index.SnapshotCache;
+import org.sonar.core.issue.DefaultIssue;
+import org.sonar.core.persistence.AbstractDaoTestCase;
+import org.sonar.core.resource.ResourceDao;
+
+import java.util.Collection;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class ScanIssueStorageTest extends AbstractDaoTestCase {
+  @Test
+  public void should_load_component_id_from_cache() throws Exception {
+    SnapshotCache snapshotCache = mock(SnapshotCache.class);
+    when(snapshotCache.get("struts:Action.java")).thenReturn(new Snapshot().setResourceId(123));
+
+    ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis()));
+    int componentId = storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java"));
+
+    assertThat(componentId).isEqualTo(123);
+  }
+
+  @Test
+  public void should_load_component_id_from_db() throws Exception {
+    setupData("should_load_component_id_from_db");
+    SnapshotCache snapshotCache = mock(SnapshotCache.class);
+    when(snapshotCache.get("struts:Action.java")).thenReturn(null);
+
+    ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis()));
+    int componentId = storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java"));
+
+    assertThat(componentId).isEqualTo(123);
+  }
+
+  @Test
+  public void should_fail_if_unknown_component() throws Exception {
+    setupData("should_fail_if_unknown_component");
+    SnapshotCache snapshotCache = mock(SnapshotCache.class);
+    when(snapshotCache.get("struts:Action.java")).thenReturn(null);
+
+    ScanIssueStorage storage = new ScanIssueStorage(getMyBatis(), new FakeRuleFinder(), snapshotCache, new ResourceDao(getMyBatis()));
+    try {
+      storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java"));
+      fail();
+    } catch (Exception e) {
+      assertThat(e).hasMessage("Unknown component: struts:Action.java");
+    }
+  }
+
+  static class FakeRuleFinder implements RuleFinder {
+
+    @Override
+    public Rule findById(int ruleId) {
+      return null;
+    }
+
+    @Override
+    public Rule findByKey(String repositoryKey, String key) {
+      return null;
+    }
+
+    @Override
+    public Rule findByKey(RuleKey key) {
+      Rule rule = new Rule().setRepositoryKey(key.repository()).setKey(key.rule());
+      rule.setId(200);
+      return rule;
+    }
+
+    @Override
+    public Rule find(RuleQuery query) {
+      return null;
+    }
+
+    @Override
+    public Collection<Rule> findAll(RuleQuery query) {
+      return null;
+    }
+  }
+}
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_fail_if_unknown_component.xml b/sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_fail_if_unknown_component.xml
new file mode 100644 (file)
index 0000000..5ed00ba
--- /dev/null
@@ -0,0 +1 @@
+<dataset></dataset>
\ No newline at end of file
diff --git a/sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_load_component_id_from_db.xml b/sonar-batch/src/test/resources/org/sonar/batch/issue/ScanIssueStorageTest/should_load_component_id_from_db.xml
new file mode 100644 (file)
index 0000000..cf7e75f
--- /dev/null
@@ -0,0 +1,3 @@
+<dataset>
+  <projects id="123" kee="struts:Action.java" />
+</dataset>
\ No newline at end of file