diff options
author | Godin <mandrikov@gmail.com> | 2010-10-18 16:53:34 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-10-18 16:53:34 +0000 |
commit | ba547c4d3980245737a8ee4979d20bb8a584510b (patch) | |
tree | 4ea9a04a9d20d2c91a7c9600b7399679c779f97e /plugins/sonar-findbugs-plugin | |
parent | c62f720adc4e545e4ac47d3d596692e077620e23 (diff) | |
download | sonarqube-ba547c4d3980245737a8ee4979d20bb8a584510b.tar.gz sonarqube-ba547c4d3980245737a8ee4979d20bb8a584510b.zip |
SONAR-1772: Execute findbugs in separate thread and terminate after "sonar.findbugs.timeout" milliseconds
Diffstat (limited to 'plugins/sonar-findbugs-plugin')
4 files changed, 35 insertions, 3 deletions
diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java index 7969741cdb6..7d0510c6855 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java @@ -72,4 +72,8 @@ public class FindbugsConfiguration implements BatchExtension { public String getEffort() { return StringUtils.lowerCase(project.getConfiguration().getString(CoreProperties.FINDBUGS_EFFORT_PROPERTY, CoreProperties.FINDBUGS_EFFORT_DEFAULT_VALUE)); } + + public long getTimeout() { + return project.getConfiguration().getLong(CoreProperties.FINDBUGS_TIMEOUT_PROPERTY, FindbugsConstants.FINDBUGS_TIMEOUT_DEFAULT_VALUE); + } } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java index 363cf01ddf2..69d72ee3e13 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConstants.java @@ -33,4 +33,5 @@ public final class FindbugsConstants { */ public static final String GENERATE_XML_KEY = "sonar.findbugs.generateXml"; public static final boolean GENERATE_XML_DEFAULT_VALUE = true; // TODO should be false + public static final long FINDBUGS_TIMEOUT_DEFAULT_VALUE = 600000; } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java index 8953f013270..cc9cc188b3e 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsExecutor.java @@ -16,6 +16,9 @@ import edu.umd.cs.findbugs.config.UserPreferences; import java.io.File; import java.io.OutputStream; import java.io.PrintStream; +import java.util.concurrent.Callable; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; /** * @since 2.4 @@ -70,7 +73,9 @@ public class FindbugsExecutor implements BatchExtension { engine.finishSettings(); - engine.execute(); + Executors.newSingleThreadExecutor() + .submit(new FindbugsTask(engine)) + .get(configuration.getTimeout(), TimeUnit.MILLISECONDS); profiler.stop(); return xmlReport; @@ -82,4 +87,16 @@ public class FindbugsExecutor implements BatchExtension { } } + private class FindbugsTask implements Callable<Object> { + private FindBugs2 engine; + + public FindbugsTask(FindBugs2 engine) { + this.engine = engine; + } + + public Object call() throws Exception { + engine.execute(); + return null; + } + } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java index 6d8801b985a..a8077941984 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsExecutorTest.java @@ -2,6 +2,7 @@ package org.sonar.plugins.findbugs; import org.apache.commons.io.FileUtils; import org.junit.Test; +import org.sonar.api.utils.SonarException; import edu.umd.cs.findbugs.Project; @@ -20,13 +21,21 @@ public class FindbugsExecutorTest { FindbugsConfiguration conf = mockConf(); File report = new File("target/test-tmp/findbugs-report.xml"); when(conf.getTargetXMLReport()).thenReturn(report); - FindbugsExecutor executor = new FindbugsExecutor(conf); - executor.execute(); + + new FindbugsExecutor(conf).execute(); assertThat(report.exists(), is(true)); assertThat(FileUtils.readFileToString(report), containsString("<BugInstance")); } + @Test(expected = SonarException.class) + public void shouldTerminateOnTimeout() throws Exception { + FindbugsConfiguration conf = mockConf(); + when(conf.getTimeout()).thenReturn(1L); + + new FindbugsExecutor(conf).execute(); + } + private FindbugsConfiguration mockConf() throws Exception { FindbugsConfiguration conf = mock(FindbugsConfiguration.class); Project project = new Project(); @@ -37,6 +46,7 @@ public class FindbugsExecutorTest { when(conf.saveExcludeConfigXml()).thenReturn(new File("test-resources/findbugs-exclude.xml")); when(conf.saveIncludeConfigXml()).thenReturn(new File("test-resources/findbugs-include.xml")); when(conf.getEffort()).thenReturn("default"); + when(conf.getTimeout()).thenReturn(FindbugsConstants.FINDBUGS_TIMEOUT_DEFAULT_VALUE); return conf; } |