Enhance JarCommand to support lists of includes and excludes

The lists are comma separated. In addition, user can add prefixes
"+" or "-", to signal that those values should be removed from the
default list, not added to a fresh one. E.g.

$ spring jar app.jar --include lib/*.jar,-static/** --exclude -**/*.jar

to include a jar file specifically, and make sure it is not excluded,
and additionally not include the static/** resources that would otherwise
be included in the defaults. As soon as "+" or "-" prefixes are detected
the default entries are all added (except the ones exlcuded with "-").

Fixes gh-1090
This commit is contained in:
Dave Syer
2014-06-12 18:08:33 +01:00
parent d842446186
commit 2c691e5ae5
6 changed files with 142 additions and 20 deletions

View File

@@ -20,16 +20,19 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Test;
import org.springframework.boot.cli.command.jar.ResourceMatcher.MatchedResource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@@ -40,16 +43,26 @@ import static org.junit.Assert.assertTrue;
*/
public class ResourceMatcherTests {
private final ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList(
"alpha/**", "bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded"));
@Test
public void nonExistentRoot() throws IOException {
List<MatchedResource> matchedResources = this.resourceMatcher.find(Arrays
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("alpha/**",
"bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded"));
List<MatchedResource> matchedResources = resourceMatcher.find(Arrays
.asList(new File("does-not-exist")));
assertEquals(0, matchedResources.size());
}
@SuppressWarnings("unchecked")
@Test
public void defaults() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList(""),
Arrays.asList(""));
assertTrue(((Collection<String>) ReflectionTestUtils.getField(resourceMatcher,
"includes")).contains("static/**"));
assertTrue(((Collection<String>) ReflectionTestUtils.getField(resourceMatcher,
"excludes")).contains("**/*.jar"));
}
@Test
public void excludedWins() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"),
@@ -59,6 +72,40 @@ public class ResourceMatcherTests {
assertThat(found, not(hasItem(new FooJarMatcher(MatchedResource.class))));
}
@SuppressWarnings("unchecked")
@Test
public void includedDeltas() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(
Arrays.asList("-static/**"), Arrays.asList(""));
Collection<String> includes = (Collection<String>) ReflectionTestUtils.getField(
resourceMatcher, "includes");
assertTrue(includes.contains("templates/**"));
assertFalse(includes.contains("static/**"));
}
@SuppressWarnings("unchecked")
@Test
public void includedDeltasAndNewEntries() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("-static/**",
"foo.jar"), Arrays.asList("-**/*.jar"));
Collection<String> includes = (Collection<String>) ReflectionTestUtils.getField(
resourceMatcher, "includes");
assertTrue(includes.contains("foo.jar"));
assertTrue(includes.contains("templates/**"));
assertFalse(includes.contains("static/**"));
assertFalse(((Collection<String>) ReflectionTestUtils.getField(resourceMatcher,
"excludes")).contains("**/*.jar"));
}
@SuppressWarnings("unchecked")
@Test
public void excludedDeltas() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList(""),
Arrays.asList("-**/*.jar"));
assertFalse(((Collection<String>) ReflectionTestUtils.getField(resourceMatcher,
"excludes")).contains("**/*.jar"));
}
@Test
public void jarFileAlwaysMatches() throws Exception {
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("*"),
@@ -73,7 +120,9 @@ public class ResourceMatcherTests {
@Test
public void resourceMatching() throws IOException {
List<MatchedResource> matchedResources = this.resourceMatcher.find(Arrays.asList(
ResourceMatcher resourceMatcher = new ResourceMatcher(Arrays.asList("alpha/**",
"bravo/*", "*"), Arrays.asList(".*", "alpha/**/excluded"));
List<MatchedResource> matchedResources = resourceMatcher.find(Arrays.asList(
new File("src/test/resources/resource-matcher/one"), new File(
"src/test/resources/resource-matcher/two"), new File(
"src/test/resources/resource-matcher/three")));