Fix generation of filename with empty artifactId

Closes gh-831
This commit is contained in:
Stephane Nicoll 2019-02-14 13:10:48 +01:00
parent f18c08f88f
commit 24887e1ab3
3 changed files with 30 additions and 10 deletions

View File

@ -61,6 +61,7 @@ import org.springframework.http.ResponseEntity.BodyBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
@ -320,8 +321,11 @@ public class MainController extends AbstractInitializrController {
"application/x-compress");
}
private static String generateFileName(ProjectRequest request, String extension) {
String tmp = request.getArtifactId().replaceAll(" ", "_");
private String generateFileName(ProjectRequest request, String extension) {
String candidate = (StringUtils.hasText(request.getArtifactId())
? request.getArtifactId()
: this.metadataProvider.get().getArtifactId().getContent());
String tmp = candidate.replaceAll(" ", "_");
try {
return URLEncoder.encode(tmp, "UTF-8") + "." + extension;
}

View File

@ -166,17 +166,17 @@ public abstract class AbstractInitializrIntegrationTests {
}
protected ProjectAssert downloadZip(String context) {
byte[] body = downloadArchive(context);
byte[] body = downloadArchive(context).getBody();
return zipProjectAssert(body);
}
protected ProjectAssert downloadTgz(String context) {
byte[] body = downloadArchive(context);
byte[] body = downloadArchive(context).getBody();
return tgzProjectAssert(body);
}
protected byte[] downloadArchive(String context) {
return this.restTemplate.getForObject(createUrl(context), byte[].class);
protected ResponseEntity<byte[]> downloadArchive(String context) {
return this.restTemplate.getForEntity(createUrl(context), byte[].class);
}
protected ResponseEntity<String> invokeHome(String userAgentHeader,

View File

@ -48,8 +48,12 @@ class MainControllerIntegrationTests
@Test
void simpleZipProject() {
downloadZip("/starter.zip?style=web&style=jpa").isJavaProject()
.hasFile(".gitignore").hasExecutableFile("mvnw").isMavenProject()
ResponseEntity<byte[]> entity = downloadArchive(
"/starter.zip?style=web&style=jpa");
assertArchiveResponseHeaders(entity, MediaType.valueOf("application/zip"),
"demo.zip");
zipProjectAssert(entity.getBody()).isJavaProject().hasFile(".gitignore")
.hasExecutableFile("mvnw").isMavenProject()
.hasStaticAndTemplatesResources(true).pomAssert().hasDependenciesCount(3)
.hasSpringBootStarterDependency("web")
.hasSpringBootStarterDependency("data-jpa") // alias jpa -> data-jpa
@ -58,12 +62,24 @@ class MainControllerIntegrationTests
@Test
void simpleTgzProject() {
downloadTgz("/starter.tgz?style=org.acme:foo").isJavaProject()
.hasFile(".gitignore").hasExecutableFile("mvnw").isMavenProject()
ResponseEntity<byte[]> entity = downloadArchive(
"/starter.tgz?style=org.acme:foo");
assertArchiveResponseHeaders(entity, MediaType.valueOf("application/x-compress"),
"demo.tar.gz");
tgzProjectAssert(entity.getBody()).isJavaProject().hasFile(".gitignore")
.hasExecutableFile("mvnw").isMavenProject()
.hasStaticAndTemplatesResources(false).pomAssert().hasDependenciesCount(2)
.hasDependency("org.acme", "foo", "1.3.5");
}
private void assertArchiveResponseHeaders(ResponseEntity<byte[]> entity,
MediaType contentType, String fileName) {
assertThat(entity.getHeaders().getContentType()).isEqualTo(contentType);
assertThat(entity.getHeaders().getContentDisposition()).isNotNull();
assertThat(entity.getHeaders().getContentDisposition().getFilename())
.isEqualTo(fileName);
}
@Test
void dependencyInRange() {
Dependency biz = Dependency.create("org.acme", "biz", "1.3.5", "runtime");