Migrate to android-json

Migrate from `org.json:json` to the clean room Apache 2.0 licensed
version that was developed for Android.

Fixes gh-5929
This commit is contained in:
Phillip Webb
2017-01-04 21:18:08 -08:00
parent 4cb7d86aec
commit cc7c2ebb87
22 changed files with 389 additions and 259 deletions

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");