Polish contribution

Closes gh-349
This commit is contained in:
Stephane Nicoll
2017-02-10 14:49:34 +01:00
parent 5296d6a05f
commit 09fc98ef96
101 changed files with 815 additions and 1125 deletions

View File

@@ -39,6 +39,7 @@ public abstract class AbstractFullStackInitializrIntegrationTests
protected String host = "localhost";
@Override
protected String createUrl(String context) {
return "http://" + host + ":" + port
+ (context.startsWith("/") ? context : "/" + context);

View File

@@ -35,7 +35,8 @@ import org.springframework.test.context.TestExecutionListeners.MergeMode;
* @author Stephane Nicoll
*/
@ContextConfiguration(classes = RestTemplateConfig.class)
@TestExecutionListeners(mergeMode = MergeMode.MERGE_WITH_DEFAULTS, listeners = MockMvcClientHttpRequestFactoryTestExecutionListener.class)
@TestExecutionListeners(mergeMode = MergeMode.MERGE_WITH_DEFAULTS,
listeners = MockMvcClientHttpRequestFactoryTestExecutionListener.class)
@AutoConfigureMockMvc
@AutoConfigureRestDocs(outputDir = "target/snippets", uriPort = 80, uriHost = "start.spring.io")
public abstract class AbstractInitializrControllerIntegrationTests
@@ -47,6 +48,7 @@ public abstract class AbstractInitializrControllerIntegrationTests
private
MockMvcClientHttpRequestFactory requests;
@Override
protected String createUrl(String context) {
return context.startsWith("/") ? context : "/" + context;
}
@@ -60,10 +62,8 @@ public abstract class AbstractInitializrControllerIntegrationTests
@Bean
RestTemplateCustomizer mockMvcCustomizer(BeanFactory beanFactory) {
return template -> {
template.setRequestFactory(
beanFactory.getBean(MockMvcClientHttpRequestFactory.class));
};
return template -> template.setRequestFactory(
beanFactory.getBean(MockMvcClientHttpRequestFactory.class));
}
}
}

View File

