Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected Exclude pattern behavior with Maven to avoid excluding all files #340

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
corrected problem with directory excludes excluding all directories.
  • Loading branch information
ctrimble committed Apr 28, 2015
commit bee35eb4a1f762970f09b7124e19fcb101eda42f
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,34 @@
* @author Christian Trimble
*/
public class FilteringIT {
URL filteredSchemaUrl;

@Before
public void setUp() throws MalformedURLException {
filteredSchemaUrl = new File("./src/test/resources/schema/filtering").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");
}
}
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
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"
}
}
}