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();
}
}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}