import org.sonar.api.Plugin;
import org.sonar.api.batch.CoverageExtension;
import org.sonar.api.batch.InstantiationStrategy;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
import org.sonar.batch.bootstrapper.EnvironmentInformation;
import java.util.List;
}
}
installExtensionProviders(module);
+ installMetrics(module);
+ }
+
+ private void installMetrics(Module module) {
+ for (Metrics metrics : module.getComponents(Metrics.class)) {
+ for (Metric metric : metrics.getMetrics()) {
+ module.addComponent(metric.getKey(), metric);
+ }
+ }
}
void installExtensionProviders(Module module) {
package org.sonar.batch.bootstrap;
import org.sonar.api.Plugins;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
+import org.sonar.api.measures.Metrics;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.ServerHttpClient;
import org.sonar.batch.DefaultResourceCreationLock;
addComponent(PastSnapshotFinderByVersion.class);
addComponent(PastMeasuresLoader.class);
addComponent(PastSnapshotFinder.class);
+ addCoreMetrics();
addBatchExtensions();
}
installer.install(this);
}
+ void addCoreMetrics() {
+ for (Metric metric : CoreMetrics.getMetrics()) {
+ addComponent(metric.getKey(), metric);
+ }
+ }
+
@Override
protected void doStart() {
ProjectTree projectTree = getComponent(ProjectTree.class);
for (Class clazz : Phases.getPhaseClasses()) {
addComponent(clazz);
}
-
- // TODO move metrics to BatchComponents
- for (Metric metric : CoreMetrics.getMetrics()) {
- addComponent(metric.getKey(), metric);
- }
- for (Metrics metricRepo : getComponents(Metrics.class)) {
- for (Metric metric : metricRepo.getMetrics()) {
- addComponent(metric.getKey(), metric);
- }
- }
}
private void addProjectPluginExtensions() {
import com.google.common.collect.Lists;
import org.apache.commons.lang.ClassUtils;
import org.picocontainer.MutablePicoContainer;
+import org.picocontainer.PicoContainer;
import org.sonar.api.BatchExtension;
import org.sonar.api.batch.maven.DependsUponMavenPlugin;
import org.sonar.api.batch.maven.MavenPluginHandler;
}
private List<BatchExtension> getExtensions() {
- return picoContainer.getComponents(BatchExtension.class);
+ List<BatchExtension> extensions = Lists.newArrayList();
+ completeBatchExtensions(picoContainer, extensions);
+ return extensions;
+ }
+
+ private void completeBatchExtensions(PicoContainer picoContainer, List<BatchExtension> extensions) {
+ if (picoContainer!=null) {
+ extensions.addAll(picoContainer.getComponents(BatchExtension.class));
+ completeBatchExtensions(picoContainer.getParent(), extensions);
+ }
}
private <T> List<T> getFilteredExtensions(Class<T> type, Project project) {
import org.sonar.api.BatchExtension;
import org.sonar.api.ServerExtension;
+import org.sonar.api.batch.InstantiationStrategy;
import java.util.List;
/**
* @since 1.10
*/
+@InstantiationStrategy(InstantiationStrategy.PER_BATCH)
public interface Metrics extends BatchExtension, ServerExtension {
List<Metric> getMetrics();
}
import com.google.common.collect.Lists;
import org.junit.Test;
+import org.picocontainer.Characteristics;
+import org.picocontainer.DefaultPicoContainer;
+import org.picocontainer.PicoContainer;
import org.picocontainer.containers.TransientPicoContainer;
import org.sonar.api.BatchExtension;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import java.util.ArrayList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.internal.matchers.IsCollectionContaining.hasItem;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
import static org.mockito.Mockito.mock;
public class BatchExtensionDictionnaryTest {
}
@Test
- public void getFilteredExtensions() {
+ public void testGetFilteredExtensions() {
Sensor sensor1 = new FakeSensor(), sensor2 = new FakeSensor();
Decorator decorator = mock(Decorator.class);
assertEquals(2, sensors.size());
}
+ @Test
+ public void shouldSearchInParentContainers() {
+ TransientPicoContainer grandParent = new TransientPicoContainer();
+ grandParent.as(Characteristics.CACHE).addComponent("ncloc", CoreMetrics.NCLOC);
+
+ DefaultPicoContainer parent = (DefaultPicoContainer)grandParent.makeChildContainer();
+ parent.as(Characteristics.CACHE).addComponent("coverage", CoreMetrics.COVERAGE);
+
+ DefaultPicoContainer child = (DefaultPicoContainer)parent.makeChildContainer();
+ child.as(Characteristics.CACHE).addComponent("complexity", CoreMetrics.COMPLEXITY);
+
+ BatchExtensionDictionnary dictionnary = new BatchExtensionDictionnary(child);
+ assertThat(dictionnary.select(Metric.class).size(), is(3));
+ assertThat(dictionnary.select(Metric.class), hasItems(CoreMetrics.NCLOC, CoreMetrics.COVERAGE, CoreMetrics.COMPLEXITY));
+ }
+
@Test
public void sortExtensionsByDependency() {
BatchExtension a = new MethodDependentOf(null);