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

Commit

Permalink
Add an option to look for RAML files in the classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
jaceko committed May 4, 2016
1 parent ab6f2da commit c4356f0
Show file tree
Hide file tree
Showing 40 changed files with 739 additions and 152 deletions.
16 changes: 15 additions & 1 deletion io-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
<artifactId>plexus-utils</artifactId>
<version>3.0.22</version>
</dependency>

<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -32,6 +35,17 @@
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>uk.gov.justice.maven</groupId>
<artifactId>raml-for-testing-io</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

</project>
32 changes: 32 additions & 0 deletions io-utils/src/main/java/uk/gov/justice/raml/io/AntPathMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package uk.gov.justice.raml.io;

import com.google.common.base.Predicate;

import static java.io.File.separatorChar;
import static org.codehaus.plexus.util.SelectorUtils.matchPath;

/**
* matches resource path against Ant url pattern
*/
public class AntPathMatcher implements Predicate<String> {
private final String antPattern;

/**
* @param antPattern - ant pattern to be used in matching
*/
public AntPathMatcher(final String antPattern) {
this.antPattern = antPattern;
}

/**
* Matches path against the antPattern
*
* @param path - resource path
* @return - returns true if the path matches the pattern provided in the constructor, false otherwise
*/
@Override
public boolean apply(final String path) {
return matchPath(antPattern.replace('/', separatorChar), path);
}
}

68 changes: 53 additions & 15 deletions io-utils/src/main/java/uk/gov/justice/raml/io/FileTreeScanner.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package uk.gov.justice.raml.io;

import org.codehaus.plexus.util.DirectoryScanner;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

import static java.lang.String.format;
import static java.util.Arrays.*;
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;

/**
* Utility class for searching directories.
*/
public class FileTreeScanner {

private static final Logger LOGGER = LoggerFactory.getLogger(FileTreeScanner.class);
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
Expand All @@ -31,18 +45,42 @@ public class FileTreeScanner {
* @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();
}
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());
}

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(i -> new AntPathMatcher(i)).collect(toList());
Predicate<String> includesFilter = notEmpty(includes)
? or(includesMatcher)
: new AntPathMatcher(RAML_PATTERN);

LOGGER.debug(format("Finding files in: %s", baseDir.toString()));
List<Predicate<String>> excludesMatcher = stream(excludes).map(i -> not(new AntPathMatcher(i))).collect(toList());
Predicate<String> excludesFilter = and(excludesMatcher);

DirectoryScanner scanner = new DirectoryScanner();
scanner.setBasedir(baseDir.toFile());
scanner.setIncludes(includes);
scanner.setExcludes(excludes);
scanner.scan();
return and(includesFilter, excludesFilter);
}

return asList(scanner.getIncludedFiles())
.stream()
.map(Paths::get)
.collect(Collectors.toList());
private boolean notEmpty(final String[] includes) {
return includes != null && includes.length > 0;
}

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

import com.google.common.base.Predicate;
import org.junit.Test;

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

public class AntPathMatcherTest {
private Predicate<String> antPathMatcher;

@Test
public void shouldMatchFilesWithExtension() throws Exception {
antPathMatcher = new AntPathMatcher("**/*.raml");
assertThat(antPathMatcher.apply("abc.raml"), is(true));
assertThat(antPathMatcher.apply("aaa-bcd.raml"), is(true));
assertThat(antPathMatcher.apply("aaa-bcd.aml"), is(false));
}

@Test
public void shouldMatchFilesEndingWithSpecificEnding() throws Exception {
antPathMatcher = new AntPathMatcher("**/*abc.raml");
assertThat(antPathMatcher.apply("abc.raml"), is(true));
assertThat(antPathMatcher.apply("aaa-aabc.raml"), is(true));
assertThat(antPathMatcher.apply("aaa-bcd.aml"), is(false));
}
}
122 changes: 120 additions & 2 deletions io-utils/src/test/java/uk/gov/justice/raml/io/FileTreeScannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import java.nio.file.Paths;
import java.util.Collection;

import static org.hamcrest.CoreMatchers.equalTo;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