@@ -22,7 +22,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@@ -67,8 +66,8 @@ import static org.junit.Assert.assertTrue;
@SpringBootTest(classes = Config.class)
public abstract class AbstractInitializrIntegrationTests {
protected static final MediaType CURRENT_METADATA_MEDIA_TYPE = InitializrMetadataVersion.V2_1
.getMediaType();
protected static final MediaType CURRENT_METADATA_MEDIA_TYPE =
InitializrMetadataVersion.V2_1.getMediaType();
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@@ -87,7 +86,7 @@ public abstract class AbstractInitializrIntegrationTests {
protected String htmlHome() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
return restTemplate.exchange(createUrl("/"), HttpMethod.GET,
new HttpEntity<Void>(headers), String.class).getBody();
}
@@ -199,28 +198,24 @@ public abstract class AbstractInitializrIntegrationTests {
protected File writeArchive(byte[] body) throws IOException {
File archiveFile = folder.newFile();
FileOutputStream stream = new FileOutputStream(archiveFile);
try {
try (FileOutputStream stream = new FileOutputStream(archiveFile)) {
stream.write(body);
}
finally {
stream.close();
}
return archiveFile;
}
protected JSONObject readJsonFrom(String path) {
try {
ClassPathResource resource = new ClassPathResource(path);
InputStream stream = resource.getInputStream();
try {
try (InputStream stream = resource.getInputStream()) {
String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
String placeholder = "";
if (this instanceof AbstractInitializrControllerIntegrationTests) {
placeholder = ((AbstractInitializrControllerIntegrationTests) this).host;
}
if (this instanceof AbstractFullStackInitializrIntegrationTests) {
AbstractFullStackInitializrIntegrationTests test = (AbstractFullStackInitializrIntegrationTests) this;
AbstractFullStackInitializrIntegrationTests test =
(AbstractFullStackInitializrIntegrationTests) this;
placeholder = test.host + ":" + test.port;
}
// Let's parse the port as it is random
@@ -228,9 +223,6 @@ public abstract class AbstractInitializrIntegrationTests {
String content = json.replaceAll("@host@", placeholder);
return new JSONObject(content);
}
finally {
stream.close();
}
}
catch (Exception e) {
throw new IllegalStateException("Cannot read JSON from path=" + path);
@@ -244,7 +236,7 @@ public abstract class AbstractInitializrIntegrationTests {
private enum ArchiveType {
ZIP,
TGZ;
TGZ
}
@EnableAutoConfiguration

View File

@@ -21,16 +21,15 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.mock.env.MockEnvironment;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Stephane Nicoll
*/
public class CloudfoundryEnvironmentPostProcessorTests {
private final CloudfoundryEnvironmentPostProcessor postProcessor = new CloudfoundryEnvironmentPostProcessor();
private final CloudfoundryEnvironmentPostProcessor postProcessor =
new CloudfoundryEnvironmentPostProcessor();
private final MockEnvironment environment = new MockEnvironment();
private final SpringApplication application = new SpringApplication();
@@ -40,10 +39,12 @@ public class CloudfoundryEnvironmentPostProcessorTests {
"http://user:pass@example.com/bar/biz?param=one");
postProcessor.postProcessEnvironment(environment, application);
assertThat(environment.getProperty("initializr.stats.elastic.uri"),
is("http://example.com/bar/biz?param=one"));
assertThat(environment.getProperty("initializr.stats.elastic.username"), is("user"));
assertThat(environment.getProperty("initializr.stats.elastic.password"), is("pass"));
assertThat(environment.getProperty("initializr.stats.elastic.uri"))
.isEqualTo("http://example.com/bar/biz?param=one");
assertThat(environment.getProperty("initializr.stats.elastic.username"))
.isEqualTo("user");
assertThat(environment.getProperty("initializr.stats.elastic.password"))
.isEqualTo("pass");
}
@Test
@@ -52,19 +53,19 @@ public class CloudfoundryEnvironmentPostProcessorTests {
"http://example.com/bar/biz?param=one");
postProcessor.postProcessEnvironment(environment, application);
assertThat(environment.getProperty("initializr.stats.elastic.uri"),
is("http://example.com/bar/biz?param=one"));
assertThat(environment.getProperty("initializr.stats.elastic.username"), is(nullValue()));
assertThat(environment.getProperty("initializr.stats.elastic.password"), is(nullValue()));
assertThat(environment.getProperty("initializr.stats.elastic.uri"))
.isEqualTo("http://example.com/bar/biz?param=one");
assertThat(environment.getProperty("initializr.stats.elastic.username")).isNull();
assertThat(environment.getProperty("initializr.stats.elastic.password")).isNull();
}
@Test
public void parseNoVcapUri() {
postProcessor.postProcessEnvironment(environment, application);
assertThat(environment.getProperty("initializr.stats.elastic.uri"), is(nullValue()));
assertThat(environment.getProperty("initializr.stats.elastic.username"), is(nullValue()));
assertThat(environment.getProperty("initializr.stats.elastic.password"), is(nullValue()));
assertThat(environment.getProperty("initializr.stats.elastic.uri")).isNull();
assertThat(environment.getProperty("initializr.stats.elastic.username")).isNull();
assertThat(environment.getProperty("initializr.stats.elastic.password")).isNull();
}
}

View File

@@ -34,7 +34,8 @@ import static org.junit.Assert.assertEquals;
*/
public class DependencyMetadataJsonMapperTests {
private final DependencyMetadataJsonMapper mapper = new DependencyMetadataV21JsonMapper();
private final DependencyMetadataJsonMapper mapper =
new DependencyMetadataV21JsonMapper();
@Test
public void mapDependency() throws Exception {

View File

@@ -31,11 +31,13 @@ import static org.junit.Assert.assertTrue;
*/
public class InitializrMetadataJsonMapperTests {
private final InitializrMetadataJsonMapper jsonMapper = new InitializrMetadataV21JsonMapper();
private final InitializrMetadataJsonMapper jsonMapper =
new InitializrMetadataV21JsonMapper();
@Test
public void withNoAppUrl() {
InitializrMetadata metadata = new InitializrMetadataTestBuilder().addType("foo", true, "/foo.zip", "none", "test")
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addType("foo", true, "/foo.zip", "none", "test")
.addDependencyGroup("foo", "one", "two").build();
String json = jsonMapper.write(metadata, null);
JSONObject result = new JSONObject(json);
@@ -45,7 +47,8 @@ public class InitializrMetadataJsonMapperTests {
@Test
public void withAppUrl() {
InitializrMetadata metadata = new InitializrMetadataTestBuilder().addType("foo", true, "/foo.zip", "none", "test")
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addType("foo", true, "/foo.zip", "none", "test")
.addDependencyGroup("foo", "one", "two").build();
String json = jsonMapper.write(metadata, "http://server:8080/my-app");
JSONObject result = new JSONObject(json);

View File

@@ -35,9 +35,9 @@ public class LinkMapperTests {
@Test
public void mapSimpleRel() {
List<Link> links = new ArrayList<Link>();
List<Link> links = new ArrayList<>();
links.add(Link.create("a", "https://example.com", "some description"));
Map<String, Object> model = (Map<String, Object>) LinkMapper.mapLinks(links);
Map<String, Object> model = LinkMapper.mapLinks(links);
assertEquals(1, model.size());
assertTrue(model.containsKey("a"));
@SuppressWarnings("unchecked")
@@ -49,9 +49,9 @@ public class LinkMapperTests {
@Test
public void mapTemplatedRel() {
List<Link> links = new ArrayList<Link>();
List<Link> links = new ArrayList<>();
links.add(Link.create("a", "https://example.com/{bootVersion}/a", true));
Map<String, Object> model = (Map<String, Object>) LinkMapper.mapLinks(links);
Map<String, Object> model = LinkMapper.mapLinks(links);
assertEquals(1, model.size());
assertTrue(model.containsKey("a"));
@SuppressWarnings("unchecked")
@@ -63,10 +63,10 @@ public class LinkMapperTests {
@Test
public void mergeSeveralLinksInArray() {
List<Link> links = new ArrayList<Link>();
List<Link> links = new ArrayList<>();
links.add(Link.create("a", "https://example.com", "some description"));
links.add(Link.create("a", "https://example.com/2"));
Map<String, Object> model = (Map<String, Object>) LinkMapper.mapLinks(links);
Map<String, Object> model = LinkMapper.mapLinks(links);
assertEquals(1, model.size());
assertTrue(model.containsKey("a"));
@SuppressWarnings("unchecked")
@@ -78,20 +78,20 @@ public class LinkMapperTests {
@Test
public void keepOrdering() {
List<Link> links = new ArrayList<Link>();
List<Link> links = new ArrayList<>();
links.add(Link.create("a", "https://example.com"));
links.add(Link.create("b", "https://example.com"));
Map<String, Object> model = (Map<String, Object>) LinkMapper.mapLinks(links);
Map<String, Object> model = LinkMapper.mapLinks(links);
assertEquals("[a, b]", model.keySet().toString());
}
@Test
public void keepOrderingWithMultipleUrlForSameRel() {
List<Link> links = new ArrayList<Link>();
List<Link> links = new ArrayList<>();
links.add(Link.create("a", "https://example.com"));
links.add(Link.create("b", "https://example.com"));
links.add(Link.create("a", "https://example.com"));
Map<String, Object> model = (Map<String, Object>) LinkMapper.mapLinks(links);
Map<String, Object> model = LinkMapper.mapLinks(links);
assertEquals("[a, b]", model.keySet().toString());
}

View File

@@ -33,7 +33,8 @@ import org.springframework.test.context.ActiveProfiles;
* @author Stephane Nicoll
*/
@ActiveProfiles("test-default")
public class CommandLineExampleIntegrationTests extends AbstractInitializrControllerIntegrationTests {
public class CommandLineExampleIntegrationTests
extends AbstractInitializrControllerIntegrationTests {
@Test
public void generateDefaultProject() {
@@ -67,7 +68,8 @@ public class CommandLineExampleIntegrationTests extends AbstractInitializrContro
@Test
public void generateMavenPomWithWarPackaging() {
ResponseEntity<String> response = getRestTemplate().getForEntity(createUrl("/pom.xml?packaging=war"), String.class);
ResponseEntity<String> response = getRestTemplate().getForEntity(
createUrl("/pom.xml?packaging=war"), String.class);
PomAssert pomAssert = new PomAssert(response.getBody());
pomAssert.hasPackaging("war");
}

View File

@@ -0,0 +1,155 @@
/*
* Copyright 2012-2017 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.project;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Stephane Nicoll
*/
class HomePage {
@FindBy(id = "form")
private WebElement form;
private final WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public Object value(String id) {
return getInputValue(form.findElement(By.id(id)));
}
private Object getInputValue(WebElement input) {
Object value = null;
String type = input.getAttribute("type");
if ("select".equals(input.getTagName())) {
Select select = new Select(input);
if (select.isMultiple()) {
value = select.getAllSelectedOptions().stream().map(this::getValue)
.collect(Collectors.toList());
}
else {
value = getValue(select.getFirstSelectedOption());
}
}
else if (Arrays.asList("checkbox", "radio").contains(type)) {
if (input.isSelected()) {
value = getValue(input);
}
else {
if (Objects.equals(type, "checkbox")) {
value = false;
}
}
}
else {
value = getValue(input);
}
return value;
}
private String getValue(WebElement input) {
return input.getAttribute("value");
}
public WebElement dependency(String value) {
for (WebElement element : form.findElements(By.name("style"))) {
if (value.equals(element.getAttribute("value"))) {
return element;
}
}
throw new AssertionError("Dependency not found: " + value);
}
public void advanced() {
form.findElement(By.cssSelector(".tofullversion"))
.findElement(By.tagName("a")).click();
}
public void simple() {
form.findElement(By.cssSelector(".tosimpleversion")).click();
}
public void artifactId(String text) {
form.findElement(By.id("artifactId")).clear();
form.findElement(By.id("artifactId")).sendKeys(text);
}
public void autocomplete(String text) {
form.findElement(By.id("autocomplete")).sendKeys(text);
}
public void bootVersion(String text) {
form.findElement(By.id("bootVersion")).sendKeys(text);
form.click();
}
public void description(String text) {
form.findElement(By.id("description")).clear();
form.findElement(By.id("description")).sendKeys(text);
}
public void groupId(String text) {
form.findElement(By.id("groupId")).clear();
form.findElement(By.id("groupId")).sendKeys(text);
}
public void language(String text) {
form.findElement(By.id("language")).sendKeys(text);
}
public void name(String text) {
form.findElement(By.id("name")).clear();
form.findElement(By.id("name")).sendKeys(text);
}
public void packaging(String text) {
form.findElement(By.id("packaging")).sendKeys(text);
}
public void packageName(String text) {
form.findElement(By.id("packageName")).clear();
form.findElement(By.id("packageName")).sendKeys(text);
}
public void type(String text) {
form.findElement(By.id("type")).sendKeys(text);
}
public HomePage submit() {
String url = driver.getCurrentUrl();
form.findElement(By.name("generate-project")).click();
assertThat(driver.getCurrentUrl()).isEqualTo(url);
return this;
}
}

View File

@@ -16,7 +16,7 @@
package io.spring.initializr.web.project;
import java.util.Arrays;
import java.util.Collections;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.web.AbstractInitializrControllerIntegrationTests;
@@ -48,14 +48,18 @@ public class LegacyStsControllerIntegrationTests
String body = htmlHome();
assertTrue("groupId not found", body.contains("com.example"));
assertTrue("artifactId not found", body.contains("demo"));
assertTrue("custom description not found", body.contains("Demo project for Spring Boot"));
assertTrue("Wrong body:\n" + body, body.contains("<input type=\"radio\" name=\"language\" value=\"groovy\"/>"));
assertTrue("Wrong body:\n" + body, body.contains("<input type=\"radio\" name=\"language\" value=\"java\" checked=\"true\"/>"));
assertTrue("custom description not found",
body.contains("Demo project for Spring Boot"));
assertTrue("Wrong body:\n" + body,
body.contains("<input type=\"radio\" name=\"language\" value=\"groovy\"/>"));
assertTrue("Wrong body:\n" + body,
body.contains("<input type=\"radio\" name=\"language\" value=\"java\" checked=\"true\"/>"));
}
@Override
protected String htmlHome() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Arrays.asList(MediaType.TEXT_HTML));
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
return getRestTemplate().exchange(createUrl("/sts"), HttpMethod.GET,
new HttpEntity<Void>(headers), String.class).getBody();
}

View File

@@ -28,15 +28,18 @@ import static org.junit.Assert.assertTrue;
* @author Stephane Nicoll
*/
@ActiveProfiles({ "test-default", "test-custom-defaults" })
public class MainControllerDefaultsIntegrationTests extends AbstractInitializrControllerIntegrationTests {
public class MainControllerDefaultsIntegrationTests
extends AbstractInitializrControllerIntegrationTests {
// see defaults customization
@Test
public void generateDefaultPom() {
String content = getRestTemplate().getForObject(createUrl("/pom.xml?style=web"), String.class);
String content = getRestTemplate().getForObject(
createUrl("/pom.xml?style=web"), String.class);
PomAssert pomAssert = new PomAssert(content);
pomAssert.hasGroupId("org.foo").hasArtifactId("foo-bar").hasVersion("1.2.4-SNAPSHOT").hasPackaging("jar")
pomAssert.hasGroupId("org.foo").hasArtifactId("foo-bar")
.hasVersion("1.2.4-SNAPSHOT").hasPackaging("jar")
.hasName("FooBar").hasDescription("FooBar Project");
}

View File

@@ -34,13 +34,15 @@ import static org.junit.Assert.assertThat;
* @author Stephane Nicoll
*/
@ActiveProfiles("test-default")
public class MainControllerDependenciesTests extends AbstractInitializrControllerIntegrationTests {
public class MainControllerDependenciesTests
extends AbstractInitializrControllerIntegrationTests {
@Test
public void noBootVersion() {
ResponseEntity<String> response = execute("/dependencies", String.class, null, "application/json");
ResponseEntity<String> response = execute("/dependencies", String.class, null,
"application/json");
assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
validateContentType(response, AbstractInitializrControllerIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateContentType(response, CURRENT_METADATA_MEDIA_TYPE);
validateDependenciesOutput("1.1.4", new JSONObject(response.getBody()));
}
@@ -49,12 +51,13 @@ public class MainControllerDependenciesTests extends AbstractInitializrControlle
ResponseEntity<String> response = execute("/dependencies?bootVersion=1.2.1.RELEASE",
String.class, null, "application/json");
assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG), not(nullValue()));
validateContentType(response, AbstractInitializrControllerIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateContentType(response, CURRENT_METADATA_MEDIA_TYPE);
validateDependenciesOutput("1.2.1", new JSONObject(response.getBody()));
}
protected void validateDependenciesOutput(String version, JSONObject actual) {
JSONObject expected = readJsonFrom("metadata/dependencies/test-dependencies-" + version + ".json");
JSONObject expected = readJsonFrom(
"metadata/dependencies/test-dependencies-" + version + ".json");
JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT);
}

View File

@@ -34,11 +34,13 @@ import static org.junit.Assert.assertTrue;
* @author Stephane Nicoll
*/
@ActiveProfiles({ "test-default", "test-custom-env" })
public class MainControllerEnvIntegrationTests extends AbstractInitializrControllerIntegrationTests {
public class MainControllerEnvIntegrationTests
extends AbstractInitializrControllerIntegrationTests {
@Test
public void downloadCliWithCustomRepository() throws Exception {
ResponseEntity<?> entity = getRestTemplate().getForEntity(createUrl("/spring"), String.class);
ResponseEntity<?> entity = getRestTemplate().getForEntity(
createUrl("/spring"), String.class);
assertEquals(HttpStatus.FOUND, entity.getStatusCode());
String expected = "https://repo.spring.io/lib-release/org/springframework/boot/spring-boot-cli/1.1.4.RELEASE/spring-boot-cli-1.1.4.RELEASE-bin.zip";
assertEquals(new URI(expected), entity.getHeaders().getLocation());
@@ -55,7 +57,7 @@ public class MainControllerEnvIntegrationTests extends AbstractInitializrControl
@Test
public void generateProjectWithInvalidName() {
downloadZip("/starter.zip?style=data-jpa&name=Invalid")
.isJavaProject((String) ProjectAssert.DEFAULT_PACKAGE_NAME, "FooBarApplication")
.isJavaProject(ProjectAssert.DEFAULT_PACKAGE_NAME, "FooBarApplication")
.isMavenProject()
.hasStaticAndTemplatesResources(false).pomAssert()
.hasDependenciesCount(2)
@@ -66,7 +68,8 @@ public class MainControllerEnvIntegrationTests extends AbstractInitializrControl
@Test
public void googleAnalytics() {
String body = htmlHome();
assertTrue("google analytics should be enabled", body.contains("ga('create', 'UA-1234567-89', 'auto');"));
assertTrue("google analytics should be enabled",
body.contains("ga('create', 'UA-1234567-89', 'auto');"));
}
}

View File

@@ -270,7 +270,6 @@ public class MainControllerIntegrationTests
validateContentType(response,
AbstractInitializrControllerIntegrationTests.CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(new JSONObject(response.getBody()));
;
}
@Test
@@ -379,33 +378,36 @@ public class MainControllerIntegrationTests
assertFalse("Wrong body:\n" + body, body.contains("{{"));
assertFalse("Wrong body:\n" + body, body.contains("}}"));
assertTrue("Wrong body:\n" + body, body.contains("<option value=\"groovy\">"));
assertTrue("Wrong body:\n" + body, body.contains("<option value=\"java\" selected>"));
assertTrue("Wrong body:\n" + body,
body.contains("<option value=\"java\" selected>"));
}
@Test
public void homeIsJson() {
String body = invokeHome(null, (String[]) null).getBody();
assertTrue("Wrong body:\n$body", body.contains("\"dependencies\""));
assertTrue("Wrong body:\n" + body, body.contains("\"dependencies\""));
}
@Test
public void webIsAddedPom() {
String body = getRestTemplate().getForObject(createUrl("/pom.xml?packaging=war"), String.class);
String body = getRestTemplate().getForObject(
createUrl("/pom.xml?packaging=war"), String.class);
assertTrue("Wrong body:\n" + body, body.contains("spring-boot-starter-web"));
assertTrue("Wrong body:\n" + body, body.contains("provided"));
}
@Test
public void webIsAddedGradle() {
String body = getRestTemplate().getForObject(createUrl("/build.gradle?packaging=war"), String.class);
assertTrue("Wrong body:\n$body", body.contains("spring-boot-starter-web"));
assertTrue("Wrong body:\n$body", body.contains("providedRuntime"));
String body = getRestTemplate().getForObject(
createUrl("/build.gradle?packaging=war"), String.class);
assertTrue("Wrong body:\n" + body, body.contains("spring-boot-starter-web"));
assertTrue("Wrong body:\n" + body, body.contains("providedRuntime"));
}
@Test
public void homeHasWebStyle() {
String body = htmlHome();
assertTrue("Wrong body:\n$body", body.contains("name=\"style\" value=\"web\""));
assertTrue("Wrong body:\n" + body, body.contains("name=\"style\" value=\"web\""));
}
@Test
@@ -419,12 +421,14 @@ public class MainControllerIntegrationTests
public void homeHasOnlyProjectFormatTypes() {
String body = htmlHome();
assertTrue("maven project not found", body.contains("Maven Project"));
assertFalse("maven pom type should have been filtered", body.contains("Maven POM"));
assertFalse("maven pom type should have been filtered",
body.contains("Maven POM"));
}
@Test
public void downloadStarter() {
byte[] body = getRestTemplate().getForObject(createUrl("starter.zip"), byte[].class);
byte[] body = getRestTemplate().getForObject(
createUrl("starter.zip"), byte[].class);
assertNotNull(body);
assertTrue(body.length > 100);
}

View File

@@ -54,7 +54,8 @@ public class MainControllerServiceMetadataIntegrationTests
// Basic assertions
assertEquals(localMetadata.getDependencies().getContent().size(),
metadata.getDependencies().getContent().size());
assertEquals(localMetadata.getTypes().getContent().size(), metadata.getTypes().getContent().size());
assertEquals(localMetadata.getTypes().getContent().size(),
metadata.getTypes().getContent().size());
assertEquals(localMetadata.getBootVersions().getContent().size(),
metadata.getBootVersions().getContent().size());
assertEquals(localMetadata.getPackagings().getContent().size(),

View File

@@ -32,7 +32,8 @@ import org.springframework.test.context.ActiveProfiles;
@ActiveProfiles("test-default")
@Import(ProjectRequestPostProcessorConfiguration.class)
public class ProjectGenerationPostProcessorTests extends AbstractInitializrControllerIntegrationTests {
public class ProjectGenerationPostProcessorTests
extends AbstractInitializrControllerIntegrationTests {
@Test

View File

@@ -19,8 +19,6 @@ package io.spring.initializr.web.project;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
import io.spring.initializr.test.generator.ProjectAssert;
import io.spring.initializr.web.AbstractFullStackInitializrIntegrationTests;
@@ -28,17 +26,12 @@ import org.junit.After;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.util.StreamUtils;
@@ -49,13 +42,14 @@ import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
* @author Stephane Nicoll
*/
@ActiveProfiles("test-default")
public class NewGenerationSmokeTests extends AbstractFullStackInitializrIntegrationTests {
public class ProjectGenerationSmokeTests
extends AbstractFullStackInitializrIntegrationTests {
private File downloadDir;
private WebDriver driver;
private Actions actions;
private Action enterAction;
@@ -73,7 +67,7 @@ public class NewGenerationSmokeTests extends AbstractFullStackInitializrIntegrat
"application/zip,application/x-compress,application/octet-stream");
driver = new FirefoxDriver(fxProfile);
actions = new Actions(driver);
Actions actions = new Actions(driver);
enterAction = actions.sendKeys(Keys.ENTER).build();
}
@@ -314,7 +308,7 @@ public class NewGenerationSmokeTests extends AbstractFullStackInitializrIntegrat
ProjectAssert projectAssert = zipProjectAssert(from("demo.zip"));
projectAssert.hasBaseDir("demo").isMavenProject()
.isGroovyProject("com.example.acme",
(String) ProjectAssert.DEFAULT_APPLICATION_NAME)
ProjectAssert.DEFAULT_APPLICATION_NAME)
.hasStaticAndTemplatesResources(false).pomAssert().hasDependenciesCount(3)
.hasSpringBootStarterRootDependency().hasSpringBootStarterTest()
.hasDependency("org.codehaus.groovy", "groovy");
@@ -352,8 +346,7 @@ public class NewGenerationSmokeTests extends AbstractFullStackInitializrIntegrat
private HomePage toHome(String path) {
driver.get("http://localhost:" + port + path);
HomePage page = new HomePage(driver);
return page;
return new HomePage(driver);
}
private ProjectAssert assertSimpleProject() throws Exception {
@@ -378,123 +371,3 @@ public class NewGenerationSmokeTests extends AbstractFullStackInitializrIntegrat
}
class HomePage {
@FindBy(id = "form")
private WebElement form;
private WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public Object value(String id) {
return getInputValue(form.findElement(By.id(id)));
}
private Object getInputValue(WebElement input) {
Object value = null;
String type = input.getAttribute("type");
if ("select".equals(input.getTagName())) {
Select select = new Select(input);
if (select.isMultiple()) {
value = select.getAllSelectedOptions().stream().map(it -> getValue(it))
.collect(Collectors.toList());
}
else {
value = getValue(select.getFirstSelectedOption());
}
}
else if (Arrays.asList("checkbox", "radio").contains(type)) {
if (input.isSelected()) {
value = getValue(input);
}
else {
if (type == "checkbox") {
value = false;
}
}
}
else {
value = getValue(input);
}
return value;
}
private String getValue(WebElement input) {
return input.getAttribute("value");
}
public WebElement dependency(String value) {
for (WebElement element : form.findElements(By.name("style"))) {
if (value.equals(element.getAttribute("value"))) {
return element;
}
}
throw new AssertionError("Dependency not found: " + value);
}
public void advanced() {
form.findElement(By.cssSelector(".tofullversion"))
.findElement(By.tagName("a")).click();
}
public void simple() {
form.findElement(By.cssSelector(".tosimpleversion")).click();
}
public void artifactId(String text) {
form.findElement(By.id("artifactId")).clear();
form.findElement(By.id("artifactId")).sendKeys(text);
}
public void autocomplete(String text) {
form.findElement(By.id("autocomplete")).sendKeys(text);
}
public void bootVersion(String text) {
form.findElement(By.id("bootVersion")).sendKeys(text);
form.click();
}
public void description(String text) {
form.findElement(By.id("description")).clear();
form.findElement(By.id("description")).sendKeys(text);
}
public void groupId(String text) {
form.findElement(By.id("groupId")).clear();
form.findElement(By.id("groupId")).sendKeys(text);
}
public void language(String text) {
form.findElement(By.id("language")).sendKeys(text);
}
public void name(String text) {
form.findElement(By.id("name")).clear();
form.findElement(By.id("name")).sendKeys(text);
}
public void packaging(String text) {
form.findElement(By.id("packaging")).sendKeys(text);
}
public void packageName(String text) {
form.findElement(By.id("packageName")).clear();
form.findElement(By.id("packageName")).sendKeys(text);
}
public void type(String text) {
form.findElement(By.id("type")).sendKeys(text);
}
public HomePage submit() {
String url = driver.getCurrentUrl();
form.findElement(By.name("generate-project")).click();
assertThat(driver.getCurrentUrl()).isEqualTo(url);
return this;
}
}

View File

@@ -33,7 +33,8 @@ import static org.junit.Assert.assertSame;
*/
public class DefaultDependencyMetadataProviderTests {
private final DependencyMetadataProvider provider = new DefaultDependencyMetadataProvider();
private final DependencyMetadataProvider provider =
new DefaultDependencyMetadataProvider();
@Test
public void filterDependencies() {
@@ -180,7 +181,8 @@ public class DefaultDependencyMetadataProviderTests {
"2.0.0.RELEASE", "repo-foo", "repo-bar"));
bom.getMappings().add(BillOfMaterials.Mapping.create("1.1.0.RELEASE",
"3.0.0.RELEASE", "repo-biz"));
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults().addBom("bom-foo", bom)
InitializrMetadata metadata = InitializrMetadataTestBuilder
.withDefaults().addBom("bom-foo", bom)
.addRepository("repo-foo", "foo", "http://localhost", false)
.addRepository("repo-bar", "bar", "http://localhost", false)
.addRepository("repo-biz", "biz", "http://localhost", false)

View File

@@ -56,15 +56,19 @@ public class DefaultInitializrMetadataProviderTests {
@Test
public void bootVersionsAreReplaced() {
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addBootVersion("0.0.9.RELEASE", true).addBootVersion("0.0.8.RELEASE", false).build();
.addBootVersion("0.0.9.RELEASE", true)
.addBootVersion("0.0.8.RELEASE", false)
.build();
assertEquals("0.0.9.RELEASE", metadata.getBootVersions().getDefault().getId());
DefaultInitializrMetadataProvider provider = new DefaultInitializrMetadataProvider(metadata, restTemplate);
DefaultInitializrMetadataProvider provider =
new DefaultInitializrMetadataProvider(metadata, restTemplate);
expectJson(metadata.getConfiguration().getEnv().getSpringBootMetadataUrl(),
"metadata/sagan/spring-boot.json");
InitializrMetadata updatedMetadata = provider.get();
assertNotNull(updatedMetadata.getBootVersions());
List<DefaultMetadataElement> updatedBootVersions = updatedMetadata.getBootVersions().getContent();
List<DefaultMetadataElement> updatedBootVersions =
updatedMetadata.getBootVersions().getContent();
assertEquals(4, updatedBootVersions.size());
assertBootVersion(updatedBootVersions.get(0), "1.4.1 (SNAPSHOT)", false);
assertBootVersion(updatedBootVersions.get(1), "1.4.0", true);
@@ -75,15 +79,18 @@ public class DefaultInitializrMetadataProviderTests {
@Test
public void defaultBootVersionIsAlwaysSet() {
InitializrMetadata metadata = new InitializrMetadataTestBuilder()
.addBootVersion("0.0.9.RELEASE", true).addBootVersion("0.0.8.RELEASE", false).build();
.addBootVersion("0.0.9.RELEASE", true)
.addBootVersion("0.0.8.RELEASE", false).build();
assertEquals("0.0.9.RELEASE", metadata.getBootVersions().getDefault().getId());
DefaultInitializrMetadataProvider provider = new DefaultInitializrMetadataProvider(metadata, restTemplate);
DefaultInitializrMetadataProvider provider =
new DefaultInitializrMetadataProvider(metadata, restTemplate);
expectJson(metadata.getConfiguration().getEnv().getSpringBootMetadataUrl(),
"metadata/sagan/spring-boot-no-default.json");
InitializrMetadata updatedMetadata = provider.get();
assertNotNull(updatedMetadata.getBootVersions());
List<DefaultMetadataElement> updatedBootVersions = updatedMetadata.getBootVersions().getContent();
List<DefaultMetadataElement> updatedBootVersions =
updatedMetadata.getBootVersions().getContent();
assertEquals(4, updatedBootVersions.size());
assertBootVersion(updatedBootVersions.get(0), "1.3.1 (SNAPSHOT)", true);
assertBootVersion(updatedBootVersions.get(1), "1.3.0", false);
@@ -91,7 +98,8 @@ public class DefaultInitializrMetadataProviderTests {
assertBootVersion(updatedBootVersions.get(3), "1.2.5", false);
}
private static void assertBootVersion(DefaultMetadataElement actual, String name, boolean defaultVersion) {
private static void assertBootVersion(DefaultMetadataElement actual, String name,
boolean defaultVersion) {
assertEquals(name, actual.getName());
assertEquals(defaultVersion, actual.isDefault());
}

View File

@@ -40,18 +40,23 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
*/
public class SpringBootMetadataReaderTests {
private final InitializrMetadata metadata = InitializrMetadataBuilder.create().build();
private final InitializrMetadata metadata =
InitializrMetadataBuilder.create().build();
private final RestTemplate restTemplate = new RestTemplate();
private final MockRestServiceServer server = MockRestServiceServer.bindTo(restTemplate).build();
private final MockRestServiceServer server =
MockRestServiceServer.bindTo(restTemplate).build();
@Test
public void readAvailableVersions() {
server.expect(requestTo("https://spring.io/project_metadata/spring-boot")).andRespond(
withSuccess(new ClassPathResource("metadata/sagan/spring-boot.json"), MediaType.APPLICATION_JSON));
server.expect(requestTo("https://spring.io/project_metadata/spring-boot"))
.andRespond(withSuccess(
new ClassPathResource("metadata/sagan/spring-boot.json"),
MediaType.APPLICATION_JSON));
List<DefaultMetadataElement> versions = new SpringBootMetadataReader(restTemplate,
metadata.getConfiguration().getEnv().getSpringBootMetadataUrl()).getBootVersions();
metadata.getConfiguration().getEnv()
.getSpringBootMetadataUrl()).getBootVersions();
assertNotNull("spring boot versions should not be null", versions);
AtomicBoolean defaultFound = new AtomicBoolean(false);
versions.forEach(it -> {
@@ -59,7 +64,7 @@ public class SpringBootMetadataReaderTests {
assertNotNull("Name must be set", it.getName());
if (it.isDefault()) {
if (defaultFound.get()) {
fail("One default version was already found $it.id");
fail("One default version was already found " + it.getId());
}
defaultFound.set(true);
}

View File

@@ -43,7 +43,7 @@ import org.springframework.restdocs.templates.TemplateEngine;
*/
public class ResponseFieldSnippet extends TemplatedSnippet {
private String path;
private final String path;
private final JsonFieldProcessor fieldProcessor = new JsonFieldProcessor();
@@ -96,7 +96,6 @@ public class ResponseFieldSnippet extends TemplatedSnippet {
@Override
protected Map<String, Object> createModel(Operation operation) {
String value = "{}";
try {
Object object = objectMapper.readValue(
operation.getResponse().getContentAsString(), Object.class);
@@ -104,12 +103,12 @@ public class ResponseFieldSnippet extends TemplatedSnippet {
if (field instanceof List && index != null) {
field = ((List<?>) field).get(index);
}
value = objectMapper.writeValueAsString(field);
return Collections.singletonMap("value",
objectMapper.writeValueAsString(field));
}
catch (Exception e) {
throw new IllegalStateException(e);
catch (Exception ex) {
throw new IllegalStateException(ex);
}
return Collections.singletonMap("value", value);
}
}

View File

@@ -30,7 +30,8 @@ import org.springframework.test.context.ActiveProfiles;
* @author Stephane Nicoll
*/
@ActiveProfiles("test-default")
public class UiControllerIntegrationTests extends AbstractInitializrControllerIntegrationTests {
public class UiControllerIntegrationTests
extends AbstractInitializrControllerIntegrationTests {
@Test
public void dependenciesNoVersion() {
@@ -41,13 +42,15 @@ public class UiControllerIntegrationTests extends AbstractInitializrControllerIn
@Test
public void dependenciesSpecificVersion() {
ResponseEntity<String> response = execute("/ui/dependencies?version=1.1.2.RELEASE", String.class, null);
ResponseEntity<String> response = execute(
"/ui/dependencies?version=1.1.2.RELEASE", String.class, null);
validateContentType(response, MediaType.APPLICATION_JSON);
validateDependenciesOutput("1.1.2", new JSONObject(response.getBody()));
}
protected void validateDependenciesOutput(String version, JSONObject actual) {
JSONObject expected = readJsonFrom("metadata/ui/test-dependencies-" + version + ".json");
JSONObject expected = readJsonFrom(
"metadata/ui/test-dependencies-" + version + ".json");
JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT);
}
}