@@ -49,21 +49,11 @@ public class Caches implements BatchComponent, Startable { | |||
private Persistit persistit; | |||
private Volume volume; | |||
public <K extends Serializable, V extends Serializable> Cache<K, V> createCache(String cacheName) { | |||
Preconditions.checkState(volume != null && volume.isOpened(), "Caches are not started"); | |||
Preconditions.checkState(!cacheNames.contains(cacheName), "Cache is already created: " + cacheName); | |||
try { | |||
Exchange exchange = persistit.getExchange(volume, cacheName, true); | |||
Cache<K, V> cache = new Cache<K, V>(cacheName, exchange); | |||
cacheNames.add(cacheName); | |||
return cache; | |||
} catch (Exception e) { | |||
throw new IllegalStateException("Fail to create cache: " + cacheName, e); | |||
} | |||
public Caches() { | |||
initPersistit(); | |||
} | |||
@Override | |||
public void start() { | |||
private void initPersistit() { | |||
try { | |||
tempDir = Files.createTempDir(); | |||
persistit = new Persistit(); | |||
@@ -85,6 +75,23 @@ public class Caches implements BatchComponent, Startable { | |||
} | |||
} | |||
public <K extends Serializable, V extends Serializable> Cache<K, V> createCache(String cacheName) { | |||
Preconditions.checkState(volume != null && volume.isOpened(), "Caches are not initialized"); | |||
Preconditions.checkState(!cacheNames.contains(cacheName), "Cache is already created: " + cacheName); | |||
try { | |||
Exchange exchange = persistit.getExchange(volume, cacheName, true); | |||
Cache<K, V> cache = new Cache<K, V>(cacheName, exchange); | |||
cacheNames.add(cacheName); | |||
return cache; | |||
} catch (Exception e) { | |||
throw new IllegalStateException("Fail to create cache: " + cacheName, e); | |||
} | |||
} | |||
@Override | |||
public void start() { | |||
} | |||
@Override | |||
public void stop() { | |||
if (persistit != null) { |
@@ -19,13 +19,12 @@ | |||
*/ | |||
package org.sonar.batch.issue; | |||
import com.google.common.collect.Maps; | |||
import org.sonar.api.BatchComponent; | |||
import org.sonar.api.issue.Issue; | |||
import org.sonar.batch.index.Cache; | |||
import org.sonar.batch.index.Caches; | |||
import java.util.Collection; | |||
import java.util.Collections; | |||
import java.util.Map; | |||
/** | |||
* Shared issues among all project modules | |||
@@ -33,28 +32,22 @@ import java.util.Map; | |||
public class IssueCache implements BatchComponent { | |||
// component key -> issue key -> issue | |||
private final Map<String, Map<String, Issue>> componentIssues = Maps.newHashMap(); | |||
private final Cache<String, Issue> cache; | |||
public IssueCache(Caches caches) { | |||
cache = caches.createCache("issues"); | |||
} | |||
public Collection<Issue> componentIssues(String componentKey) { | |||
Map<String, Issue> issuesByKey = componentIssues.get(componentKey); | |||
return issuesByKey == null ? Collections.<Issue>emptyList() : issuesByKey.values(); | |||
return cache.values(componentKey); | |||
} | |||
public Issue componentIssue(String componentKey, String issueKey) { | |||
Map<String, Issue> issuesByKey = componentIssues.get(componentKey); | |||
if (issuesByKey != null) { | |||
return issuesByKey.get(issueKey); | |||
} | |||
return null; | |||
return cache.get(componentKey, issueKey); | |||
} | |||
public IssueCache addOrUpdate(Issue issue) { | |||
Map<String, Issue> issuesByKey = componentIssues.get(issue.componentKey()); | |||
if (issuesByKey == null) { | |||
issuesByKey = Maps.newHashMap(); | |||
componentIssues.put(issue.componentKey(), issuesByKey); | |||
} | |||
issuesByKey.put(issue.key(), issue); | |||
cache.put(issue.componentKey(), issue.key(), issue); | |||
return this; | |||
} | |||
} |
@@ -74,6 +74,4 @@ public class IssuePersister implements ScanPersister { | |||
} | |||
} | |||
} | |||
} |
@@ -90,7 +90,6 @@ public class ProjectScanContainer extends ComponentContainer { | |||
// issues | |||
IssueWorkflow.class, | |||
ScanIssues.class, | |||
DeprecatedViolations.class, | |||
IssueCache.class, | |||
IssuePersister.class, |
@@ -37,12 +37,7 @@ public class CachesTest { | |||
} | |||
@Test | |||
public void should_start_and_stop_persistit() throws Exception { | |||
assertThat(caches.tempDir()).isNull(); | |||
assertThat(caches.persistit()).isNull(); | |||
caches.start(); | |||
public void should_stop_and_clean_temp_dir() throws Exception { | |||
File tempDir = caches.tempDir(); | |||
assertThat(tempDir).isDirectory().exists(); | |||
assertThat(caches.persistit()).isNotNull(); | |||
@@ -74,16 +69,6 @@ public class CachesTest { | |||
} | |||
} | |||
@Test | |||
public void should_not_create_cache_before_starting() { | |||
try { | |||
caches.createCache("too_early"); | |||
fail(); | |||
} catch (IllegalStateException e) { | |||
assertThat(e).hasMessage("Caches are not started"); | |||
} | |||
} | |||
static class Element implements Serializable { | |||
} |
@@ -50,7 +50,7 @@ public class IssueCacheTest { | |||
@Test | |||
public void should_add_new_issue() throws Exception { | |||
IssueCache cache = new IssueCache(); | |||
IssueCache cache = new IssueCache(caches); | |||
DefaultIssue issue1 = new DefaultIssue().setKey("111").setComponentKey("org.struts.Action"); | |||
DefaultIssue issue2 = new DefaultIssue().setKey("222").setComponentKey("org.struts.Action"); | |||
DefaultIssue issue3 = new DefaultIssue().setKey("333").setComponentKey("org.struts.Filter"); | |||
@@ -62,7 +62,7 @@ public class IssueCacheTest { | |||
@Test | |||
public void should_update_existing_issue() throws Exception { | |||
IssueCache cache = new IssueCache(); | |||
IssueCache cache = new IssueCache(caches); | |||
DefaultIssue issue = new DefaultIssue().setKey("111").setComponentKey("org.struts.Action").setSeverity(Severity.BLOCKER); | |||
cache.addOrUpdate(issue); | |||
@@ -24,6 +24,7 @@ import org.sonar.api.issue.Issue; | |||
import org.sonar.core.issue.DefaultIssue; | |||
import static org.fest.assertions.Assertions.assertThat; | |||
import static org.fest.assertions.Fail.fail; | |||
public class SetResolutionTest { | |||
@Test | |||
@@ -38,6 +39,7 @@ public class SetResolutionTest { | |||
public void resolution_should_not_be_empty() throws Exception { | |||
try { | |||
new SetResolution(""); | |||
fail(); | |||
} catch (IllegalArgumentException e) { | |||
assertThat(e).hasMessage("Resolution must be set"); | |||
} |
@@ -23,6 +23,7 @@ import org.junit.Test; | |||
import org.sonar.core.issue.DefaultIssue; | |||
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.verify; | |||
import static org.mockito.Mockito.when; | |||
@@ -64,6 +65,7 @@ public class TransitionTest { | |||
public void key_should_be_set() throws Exception { | |||
try { | |||
Transition.builder("").from("OPEN").to("CLOSED").build(); | |||
fail(); | |||
} catch (Exception e) { | |||
assertThat(e).hasMessage("Transition key must be set"); | |||
} | |||
@@ -73,6 +75,7 @@ public class TransitionTest { | |||
public void key_should_be_lower_case() throws Exception { | |||
try { | |||
Transition.builder("CLOSE").from("OPEN").to("CLOSED").build(); | |||
fail(); | |||
} catch (Exception e) { | |||
assertThat(e).hasMessage("Transition key must be lower-case"); | |||
} | |||
@@ -82,6 +85,7 @@ public class TransitionTest { | |||
public void originating_status_should_be_set() throws Exception { | |||
try { | |||
Transition.builder("close").from("").to("CLOSED").build(); | |||
fail(); | |||
} catch (Exception e) { | |||
assertThat(e).hasMessage("Originating status must be set"); | |||
} | |||
@@ -91,6 +95,7 @@ public class TransitionTest { | |||
public void destination_status_should_be_set() throws Exception { | |||
try { | |||
Transition.builder("close").from("OPEN").to("").build(); | |||
fail(); | |||
} catch (Exception e) { | |||
assertThat(e).hasMessage("Destination status must be set"); | |||
} |
@@ -21,13 +21,14 @@ package org.sonar.api.issue; | |||
import org.sonar.api.rule.RuleKey; | |||
import java.io.Serializable; | |||
import java.util.Date; | |||
import java.util.Map; | |||
/** | |||
* @since 3.6 | |||
*/ | |||
public interface Issue { | |||
public interface Issue extends Serializable { | |||
int DESCRIPTION_MAX_SIZE = 4000; | |||
String STATUS_OPEN = "OPEN"; |