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

Commit

Permalink
Merge ca1bfc3 into 14e686f
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Aug 21, 2019
2 parents 14e686f + ca1bfc3 commit 319df85
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import uk.gov.justice.maven.generator.io.files.parser.FileParser;
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;
import uk.gov.justice.maven.generator.io.files.parser.io.UrlsToScanFinder;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -22,7 +25,6 @@
*/
public class GenerateGoalProcessor {
private static final Logger LOGGER = getLogger(GenerateGoalProcessor.class);
private static final String CLASSPATH = "CLASSPATH";

private final MojoGeneratorFactory mojoGeneratorFactory;
private final FileParser parser;
Expand Down Expand Up @@ -52,35 +54,37 @@ public void generate(final GenerateGoalConfig config) throws IOException {
if (config.getGenerationPath()==GenerateMojo.GenerationPath.SOURCE_AND_CLASS_PATH) {
final Map<Path, Collection<Path>> combinedPaths = new HashMap<>();

classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);
classPaths = getPaths(Paths.get("CLASSPATH"), includes, excludes);

combinedPaths.put(config.getSourceDirectory(), paths);
combinedPaths.put((Paths.get(format("%s/%s", config.getSourceDirectory().toString(), CLASSPATH))), classPaths);
combinedPaths.put((Paths.get(format("%s/%s", config.getSourceDirectory().toString(), "CLASSPATH"))), classPaths);

combinedPaths.forEach((baseDir, combinedPath) ->
parseSourceDirectory(config, generatorConfig, combinedPath, isPathFromClasspath(baseDir, config.getGenerationPath())));
} else {
if ((config.getGenerationPath()==GenerateMojo.GenerationPath.CLASSPATH)) {
classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);
classPaths = getPaths(Paths.get("CLASSPATH"), includes, excludes);

parseSourceDirectory(config, generatorConfig,
classPaths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
} else {

if(StringUtils.isNoneBlank(config.getSourceDirectory().toString()) && !config.getSourceDirectory().toString().contains(CLASSPATH)){
if(StringUtils.isNoneBlank(config.getSourceDirectory().toString()) && !config.getSourceDirectory().toString().contains("CLASSPATH")){
parseSourceDirectory(config, generatorConfig, paths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
}
else{
classPaths = getPaths(Paths.get(CLASSPATH), includes, excludes);
classPaths = getPaths(Paths.get("CLASSPATH"), includes, excludes);

parseSourceDirectory(config, generatorConfig, classPaths, isPathFromClasspath(config.getSourceDirectory(), config.getGenerationPath()));
}
}
}
}

private Collection<Path> getPaths(final Path path, final String[] includes, final String[] excludes) throws IOException {
return scannerFactory.create().find(path, includes, excludes);
private Collection<Path> getPaths(final Path baseDir, final String[] includes, final String[] excludes) throws IOException {

final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
return scannerFactory.create().find(urlsToScan, includes, excludes);
}

private void parseSourceDirectory(final GenerateGoalConfig config, final GeneratorConfig generatorConfig,
Expand All @@ -91,11 +95,11 @@ private void parseSourceDirectory(final GenerateGoalConfig config, final Generat
}

private Path getRequiredPath(final GenerateGoalConfig config) {
return config.getSourceDirectory().toString().contains(CLASSPATH)? config.getSourceDirectory():Paths.get(format("%s/%s",config.getSourceDirectory().toString(),CLASSPATH));
return config.getSourceDirectory().toString().contains("CLASSPATH")? config.getSourceDirectory():Paths.get(format("%s/%s",config.getSourceDirectory().toString(), "CLASSPATH"));
}

private boolean isPathFromClasspath(final Path baseDir, final GenerateMojo.GenerationPath generationPath){
return baseDir.toString().contains(CLASSPATH) || (generationPath != null && generationPath.name()==(CLASSPATH));
return baseDir.toString().contains(("CLASSPATH")) || (generationPath != null && "CLASSPATH".equals(generationPath.name()));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import uk.gov.justice.maven.generator.io.files.parser.core.GeneratorConfig;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScanner;
import uk.gov.justice.maven.generator.io.files.parser.io.FileTreeScannerFactory;
import uk.gov.justice.maven.generator.io.files.parser.io.UrlsToScanFinder;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -88,7 +90,9 @@ public void shouldCallGeneratorWithRamlFilesAndConfig() throws Exception {
File ramlFile2 = sourceDirectory.newFile("file2.raml");
String ramlString2 = "#%RAML 0.8\nbaseUri: \"http://b:8080/\"\n";
write(ramlFile2, ramlString2);
when(scanner.find(sourceDirectory.getRoot().toPath(), includes, excludes))
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(sourceDirectory.getRoot().toPath());

when(scanner.find(urlsToScan, includes, excludes))
.thenReturn(asList(ramlFile.toPath(), ramlFile2.toPath()));

generateGoalProcessor.generate(config);
Expand All @@ -115,7 +119,8 @@ public void shouldCallGeneratorWithRamlFilesAndConfig() throws Exception {
public void shouldCallGeneratorWithEmptyRamlForEmptyFile() throws Exception {
File ramlFile = sourceDirectory.newFile("file3.raml");

when(scanner.find(sourceDirectory.getRoot().toPath(), includes, excludes))
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(sourceDirectory.getRoot().toPath());
when(scanner.find(urlsToScan, includes, excludes))
.thenReturn(singletonList(ramlFile.toPath()));

generateGoalProcessor.generate(config);
Expand Down Expand Up @@ -143,7 +148,8 @@ public void shouldPassPatternsToFileTreeScanner() throws Exception {
String ramlString1 = "#%RAML 0.8\nbaseUri: \"http://c:8080/\"\n";
write(ramlFile, ramlString1);

when(scanner.find(sourceDirectory.getRoot().toPath(), customIncludes, customExcludes))
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(sourceDirectory.getRoot().toPath());
when(scanner.find(urlsToScan, customIncludes, customExcludes))
.thenReturn(singletonList(ramlFile.toPath()));

generateGoalProcessor.generate(config);
Expand All @@ -158,9 +164,9 @@ public void shouldPassPatternsToFileTreeScanner() throws Exception {
}

@Test
public void shouldNotInstatiateGeneratorIfNoRamlFilesToProcess() throws IOException {
public void shouldNotInstantiateGeneratorIfNoRamlFilesToProcess() throws IOException {

when(scanner.find(any(Path.class), any(String[].class), any(String[].class))).thenReturn(emptyList());
when(scanner.find(any(List.class), any(String[].class), any(String[].class))).thenReturn(emptyList());
generateGoalProcessor.generate(config);

verifyZeroInteractions(mojoGeneratorFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;
Expand All @@ -29,51 +28,38 @@
*/
public class FileTreeScanner {

private static final String CLASSPATH = "CLASSPATH";
private static final String RAML_PATTERN = "**/*.raml";

/**
* Finding all files within a directory that fulfil a set of include and exclude patterns, using standard
* Ant patterns - {@see http://ant.apache.org/manual/dirtasks.html#patterns}.
*
* @param baseDir the path to search under
* @param urlsToScan Either the base directory or a list of URLs from the classpath
* @param includes the path patterns to include
* @param excludes the path patterns to exclude
* @return a list of paths to matching files under the specified directory
*/
public Collection<Path> find(final Path baseDir, final String[] includes, final String[] excludes) throws IOException {
if (!shouldSearchOnClasspath(baseDir) && !baseDir.toFile().exists()) {
return emptyList();
}
public Collection<Path> find(final List<URL> urlsToScan, final String[] includes, final String[] excludes) throws IOException {

final ConfigurationBuilder configuration = new ConfigurationBuilder()
.filterInputsBy(filterOf(includes, excludes))
.setUrls(urlsToScan)
.setScanners(new ResourcesScanner());

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

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

private Collection<URL> urlsToScan(final Path baseDir) throws MalformedURLException {
return shouldSearchOnClasspath(baseDir)
? ClasspathHelper.forClassLoader()
: asList(baseDir.toUri().toURL());
}

private boolean shouldSearchOnClasspath(final Path baseDir) {
return baseDir == null || baseDir.toString().contains(CLASSPATH);
}

private Predicate<String> filterOf(final String[] includes, final String[] excludes) {
List<AntPathMatcher> includesMatcher = stream(includes).map(AntPathMatcher::new).collect(toList());
Predicate<String> includesFilter = notEmpty(includes)
final List<AntPathMatcher> includesMatcher = stream(includes).map(AntPathMatcher::new).collect(toList());
final Predicate<String> includesFilter = notEmpty(includes)
? or(includesMatcher)
: new AntPathMatcher(RAML_PATTERN);

List<Predicate<String>> excludesMatcher = stream(excludes).map(i -> not(new AntPathMatcher(i))).collect(toList());
Predicate<String> excludesFilter = and(excludesMatcher);
final List<Predicate<String>> excludesMatcher = stream(excludes).map(i -> not(new AntPathMatcher(i))).collect(toList());
final Predicate<String> excludesFilter = and(excludesMatcher);

return and(includesFilter, excludesFilter);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.gov.justice.maven.generator.io.files.parser.io;

import static java.util.Collections.singletonList;
import static org.reflections.util.ClasspathHelper.forClassLoader;

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

public class UrlsToScanFinder {

public List<URL> urlsToScan(final Path baseDir) throws MalformedURLException {

if(shouldSearchOnClasspath(baseDir)) {
return new ArrayList<>(forClassLoader());
}

return singletonList(baseDir.toUri().toURL());
}

private boolean shouldSearchOnClasspath(final Path baseDir) {
return baseDir == null || baseDir.toString().contains("CLASSPATH");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

Expand All @@ -21,13 +25,14 @@ public class FileTreeScannerTest {
@SuppressWarnings("unchecked")
public void shouldFindMatchingFilesInBaseDir() throws Exception {

String[] includes = {"**/*.raml"};
String[] excludes = {"**/*ignore.raml"};
Path baseDir = Paths.get("src/test/resources/raml/");
final String[] includes = {"**/*.raml"};
final String[] excludes = {"**/*ignore.raml"};
final Path baseDir = Paths.get(getRamlDirectory());
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);

FileTreeScanner fileTreeResolver = new FileTreeScanner();
final FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, hasSize(3));
assertThat(paths, containsInAnyOrder(
Expand All @@ -40,13 +45,14 @@ public void shouldFindMatchingFilesInBaseDir() throws Exception {
@SuppressWarnings("unchecked")
public void shouldFindMatchingFilesInClasspathIfBaseDirSetToCLASSPATH() throws Exception {

String[] includes = {"**/*.raml"};
String[] excludes = {"**/*ignore.raml"};
Path baseDir = Paths.get("CLASSPATH");
final String[] includes = {"**/*.raml"};
final String[] excludes = {"**/*ignore.raml"};
final Path baseDir = Paths.get("CLASSPATH");

FileTreeScanner fileTreeResolver = new FileTreeScanner();
final FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("raml/external-3.raml")),
Expand All @@ -61,12 +67,12 @@ public void shouldFindMatchingFilesInClasspathIfBaseDirSetToCLASSPATH() throws E
@SuppressWarnings("unchecked")
public void shouldFindMatchingFilesInClasspathIfBaseDirNULL() throws Exception {

String[] includes = {"**/*.raml"};
String[] excludes = {"**/*ignore.raml"};
final String[] includes = {"**/*.raml"};
final String[] excludes = {"**/*ignore.raml"};

FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(null, includes, excludes);
final FileTreeScanner fileTreeResolver = new FileTreeScanner();
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(null);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, hasSize(5));
assertThat(paths, containsInAnyOrder(
Expand All @@ -82,13 +88,13 @@ public void shouldFindMatchingFilesInClasspathIfBaseDirNULL() throws Exception {
@SuppressWarnings("unchecked")
public void shouldIncludeMultipleFiles() throws Exception {

String[] includes = {"**/*example-1.raml", "**/*example-2.raml"};
String[] excludes = {};
Path baseDir = Paths.get("src/test/resources/raml/");

FileTreeScanner fileTreeResolver = new FileTreeScanner();
final String[] includes = {"**/*example-1.raml", "**/*example-2.raml"};
final String[] excludes = {};

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final Path baseDir = Paths.get(getRamlDirectory());
final FileTreeScanner fileTreeResolver = new FileTreeScanner();
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, hasSize(2));
assertThat(paths, containsInAnyOrder(
Expand All @@ -100,13 +106,15 @@ public void shouldIncludeMultipleFiles() throws Exception {
@SuppressWarnings("unchecked")
public void shouldIncludeRamlFilesByDefaultIfNoIncludesSpecified() throws Exception {

String[] includes = {};
String[] excludes = {};
Path baseDir = Paths.get("src/test/resources/raml/");
final String[] includes = {};
final String[] excludes = {};

final Path baseDir = Paths.get(getRamlDirectory());

FileTreeScanner fileTreeResolver = new FileTreeScanner();
final FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, hasSize(4));
assertThat(paths, containsInAnyOrder(
Expand All @@ -121,13 +129,14 @@ public void shouldIncludeRamlFilesByDefaultIfNoIncludesSpecified() throws Except
@SuppressWarnings("unchecked")
public void shouldExcludeMultipleFiles() throws Exception {

String[] includes = {"**/*raml"};
String[] excludes = {"**/*ignore.raml", "**/*example-2.raml"};
Path baseDir = Paths.get("src/test/resources/raml/");
final String[] includes = {"**/*raml"};
final String[] excludes = {"**/*ignore.raml", "**/*example-2.raml"};
final Path baseDir = Paths.get(getRamlDirectory());

FileTreeScanner fileTreeResolver = new FileTreeScanner();
final FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, hasSize(2));
assertThat(paths, containsInAnyOrder(
Expand All @@ -139,16 +148,18 @@ public void shouldExcludeMultipleFiles() throws Exception {
@SuppressWarnings("unchecked")
public void shouldReturnEmptyCollectionIfBaseDirDoesNotExist() throws Exception {

String[] includes = {"**/*.messaging.raml"};
String[] excludes = {"**/*external-ignore.raml"};
Path baseDir = Paths.get("C:\\workspace-moj\\raml-maven\\raml-maven-plugin-it\\src\\raml");
final String[] includes = {"**/*.messaging.raml"};
final String[] excludes = {"**/*external-ignore.raml"};
final Path baseDir = Paths.get("C:\\workspace-moj\\raml-maven\\raml-maven-plugin-it\\src\\raml");

FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(baseDir, includes, excludes);
final FileTreeScanner fileTreeResolver = new FileTreeScanner();
final List<URL> urlsToScan = new UrlsToScanFinder().urlsToScan(baseDir);
final Collection<Path> paths = fileTreeResolver.find(urlsToScan, includes, excludes);

assertThat(paths, empty());
}


private URI getRamlDirectory() throws URISyntaxException {
return getClass().getClassLoader().getResource("raml").toURI();
}
}
Loading

0 comments on commit 319df85

Please sign in to comment.