aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2012-10-27 00:29:44 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2012-10-27 00:29:44 +0200
commitf7a628ba80b14f5ae3a61853b7a04eca7178755f (patch)
treed79388c497d2cf323f42bce41e2b9e709e02a510 /sonar-batch
parente123a94cb32ab7b73fdaffb15feea9356ddcf94f (diff)
downloadsonarqube-f7a628ba80b14f5ae3a61853b7a04eca7178755f.tar.gz
sonarqube-f7a628ba80b14f5ae3a61853b7a04eca7178755f.zip
SONAR-3895 remove unused code and fix quality flaws
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java19
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcLeakPrevention.java29
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java18
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java2
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java8
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchModuleTest.java50
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java8
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapExtensionExecutorTest.java55
10 files changed, 130 insertions, 63 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
index f7db951df26..e005e4f948f 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchModule.java
@@ -19,7 +19,6 @@
*/
package org.sonar.batch.bootstrap;
-import org.sonar.api.Plugins;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ResourceTypes;
@@ -69,7 +68,6 @@ public class BatchModule extends Module {
container.addSingleton(MemoryOptimizer.class);
container.addSingleton(DefaultResourcePersister.class);
container.addSingleton(SourcePersister.class);
- container.addSingleton(Plugins.class);
container.addSingleton(MeasuresDao.class);
container.addSingleton(CacheRuleFinder.class);
container.addSingleton(CacheMetricFinder.class);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
index 69a74c8f91d..64fbb25f8e7 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchPluginRepository.java
@@ -99,29 +99,10 @@ public class BatchPluginRepository implements PluginRepository {
}
}
- public Collection<Plugin> getPlugins() {
- return pluginsByKey.values();
- }
-
public Plugin getPlugin(String key) {
return pluginsByKey.get(key);
}
- public Map<String, Plugin> getPluginsByKey() {
- return Collections.unmodifiableMap(pluginsByKey);
- }
-
- // TODO remove this method. Not used in batch.
- public Property[] getProperties(Plugin plugin) {
- if (plugin != null) {
- Class<? extends Plugin> classInstance = plugin.getClass();
- if (classInstance.isAnnotationPresent(Properties.class)) {
- return classInstance.getAnnotation(Properties.class).value();
- }
- }
- return new Property[0];
- }
-
public Collection<PluginMetadata> getMetadata() {
return metadataByKey.values();
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
index 8e75b551819..d170217cc38 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcDriverHolder.java
@@ -139,7 +139,7 @@ public class JdbcDriverHolder {
Class<?> lpClass = defineClass("org.sonar.batch.bootstrap.JdbcLeakPrevention", classBytes, 0, offset, this.getClass().getProtectionDomain());
Object obj = lpClass.newInstance();
- List<String> driverNames = (List<String>) obj.getClass().getMethod("clearJdbcDriverRegistrations").invoke(obj);
+ List<String> driverNames = (List<String>) obj.getClass().getMethod("unregisterDrivers").invoke(obj);
for (String name : driverNames) {
LOG.debug("To prevent a memory leak, the JDBC Driver [{}] has been forcibly deregistered", name);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcLeakPrevention.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcLeakPrevention.java
index f067a2c08ae..001ecc3cf57 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcLeakPrevention.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/JdbcLeakPrevention.java
@@ -26,6 +26,7 @@ import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
+import java.util.Set;
/**
* Companion of {@link JdbcDriverHolder} and allows it to deregister JDBC drivers.
@@ -38,27 +39,33 @@ import java.util.List;
public class JdbcLeakPrevention {
/**
- * @return list of names of deregistered drivers
+ * @return names of the drivers that have been unregistered
*/
- public List<String> clearJdbcDriverRegistrations() throws SQLException {
- List<String> driverNames = new ArrayList<String>();
- HashSet<Driver> originalDrivers = new HashSet<Driver>();
+ public List<String> unregisterDrivers() throws SQLException {
+ Set<Driver> registeredDrivers = registeredDrivers();
+
+ List<String> unregisteredNames = new ArrayList<String>();
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
- originalDrivers.add(drivers.nextElement());
- }
- drivers = DriverManager.getDrivers();
- while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
if (driver.getClass().getClassLoader() != this.getClass().getClassLoader()) {
continue;
}
- if (originalDrivers.contains(driver)) {
- driverNames.add(driver.getClass().getCanonicalName());
+ if (registeredDrivers.contains(driver)) {
+ unregisteredNames.add(driver.getClass().getCanonicalName());
}
DriverManager.deregisterDriver(driver);
}
- return driverNames;
+ return unregisteredNames;
+ }
+
+ private Set<Driver> registeredDrivers() {
+ Set<Driver> registeredDrivers = new HashSet<Driver>();
+ Enumeration<Driver> drivers = DriverManager.getDrivers();
+ while (drivers.hasMoreElements()) {
+ registeredDrivers.add(drivers.nextElement());
+ }
+ return registeredDrivers;
}
}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java
index 105a3f693b5..fcafd4856a8 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/Module.java
@@ -19,17 +19,11 @@
*/
package org.sonar.batch.bootstrap;
-import org.picocontainer.ComponentAdapter;
import org.sonar.api.platform.ComponentContainer;
-import org.sonar.api.platform.PluginMetadata;
-
-import javax.annotation.Nullable;
-
-import java.util.List;
/**
* Module describes group of components - {@link #configure()}.
- * Several modules can be grouped together - {@link #install(Module)}, {@link #installChild(Module)}.
+ * Several modules can be grouped together - {@link #installChild(Module)}.
* <p/>
*/
public abstract class Module {
@@ -53,16 +47,6 @@ public abstract class Module {
}
/**
- * Installs module into this module.
- *
- * @return this
- */
- public final Module install(Module module) {
- module.init(container);
- return this;
- }
-
- /**
* Installs module into new scope - see http://picocontainer.org/scopes.html
*
* @return installed module
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
index 13bde69f4da..8ddbf991cf5 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java
@@ -27,7 +27,6 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
-import org.sonar.api.utils.IocContainer;
import org.sonar.batch.DefaultProfileLoader;
import org.sonar.batch.DefaultProjectClasspath;
import org.sonar.batch.DefaultProjectFileSystem2;
@@ -73,7 +72,6 @@ public class ProjectModule extends Module {
container.addSingleton(project.getConfiguration());
container.addSingleton(ProjectSettings.class);
container.addSingleton(UnsupportedProperties.class);
- container.addSingleton(IocContainer.class);
for (Object component : projectDefinition.getContainerExtensions()) {
container.addSingleton(component);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
index f90c60d3480..de4ace57ffc 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java
@@ -22,20 +22,18 @@ package org.sonar.batch.phases;
import com.google.common.collect.Lists;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.Project;
-import org.sonar.batch.phases.ProjectInitializer;
import org.sonar.batch.events.EventBus;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.PersistenceManager;
import java.util.Collection;
-import java.util.List;
public final class Phases {
public static Collection<Class> getPhaseClasses() {
return Lists.<Class>newArrayList(DecoratorsExecutor.class, MavenPhaseExecutor.class, MavenPluginsConfigurator.class,
- PostJobsExecutor.class, SensorsExecutor.class,
- InitializersExecutor.class, ProjectInitializer.class, UpdateStatusJob.class);
+ PostJobsExecutor.class, SensorsExecutor.class,
+ InitializersExecutor.class, ProjectInitializer.class, UpdateStatusJob.class);
}
private EventBus eventBus;
@@ -76,7 +74,7 @@ public final class Phases {
PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index,
EventBus eventBus, ProjectInitializer pi) {
this(decoratorsExecutor, mavenPhaseExecutor, mavenPluginsConfigurator, initializersExecutor, postJobsExecutor,
- sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi);
+ sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi);
}
/**
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchModuleTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchModuleTest.java
new file mode 100644
index 00000000000..9f297a762a2
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchModuleTest.java
@@ -0,0 +1,50 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.batch.bootstrap;
+
+import org.junit.Test;
+import org.sonar.api.batch.InstantiationStrategy;
+import org.sonar.api.platform.ComponentContainer;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class BatchModuleTest {
+ @Test
+ public void should_register_batch_extensions() {
+ final ExtensionInstaller extensionInstaller = mock(ExtensionInstaller.class);
+ Module bootstrapModule = new Module() {
+ @Override
+ protected void configure() {
+ // used to install project extensions
+ container.addSingleton(extensionInstaller);
+ }
+ };
+ bootstrapModule.init();
+ BatchModule module = new BatchModule();
+ bootstrapModule.installChild(module);
+
+ verify(extensionInstaller).install(any(ComponentContainer.class), eq(InstantiationStrategy.BATCH));
+ assertThat(module.container.getComponentByType(MetricProvider.class)).isNotNull();
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java
index 080e48c767c..c1a56e96f06 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BatchPluginRepositoryTest.java
@@ -62,7 +62,6 @@ public class BatchPluginRepositoryTest {
repository.doStart(Arrays.asList(checkstyle));
- assertThat(repository.getPlugins().size(), Matchers.is(1));
assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
assertThat(repository.getMetadata().size(), Matchers.is(1));
assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle"));
@@ -82,7 +81,6 @@ public class BatchPluginRepositoryTest {
repository.doStart(Arrays.asList(checkstyle, checkstyleExt));
- assertThat(repository.getPlugins().size(), Matchers.is(2));
assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
assertThat(repository.getPlugin("checkstyleextensions"), not(nullValue()));
assertThat(repository.getMetadata().size(), Matchers.is(2));
@@ -93,7 +91,7 @@ public class BatchPluginRepositoryTest {
@Test
public void shouldLoadPluginDeprecatedExtensions() throws IOException {
RemotePlugin checkstyle = new RemotePlugin("checkstyle", true)
- .addFilename("checkstyle-ext.xml");
+ .addFilename("checkstyle-ext.xml");
PluginDownloader downloader = mock(PluginDownloader.class);
when(downloader.downloadPlugin(checkstyle)).thenReturn(copyFiles("sonar-checkstyle-plugin-2.8.jar", "checkstyle-ext.xml"));
@@ -102,12 +100,11 @@ public class BatchPluginRepositoryTest {
repository.doStart(Arrays.asList(checkstyle));
- assertThat(repository.getPlugins().size(), Matchers.is(1));
assertThat(repository.getPlugin("checkstyle"), not(nullValue()));
assertThat(repository.getMetadata().size(), Matchers.is(1));
assertThat(repository.getMetadata("checkstyle").getName(), Matchers.is("Checkstyle"));
assertThat(repository.getMetadata("checkstyle").getDeployedFiles().size(), Matchers.is(5)); // plugin + 3 dependencies + 1 deprecated
- // extension
+ // extension
}
@Test
@@ -125,7 +122,6 @@ public class BatchPluginRepositoryTest {
repository.doStart(Arrays.asList(checkstyle, checkstyleExt));
- assertThat(repository.getPlugins().size(), Matchers.is(0));
assertThat(repository.getMetadata().size(), Matchers.is(0));
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapExtensionExecutorTest.java b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapExtensionExecutorTest.java
new file mode 100644
index 00000000000..6ee1a684040
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/bootstrap/BootstrapExtensionExecutorTest.java
@@ -0,0 +1,55 @@
+package org.sonar.batch.bootstrap;
+
+import org.junit.Test;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+import org.picocontainer.Startable;
+import org.sonar.api.batch.InstantiationStrategy;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.config.Settings;
+import org.sonar.api.platform.ComponentContainer;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class BootstrapExtensionExecutorTest {
+ private ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create().setKey("foo"));
+
+ @Test
+ public void start() {
+ ComponentContainer container = new ComponentContainer();
+ // dependencies required for ProjectExclusions
+ container.addSingleton(reactor);
+ container.addSingleton(new Settings());
+
+ // declare a bootstrap component
+ final Startable bootstrapComponent = mock(Startable.class);
+ ExtensionInstaller installer = mock(ExtensionInstaller.class);
+ doAnswer(new Answer() {
+ public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
+ ComponentContainer childContainer = (ComponentContainer) invocationOnMock.getArguments()[0];
+ childContainer.addSingleton(bootstrapComponent);
+ return null;
+ }
+ }).when(installer).install(any(ComponentContainer.class), eq(InstantiationStrategy.BOOTSTRAP));
+
+ BootstrapExtensionExecutor executor = new BootstrapExtensionExecutor(container, installer);
+ executor.start();
+
+ // should install bootstrap components into a ephemeral container
+ verify(installer).install(any(ComponentContainer.class), eq(InstantiationStrategy.BOOTSTRAP));
+ verify(bootstrapComponent).start();
+ verify(bootstrapComponent).stop();
+
+ // the ephemeral container is destroyed
+ assertThat(container.getComponentByType(ProjectExclusions.class)).isNull();
+ assertThat(container.getChild()).isNull();
+ }
+
+
+}