Add more configurable defaults

This commit introduces a set of customizable defaults for the generated
project, that is: groupId, artifactId, version, name, description and
packageName.

This complement the existing configurable defaults that are already
provided for action type, build type, language, java version and spring
boot version.

Fixes gh-19
This commit is contained in:
Stephane Nicoll 2014-08-19 02:50:58 +02:00
parent a748819900
commit c22e357ee9
7 changed files with 180 additions and 61 deletions

View File

@ -34,11 +34,22 @@ class InitializrMetadata {
final List<BootVersion> bootVersions = new ArrayList<BootVersion>()
final Defaults defaults = new Defaults();
/**
* Initializes a {@link ProjectRequest} instance with the defaults
* defined in this instance.
*/
void initializeProjectRequest(ProjectRequest request) {
defaults.properties.each { key, value ->
if (request.hasProperty(key) && !(key in ['class', 'metaClass'])) {
request[key] = value
}
}
request.type = getDefault(types, request.type)
request.packaging = getDefault(packagings, request.packaging)
request.javaVersion = getDefault(javaVersions, request.javaVersion)
request.language = getDefault(languages, request.language)
request.bootVersion = getDefault(bootVersions, request.bootVersion)
request
}
@ -78,6 +89,30 @@ class InitializrMetadata {
static class BootVersion extends DefaultIdentifiableElement {
}
static class Defaults {
String groupId = 'org.test'
String artifactId
String version = '0.0.1-SNAPSHOT'
String name = 'demo'
String description = 'Demo project for Spring Boot'
String packageName
/**
* Return the artifactId or the name of the project if none is set.
*/
String getArtifactId() {
artifactId == null ? name : artifactId
}
/**
* Return the package name or the name of the project if none is set
*/
String getPackageName() {
packageName == null ? name.replace('-', '.') : packageName
}
}
static class DefaultIdentifiableElement extends IdentifiableElement {
@JsonIgnore

View File

@ -1,32 +1,43 @@
/*
* Copyright 2012-2014 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
*
* http://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
/**
* A request to generate a project.
*
* @author Dave Syer
* @author Stephane Nicoll
* @since 1.0
*/
class ProjectRequest {
def style = []
String name = 'demo'
String type = 'starter'
String description = 'Demo project for Spring Boot'
String groupId = 'org.test'
String name
String type
String description
String groupId
String artifactId
String version = '0.0.1-SNAPSHOT'
String version
String bootVersion
String packaging = 'jar'
String language = 'java'
String packaging
String language
String packageName
String javaVersion = '1.7'
String getArtifactId() {
artifactId == null ? name : artifactId
}
String getPackageName() {
packageName == null ? name.replace('-', '.') : packageName
}
String javaVersion
boolean isWebStyle() {
style.any { webStyle(it) }

View File

@ -27,31 +27,31 @@
<div class="form-group">
<label for="groupId" class="col-md-3 control-label">Group</label>
<div class="col-md-8">
<input id="groupId" class="form-control" type="text" value="org.demo" name="groupId">
<input id="groupId" class="form-control" type="text" value="${defaults.groupId}" name="groupId">
</div>
</div>
<div class="form-group">
<label for="artifactId" class="col-md-3 control-label">Artifact</label>
<div class="col-md-8">
<input id="artifactId" class="form-control" type="text" value="demo" name="artifactId">
<input id="artifactId" class="form-control" type="text" value="${defaults.artifactId}" name="artifactId">
</div>
</div>
<div class="form-group">
<label for="name" class="col-md-3 control-label">Name</label>
<div class="col-md-8">
<input id="name" class="form-control" type="text" value="demo" name="name">
<input id="name" class="form-control" type="text" value="${defaults.name}" name="name">
</div>
</div>
<div class="form-group">
<label for="description" class="col-md-3 control-label">Description</label>
<div class="col-md-8">
<input id="description" class="form-control" type="text" value="Demo project" name="description">
<input id="description" class="form-control" type="text" value="${defaults.description}" name="description">
</div>
</div>
<div class="form-group">
<label for="packageName" class="col-md-3 control-label">Package Name</label>
<div class="col-md-8">
<input id="packageName" class="form-control" type="text" value="demo" name="packageName">
<input id="packageName" class="form-control" type="text" value="${defaults.packageName}" name="packageName">
</div>
</div>
<div class="form-group">

View File

@ -0,0 +1,64 @@
/*
* Copyright 2012-2014 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
*
* http://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.web
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.IntegrationTest
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.http.HttpEntity
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpMethod
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.web.client.RestTemplate
/**
* @author Stephane Nicoll
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@WebAppConfiguration
@IntegrationTest('server.port=0')
abstract class AbstractMainControllerIntegrationTests {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@Value('${local.server.port}')
private int port
final RestTemplate restTemplate = new RestTemplate()
String createUrl(String context) {
return 'http://localhost:' + port + context
}
String htmlHome() {
HttpHeaders headers = new HttpHeaders()
headers.setAccept([MediaType.TEXT_HTML])
restTemplate.exchange(createUrl('/'), HttpMethod.GET, new HttpEntity<Void>(headers), String).body
}
@EnableAutoConfiguration
static class Config {}
}

View File

@ -0,0 +1,36 @@
package io.spring.initializr.web
import io.spring.initializr.support.PomAssert
import org.junit.Test
import org.springframework.test.context.ActiveProfiles
import static org.junit.Assert.assertTrue
/**
* @author Stephane Nicoll
*/
@ActiveProfiles(['test-default', 'test-custom-defaults'])
class MainControllerDefaultsIntegrationTests extends AbstractMainControllerIntegrationTests {
// see defaults customization
@Test
void generateDefaultPom() {
String content = restTemplate.getForObject(createUrl('/pom.xml?style=web'), String)
PomAssert pomAssert = new PomAssert(content)
pomAssert.hasGroupId('org.foo').hasArtifactId('foo-bar').hasVersion('1.2.4-SNAPSHOT').hasPackaging('jar')
.hasName('FooBar').hasDescription('FooBar Project').hasStartClass('org.foo.demo.Application')
}
@Test
void defaultsAppliedToHome() {
String body = htmlHome()
assertTrue 'custom groupId not found', body.contains('org.foo')
assertTrue 'custom artifactId not found', body.contains('foo-bar')
assertTrue 'custom name not found', body.contains('FooBar')
assertTrue 'custom description not found', body.contains('FooBar Project')
assertTrue 'custom package not found', body.contains('org.foo.demo')
}
}

View File

@ -17,40 +17,19 @@
package io.spring.initializr.web
import io.spring.initializr.support.ProjectAssert
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.IntegrationTest
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.http.*
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.web.WebAppConfiguration
import org.springframework.web.client.RestTemplate
import static org.junit.Assert.*
/**
* @author Stephane Nicoll
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Config.class)
@WebAppConfiguration
@IntegrationTest('server.port=0')
@ActiveProfiles('test-default')
class MainControllerIntegrationTests {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@Value('${local.server.port}')
private int port
final RestTemplate restTemplate = new RestTemplate()
class MainControllerIntegrationTests extends AbstractMainControllerIntegrationTests {
@Test
public void simpleZipProject() {
@ -80,7 +59,7 @@ class MainControllerIntegrationTests {
@Test
void homeIsForm() {
String body = home()
String body = htmlHome()
assertTrue 'Wrong body:\n' + body, body.contains('action="/starter.zip"')
}
@ -113,13 +92,13 @@ class MainControllerIntegrationTests {
@Test
void homeHasWebStyle() {
String body = home()
String body = htmlHome()
assertTrue('Wrong body:\n' + body, body.contains('name="style" value="web"'))
}
@Test
void homeHasBootVersion() {
String body = home()
String body = htmlHome()
assertTrue('Wrong body:\n' + body, body.contains('name="bootVersion"'))
assertTrue('Wrong body:\n' + body, body.contains('1.2.0.BUILD-SNAPSHOT"'))
}
@ -138,12 +117,6 @@ class MainControllerIntegrationTests {
assertNotNull(response.body)
}
private String home() {
HttpHeaders headers = new HttpHeaders()
headers.setAccept([MediaType.TEXT_HTML])
restTemplate.exchange(createUrl('/'), HttpMethod.GET, new HttpEntity<Void>(headers), String).body
}
private ProjectAssert downloadZip(String context) {
byte[] body = restTemplate.getForObject(createUrl(context), byte[])
@ -174,12 +147,4 @@ class MainControllerIntegrationTests {
archiveFile
}
String createUrl(String context) {
return 'http://localhost:' + port + context
}
@EnableAutoConfiguration
static class Config {}
}

View File

@ -0,0 +1,8 @@
initializr:
defaults:
groupId: org.foo
artifactId: foo-bar
version: 1.2.4-SNAPSHOT
name: FooBar
description: FooBar Project
packageName: org.foo.demo