From bee35eb4a1f762970f09b7124e19fcb101eda42f Mon Sep 17 00:00:00 2001 From: Christian Trimble Date: Mon, 27 Apr 2015 12:37:42 -0700 Subject: [PATCH 1/2] corrected problem with directory excludes excluding all directories. --- .../integration/filtering/FilteringIT.java | 31 +++++++- .../util/CodeGenerationHelper.java | 8 +++ .../resources/schema/filtering/.svn/svn.json | 10 +++ .../resources/schema/filtering/excluded.json | 11 ++- .../resources/schema/filtering/sub/sub.json | 10 +++ .../maven/MatchPatternsFileFilter.java | 2 +- .../maven/MatchPatternsFileFilterTest.java | 70 +++++++++++++++++++ .../schema/.svn/sub/sub-svn-file.json | 9 +++ .../filtered/schema/.svn/svn-file.json | 9 +++ 9 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/.svn/svn.json create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub.json create mode 100644 jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/sub/sub-svn-file.json create mode 100644 jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/svn-file.json diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java index e97a9582e..3a04313d0 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java @@ -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; /** @@ -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"); + } } diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java index 995ff6c72..57bc1ec4d 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/util/CodeGenerationHelper.java @@ -170,6 +170,14 @@ public static ClassLoader generateAndCompile(String schema, String targetPackage } + public static ClassLoader generateAndCompile(URL schema, String targetPackage, Map configValues) { + + File outputDirectory = generate(schema, targetPackage, configValues); + + return compile(outputDirectory); + + } + public static File createTemporaryOutputFolder() { String tempDirectoryName = System.getProperty("java.io.tmpdir"); diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/.svn/svn.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/.svn/svn.json new file mode 100644 index 000000000..9275a8b6a --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/.svn/svn.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "a location on s3", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/excluded.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/excluded.json index c182d4112..0eb6b5cc7 100644 --- a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/excluded.json +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/excluded.json @@ -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" + } + } +} diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub.json new file mode 100644 index 000000000..456378e56 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "a generic sub document", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/MatchPatternsFileFilter.java b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/MatchPatternsFileFilter.java index 80738e840..36eef14d1 100644 --- a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/MatchPatternsFileFilter.java +++ b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/MatchPatternsFileFilter.java @@ -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; diff --git a/jsonschema2pojo-maven-plugin/src/test/java/org/jsonschema2pojo/maven/MatchPatternsFileFilterTest.java b/jsonschema2pojo-maven-plugin/src/test/java/org/jsonschema2pojo/maven/MatchPatternsFileFilterTest.java index cd7901285..e9c8f26be 100644 --- a/jsonschema2pojo-maven-plugin/src/test/java/org/jsonschema2pojo/maven/MatchPatternsFileFilterTest.java +++ b/jsonschema2pojo-maven-plugin/src/test/java/org/jsonschema2pojo/maven/MatchPatternsFileFilterTest.java @@ -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 { @@ -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); } diff --git a/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/sub/sub-svn-file.json b/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/sub/sub-svn-file.json new file mode 100644 index 000000000..e4f6554e7 --- /dev/null +++ b/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/sub/sub-svn-file.json @@ -0,0 +1,9 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/svn-file.json b/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/svn-file.json new file mode 100644 index 000000000..01ea3e1e0 --- /dev/null +++ b/jsonschema2pojo-maven-plugin/src/test/resources/filtered/schema/.svn/svn-file.json @@ -0,0 +1,9 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "subProp": { + "$ref": "sub/sub-svn-file.json" + } + } +} From f3c8dedc3993ff4bef0f1d8abf5c2948bc7ac26e Mon Sep 17 00:00:00 2001 From: Christian Trimble Date: Tue, 28 Apr 2015 00:36:42 -0700 Subject: [PATCH 2/2] apply default excludes anytime a maven plugin is configured with sourceDirectory and not sourcePaths. --- .../integration/filtering/FilteringIT.java | 11 +++++++++++ .../test/resources/schema/filtering/sub/.svn/sub.json | 10 ++++++++++ .../resources/schema/filtering/sub/sub2/.svn/sub.json | 10 ++++++++++ .../test/resources/schema/filtering/sub/sub2/sub.json | 10 ++++++++++ .../jsonschema2pojo/maven/Jsonschema2PojoMojo.java | 6 +++++- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/.svn/sub.json create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/.svn/sub.json create mode 100644 jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/sub.json diff --git a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java index 3a04313d0..dff1c96d6 100644 --- a/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java +++ b/jsonschema2pojo-integration-tests/src/test/java/org/jsonschema2pojo/integration/filtering/FilteringIT.java @@ -34,10 +34,12 @@ */ 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 @@ -63,4 +65,13 @@ public void shouldIncludeNestedFilesWithFiltering() throws ClassNotFoundExceptio 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"); + } } diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/.svn/sub.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/.svn/sub.json new file mode 100644 index 000000000..456378e56 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/.svn/sub.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "a generic sub document", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/.svn/sub.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/.svn/sub.json new file mode 100644 index 000000000..456378e56 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/.svn/sub.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "a generic sub document", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/sub.json b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/sub.json new file mode 100644 index 000000000..456378e56 --- /dev/null +++ b/jsonschema2pojo-integration-tests/src/test/resources/schema/filtering/sub/sub2/sub.json @@ -0,0 +1,10 @@ +{ + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "a generic sub document", + "type": "object", + "properties": { + "prop": { + "type": "string" + } + } +} diff --git a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java index 20f3a0f04..1bde91da6 100644 --- a/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java +++ b/jsonschema2pojo-maven-plugin/src/main/java/org/jsonschema2pojo/maven/Jsonschema2PojoMojo.java @@ -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(); }