Merge branch '1.5.x'

This commit is contained in:
Phillip Webb
2017-01-04 23:52:40 -08:00
23 changed files with 450 additions and 284 deletions

View File

@@ -233,7 +233,8 @@ class InitializrService {
return null;
}
private JSONObject getContentAsJson(HttpEntity entity) throws IOException {
private JSONObject getContentAsJson(HttpEntity entity)
throws IOException, JSONException {
return new JSONObject(getContent(entity));
}

View File

@@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
@@ -58,8 +59,9 @@ class InitializrServiceMetadata {
/**
* Creates a new instance using the specified root {@link JSONObject}.
* @param root the root JSONObject
* @throws JSONException on JSON parsing failure
*/
InitializrServiceMetadata(JSONObject root) {
InitializrServiceMetadata(JSONObject root) throws JSONException {
this.dependencies = parseDependencies(root);
this.projectTypes = parseProjectTypes(root);
this.defaults = Collections.unmodifiableMap(parseDefaults(root));
@@ -124,7 +126,8 @@ class InitializrServiceMetadata {
return this.defaults;
}
private Map<String, Dependency> parseDependencies(JSONObject root) {
private Map<String, Dependency> parseDependencies(JSONObject root)
throws JSONException {
Map<String, Dependency> result = new HashMap<String, Dependency>();
if (!root.has(DEPENDENCIES_EL)) {
return result;
@@ -138,7 +141,8 @@ class InitializrServiceMetadata {
return result;
}
private MetadataHolder<String, ProjectType> parseProjectTypes(JSONObject root) {
private MetadataHolder<String, ProjectType> parseProjectTypes(JSONObject root)
throws JSONException {
MetadataHolder<String, ProjectType> result = new MetadataHolder<String, ProjectType>();
if (!root.has(TYPE_EL)) {
return result;
@@ -158,7 +162,7 @@ class InitializrServiceMetadata {
return result;
}
private Map<String, String> parseDefaults(JSONObject root) {
private Map<String, String> parseDefaults(JSONObject root) throws JSONException {
Map<String, String> result = new HashMap<String, String>();
Iterator<?> keys = root.keys();
while (keys.hasNext()) {
@@ -174,7 +178,8 @@ class InitializrServiceMetadata {
return result;
}
private void parseGroup(JSONObject group, Map<String, Dependency> dependencies) {
private void parseGroup(JSONObject group, Map<String, Dependency> dependencies)
throws JSONException {
if (group.has(VALUES_EL)) {
JSONArray content = group.getJSONArray(VALUES_EL);
for (int i = 0; i < content.length(); i++) {
@@ -184,14 +189,15 @@ class InitializrServiceMetadata {
}
}
private Dependency parseDependency(JSONObject object) {
private Dependency parseDependency(JSONObject object) throws JSONException {
String id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null);
String description = getStringValue(object, DESCRIPTION_ATTRIBUTE, null);
return new Dependency(id, name, description);
}
private ProjectType parseType(JSONObject object, String defaultId) {
private ProjectType parseType(JSONObject object, String defaultId)
throws JSONException {
String id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null);
String action = getStringValue(object, ACTION_ATTRIBUTE, null);
@@ -204,14 +210,15 @@ class InitializrServiceMetadata {
return new ProjectType(id, name, action, defaultType, tags);
}
private String getStringValue(JSONObject object, String name, String defaultValue) {
private String getStringValue(JSONObject object, String name, String defaultValue)
throws JSONException {
return object.has(name) ? object.getString(name) : defaultValue;
}
private Map<String, String> parseStringItems(JSONObject json) {
private Map<String, String> parseStringItems(JSONObject json) throws JSONException {
Map<String, String> result = new HashMap<String, String>();
for (Object k : json.keySet()) {
String key = (String) k;
for (Iterator<?> iterator = json.keys(); iterator.hasNext();) {
String key = (String) iterator.next();
Object value = json.get(key);
if (value instanceof String) {
result.put(key, (String) value);

View File

@@ -28,6 +28,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.hamcrest.Matcher;
import org.json.JSONException;
import org.json.JSONObject;
import org.mockito.ArgumentMatcher;
@@ -95,7 +96,7 @@ public abstract class AbstractHttpClientMockTests {
}
protected void mockProjectGenerationError(int status, String message)
throws IOException {
throws IOException, JSONException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
@@ -105,7 +106,8 @@ public abstract class AbstractHttpClientMockTests {
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
}
protected void mockMetadataGetError(int status, String message) throws IOException {
protected void mockMetadataGetError(int status, String message)
throws IOException, JSONException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json");
@@ -156,7 +158,7 @@ public abstract class AbstractHttpClientMockTests {
return "attachment; filename=\"" + fileName + "\"";
}
private String createJsonError(int status, String message) {
private String createJsonError(int status, String message) throws JSONException {
JSONObject json = new JSONObject();
json.put("status", status);
if (message != null) {

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;
@@ -37,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class InitializrServiceMetadataTests {
@Test
public void parseDefaults() {
public void parseDefaults() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDefaults().get("bootVersion")).isEqualTo("1.1.8.RELEASE");
assertThat(metadata.getDefaults().get("javaVersion")).isEqualTo("1.7");
@@ -55,7 +56,7 @@ public class InitializrServiceMetadataTests {
}
@Test
public void parseDependencies() {
public void parseDependencies() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDependencies()).hasSize(5);
@@ -70,7 +71,7 @@ public class InitializrServiceMetadataTests {
}
@Test
public void parseTypes() {
public void parseTypes() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
ProjectType projectType = metadata.getProjectTypes().get("maven-project");
assertThat(projectType).isNotNull();
@@ -78,7 +79,8 @@ public class InitializrServiceMetadataTests {
assertThat(projectType.getTags().get("format")).isEqualTo("project");
}
private static InitializrServiceMetadata createInstance(String version) {
private static InitializrServiceMetadata createInstance(String version)
throws JSONException {
try {
return new InitializrServiceMetadata(readJson(version));
}
@@ -87,7 +89,7 @@ public class InitializrServiceMetadataTests {
}
}
private static JSONObject readJson(String version) throws IOException {
private static JSONObject readJson(String version) throws IOException, JSONException {
Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json");
InputStream stream = resource.getInputStream();

View File

@@ -16,8 +16,6 @@
package org.springframework.boot.cli.command.init;
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.junit.Rule;
@@ -42,14 +40,14 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
private final InitializrService invoker = new InitializrService(this.http);
@Test
public void loadMetadata() throws IOException {
public void loadMetadata() throws Exception {
mockSuccessfulMetadataGet(false);
InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar");
assertThat(metadata).isNotNull();
}
@Test
public void generateSimpleProject() throws IOException {
public void generateSimpleProject() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", "foo.zip");
@@ -59,7 +57,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectCustomTargetFilename() throws IOException {
public void generateProjectCustomTargetFilename() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.setOutput("bar.zip");
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
@@ -69,7 +67,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectNoDefaultFileName() throws IOException {
public void generateProjectNoDefaultFileName() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null);
@@ -78,7 +76,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectBadRequest() throws IOException {
public void generateProjectBadRequest() throws Exception {
String jsonMessage = "Unknown dependency foo:bar";
mockProjectGenerationError(400, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
@@ -89,7 +87,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectBadRequestNoExtraMessage() throws IOException {
public void generateProjectBadRequestNoExtraMessage() throws Exception {
mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
@@ -98,7 +96,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void generateProjectNoContent() throws IOException {
public void generateProjectNoContent() throws Exception {
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
@@ -110,7 +108,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataBadRequest() throws IOException {
public void loadMetadataBadRequest() throws Exception {
String jsonMessage = "whatever error on the server";
mockMetadataGetError(500, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
@@ -120,7 +118,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataInvalidJson() throws IOException {
public void loadMetadataInvalidJson() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json");
mockStatus(response, 200);
@@ -132,7 +130,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
@Test
public void loadMetadataNoContent() throws IOException {
public void loadMetadataNoContent() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
@@ -143,7 +141,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
}
private ProjectGenerationResponse generateProject(ProjectGenerationRequest request,
MockHttpProjectGenerationRequest mockRequest) throws IOException {
MockHttpProjectGenerationRequest mockRequest) throws Exception {
mockSuccessfulProjectGeneration(mockRequest);
ProjectGenerationResponse entity = this.invoker.generate(request);
assertThat(entity.getContent()).as("wrong body content")

View File

@@ -23,6 +23,7 @@ import java.nio.charset.Charset;
import java.util.Collections;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
@@ -169,7 +170,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildNoMatch() {
public void buildNoMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("does-not-exist", null);
this.thrown.expect(ReportableException.class);
@@ -178,7 +179,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildMultipleMatch() {
public void buildMultipleMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata("types-conflict");
setBuildAndFormat("gradle", null);
this.thrown.expect(ReportableException.class);
@@ -188,7 +189,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void buildOneMatch() {
public void buildOneMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", null);
assertThat(this.request.generateUrl(metadata))
@@ -196,7 +197,7 @@ public class ProjectGenerationRequestTests {
}
@Test
public void typeAndBuildAndFormat() {
public void typeAndBuildAndFormat() throws Exception {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", "project");
this.request.setType("maven-build");
@@ -205,14 +206,14 @@ public class ProjectGenerationRequestTests {
}
@Test
public void invalidType() throws URISyntaxException {
public void invalidType() throws Exception {
this.request.setType("does-not-exist");
this.thrown.expect(ReportableException.class);
this.request.generateUrl(createDefaultMetadata());
}
@Test
public void noTypeAndNoDefault() throws URISyntaxException {
public void noTypeAndNoDefault() throws Exception {
this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("no default is defined");
this.request.generateUrl(readMetadata("types-conflict"));
@@ -243,11 +244,12 @@ public class ProjectGenerationRequestTests {
return new InitializrServiceMetadata(projectType);
}
private static InitializrServiceMetadata readMetadata() {
private static InitializrServiceMetadata readMetadata() throws JSONException {
return readMetadata("2.0.0");
}
private static InitializrServiceMetadata readMetadata(String version) {
private static InitializrServiceMetadata readMetadata(String version)
throws JSONException {
try {
Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json");