Skip to content
This repository has been archived by the owner on May 26, 2020. It is now read-only.

Commit

Permalink
Add maven project to lintchecker config
Browse files Browse the repository at this point in the history
  • Loading branch information
skiddykong committed Apr 12, 2017
1 parent 3a73f57 commit ead2de5
Show file tree
Hide file tree
Showing 27 changed files with 424 additions and 64 deletions.
28 changes: 15 additions & 13 deletions io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.gov.justice.raml.io;

import com.google.common.base.Predicate;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;
import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Predicates.or;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;

import java.io.IOException;
import java.net.MalformedURLException;
Expand All @@ -16,13 +18,11 @@
import java.util.Set;
import java.util.regex.Pattern;

import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.not;
import static com.google.common.base.Predicates.or;
import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.toList;
import com.google.common.base.Predicate;
import org.reflections.Reflections;
import org.reflections.scanners.ResourcesScanner;
import org.reflections.util.ClasspathHelper;
import org.reflections.util.ConfigurationBuilder;

/**
* Utility class for searching directories.
Expand All @@ -45,12 +45,14 @@ public Collection<Path> find(final Path baseDir, final String[] includes, final
if (!shouldSearchOnClasspath(baseDir) && !baseDir.toFile().exists()) {
return emptyList();
}
Reflections reflections = new Reflections(

final Reflections reflections = new Reflections(
new ConfigurationBuilder()
.filterInputsBy(filterOf(includes, excludes))
.setUrls(urlsToScan(baseDir))
.setScanners(new ResourcesScanner()));
Set<String> resources = reflections.getResources(Pattern.compile(".*"));

return resources.stream().map(Paths::get).collect(toList());
}

Expand Down
19 changes: 19 additions & 0 deletions lint-checker-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@
<artifactId>raml-parser</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
package uk.gov.justice.raml.maven.lintchecker;

import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

/**
* Contains any configuration needed by any of the {@link LintCheckRule}s. Currently this class
* contains nothing, but to future proof the {@link LintCheckRule} interface we pass this class
* in the execute() method. Add any configuration your rule may need here.
*/
public class LintCheckConfiguration {

private final MavenProject mavenProject;
private final Log log;

public LintCheckConfiguration(final MavenProject mavenProject, final Log log) {
this.mavenProject = mavenProject;
this.log = log;
}

public MavenProject getMavenProject() {
return mavenProject;
}

public Log getLog() {
return log;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.raml.maven.lintchecker;

public class LintCheckPluginException extends LintCheckerException {

public LintCheckPluginException(final String message) {
super(message);
}

public LintCheckPluginException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package uk.gov.justice.raml.maven.lintchecker;

public class LintCheckRuleFailedException extends LintCheckerException {

public LintCheckRuleFailedException(final String message) {
super(message);
}

public LintCheckRuleFailedException(final String message, final Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package uk.gov.justice.raml.maven.lintchecker;

import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule;

/**
* Exception that is thrown if any of the {@link LintCheckRule}s fail.
*/
public class LintCheckerException extends Exception {
public abstract class LintCheckerException extends Exception {

public LintCheckerException(final String message) {
super(message);
}

public LintCheckerException(final String message, final Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
package uk.gov.justice.raml.maven.lintchecker;
package uk.gov.justice.raml.maven.lintchecker.rules;

import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration;
import uk.gov.justice.raml.maven.lintchecker.LintCheckerException;

import org.raml.model.Raml;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.justice.raml.maven.lintchecker;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class LintCheckConfigurationTest {

@Mock
private MavenProject mavenProject;

@Mock
private Log log;

@Test
public void shouldCreateConfiguration() {

final LintCheckConfiguration lintCheckConfiguration = new LintCheckConfiguration(mavenProject, log);

assertThat(lintCheckConfiguration.getLog(), is(log));
assertThat(lintCheckConfiguration.getMavenProject(), is(mavenProject));


}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.justice.raml.maven.lintchecker;

import org.junit.Test;

public class LintCheckPluginExceptionTest {

@Test
public void shouldCreateException() {
final LintCheckPluginException exception = new LintCheckPluginException("test");
new LintCheckPluginException("test", exception);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.gov.justice.raml.maven.lintchecker;

import org.junit.Test;

public class LintCheckRuleFailedExceptionTest {


@Test
public void shouldCreateException() {
final LintCheckerException exception = new LintCheckRuleFailedException("test");
new LintCheckRuleFailedException("test", exception);
}

}
42 changes: 37 additions & 5 deletions raml-maven-plugin-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,48 @@
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>uk.gov.justice.maven</groupId>
<artifactId>raml-maven</artifactId>
<version>1.5.0-SNAPSHOT</version>
</parent>

<modelVersion>4.0.0</modelVersion>
<groupId>uk.gov.justice.maven</groupId>
<artifactId>raml-maven-plugin-it</artifactId>
<version>1.4.0-SNAPSHOT</version>

<!-- TODO add parent after extracting framewrok specific parent pom that does not contain raml-maven-plugin configuration-->
<!-- remember to manually update version -->

<build>
<plugins>
<!--
we need to override the maven enforcer rules here because
we are testing with snapshot plugin versions
-->
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-rules</id>
<phase>none</phase>
</execution>
<execution>
<id>enforce-rules-local</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<!--Available rules - http://maven.apache.org/enforcer/enforcer-rules/-->
<rules>
<requireJavaVersion>
<version>${enforcer.java.version.range}</version>
</requireJavaVersion>
<requireMavenVersion>
<version>${enforcer.maven.version.range}</version>
</requireMavenVersion>
<dependencyConvergence/>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion raml-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.4</version>
<version>3.5</version>
<configuration>
<skip>false</skip>
<goalPrefix>raml</goalPrefix>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package uk.gov.justice.raml.maven.lintchecker;
package uk.gov.justice.raml.maven.lintchecker.processor;

import uk.gov.justice.raml.io.FileTreeScanner;
import uk.gov.justice.raml.io.files.parser.RamlFileParser;
import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration;
import uk.gov.justice.raml.maven.lintchecker.LintCheckerException;
import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule;

import java.io.File;
import java.io.IOException;
Expand All @@ -24,19 +27,16 @@ public LintCheckGoalProcessor(
this.fileTreeScanner = fileTreeScanner;
}

public void execute(
final File sourceDirectory,
final List<LintCheckRule> rules,
final List<String> includes,
final List<String> excludes) throws MojoExecutionException {
public void execute(final LintCheckerGoalConfig lintCheckerGoalConfig) throws MojoExecutionException {

final Collection<Path> paths = getPaths(sourceDirectory, includes, excludes);
final Collection<Raml> ramls = ramlFileParser.ramlOf(sourceDirectory.toPath(), paths);
final Collection<Path> paths = getPaths(lintCheckerGoalConfig.getSourceDirectory(), lintCheckerGoalConfig.getIncludes(), lintCheckerGoalConfig.getExcludes());
final Collection<Raml> ramls = ramlFileParser.ramlOf(lintCheckerGoalConfig.getSourceDirectory().toPath(), paths);

for (final Raml raml : ramls) {
for (final LintCheckRule rule : rules) {
for (final LintCheckRule rule : lintCheckerGoalConfig.getRules()) {
try {
rule.execute(raml, new LintCheckConfiguration());
rule.execute(raml, new LintCheckConfiguration(lintCheckerGoalConfig.getCurrentProject(),
lintCheckerGoalConfig.getLog()));
} catch (final LintCheckerException e) {
throw new MojoExecutionException("Lint checker rule failed for rule " + rule.getClass().getSimpleName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package uk.gov.justice.raml.maven.lintchecker.processor;

import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckRule;

import java.io.File;
import java.util.List;

import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;

public class LintCheckerGoalConfig {
private final File sourceDirectory;
private final List<LintCheckRule> rules;
private final List<String> includes;
private final List<String> excludes;
private final MavenProject currentProject;
private final Log log;

public LintCheckerGoalConfig(final File sourceDirectory,
final List<LintCheckRule> rules,
final List<String> includes,
final List<String> excludes,
final MavenProject currentProject,
final Log log) {
this.sourceDirectory = sourceDirectory;
this.rules = rules;
this.includes = includes;
this.excludes = excludes;
this.currentProject = currentProject;
this.log = log;
}

public File getSourceDirectory() {
return sourceDirectory;
}

public List<LintCheckRule> getRules() {
return rules;
}

public List<String> getIncludes() {
return includes;
}

public List<String> getExcludes() {
return excludes;
}

public MavenProject getCurrentProject() {
return currentProject;
}

public Log getLog() {
return log;
}
}
Loading

0 comments on commit ead2de5

Please sign in to comment.