Skip to content

Commit

Permalink
Issue checkstyle#40: get rid of dependency to sonar-java-plugin
Browse files Browse the repository at this point in the history
Issue checkstyle#40: get rid of dependency to sonar-java-plugin and only depend on java-squid
  • Loading branch information
marschall committed Feb 14, 2017
1 parent 078a2b2 commit 6f66c8f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
8 changes: 5 additions & 3 deletions checkstyle-sonar-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@
<!-- till https://github.com/checkstyle/sonar-checkstyle/issues/40 -->
<dependency>
<groupId>org.sonarsource.java</groupId>
<artifactId>sonar-java-plugin</artifactId>
<type>sonar-plugin</type>
<artifactId>java-squid</artifactId>
<version>${sonar-java.version}</version>
<scope>provided</scope>
<!-- to avoid 4.5.1 api pickup -->
<exclusions>
<exclusion>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-plugin-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.codehaus.sonar</groupId>
<artifactId>sonar-colorizer</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- required to load external descriptiona, see CheckstyleRulesDefinition -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchExtension;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.plugins.java.api.JavaResourceLocator;
import org.sonar.java.JavaClasspath;

import com.google.common.annotations.VisibleForTesting;
import com.puppycrawl.tools.checkstyle.Checker;
Expand All @@ -49,13 +49,13 @@ public class CheckstyleExecutor implements BatchExtension {

private final CheckstyleConfiguration configuration;
private final CheckstyleAuditListener listener;
private final JavaResourceLocator javaResourceLocator;
private final JavaClasspath javaClasspath;

public CheckstyleExecutor(CheckstyleConfiguration configuration,
CheckstyleAuditListener listener, JavaResourceLocator javaResourceLocator) {
CheckstyleAuditListener listener, JavaClasspath javaClasspath) {
this.configuration = configuration;
this.listener = listener;
this.javaResourceLocator = javaResourceLocator;
this.javaClasspath = javaClasspath;
}

/**
Expand Down Expand Up @@ -135,7 +135,7 @@ URL getUrl(URI uri) {
}

private URLClassLoader createClassloader() {
Collection<File> classpathElements = javaResourceLocator.classpath();
Collection<File> classpathElements = javaClasspath.getElements();
List<URL> urls = new ArrayList<>(classpathElements.size());
for (File file : classpathElements) {
urls.add(getUrl(file.toURI()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.sonar.api.SonarPlugin;
import org.sonar.api.config.PropertyDefinition;
import org.sonar.api.resources.Qualifiers;
import org.sonar.java.JavaClasspath;

public final class CheckstylePlugin extends SonarPlugin {

Expand Down Expand Up @@ -60,6 +61,9 @@ public List getExtensions() {
.name("Generate XML Report").type(PropertyType.BOOLEAN).hidden()
.build(),

// since we no longer depend on sonar-java-plugin but only
// java-squid we have to manually register this component
JavaClasspath.class,
CheckstyleSensor.class, CheckstyleConfiguration.class,
CheckstyleExecutor.class, CheckstyleAuditListener.class,
CheckstyleProfileExporter.class, CheckstyleProfileImporter.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.sonar.java.DefaultJavaResourceLocator;
import org.sonar.java.JavaClasspath;
import org.sonar.plugins.java.api.JavaResourceLocator;

import com.google.common.collect.ImmutableList;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
Expand All @@ -62,7 +60,7 @@ public void execute() throws CheckstyleException {
CheckstyleConfiguration conf = mockConf();
CheckstyleAuditListener listener = mockListener();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener,
createJavaResourceLocator());
createJavaClasspath());
executor.execute();

verify(listener, times(1)).auditStarted(any(AuditEvent.class));
Expand Down Expand Up @@ -91,7 +89,7 @@ public void executeException() throws CheckstyleException {
thrown.expectMessage("Can not execute Checkstyle");
CheckstyleConfiguration conf = mockConf();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, null,
createJavaResourceLocator());
createJavaClasspath());
executor.execute();
}

Expand All @@ -101,14 +99,14 @@ public void getUrlException() throws URISyntaxException {
thrown.expectMessage("Fail to create the project classloader. "
+ "Classpath element is invalid: htp://aa");
CheckstyleExecutor executor = new CheckstyleExecutor(null, null,
createJavaResourceLocator());
createJavaClasspath());
executor.getUrl(new URI("htp://aa"));
}

private static JavaResourceLocator createJavaResourceLocator() {
private static JavaClasspath createJavaClasspath() {
JavaClasspath javaClasspath = mock(JavaClasspath.class);
when(javaClasspath.getElements()).thenReturn(ImmutableList.of(new File(".")));
return new DefaultJavaResourceLocator(null, javaClasspath, null);
return javaClasspath;
}

@Test
Expand All @@ -124,7 +122,7 @@ public void canGenerateXmlReportInEnglish() throws CheckstyleException, IOExcept
when(conf.getTargetXmlReport()).thenReturn(report);
CheckstyleAuditListener listener = mockListener();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener,
createJavaResourceLocator());
createJavaClasspath());
executor.execute();

Assert.assertTrue(report.exists());
Expand All @@ -148,7 +146,7 @@ public void canGenerateXmlReportNull() throws CheckstyleException {
when(conf.getTargetXmlReport()).thenReturn(null);
CheckstyleAuditListener listener = mockListener();
CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener,
createJavaResourceLocator());
createJavaClasspath());
executor.execute();

Assert.assertFalse(report.exists());
Expand Down

0 comments on commit 6f66c8f

Please sign in to comment.