Rework 155c60b7 to structure the code consistently, in particular with a more
natural order of attributes. Update test to use non-default values to ensure
that the customization has been applied.

See gh-2793
pull/2796/merge
Stephane Nicoll 10 years ago
parent 0b8fd67507
commit b20d11fe9c

@ -79,35 +79,35 @@ public class InitCommand extends OptionParsingCommand {
private OptionSpec<Void> listCapabilities; private OptionSpec<Void> listCapabilities;
private OptionSpec<String> bootVersion; private OptionSpec<String> groupId;
private OptionSpec<String> dependencies; private OptionSpec<String> artifactId;
private OptionSpec<String> javaVersion; private OptionSpec<String> version;
private OptionSpec<String> packaging; private OptionSpec<String> name;
private OptionSpec<String> build; private OptionSpec<String> description;
private OptionSpec<String> format; private OptionSpec<String> packaging;
private OptionSpec<String> type; private OptionSpec<String> type;
private OptionSpec<Void> extract; private OptionSpec<String> build;
private OptionSpec<Void> force; private OptionSpec<String> format;
private OptionSpec<String> language; private OptionSpec<String> javaVersion;
private OptionSpec<String> groupId; private OptionSpec<String> language;
private OptionSpec<String> artifactId; private OptionSpec<String> bootVersion;
private OptionSpec<String> name; private OptionSpec<String> dependencies;
private OptionSpec<String> version; private OptionSpec<Void> extract;
private OptionSpec<String> description; private OptionSpec<Void> force;
InitOptionHandler(InitializrService initializrService) { InitOptionHandler(InitializrService initializrService) {
this.serviceCapabilitiesReport = new ServiceCapabilitiesReportGenerator( this.serviceCapabilitiesReport = new ServiceCapabilitiesReportGenerator(
@ -129,48 +129,48 @@ public class InitCommand extends OptionParsingCommand {
} }
private void projectGenerationOptions() { private void projectGenerationOptions() {
this.bootVersion = option(Arrays.asList("boot-version", "b"), this.groupId = option(Arrays.asList("groupId", "g"),
"Spring Boot version to use (for example '1.2.0.RELEASE')") "Project coordinates (for example 'org.test')")
.withRequiredArg();
this.artifactId = option(Arrays.asList("artifactId", "a"),
"Project coordinates; infer archive name (for example 'test')")
.withRequiredArg();
this.version = option(Arrays.asList("version", "v"),
"Project version (for example '0.0.1-SNAPSHOT')")
.withRequiredArg();
this.name = option(Arrays.asList("name", "n"),
"Project name; infer application name")
.withRequiredArg();
this.description = option("description",
"Project description")
.withRequiredArg(); .withRequiredArg();
this.dependencies = option(
Arrays.asList("dependencies", "d"),
"Comma separated list of dependencies to include in the "
+ "generated project").withRequiredArg();
this.javaVersion = option(Arrays.asList("java-version", "j"),
"Java version to use (for example '1.8')").withRequiredArg();
this.packaging = option(Arrays.asList("packaging", "p"), this.packaging = option(Arrays.asList("packaging", "p"),
"Packaging type to use (for example 'jar')").withRequiredArg(); "Project packaging (for example 'jar')").withRequiredArg();
this.type = option(
Arrays.asList("type", "t"),
"Project type. Not normally needed if you use --build "
+ "and/or --format. Check the capabilities of the service "
+ "(--list) for more details").withRequiredArg();
this.build = option("build", this.build = option("build",
"The build system to use (for example 'maven' or 'gradle')") "Build system to use (for example 'maven' or 'gradle')")
.withRequiredArg().defaultsTo("maven"); .withRequiredArg().defaultsTo("maven");
this.format = option( this.format = option(
"format", "format",
"The format of the generated content (for example 'build' for a build file, " "Format of the generated content (for example 'build' for a build file, "
+ "'project' for a project archive)").withRequiredArg() + "'project' for a project archive)").withRequiredArg()
.defaultsTo("project"); .defaultsTo("project");
this.type = option( this.javaVersion = option(Arrays.asList("java-version", "j"),
Arrays.asList("type", "t"), "Language level (for example '1.8')").withRequiredArg();
"The project type to use. Not normally needed if you use --build " this.language = option(Arrays.asList("language", "l"),
+ "and/or --format. Check the capabilities of the service " "Programming language (for example 'java')")
+ "(--list) for more details").withRequiredArg();
this.language = option(Arrays.asList("language", "lang"),
"Programming Language to use (for example 'java')")
.withRequiredArg();
this.groupId = option(Arrays.asList("groupId", "g"),
"The project group that produced the dependency (for example 'org.test')")
.withRequiredArg();
this.artifactId = option(Arrays.asList("artifactId", "a"),
"The unique id for an artifact produced by the project group (for example 'test')")
.withRequiredArg();
this.name = option(Arrays.asList("name", "n"),
"The full name of the project.")
.withRequiredArg();
this.version = option(Arrays.asList("version", "v"),
"The version of the dependency (for example '0.0.1-SNAPSHOT')")
.withRequiredArg(); .withRequiredArg();
this.description = option(Arrays.asList("description", "des"), this.bootVersion = option(Arrays.asList("boot-version", "b"),
"A detailed description of the project") "Spring Boot version (for example '1.2.0.RELEASE')")
.withRequiredArg(); .withRequiredArg();
this.dependencies = option(
Arrays.asList("dependencies", "d"),
"Comma-separated list of dependency identifiers to include in the "
+ "generated project").withRequiredArg();
} }
private void otherOptions() { private void otherOptions() {

@ -25,12 +25,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.http.client.utils.URIBuilder; import org.apache.http.client.utils.URIBuilder;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* Represent the settings to apply to generating the project. * Represent the settings to apply to generating the project.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Eddú Meléndez
* @since 1.2.0 * @since 1.2.0
*/ */
class ProjectGenerationRequest { class ProjectGenerationRequest {
@ -43,33 +45,33 @@ class ProjectGenerationRequest {
private boolean extract; private boolean extract;
private String bootVersion; private String groupId;
private List<String> dependencies = new ArrayList<String>(); private String artifactId;
private String javaVersion; private String version;
private String name;
private String description;
private String packaging; private String packaging;
private String type;
private String build; private String build;
private String format; private String format;
private boolean detectType; private boolean detectType;
private String type; private String javaVersion;
private String language; private String language;
private String groupId; private String bootVersion;
private String artifactId;
private String name;
private String version;
private String description; private List<String> dependencies = new ArrayList<String>();
/** /**
* The URL of the service to use. * The URL of the service to use.
@ -117,35 +119,63 @@ class ProjectGenerationRequest {
} }
/** /**
* The Spring Boot version to use or {@code null} if it should not be customized. * The groupId to use or {@code null} if it should not be customized.
* @return the Spring Boot version of {@code null} * @return the groupId or {@code null}
*/ */
public String getBootVersion() { public String getGroupId() {
return this.bootVersion; return this.groupId;
} }
public void setBootVersion(String bootVersion) { public void setGroupId(String groupId) {
this.bootVersion = bootVersion; this.groupId = groupId;
} }
/** /**
* The identifiers of the dependencies to include in the project. * The artifactId to use or {@code null} if it should not be customized.
* @return the dependency identifiers * @return the artifactId or {@code null}
*/ */
public List<String> getDependencies() { public String getArtifactId() {
return this.dependencies; return this.artifactId;
}
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
} }
/** /**
* The Java version to use or {@code null} if it should not be customized. * The artifact version to use or {@code null} if it should not be customized.
* @return the Java version or {@code null} * @return the artifact version or {@code null}
*/ */
public String getJavaVersion() { public String getVersion() {
return this.javaVersion; return this.version;
} }
public void setJavaVersion(String javaVersion) { public void setVersion(String version) {
this.javaVersion = javaVersion; this.version = version;
}
/**
* The name to use or {@code null} if it should not be customized.
* @return the name or {@code null}
*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* The description to use or {@code null} if it should not be customized.
* @return the description or {@code null}
*/
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
} }
/** /**
@ -160,6 +190,19 @@ class ProjectGenerationRequest {
this.packaging = packaging; this.packaging = packaging;
} }
/**
* The type of project to generate. Should match one of the advertized type that the
* service supports. If not set, the default is retrieved from the service metadata.
* @return the project type
*/
public String getType() {
return this.type;
}
public void setType(String type) {
this.type = type;
}
/** /**
* The build type to use. Ignored if a type is set. Can be used alongside the * The build type to use. Ignored if a type is set. Can be used alongside the
* {@link #getFormat() format} to identify the type to use. * {@link #getFormat() format} to identify the type to use.
@ -199,20 +242,20 @@ class ProjectGenerationRequest {
} }
/** /**
* The type of project to generate. Should match one of the advertized type that the * The Java version to use or {@code null} if it should not be customized.
* service supports. If not set, the default is retrieved from the service metadata. * @return the Java version or {@code null}
* @return the project type
*/ */
public String getType() { public String getJavaVersion() {
return this.type; return this.javaVersion;
} }
public void setType(String type) { public void setJavaVersion(String javaVersion) {
this.type = type; this.javaVersion = javaVersion;
} }
/** /**
* The programming language to use or {@code null} if it should not be customized. * The programming language to use or {@code null} if it should not be customized.
* @return the programming language or {@code null}
*/ */
public String getLanguage() { public String getLanguage() {
return this.language; return this.language;
@ -223,58 +266,23 @@ class ProjectGenerationRequest {
} }
/** /**
* The groupId to use or {@code null} if it should not be customized. * The Spring Boot version to use or {@code null} if it should not be customized.
*/ * @return the Spring Boot version or {@code null}
public String getGroupId() {
return this.groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
/**
* The artifactId to use or {@code null} if it should not be customized.
*/
public String getArtifactId() {
return this.artifactId;
}
public void setArtifactId(String artifactId) {
this.artifactId = artifactId;
}
/**
* The name to use or {@code null} if it should not be customized.
*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* The artifact version to use or {@code null} if it should not be customized.
*/ */
public String getVersion() { public String getBootVersion() {
return this.version; return this.bootVersion;
} }
public void setVersion(String version) { public void setBootVersion(String bootVersion) {
this.version = version; this.bootVersion = bootVersion;
} }
/** /**
* * The identifiers of the dependencies to include in the project.
* @return the dependency identifiers
*/ */
public String getDescription() { public List<String> getDependencies() {
return this.description; return this.dependencies;
}
public void setDescription(String description) {
this.description = description;
} }
/** /**
@ -295,40 +303,40 @@ class ProjectGenerationRequest {
sb.append(projectType.getAction()); sb.append(projectType.getAction());
builder.setPath(sb.toString()); builder.setPath(sb.toString());
if (this.artifactId != null) {
builder.setParameter("artifactId", this.artifactId);
}
if (this.bootVersion != null) {
builder.setParameter("bootVersion", this.bootVersion);
}
if (!this.dependencies.isEmpty()) { if (!this.dependencies.isEmpty()) {
builder.setParameter("dependencies", builder.setParameter("dependencies",
StringUtils.collectionToCommaDelimitedString(this.dependencies)); StringUtils.collectionToCommaDelimitedString(this.dependencies));
} }
if (this.description != null) {
builder.setParameter("description", this.description);
}
if (this.groupId != null) { if (this.groupId != null) {
builder.setParameter("groupId", this.groupId); builder.setParameter("groupId", this.groupId);
} }
if (this.javaVersion != null) { if (this.artifactId != null) {
builder.setParameter("javaVersion", this.javaVersion); builder.setParameter("artifactId", this.artifactId);
} }
if (this.language != null) { if (this.version != null) {
builder.setParameter("language", this.language); builder.setParameter("version", this.version);
} }
if (this.name != null) { if (this.name != null) {
builder.setParameter("name", this.name); builder.setParameter("name", this.name);
} }
if (this.description != null) {
builder.setParameter("description", this.description);
}
if (this.packaging != null) { if (this.packaging != null) {
builder.setParameter("packaging", this.packaging); builder.setParameter("packaging", this.packaging);
} }
if (this.type != null) { if (this.type != null) {
builder.setParameter("type", projectType.getId()); builder.setParameter("type", projectType.getId());
} }
if (this.version != null) { if (this.javaVersion != null) {
builder.setParameter("version", this.version); builder.setParameter("javaVersion", this.javaVersion);
}
if (this.language != null) {
builder.setParameter("language", this.language);
}
if (this.bootVersion != null) {
builder.setParameter("bootVersion", this.bootVersion);
} }
return builder.build(); return builder.build();
@ -384,7 +392,7 @@ class ProjectGenerationRequest {
private static void filter(Map<String, ProjectType> projects, String tag, private static void filter(Map<String, ProjectType> projects, String tag,
String tagValue) { String tagValue) {
for (Iterator<Map.Entry<String, ProjectType>> it = projects.entrySet().iterator(); it for (Iterator<Map.Entry<String, ProjectType>> it = projects.entrySet().iterator(); it
.hasNext();) { .hasNext(); ) {
Map.Entry<String, ProjectType> entry = it.next(); Map.Entry<String, ProjectType> entry = it.next();
String value = entry.getValue().getTags().get(tag); String value = entry.getValue().getTags().get(tag);
if (!tagValue.equals(value)) { if (!tagValue.equals(value)) {

@ -25,7 +25,6 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Before; import org.junit.Before;
@ -35,6 +34,7 @@ import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Captor; import org.mockito.Captor;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.boot.cli.command.status.ExitStatus; import org.springframework.boot.cli.command.status.ExitStatus;
import static org.hamcrest.Matchers.startsWith; import static org.hamcrest.Matchers.startsWith;
@ -262,6 +262,31 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
assertEquals("File should not have changed", fileLength, conflict.length()); assertEquals("File should not have changed", fileLength, conflict.length());
} }
@Test
public void parseProjectOptions() throws Exception {
this.handler.disableProjectGeneration();
this.command.run("-g=org.demo", "-a=acme", "-v=1.2.3-SNAPSHOT", "-n=acme-sample",
"--description=Acme sample project", "-p=war", "-t=ant-project",
"--build=grunt", "--format=web", "-j=1.9", "-l=groovy",
"-b=1.2.0.RELEASE", "-d=web,data-jpa");
assertEquals("org.demo", this.handler.lastRequest.getGroupId());
assertEquals("acme", this.handler.lastRequest.getArtifactId());
assertEquals("1.2.3-SNAPSHOT", this.handler.lastRequest.getVersion());
assertEquals("acme-sample", this.handler.lastRequest.getName());
assertEquals("Acme sample project", this.handler.lastRequest.getDescription());
assertEquals("war", this.handler.lastRequest.getPackaging());
assertEquals("ant-project", this.handler.lastRequest.getType());
assertEquals("grunt", this.handler.lastRequest.getBuild());
assertEquals("web", this.handler.lastRequest.getFormat());
assertEquals("1.9", this.handler.lastRequest.getJavaVersion());
assertEquals("groovy", this.handler.lastRequest.getLanguage());
assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion());
List<String> dependencies = this.handler.lastRequest.getDependencies();
assertEquals(2, dependencies.size());
assertTrue(dependencies.contains("web"));
assertTrue(dependencies.contains("data-jpa"));
}
@Test @Test
public void overwriteFileInArchive() throws Exception { public void overwriteFileInArchive() throws Exception {
File folder = this.temporaryFolder.newFolder(); File folder = this.temporaryFolder.newFolder();
@ -278,30 +303,6 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
assertTrue("File should have changed", fileLength != conflict.length()); assertTrue("File should have changed", fileLength != conflict.length());
} }
@Test
public void parseProjectOptions() throws Exception {
this.handler.disableProjectGeneration();
this.command.run("-b=1.2.0.RELEASE", "-d=web,data-jpa", "-j=1.9", "-p=war", "--build=grunt",
"--format=web", "-t=ant-project", "-lang=groovy", "-g=org.test", "-a=demo", "-n=demo",
"-v=0.0.1-SNAPSHOT", "-des=Demo project for Spring Boot");
assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion());
List<String> dependencies = this.handler.lastRequest.getDependencies();
assertEquals(2, dependencies.size());
assertTrue(dependencies.contains("web"));
assertTrue(dependencies.contains("data-jpa"));
assertEquals("1.9", this.handler.lastRequest.getJavaVersion());
assertEquals("war", this.handler.lastRequest.getPackaging());
assertEquals("grunt", this.handler.lastRequest.getBuild());
assertEquals("web", this.handler.lastRequest.getFormat());
assertEquals("ant-project", this.handler.lastRequest.getType());
assertEquals("groovy", this.handler.lastRequest.getLanguage());
assertEquals("org.test", this.handler.lastRequest.getGroupId());
assertEquals("demo", this.handler.lastRequest.getArtifactId());
assertEquals("demo", this.handler.lastRequest.getName());
assertEquals("0.0.1-SNAPSHOT", this.handler.lastRequest.getVersion());
assertEquals("Demo project for Spring Boot", this.handler.lastRequest.getDescription());
}
@Test @Test
public void parseTypeOnly() throws Exception { public void parseTypeOnly() throws Exception {
this.handler.disableProjectGeneration(); this.handler.disableProjectGeneration();

@ -27,6 +27,7 @@ import org.json.JSONObject;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
@ -68,7 +69,7 @@ public class ProjectGenerationRequestTests {
@Test @Test
public void customBootVersion() { public void customBootVersion() {
this.request.setBootVersion("1.2.0.RELEASE"); this.request.setBootVersion("1.2.0.RELEASE");
assertEquals(createDefaultUrl("?bootVersion=1.2.0.RELEASE&type=test-type"), assertEquals(createDefaultUrl("?type=test-type&bootVersion=1.2.0.RELEASE"),
this.request.generateUrl(createDefaultMetadata())); this.request.generateUrl(createDefaultMetadata()));
} }
@ -90,7 +91,7 @@ public class ProjectGenerationRequestTests {
@Test @Test
public void customJavaVersion() { public void customJavaVersion() {
this.request.setJavaVersion("1.8"); this.request.setJavaVersion("1.8");
assertEquals(createDefaultUrl("?javaVersion=1.8&type=test-type"), assertEquals(createDefaultUrl("?type=test-type&javaVersion=1.8"),
this.request.generateUrl(createDefaultMetadata())); this.request.generateUrl(createDefaultMetadata()));
} }
@ -115,19 +116,19 @@ public class ProjectGenerationRequestTests {
@Test @Test
public void customLanguage() { public void customLanguage() {
this.request.setLanguage("java"); this.request.setLanguage("groovy");
assertEquals(createDefaultUrl("?language=java&type=test-type"), assertEquals(createDefaultUrl("?type=test-type&language=groovy"),
this.request.generateUrl(createDefaultMetadata())); this.request.generateUrl(createDefaultMetadata()));
} }
@Test @Test
public void customProjectInfo() { public void customProjectInfo() {
this.request.setGroupId("org.test"); this.request.setGroupId("org.acme");
this.request.setArtifactId("demo"); this.request.setArtifactId("sample");
this.request.setVersion("0.0.1-SNAPSHOT"); this.request.setVersion("1.0.1-SNAPSHOT");
this.request.setDescription("Spring Boot Demo"); this.request.setDescription("Spring Boot Test");
assertEquals(createDefaultUrl("?artifactId=demo&description=Spring+Boot+Demo" assertEquals(createDefaultUrl("?groupId=org.acme&artifactId=sample&version=1.0.1-SNAPSHOT" +
+ "&groupId=org.test&type=test-type&version=0.0.1-SNAPSHOT"), "&description=Spring+Boot+Test&type=test-type"),
this.request.generateUrl(createDefaultMetadata())); this.request.generateUrl(createDefaultMetadata()));
} }

Loading…
Cancel
Save