/**
Expand All @@ -18,7 +20,7 @@ public class FileTreeScannerTest {

@Test
@SuppressWarnings("unchecked")
public void shouldFindOnlyMatchingFiles() throws Exception {
public void shouldFindMatchingFilesInBaseDir() throws Exception {

String[] includes = {"**/*.raml"};
String[] excludes = {"**/*ignore.raml"};
Expand All @@ -34,4 +36,120 @@ public void shouldFindOnlyMatchingFiles() throws Exception {
equalTo(Paths.get("example-2.raml")),
equalTo(Paths.get("subfolder/example-3.raml"))));
}

@Test
@SuppressWarnings("unchecked")
public void shouldFindMatchingFilesInClasspathIfBaseDirSetToCLASSPATH() throws Exception {

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

FileTreeScanner fileTreeResolver = new FileTreeScanner();

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

assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("raml/external-3.raml")),
equalTo(Paths.get("raml/external-4.raml")),
equalTo(Paths.get("raml/example-1.raml")),
equalTo(Paths.get("raml/example-2.raml")),
equalTo(Paths.get("raml/subfolder/example-3.raml"))
));
}

@Test
@SuppressWarnings("unchecked")
public void shouldFindMatchingFilesInClasspathIfBaseDirNULL() throws Exception {

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

FileTreeScanner fileTreeResolver = new FileTreeScanner();

Collection<Path> paths = fileTreeResolver.find(null, includes, excludes);

assertThat(paths, hasSize(5));
assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("raml/external-3.raml")),
equalTo(Paths.get("raml/external-4.raml")),
equalTo(Paths.get("raml/example-1.raml")),
equalTo(Paths.get("raml/example-2.raml")),
equalTo(Paths.get("raml/subfolder/example-3.raml"))));
}


@Test
@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();

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

assertThat(paths, hasSize(2));
assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("example-1.raml")),
equalTo(Paths.get("example-2.raml"))));
}

@Test
@SuppressWarnings("unchecked")
public void shouldIncludeRamlFilesByDefaultIfNoIncludesSpecified() throws Exception {

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

FileTreeScanner fileTreeResolver = new FileTreeScanner();

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

assertThat(paths, hasSize(4));
assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("ignore.raml")),
equalTo(Paths.get("subfolder/example-3.raml")),
equalTo(Paths.get("example-1.raml")),
equalTo(Paths.get("example-2.raml"))));

}

@Test
@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/");

FileTreeScanner fileTreeResolver = new FileTreeScanner();

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

assertThat(paths, hasSize(2));
assertThat(paths, containsInAnyOrder(
equalTo(Paths.get("example-1.raml")),
equalTo(Paths.get("subfolder/example-3.raml"))));
}

@Test
@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");

FileTreeScanner fileTreeResolver = new FileTreeScanner();

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

assertThat(paths, empty());
}


}
7 changes: 5 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>uk.gov.justice</groupId>
<artifactId>parent-pom</artifactId>
<version>1.6.4</version>
<version>1.6.6-SNAPSHOT</version>
</parent>

<groupId>uk.gov.justice.maven</groupId>
Expand All @@ -14,18 +14,21 @@
<packaging>pom</packaging>

<modules>
<module>raml-for-testing-io</module>
<module>raml-maven-plugin</module>
<module>raml-generator-core</module>
<module>io-utils</module>
<module>raml-parser</module>
<module>raml-maven-plugin-it</module>
<module>raml-generator-for-testing</module>
</modules>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>uk.gov.justice</groupId>
<artifactId>common-bom</artifactId>
<version>1.6.4</version>
<version>1.6.6-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
15 changes: 15 additions & 0 deletions raml-for-testing-io/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>raml-maven</artifactId>
<groupId>uk.gov.justice.maven</groupId>
<version>1.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>raml-for-testing-io</artifactId>


</project>
2 changes: 2 additions & 0 deletions raml-for-testing-io/src/main/resources/raml/external-3.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external-3.raml
2 changes: 2 additions & 0 deletions raml-for-testing-io/src/main/resources/raml/external-4.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external-4.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external-ignore.raml
2 changes: 2 additions & 0 deletions raml-for-testing-io/src/raml/external-1.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external-1.raml
2 changes: 2 additions & 0 deletions raml-for-testing-io/src/raml/external-2.raml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#%RAML 0.8
title: external-2.raml
Loading

0 comments on commit c4356f0

Please sign in to comment.