Skip to content

Commit

Permalink
Merge pull request #340 from ctrimble/feature_recursive-include-patterns
Browse files Browse the repository at this point in the history
Corrected Exclude Pattern Behavior with Maven.
  • Loading branch information
joelittlejohn committed Apr 29, 2015
2 parents 81adfe7 + f3c8ded commit aa643cf
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.config;
import static org.jsonschema2pojo.integration.util.CodeGenerationHelper.generateAndCompile;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;

/**
Expand All @@ -27,11 +33,45 @@
* @author Christian Trimble
*/
public class FilteringIT {
URL filteredSchemaUrl;
URL subSchemaUrl;

@Before
public void setUp() throws MalformedURLException {
filteredSchemaUrl = new File("./src/test/resources/schema/filtering").toURI().toURL();
subSchemaUrl = new File("./src/test/resources/schema/filtering/sub").toURI().toURL();
}

@Test
public void shouldFilterFiles() throws ClassNotFoundException {
ClassLoader resultsClassLoader = generateAndCompile("/schema/filtering", "com.example",
ClassLoader resultsClassLoader = generateAndCompile(filteredSchemaUrl, "com.example",
config("includes", new String[] { "**/*.json" }, "excludes", new String[] { "excluded.json" }));

resultsClassLoader.loadClass("com.example.Included");
}

@Test(expected=ClassNotFoundException.class)
public void shouldNotProcessExcludedFiles() throws ClassNotFoundException {
ClassLoader resultsClassLoader = generateAndCompile(filteredSchemaUrl, "com.example",
config("includes", new String[] { "**/*.json" }, "excludes", new String[] { "excluded.json" }));

resultsClassLoader.loadClass("com.example.Excluded");
}

@Test
public void shouldIncludeNestedFilesWithFiltering() throws ClassNotFoundException {
ClassLoader resultsClassLoader = generateAndCompile(filteredSchemaUrl, "com.example",
config("includes", new String[] { "**/*.json" }, "excludes", new String[] { "excluded.json" }));

resultsClassLoader.loadClass("com.example.sub.Sub");
}

@Test
public void shouldUseDefaultExcludesWithoutIncludesAndExcludes() throws ClassNotFoundException {
ClassLoader resultsClassLoader = generateAndCompile(subSchemaUrl, "com.example.sub",
config("includes", new String[] {}, "excludes", new String[] {}));

resultsClassLoader.loadClass("com.example.sub.Sub");
resultsClassLoader.loadClass("com.example.sub.sub2.Sub");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ public static ClassLoader generateAndCompile(String schema, String targetPackage

}

public static ClassLoader generateAndCompile(URL schema, String targetPackage, Map<String, Object> configValues) {

File outputDirectory = generate(schema, targetPackage, configValues);

return compile(outputDirectory);

}

public static File createTemporaryOutputFolder() {

String tempDirectoryName = System.getProperty("java.io.tmpdir");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a location on s3",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
an invalid schema file that should not be included.
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a schema that should be exluded.",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a generic sub document",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a generic sub document",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a generic sub document",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "a generic sub document",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,16 @@ public void execute() throws MojoExecutionException {
throw new MojoExecutionException("One of sourceDirectory or sourcePaths must be provided");
}

if (filteringEnabled()) {
if (filteringEnabled() || (sourceDirectory != null && sourcePaths == null) ) {

if (sourceDirectory == null) {
throw new MojoExecutionException("Source includes and excludes require the sourceDirectory property");
}

if( sourcePaths != null ) {
throw new MojoExecutionException("Source includes and excludes are incompatible with the sourcePaths property");
}

fileFilter = createFileFilter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public boolean accept(File file) {
try {
String path = relativePath(file);
return file.isDirectory() ?
includePatterns.matchesPatternStart(path, caseSensitive) && !excludePatterns.matchesPatternStart(path, caseSensitive) :
includePatterns.matchesPatternStart(path, caseSensitive) && !excludePatterns.matches(path, caseSensitive) :
includePatterns.matches(path, caseSensitive) && !excludePatterns.matches(path, caseSensitive);
} catch (IOException e) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,24 @@ public void shouldIncludeMatchesAndDirectoriesWhenIncluding() throws IOException
equalTo(file("example.json"))));
}

@SuppressWarnings("unchecked")
@Test
public void shouldIncludeMatchesAndDirectoriesWhenIncludingAndDefaultExcludes() throws IOException {
fileFilter = new MatchPatternsFileFilter.Builder()
.addIncludes(asList("**/*.json"))
.addDefaultExcludes()
.withSourceDirectory(basedir.getCanonicalPath())
.build();

File[] files = basedir.listFiles(fileFilter);

assertThat("all of the files were found.", asList(files),
hasItems(
equalTo(file("sub1")),
equalTo(file("excluded")),
equalTo(file("example.json"))));
}

@SuppressWarnings("unchecked")
@Test
public void shouldNoIncludedUnmatchedFiles() throws IOException {
Expand Down Expand Up @@ -123,6 +141,58 @@ public void shouldExcludeDirectories() throws IOException {
assertThat("the markdown file was not found.", asList(files), not(hasItem(file("excluded"))));
}

@Test
public void ahouldNotExcludeRegularDirectoriesWithDefaultExcludes() throws IOException {
fileFilter = new MatchPatternsFileFilter.Builder()
.addDefaultExcludes()
.addIncludes(asList("**"))
.withSourceDirectory(basedir.getCanonicalPath())
.build();

File[] files = basedir.listFiles(fileFilter);

assertThat("the sub directory was not found.", asList(files), hasItem(file("excluded")));
}

@Test
public void shouldExcludeSvnDirectoriesWithDefaultExcludes() throws IOException {
fileFilter = new MatchPatternsFileFilter.Builder()
.addDefaultExcludes()
.addIncludes(asList("**"))
.withSourceDirectory(basedir.getCanonicalPath())
.build();

File[] files = basedir.listFiles(fileFilter);

assertThat("the files in .svn directory were execluded.", asList(files), not(hasItems(file(".svn"))));
}

@Test
public void shouldExcludeFilesInSvnDirectoriesWithDefaultExcludes() throws IOException {
fileFilter = new MatchPatternsFileFilter.Builder()
.addDefaultExcludes()
.addIncludes(asList("**/*.json"))
.withSourceDirectory(basedir.getCanonicalPath())
.build();

File[] files = new File(basedir, ".svn").listFiles(fileFilter);

assertThat("the files in .svn directory were execluded.", asList(files), not(hasItems(file("svn-file.json"))));
}

@Test
public void shouldExcludeNestedFilesInSvnDirectoriesWithDefaultExcludes() throws IOException {
fileFilter = new MatchPatternsFileFilter.Builder()
.addDefaultExcludes()
.addIncludes(asList("**/*.json"))
.withSourceDirectory(basedir.getCanonicalPath())
.build();

File[] files = new File(basedir, ".svn/sub").listFiles(fileFilter);

assertThat("the files in .svn directory were execluded.", asList(files), not(hasItems(file("sub-svn-file.json"))));
}

private File file(String relativePath) {
return new File(basedir, relativePath);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"prop": {
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"subProp": {
"$ref": "sub/sub-svn-file.json"
}
}
}

0 comments on commit aa643cf

Please sign in to comment.