aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2015-07-31 10:08:36 +0200
committerDuarte Meneses <duarte.meneses@sonarsource.com>2015-07-31 12:04:26 +0200
commit187b5f6f59454df662b29692fb86b0be552b6372 (patch)
tree678281b49bcf548a9ebcfffeeea0c42f5fabec4b
parent6cd87655c83490cc9f80f10c71a12a09e05378f2 (diff)
downloadsonarqube-187b5f6f59454df662b29692fb86b0be552b6372.tar.gz
sonarqube-187b5f6f59454df662b29692fb86b0be552b6372.zip
Improve quality
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java2
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java40
-rw-r--r--sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java27
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java3
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRules.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java3
6 files changed, 53 insertions, 32 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
index 686b37a591a..b703c01a724 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempFolderProvider.java
@@ -76,7 +76,7 @@ public class TempFolderProvider extends LifecycleProviderAdapter {
return tempFolder;
}
- private Path createTempFolder(Path workingPath) {
+ private static Path createTempFolder(Path workingPath) {
try {
Files.createDirectories(workingPath);
} catch (IOException e) {
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
index 640d5c29401..b8fdca5d1d5 100644
--- a/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
+++ b/sonar-home/src/main/java/org/sonar/home/cache/PersistentCache.java
@@ -142,7 +142,7 @@ public class PersistentCache {
logger.info("cache: clearing");
try {
lock();
- deleteCacheEntries(createClearFilter());
+ deleteCacheEntries(new DirectoryClearFilter());
} catch (IOException e) {
logger.error("Error clearing cache", e);
} finally {
@@ -157,7 +157,7 @@ public class PersistentCache {
logger.info("cache: cleaning");
try {
lock();
- deleteCacheEntries(createCleanFilter());
+ deleteCacheEntries(new DirectoryCleanFilter(defaultDurationToExpireMs));
} catch (IOException e) {
logger.error("Error cleaning cache", e);
} finally {
@@ -229,26 +229,28 @@ public class PersistentCache {
}
}
- private DirectoryStream.Filter<Path> createClearFilter() throws IOException {
- return new DirectoryStream.Filter<Path>() {
- @Override
- public boolean accept(Path entry) throws IOException {
- return !LOCK_FNAME.equals(entry.getFileName().toString());
- }
- };
+ private static class DirectoryClearFilter implements DirectoryStream.Filter<Path> {
+ @Override
+ public boolean accept(Path entry) throws IOException {
+ return !LOCK_FNAME.equals(entry.getFileName().toString());
+ }
}
- private DirectoryStream.Filter<Path> createCleanFilter() throws IOException {
- return new DirectoryStream.Filter<Path>() {
- @Override
- public boolean accept(Path entry) throws IOException {
- if (LOCK_FNAME.equals(entry.getFileName().toString())) {
- return false;
- }
+ private static class DirectoryCleanFilter implements DirectoryStream.Filter<Path> {
+ private long defaultDurationToExpireMs;
- return isCacheEntryExpired(entry, PersistentCache.this.defaultDurationToExpireMs);
+ DirectoryCleanFilter(long defaultDurationToExpireMs) {
+ this.defaultDurationToExpireMs = defaultDurationToExpireMs;
+ }
+
+ @Override
+ public boolean accept(Path entry) throws IOException {
+ if (LOCK_FNAME.equals(entry.getFileName().toString())) {
+ return false;
}
- };
+
+ return isCacheEntryExpired(entry, defaultDurationToExpireMs);
+ }
}
private void putCache(String key, byte[] value) throws IOException {
@@ -280,7 +282,7 @@ public class PersistentCache {
return true;
}
- private boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException {
+ private static boolean isCacheEntryExpired(Path cacheEntryPath, long durationToExpireMs) throws IOException {
BasicFileAttributes attr = Files.readAttributes(cacheEntryPath, BasicFileAttributes.class);
long modTime = attr.lastModifiedTime().toMillis();
diff --git a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
index c327ff7c5eb..72f594bf7d1 100644
--- a/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
+++ b/sonar-home/src/test/java/org/sonar/home/cache/PersistentCacheTest.java
@@ -20,12 +20,13 @@
package org.sonar.home.cache;
import java.io.File;
+import java.io.IOException;
+
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@@ -66,6 +67,16 @@ public class PersistentCacheTest {
@Test
public void testClean() throws Exception {
+ // puts entry
+ assertCacheHit(false);
+ // negative time to make sure it is expired
+ cache = new PersistentCache(tmp.getRoot().toPath(), -100, mock(Logger.class), null);
+ cache.clean();
+ assertCacheHit(false);
+ }
+
+ @Test
+ public void testClear() throws Exception {
assertCacheHit(false);
cache.clear();
assertCacheHit(false);
@@ -78,6 +89,12 @@ public class PersistentCacheTest {
}
@Test
+ public void testPut() throws Exception {
+ cache.put(URI, VALUE.getBytes());
+ assertCacheHit(true);
+ }
+
+ @Test
public void testReconfigure() throws Exception {
cache = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), null);
assertCacheHit(false);
@@ -101,22 +118,22 @@ public class PersistentCacheTest {
assertCacheHit(false);
assertCacheHit(false);
}
-
+
@Test
public void testDifferentServerVersions() throws Exception {
assertCacheHit(false);
assertCacheHit(true);
-
+
PersistentCache cache2 = new PersistentCache(tmp.getRoot().toPath(), Long.MAX_VALUE, mock(Logger.class), "5.2");
assertCacheHit(cache2, false);
assertCacheHit(cache2, true);
-
+
}
private void assertCacheHit(boolean hit) throws Exception {
assertCacheHit(cache, hit);
}
-
+
private void assertCacheHit(PersistentCache pCache, boolean hit) throws Exception {
CacheFillerString c = new CacheFillerString();
assertThat(pCache.getString(URI, c)).isEqualTo(VALUE);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java
index b54d676db91..4883861bc56 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/Rules.java
@@ -49,6 +49,9 @@ public interface Rules {
*/
Collection<Rule> findByRepository(String repository);
+ /**
+ * @since 5.2
+ */
Collection<Rule> findByInternalKey(String repository, String internalKey);
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRules.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRules.java
index 90e9d24d155..7fa0f694f92 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRules.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultRules.java
@@ -58,17 +58,17 @@ class DefaultRules implements Rules {
rulesByRepositoryAndInternalKey = ImmutableTable.copyOf(tableBuilder);
}
- private void addToTable(Table<String, String, List<Rule>> table, DefaultRule r) {
+ private static void addToTable(Table<String, String, List<Rule>> table, DefaultRule r) {
if (r.internalKey() == null) {
return;
}
List<Rule> ruleList = table.get(r.key().repository(), r.internalKey());
-
- if(ruleList == null) {
+
+ if (ruleList == null) {
ruleList = new LinkedList<>();
}
-
+
ruleList.add(r);
table.put(r.key().repository(), r.internalKey(), ruleList);
}
@@ -97,7 +97,7 @@ class DefaultRules implements Rules {
@Override
public Collection<Rule> findByInternalKey(String repository, String internalKey) {
List<Rule> rules = rulesByRepositoryAndInternalKey.get(repository, internalKey);
-
+
return rules != null ? rules : Collections.<Rule>emptyList();
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java
index 6e18116af53..f0f7c1eb271 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/DefaultTempFolder.java
@@ -79,8 +79,7 @@ public class DefaultTempFolder implements TempFolder {
private static Path createTempFile(Path baseDir, String prefix, String suffix) {
try {
- Path p = Files.createTempFile(baseDir, prefix, suffix);
- return p;
+ return Files.createTempFile(baseDir, prefix, suffix);
} catch (IOException e) {
throw new IllegalStateException("Failed to create temp file", e);
}