--- /dev/null
+/*
+ * 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.issue;
+
+import org.sonar.api.component.Component;
+import org.sonar.api.issue.Issuable;
+import org.sonar.api.issue.Issue;
+import org.sonar.core.issue.DefaultIssueBuilder;
+
+import java.util.Collection;
+
+/**
+ * @since 3.6
+ */
+public class DefaultIssuable implements Issuable {
+
+ private final ModuleIssues moduleIssues;
+ private final Component component;
+
+ DefaultIssuable(Component component, ModuleIssues moduleIssues) {
+ this.component = component;
+ this.moduleIssues = moduleIssues;
+ }
+
+ @Override
+ public IssueBuilder newIssue() {
+ return new DefaultIssueBuilder(moduleIssues, component.key());
+ }
+
+ @Override
+ public Collection<Issue> issues() {
+ return moduleIssues.issues(component.key());
+ }
+
+ @Override
+ public Component component() {
+ return component;
+ }
+}
--- /dev/null
+/*
+ * 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.issue;
+
+import org.sonar.api.component.Component;
+import org.sonar.api.issue.Issuable;
+import org.sonar.api.resources.Scopes;
+import org.sonar.core.component.PerspectiveBuilder;
+import org.sonar.core.component.ResourceComponent;
+
+import javax.annotation.CheckForNull;
+
+public class IssuableFactory extends PerspectiveBuilder<Issuable> {
+
+ private final ModuleIssues moduleIssues;
+
+ public IssuableFactory(ModuleIssues moduleIssues) {
+ super(Issuable.class);
+ this.moduleIssues = moduleIssues;
+ }
+
+ @CheckForNull
+ @Override
+ protected Issuable loadPerspective(Class<Issuable> perspectiveClass, Component component) {
+ boolean supported = true;
+ if (component instanceof ResourceComponent) {
+ supported = Scopes.isHigherThanOrEquals(((ResourceComponent) component).scope(), Scopes.FILE);
+ }
+ if (supported) {
+ return new DefaultIssuable(component, moduleIssues);
+ }
+ return null;
+ }
+}
public void decorate(Resource resource, DecoratorContext context) {
Issuable issuable = perspectives.as(Issuable.class, resource);
- Collection<Issue> issues = issuable.issues();
- computeTotalIssues(context, issues);
- computeIssuesPerSeverities(context, issues);
- computeIssuesPerRules(context, issues);
+ if (issuable != null) {
+ Collection<Issue> issues = issuable.issues();
+ computeTotalIssues(context, issues);
+ computeIssuesPerSeverities(context, issues);
+ computeIssuesPerRules(context, issues);
+ }
}
private void computeTotalIssues(DecoratorContext context, Collection<Issue> issues) {
+++ /dev/null
-/*
- * 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.issue;
-
-import org.sonar.api.component.Component;
-import org.sonar.api.issue.Issuable;
-import org.sonar.api.issue.Issue;
-import org.sonar.core.issue.DefaultIssueBuilder;
-
-import java.util.Collection;
-
-/**
- * @since 3.6
- */
-public class ScanIssuable implements Issuable {
-
- private final ModuleIssues moduleIssues;
- private final Component component;
-
- ScanIssuable(Component component, ModuleIssues moduleIssues) {
- this.component = component;
- this.moduleIssues = moduleIssues;
- }
-
- @Override
- public IssueBuilder newIssue() {
- return new DefaultIssueBuilder(moduleIssues, component.key());
- }
-
- @Override
- public Collection<Issue> issues() {
- return moduleIssues.issues(component.key());
- }
-
- @Override
- public Component component() {
- return component;
- }
-}
+++ /dev/null
-/*
- * 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.issue;
-
-import org.sonar.api.component.Component;
-import org.sonar.api.issue.Issuable;
-import org.sonar.core.component.PerspectiveBuilder;
-
-public class ScanIssuableFactory extends PerspectiveBuilder<Issuable> {
-
- private final ModuleIssues moduleIssues;
-
- public ScanIssuableFactory(ModuleIssues moduleIssues) {
- super(Issuable.class);
- this.moduleIssues = moduleIssues;
- }
-
- @Override
- protected Issuable loadPerspective(Class<Issuable> perspectiveClass, Component component) {
- return new ScanIssuable(component, moduleIssues);
- }
-}
import org.sonar.batch.events.EventBus;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.ResourcePersister;
+import org.sonar.batch.issue.IssuableFactory;
import org.sonar.batch.issue.ModuleIssues;
import org.sonar.batch.local.DryRunExporter;
import org.sonar.batch.phases.PhaseExecutor;
import org.sonar.batch.scan.filesystem.LanguageFilters;
import org.sonar.batch.scan.filesystem.ModuleFileSystemProvider;
import org.sonar.core.component.ScanPerspectives;
-import org.sonar.batch.issue.ScanIssuableFactory;
import org.sonar.batch.scan.source.HighlightableBuilder;
public class ModuleScanContainer extends ComponentContainer {
new ProfileProvider(),
ModuleIssues.class,
- ScanIssuableFactory.class,
+ IssuableFactory.class,
HighlightableBuilder.class,
ScanPerspectives.class
*/
package org.sonar.batch.scan.source;
+import com.google.common.collect.ImmutableSet;
import org.sonar.api.component.Component;
+import org.sonar.api.resources.Qualifiers;
+import org.sonar.api.resources.Scopes;
import org.sonar.api.scan.source.Highlightable;
import org.sonar.core.component.PerspectiveBuilder;
+import org.sonar.core.component.ResourceComponent;
+
+import javax.annotation.CheckForNull;
+import java.util.Set;
/**
* @since 3.6
*/
public class HighlightableBuilder extends PerspectiveBuilder<Highlightable> {
- private final SyntaxHighlightingCache syntaxHighlightingCache;
+ private static final Set<String> SUPPORTED_QUALIFIERS = ImmutableSet.of(Qualifiers.FILE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE);
+ private final SyntaxHighlightingCache cache;
+
- public HighlightableBuilder(SyntaxHighlightingCache syntaxHighlightingCache) {
+ public HighlightableBuilder(SyntaxHighlightingCache cache) {
super(Highlightable.class);
- this.syntaxHighlightingCache = syntaxHighlightingCache;
+ this.cache = cache;
}
+ @CheckForNull
@Override
protected Highlightable loadPerspective(Class<Highlightable> perspectiveClass, Component component) {
- return new DefaultHighlightable(component, syntaxHighlightingCache);
+ boolean supported = SUPPORTED_QUALIFIERS.contains(component.qualifier());
+ if (supported && component instanceof ResourceComponent) {
+ // temporary hack waiting for the removal of JavaClass.
+ // JavaClass has the same qualifier than JavaFile, so they have to distinguished by their scope
+ supported = Scopes.FILE.equals(((ResourceComponent) component).scope());
+ }
+ if (supported) {
+ return new DefaultHighlightable(component, cache);
+ }
+ return null;
}
}
--- /dev/null
+/*
+ * 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.index;
+
+import org.junit.Test;
+
+public class CacheTest {
+ @Test
+ public void test_put() throws Exception {
+
+ }
+
+}
--- /dev/null
+/*
+ * 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.issue;
+
+import org.junit.Test;
+import org.sonar.api.component.Component;
+import org.sonar.api.issue.Issuable;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.JavaFile;
+import org.sonar.api.resources.Project;
+import org.sonar.core.component.ResourceComponent;
+import org.sonar.java.api.JavaClass;
+
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+
+public class IssuableFactoryTest {
+
+ ModuleIssues moduleIssues = mock(ModuleIssues.class);
+
+ @Test
+ public void file_should_be_issuable() throws Exception {
+ IssuableFactory factory = new IssuableFactory(moduleIssues);
+ Component component = new ResourceComponent(new File("foo/bar.c"));
+ Issuable issuable = factory.loadPerspective(Issuable.class, component);
+
+ assertThat(issuable).isNotNull();
+ assertThat(issuable.component()).isSameAs(component);
+ assertThat(issuable.issues()).isEmpty();
+ }
+
+ @Test
+ public void project_should_be_issuable() throws Exception {
+ IssuableFactory factory = new IssuableFactory(moduleIssues);
+ Component component = new ResourceComponent(new Project("Foo"));
+ Issuable issuable = factory.loadPerspective(Issuable.class, component);
+
+ assertThat(issuable).isNotNull();
+ assertThat(issuable.component()).isSameAs(component);
+ assertThat(issuable.issues()).isEmpty();
+ }
+
+ @Test
+ public void java_file_should_be_issuable() throws Exception {
+ IssuableFactory factory = new IssuableFactory(moduleIssues);
+ Component component = new ResourceComponent(new JavaFile("bar.Foo"));
+ Issuable issuable = factory.loadPerspective(Issuable.class, component);
+
+ assertThat(issuable).isNotNull();
+ assertThat(issuable.component()).isSameAs(component);
+ assertThat(issuable.issues()).isEmpty();
+ }
+
+ @Test
+ public void java_class_should_not_be_issuable() throws Exception {
+ IssuableFactory factory = new IssuableFactory(moduleIssues);
+ Component component = new ResourceComponent(JavaClass.create("bar", "Foo"));
+ Issuable issuable = factory.loadPerspective(Issuable.class, component);
+
+ assertThat(issuable).isNull();
+ }
+}
import org.junit.Test;
import org.sonar.api.component.Component;
+import org.sonar.api.resources.File;
+import org.sonar.api.resources.Project;
import org.sonar.api.scan.source.Highlightable;
+import org.sonar.core.component.ResourceComponent;
+import org.sonar.java.api.JavaClass;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
public class HighlightableBuilderTest {
+ SyntaxHighlightingCache cache = mock(SyntaxHighlightingCache.class);
+
@Test
public void should_load_default_perspective() throws Exception {
+ Component component = new ResourceComponent(new File("foo/bar.c"));
+
+ HighlightableBuilder builder = new HighlightableBuilder(cache);
+ Highlightable perspective = builder.loadPerspective(Highlightable.class, component);
+
+ assertThat(perspective).isNotNull().isInstanceOf(DefaultHighlightable.class);
+ assertThat(perspective.component()).isSameAs(component);
+ }
+
+ @Test
+ public void project_should_not_be_highlightable() {
+ Component component = new ResourceComponent(new Project("Foo"));
+
+ HighlightableBuilder builder = new HighlightableBuilder(cache);
+ Highlightable perspective = builder.loadPerspective(Highlightable.class, component);
- Component mockComponent = mock(Component.class);
- SyntaxHighlightingCache highlightingCache = mock(SyntaxHighlightingCache.class);
+ assertThat(perspective).isNull();
+ }
+
+ @Test
+ public void java_class_should_not_be_highlightable() {
+ Component component = new ResourceComponent(JavaClass.create("foo", "Bar"));
- HighlightableBuilder builder = new HighlightableBuilder(highlightingCache);
- Highlightable perspective = builder.loadPerspective(Highlightable.class, mockComponent);
+ HighlightableBuilder builder = new HighlightableBuilder(cache);
+ Highlightable perspective = builder.loadPerspective(Highlightable.class, component);
- assertThat(perspective).isInstanceOf(DefaultHighlightable.class);
+ assertThat(perspective).isNull();
}
}
import org.sonar.api.component.Component;
import org.sonar.api.component.Perspective;
+import javax.annotation.CheckForNull;
+
public abstract class PerspectiveBuilder<T extends Perspective> implements BatchComponent, ServerComponent {
private final Class<T> perspectiveClass;
return perspectiveClass;
}
+ @CheckForNull
protected abstract T loadPerspective(Class<T> perspectiveClass, Component component);
}
import javax.annotation.Nullable;
-class ResourceComponent implements Component {
+public class ResourceComponent implements Component {
private String key;
private String name;
private String longName;
private String qualifier;
+ private String scope;
private Long snapshotId;
private Long resourceId;
- ResourceComponent(Resource resource, @Nullable Snapshot snapshot) {
+ public ResourceComponent(Resource resource, @Nullable Snapshot snapshot) {
this.key = resource.getEffectiveKey();
this.name = resource.getName();
this.longName = resource.getLongName();
this.qualifier = resource.getQualifier();
+ this.scope = resource.getScope();
if (snapshot != null && snapshot.getId() != null) {
this.snapshotId = snapshot.getId().longValue();
this.resourceId = snapshot.getResourceId().longValue();
}
}
- ResourceComponent(Resource resource) {
+ public ResourceComponent(Resource resource) {
this(resource, null);
}
return qualifier;
}
+ public String scope() {
+ return scope;
+ }
+
public Long snapshotId() {
return snapshotId;
}
return builder.loadPerspective(perspectiveClass, component);
}
+ @CheckForNull
public <P extends Perspective> P as(Class<P> perspectiveClass, Resource resource) {
Resource indexedResource = resourceIndex.getResource(resource);
if (indexedResource != null) {
--- /dev/null
+/*
+ * 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.core.component;
+
+import org.junit.Test;
+import org.sonar.api.component.Component;
+import org.sonar.api.component.Perspective;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class PerspectiveBuilderTest {
+ @Test
+ public void testGetPerspectiveClass() throws Exception {
+ PerspectiveBuilder<FakePerspective> builder = new PerspectiveBuilder<FakePerspective>(FakePerspective.class) {
+ @Override
+ protected FakePerspective loadPerspective(Class<FakePerspective> perspectiveClass, Component component) {
+ return null;
+ }
+ };
+
+ assertThat(builder.getPerspectiveClass()).isEqualTo(FakePerspective.class);
+ }
+
+ static interface FakePerspective extends Perspective {
+
+ }
+}