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

Commit

Permalink
Add configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
skiddykong committed Feb 24, 2017
1 parent 3a73f57 commit d961025
Show file tree
Hide file tree
Showing 12 changed files with 159 additions and 27 deletions.
4 changes: 4 additions & 0 deletions lint-checker-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
<artifactId>raml-parser</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</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
@@ -1,11 +1,29 @@
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 LintCheckerException() {
super();
}

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

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

public LintCheckerException(final Throwable cause) {
super(cause);
}

protected LintCheckerException(final String message, final Throwable cause, final boolean enableSuppression, final boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
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
2 changes: 1 addition & 1 deletion raml-maven-plugin-it/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>uk.gov.justice.maven</groupId>
<artifactId>raml-maven-plugin-it</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.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 -->
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(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;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
package uk.gov.justice.raml.maven.lintchecker;
package uk.gov.justice.raml.maven.lintchecker.rules;

import static java.lang.String.join;

import uk.gov.justice.raml.io.FileTreeScanner;
import uk.gov.justice.raml.io.files.parser.RamlFileParser;
import uk.gov.justice.raml.maven.common.BasicMojo;
import uk.gov.justice.raml.maven.lintchecker.processor.LintCheckGoalProcessor;
import uk.gov.justice.raml.maven.lintchecker.processor.LintCheckerGoalConfig;

import java.util.List;

import org.apache.maven.artifact.DependencyResolutionRequiredException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;


@Mojo(name = "lint-check")
@Mojo(name = "lint-check", requiresDependencyResolution = ResolutionScope.RUNTIME)
public class LintCheckMojo extends BasicMojo {

private final RamlFileParser ramlFileParser = new RamlFileParser();
private final FileTreeScanner fileTreeScanner = new FileTreeScanner();

/**
* The MavenSession
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
protected MavenSession session;

/**
* Array of objects that implement the LintCheckerRule interface to execute.
*/
@SuppressWarnings("unused")
@Parameter(required = true)
protected List<LintCheckRule> rules;


@Override
public void execute() throws MojoExecutionException {

Expand All @@ -33,10 +44,6 @@ public void execute() throws MojoExecutionException {
fileTreeScanner);

lintCheckGoalProcessor.execute(
sourceDirectory,
rules,
includes,
excludes
);
new LintCheckerGoalConfig(sourceDirectory, rules, includes, excludes, session.getCurrentProject(), getLog()));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.justice.raml.maven.lintchecker;
package uk.gov.justice.raml.maven.lintchecker.processor;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
Expand All @@ -14,17 +14,23 @@

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.nio.file.Path;
import java.util.Collection;
import java.util.List;

import org.apache.maven.plugin.MojoExecutionException;
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.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.raml.model.Raml;

Expand Down Expand Up @@ -59,7 +65,14 @@ public void shouldExecuteAllRulesOnAllRamls() throws Exception {
when(fileTreeScanner.find(sourceDirectory.toPath(), includes, excludes)).thenReturn(paths);
when(ramlFileParser.ramlOf(sourceDirectory.toPath(), paths)).thenReturn(ramls);

lintCheckGoalProcessor.execute(sourceDirectory, rules, asList(includes), asList(excludes));
lintCheckGoalProcessor.execute(
new LintCheckerGoalConfig(
sourceDirectory,
rules,
asList(includes),
asList(excludes),
mock(MavenProject.class),
mock(Log.class)));

verify(lintCheckRule_1).execute(eq(raml_1), any(LintCheckConfiguration.class));
verify(lintCheckRule_1).execute(eq(raml_2), any(LintCheckConfiguration.class));
Expand Down Expand Up @@ -91,7 +104,17 @@ public void shouldThrowMojoExecutionExceptionAIfExecutingARuleFails() throws Exc
doThrow(lintCheckerException).when(lintCheckRule_1).execute(eq(raml_1), any(LintCheckConfiguration.class));

try {
lintCheckGoalProcessor.execute(sourceDirectory, rules, asList(includes), asList(excludes));
lintCheckGoalProcessor
.execute
(
new LintCheckerGoalConfig(sourceDirectory,
rules,
asList(includes),
asList(excludes),
mock(MavenProject.class),
mock(Log.class)
)
);
fail();
} catch (final MojoExecutionException expected) {
assertThat(expected.getCause(), is(lintCheckerException));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.justice.raml.maven.lintchecker.rules;

import uk.gov.justice.raml.maven.lintchecker.LintCheckConfiguration;
import uk.gov.justice.raml.maven.lintchecker.LintCheckRule;
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
@@ -1,10 +1,12 @@
package uk.gov.justice.raml.maven.lintchecker;
package uk.gov.justice.raml.maven.lintchecker.rules;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import uk.gov.justice.raml.maven.lintchecker.LintCheckerException;
import uk.gov.justice.raml.maven.lintchecker.rules.FailingLintCheckRule;
import uk.gov.justice.raml.maven.lintchecker.rules.LintCheckMojo;
import uk.gov.justice.raml.maven.lintchecker.rules.SucceedingLintCheckRule;
import uk.gov.justice.raml.maven.test.utils.BetterAbstractMojoTestCase;

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

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

import org.raml.model.Raml;
Expand Down

0 comments on commit d961025

Please sign in to comment.