aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-03-02 11:48:53 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-03-02 11:48:53 +0100
commitbcce12869f28494813918b4a8b62f3714599ec98 (patch)
treee4ccaafae408c62b62a7e329a0e6c126c77cd8b5 /sonar-plugin-api
parent6c2a68be94ec4c43e012a22276b24ce8c639f6c1 (diff)
parente29d4bfc46b6d00f80a89089a72f86b121707188 (diff)
downloadsonarqube-bcce12869f28494813918b4a8b62f3714599ec98.tar.gz
sonarqube-bcce12869f28494813918b4a8b62f3714599ec98.zip
Merge remote-tracking branch 'origin/master' into branch-5.2
Conflicts: server/sonar-server/src/main/java/org/sonar/server/computation/ComputationContext.java server/sonar-server/src/main/java/org/sonar/server/computation/ComputationService.java server/sonar-server/src/test/java/org/sonar/server/computation/ComputationServiceTest.java server/sonar-server/src/test/java/org/sonar/server/computation/step/ParseReportStepTest.java server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepMediumTest.java server/sonar-server/src/test/java/org/sonar/server/computation/step/PurgeDatastoresStepTest.java
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/JUnitTempFolder.java108
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java42
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java33
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogInterceptor.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java12
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/log/NullInterceptor.java10
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/JUnitTempFolderTest.java53
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogTesterTest.java4
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/log/NullInterceptorTest.java10
9 files changed, 236 insertions, 46 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/JUnitTempFolder.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/JUnitTempFolder.java
new file mode 100644
index 00000000000..3a8c15bf474
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/internal/JUnitTempFolder.java
@@ -0,0 +1,108 @@
+/*
+ * 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.api.utils.internal;
+
+import org.apache.commons.lang.StringUtils;
+import org.junit.rules.ExternalResource;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+import org.sonar.api.utils.TempFolder;
+
+import javax.annotation.Nullable;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * Implementation of {@link org.sonar.api.utils.TempFolder} to be used
+ * only in JUnit tests. It wraps {@link org.junit.rules.TemporaryFolder}.
+ * <p/>
+ * Example:
+ * <pre>
+ * public class MyTest {
+ * &#064;@org.junit.Rule
+ * public JUnitTempFolder temp = new JUnitTempFolder();
+ *
+ * &#064;@org.junit.Test
+ * public void myTest() throws Exception {
+ * File dir = temp.newDir();
+ * // ...
+ * }
+ * }
+ * </pre>
+ *
+ * @since 5.1
+ */
+public class JUnitTempFolder extends ExternalResource implements TempFolder {
+
+ private final TemporaryFolder junit = new TemporaryFolder();
+
+ @Override
+ public Statement apply(Statement base, Description description) {
+ return junit.apply(base, description);
+ }
+
+ @Override
+ protected void before() throws Throwable {
+ junit.create();
+ }
+
+ @Override
+ protected void after() {
+ junit.delete();
+ }
+
+ @Override
+ public File newDir() {
+ try {
+ return junit.newFolder();
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to create temp dir", e);
+ }
+ }
+
+ @Override
+ public File newDir(String name) {
+ try {
+ return junit.newFolder(name);
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to create temp dir", e);
+ }
+ }
+
+ @Override
+ public File newFile() {
+ try {
+ return junit.newFile();
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to create temp file", e);
+ }
+ }
+
+ @Override
+ public File newFile(@Nullable String prefix, @Nullable String suffix) {
+ try {
+ return junit.newFile(StringUtils.defaultString(prefix) + "-" + StringUtils.defaultString(suffix));
+ } catch (IOException e) {
+ throw new IllegalStateException("Fail to create temp file", e);
+ }
+ }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java
index fec49378945..8b33cf19f05 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/BaseLogger.java
@@ -24,127 +24,127 @@ import javax.annotation.Nullable;
abstract class BaseLogger implements Logger {
@Override
public void trace(String msg) {
- LogInterceptors.get().log(msg);
+ LogInterceptors.get().log(LoggerLevel.TRACE, msg);
doTrace(msg);
}
@Override
public void trace(String pattern, @Nullable Object arg) {
- LogInterceptors.get().log(pattern, arg);
+ LogInterceptors.get().log(LoggerLevel.TRACE, pattern, arg);
doTrace(pattern, arg);
}
@Override
public void trace(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- LogInterceptors.get().log(msg, arg1, arg2);
+ LogInterceptors.get().log(LoggerLevel.TRACE, msg, arg1, arg2);
doTrace(msg, arg1, arg2);
}
@Override
public void trace(String msg, Object... args) {
- LogInterceptors.get().log(msg, args);
+ LogInterceptors.get().log(LoggerLevel.TRACE, msg, args);
doTrace(msg, args);
}
@Override
public void debug(String msg) {
- LogInterceptors.get().log(msg);
+ LogInterceptors.get().log(LoggerLevel.DEBUG, msg);
doDebug(msg);
}
@Override
public void debug(String pattern, @Nullable Object arg) {
- LogInterceptors.get().log(pattern, arg);
+ LogInterceptors.get().log(LoggerLevel.DEBUG, pattern, arg);
doDebug(pattern, arg);
}
@Override
public void debug(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- LogInterceptors.get().log(msg, arg1, arg2);
+ LogInterceptors.get().log(LoggerLevel.DEBUG, msg, arg1, arg2);
doDebug(msg, arg1, arg2);
}
@Override
public void debug(String msg, Object... args) {
- LogInterceptors.get().log(msg, args);
+ LogInterceptors.get().log(LoggerLevel.DEBUG, msg, args);
doDebug(msg, args);
}
@Override
public void info(String msg) {
- LogInterceptors.get().log(msg);
+ LogInterceptors.get().log(LoggerLevel.INFO, msg);
doInfo(msg);
}
@Override
public void info(String msg, @Nullable Object arg) {
- LogInterceptors.get().log(msg, arg);
+ LogInterceptors.get().log(LoggerLevel.INFO, msg, arg);
doInfo(msg, arg);
}
@Override
public void info(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- LogInterceptors.get().log(msg, arg1, arg2);
+ LogInterceptors.get().log(LoggerLevel.INFO, msg, arg1, arg2);
doInfo(msg, arg1, arg2);
}
@Override
public void info(String msg, Object... args) {
- LogInterceptors.get().log(msg, args);
+ LogInterceptors.get().log(LoggerLevel.INFO, msg, args);
doInfo(msg, args);
}
@Override
public void warn(String msg) {
- LogInterceptors.get().log(msg);
+ LogInterceptors.get().log(LoggerLevel.WARN, msg);
doWarn(msg);
}
@Override
public void warn(String msg, @Nullable Object arg) {
- LogInterceptors.get().log(msg, arg);
+ LogInterceptors.get().log(LoggerLevel.WARN, msg, arg);
doWarn(msg, arg);
}
@Override
public void warn(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- LogInterceptors.get().log(msg, arg1, arg2);
+ LogInterceptors.get().log(LoggerLevel.WARN, msg, arg1, arg2);
doWarn(msg, arg1, arg2);
}
@Override
public void warn(String msg, Object... args) {
- LogInterceptors.get().log(msg, args);
+ LogInterceptors.get().log(LoggerLevel.WARN, msg, args);
doWarn(msg, args);
}
@Override
public void error(String msg) {
- LogInterceptors.get().log(msg);
+ LogInterceptors.get().log(LoggerLevel.ERROR, msg);
doError(msg);
}
@Override
public void error(String msg, @Nullable Object arg) {
- LogInterceptors.get().log(msg, arg);
+ LogInterceptors.get().log(LoggerLevel.ERROR, msg, arg);
doError(msg, arg);
}
@Override
public void error(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- LogInterceptors.get().log(msg, arg1, arg2);
+ LogInterceptors.get().log(LoggerLevel.ERROR, msg, arg1, arg2);
doError(msg, arg1, arg2);
}
@Override
public void error(String msg, Object... args) {
- LogInterceptors.get().log(msg, args);
+ LogInterceptors.get().log(LoggerLevel.ERROR, msg, args);
doError(msg, args);
}
@Override
public void error(String msg, Throwable thrown) {
- LogInterceptors.get().log(msg, thrown);
+ LogInterceptors.get().log(LoggerLevel.ERROR, msg, thrown);
doError(msg, thrown);
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
index 76e3c444829..67869605d9a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/ListInterceptor.java
@@ -19,6 +19,9 @@
*/
package org.sonar.api.utils.log;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+
import javax.annotation.Nullable;
import java.util.ArrayList;
@@ -27,37 +30,51 @@ import java.util.List;
class ListInterceptor extends LogInterceptor {
private final List<String> logs = new ArrayList<>();
+ private final ListMultimap<LoggerLevel, String> logsByLevel = ArrayListMultimap.create();
@Override
- public void log(String msg) {
+ public void log(LoggerLevel level, String msg) {
logs.add(msg);
+ logsByLevel.put(level, msg);
}
@Override
- public void log(String msg, @Nullable Object arg) {
- logs.add(ConsoleFormatter.format(msg, arg));
+ public void log(LoggerLevel level, String msg, @Nullable Object arg) {
+ String s = ConsoleFormatter.format(msg, arg);
+ logs.add(s);
+ logsByLevel.put(level, s);
}
@Override
- public void log(String msg, @Nullable Object arg1, @Nullable Object arg2) {
- logs.add(ConsoleFormatter.format(msg, arg1, arg2));
+ public void log(LoggerLevel level, String msg, @Nullable Object arg1, @Nullable Object arg2) {
+ String s = ConsoleFormatter.format(msg, arg1, arg2);
+ logs.add(s);
+ logsByLevel.put(level, s);
}
@Override
- public void log(String msg, Object... args) {
- logs.add(ConsoleFormatter.format(msg, args));
+ public void log(LoggerLevel level, String msg, Object... args) {
+ String s = ConsoleFormatter.format(msg, args);
+ logs.add(s);
+ logsByLevel.put(level, s);
}
@Override
- public void log(String msg, Throwable thrown) {
+ public void log(LoggerLevel level, String msg, Throwable thrown) {
logs.add(msg);
+ logsByLevel.put(level, msg);
}
public List<String> logs() {
return logs;
}
+ public List<String> logs(LoggerLevel level) {
+ return logsByLevel.get(level);
+ }
+
public void clear() {
logs.clear();
+ logsByLevel.clear();
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogInterceptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogInterceptor.java
index bccd7e9797a..556b1644130 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogInterceptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogInterceptor.java
@@ -23,13 +23,13 @@ import javax.annotation.Nullable;
abstract class LogInterceptor {
- abstract void log(String msg);
+ abstract void log(LoggerLevel level, String msg);
- abstract void log(String msg, @Nullable Object arg);
+ abstract void log(LoggerLevel level, String msg, @Nullable Object arg);
- abstract void log(String msg, @Nullable Object arg1, @Nullable Object arg2);
+ abstract void log(LoggerLevel level, String msg, @Nullable Object arg1, @Nullable Object arg2);
- abstract void log(String msg, Object... args);
+ abstract void log(LoggerLevel level, String msg, Object... args);
- abstract void log(String msg, Throwable thrown);
+ abstract void log(LoggerLevel level, String msg, Throwable thrown);
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
index cdc112fbbf7..ab2fe4a3fa5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/LogTester.java
@@ -42,10 +42,10 @@ import java.util.List;
* }
*
* public class MyTest {
- * &#064;Rule
+ * &#064;org.junit.Rule
* public LogTester logTester = new LogTester();
*
- * &#064;Test
+ * &#064;org.junit.Test
* public void test_log() {
* new MyClass().doSomething();
*
@@ -95,6 +95,14 @@ public class LogTester extends ExternalResource {
return ((ListInterceptor) LogInterceptors.get()).logs();
}
+ /**
+ * Logs in chronological order (item at index 0 is the oldest one) for
+ * a given level
+ */
+ public List<String> logs(LoggerLevel level) {
+ return ((ListInterceptor) LogInterceptors.get()).logs(level);
+ }
+
public LogTester clear() {
((ListInterceptor) LogInterceptors.get()).clear();
return this;
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/NullInterceptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/NullInterceptor.java
index 63e0d9ab847..6614e01bf31 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/NullInterceptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/log/NullInterceptor.java
@@ -30,27 +30,27 @@ class NullInterceptor extends LogInterceptor {
}
@Override
- void log(String msg) {
+ void log(LoggerLevel level, String msg) {
// nothing
}
@Override
- void log(String msg, Object arg) {
+ void log(LoggerLevel level, String msg, Object arg) {
// nothing
}
@Override
- void log(String msg, Object arg1, Object arg2) {
+ void log(LoggerLevel level, String msg, Object arg1, Object arg2) {
// nothing
}
@Override
- void log(String msg, Object... args) {
+ void log(LoggerLevel level, String msg, Object... args) {
// nothing
}
@Override
- void log(String msg, Throwable thrown) {
+ void log(LoggerLevel level, String msg, Throwable thrown) {
// nothing
}
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/JUnitTempFolderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/JUnitTempFolderTest.java
new file mode 100644
index 00000000000..c2086e05c36
--- /dev/null
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/internal/JUnitTempFolderTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.api.utils.internal;
+
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JUnitTempFolderTest {
+
+ @Test
+ public void apply() throws Throwable {
+ JUnitTempFolder temp = new JUnitTempFolder();
+ temp.before();
+ File dir1 = temp.newDir();
+ assertThat(dir1).isDirectory().exists();
+
+ File dir2 = temp.newDir("foo");
+ assertThat(dir2).isDirectory().exists();
+
+ File file1 = temp.newFile();
+ assertThat(file1).isFile().exists();
+
+ File file2 = temp.newFile("foo", "txt");
+ assertThat(file2).isFile().exists();
+
+ temp.after();
+ assertThat(dir1).doesNotExist();
+ assertThat(dir2).doesNotExist();
+ assertThat(file1).doesNotExist();
+ assertThat(file2).doesNotExist();
+ }
+
+}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogTesterTest.java
index 9fd01343f34..9880b1ef13a 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogTesterTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/LogTesterTest.java
@@ -54,9 +54,13 @@ public class LogTesterTest {
Loggers.get("logger2").warn("warning: {}", 42);
assertThat(sut.logs()).containsExactly("an information", "warning: 42");
+ assertThat(sut.logs(LoggerLevel.ERROR)).isEmpty();
+ assertThat(sut.logs(LoggerLevel.INFO)).containsOnly("an information");
+ assertThat(sut.logs(LoggerLevel.WARN)).containsOnly("warning: 42");
sut.clear();
assertThat(sut.logs()).isEmpty();
+ assertThat(sut.logs(LoggerLevel.INFO)).isEmpty();
sut.after();
assertThat(LogInterceptors.get()).isSameAs(NullInterceptor.NULL_INSTANCE);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/NullInterceptorTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/NullInterceptorTest.java
index ca4d14e4890..f410fcd80ae 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/NullInterceptorTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/log/NullInterceptorTest.java
@@ -28,10 +28,10 @@ public class NullInterceptorTest {
@Test
public void do_not_throws_exception() throws Exception {
// verify that... it does nothing
- NullInterceptor.NULL_INSTANCE.log("foo");
- NullInterceptor.NULL_INSTANCE.log("foo {}", 42);
- NullInterceptor.NULL_INSTANCE.log("foo {} {}", 42, 66);
- NullInterceptor.NULL_INSTANCE.log("foo {} {} {}", 42, 66, 84);
- NullInterceptor.NULL_INSTANCE.log("foo", mock(Exception.class));
+ NullInterceptor.NULL_INSTANCE.log(LoggerLevel.INFO, "foo");
+ NullInterceptor.NULL_INSTANCE.log(LoggerLevel.INFO, "foo {}", 42);
+ NullInterceptor.NULL_INSTANCE.log(LoggerLevel.INFO, "foo {} {}", 42, 66);
+ NullInterceptor.NULL_INSTANCE.log(LoggerLevel.INFO, "foo {} {} {}", 42, 66, 84);
+ NullInterceptor.NULL_INSTANCE.log(LoggerLevel.INFO, "foo", mock(Exception.class));
}
}