Clean root resource path if necessary

See gh-964
This commit is contained in:
Toon Geens 2019-07-24 14:44:08 +02:00 committed by Stephane Nicoll
parent 7a1b986f2a
commit 52872f9a72
2 changed files with 61 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import java.util.function.Predicate;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
* A {@link ProjectContributor} that contributes all of the resources found beneath a root
@ -45,7 +46,7 @@ public class MultipleResourcesProjectContributor implements ProjectContributor {
}
public MultipleResourcesProjectContributor(String rootResource, Predicate<String> executable) {
this.rootResource = rootResource;
this.rootResource = StringUtils.trimTrailingCharacter(rootResource, '/');
this.executable = executable;
}

View File

@ -0,0 +1,59 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.generator.project.contributor;
import java.io.IOException;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link MultipleResourcesProjectContributor}.
*
* @author Toon Geens
*/
class MultipleResourcesProjectContributorTests {
private static final String classLocation = MultipleResourcesProjectContributorTests.class.getPackage().getName()
.replace('.', '/');
private static final String classFile = MultipleResourcesProjectContributorTests.class.getSimpleName() + ".class";
@Test
void contribute(@TempDir Path directory) throws IOException {
String rootResource = "classpath:" + classLocation;
MultipleResourcesProjectContributor contributor = new MultipleResourcesProjectContributor(rootResource);
contributor.contribute(directory);
assertThat(directory.resolve(classFile)).exists();
}
@Test
void contributeWithTrailingSlash(@TempDir Path directory) throws IOException {
String rootResource = "classpath:" + classLocation + "/";
MultipleResourcesProjectContributor contributor = new MultipleResourcesProjectContributor(rootResource);
contributor.contribute(directory);
assertThat(directory.resolve(classFile)).exists();
}
}