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

View File

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

View File

@ -48,8 +48,12 @@ class MainControllerIntegrationTests
@Test @Test
void simpleZipProject() { void simpleZipProject() {
downloadZip("/starter.zip?style=web&style=jpa").isJavaProject() ResponseEntity<byte[]> entity = downloadArchive(
.hasFile(".gitignore").hasExecutableFile("mvnw").isMavenProject() "/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) .hasStaticAndTemplatesResources(true).pomAssert().hasDependenciesCount(3)
.hasSpringBootStarterDependency("web") .hasSpringBootStarterDependency("web")
.hasSpringBootStarterDependency("data-jpa") // alias jpa -> data-jpa .hasSpringBootStarterDependency("data-jpa") // alias jpa -> data-jpa
@ -58,12 +62,24 @@ class MainControllerIntegrationTests
@Test @Test
void simpleTgzProject() { void simpleTgzProject() {
downloadTgz("/starter.tgz?style=org.acme:foo").isJavaProject() ResponseEntity<byte[]> entity = downloadArchive(
.hasFile(".gitignore").hasExecutableFile("mvnw").isMavenProject() "/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) .hasStaticAndTemplatesResources(false).pomAssert().hasDependenciesCount(2)
.hasDependency("org.acme", "foo", "1.3.5"); .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 @Test
void dependencyInRange() { void dependencyInRange() {
Dependency biz = Dependency.create("org.acme", "biz", "1.3.5", "runtime"); Dependency biz = Dependency.create("org.acme", "biz", "1.3.5", "runtime");