Skip to content

Commit

Permalink
Migrate to android-json
Browse files Browse the repository at this point in the history
Migrate from `org.json:json` to the clean room Apache 2.0 licensed
version that was developed for Android.

Fixes gh-5929
  • Loading branch information
philwebb committed Jan 5, 2017
1 parent 4cb7d86 commit cc7c2eb
Show file tree
Hide file tree
Showing 22 changed files with 389 additions and 259 deletions.
8 changes: 4 additions & 4 deletions spring-boot-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</dependency>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
Expand All @@ -54,10 +58,6 @@
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ private String extractMessage(HttpEntity entity) {
return null;
}

private JSONObject getContentAsJson(HttpEntity entity) throws IOException {
private JSONObject getContentAsJson(HttpEntity entity)
throws IOException, JSONException {
return new JSONObject(getContent(entity));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -124,7 +126,8 @@ public Map<String, String> getDefaults() {
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;
Expand All @@ -138,7 +141,8 @@ private Map<String, Dependency> parseDependencies(JSONObject root) {
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;
Expand All @@ -158,7 +162,7 @@ private MetadataHolder<String, ProjectType> parseProjectTypes(JSONObject root) {
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()) {
Expand All @@ -174,7 +178,8 @@ private Map<String, String> parseDefaults(JSONObject root) {
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++) {
Expand All @@ -184,14 +189,15 @@ private void parseGroup(JSONObject group, Map<String, Dependency> dependencies)
}
}

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);
Expand All @@ -204,14 +210,15 @@ private ProjectType parseType(JSONObject object, String defaultId) {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -95,7 +96,7 @@ protected void mockSuccessfulProjectGeneration(
}

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);
Expand All @@ -105,7 +106,8 @@ protected void mockProjectGenerationError(int status, String message)
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");
Expand Down Expand Up @@ -156,7 +158,7 @@ private String contentDispositionValue(String fileName) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.InputStream;
import java.nio.charset.Charset;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

Expand All @@ -37,7 +38,7 @@
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");
Expand All @@ -55,7 +56,7 @@ public void parseDefaults() {
}

@Test
public void parseDependencies() {
public void parseDependencies() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDependencies()).hasSize(5);

Expand All @@ -70,15 +71,16 @@ public void parseDependencies() {
}

@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();
assertThat(projectType.getTags().get("build")).isEqualTo("maven");
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));
}
Expand All @@ -87,7 +89,7 @@ private static InitializrServiceMetadata createInstance(String version) {
}
}

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -59,7 +57,7 @@ public void generateSimpleProject() throws IOException {
}

@Test
public void generateProjectCustomTargetFilename() throws IOException {
public void generateProjectCustomTargetFilename() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.setOutput("bar.zip");
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
Expand All @@ -69,7 +67,7 @@ public void generateProjectCustomTargetFilename() throws IOException {
}

@Test
public void generateProjectNoDefaultFileName() throws IOException {
public void generateProjectNoDefaultFileName() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null);
Expand All @@ -78,7 +76,7 @@ public void generateProjectNoDefaultFileName() throws IOException {
}

@Test
public void generateProjectBadRequest() throws IOException {
public void generateProjectBadRequest() throws Exception {
String jsonMessage = "Unknown dependency foo:bar";
mockProjectGenerationError(400, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
Expand All @@ -89,7 +87,7 @@ public void generateProjectBadRequest() throws IOException {
}

@Test
public void generateProjectBadRequestNoExtraMessage() throws IOException {
public void generateProjectBadRequestNoExtraMessage() throws Exception {
mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class);
Expand All @@ -98,7 +96,7 @@ public void generateProjectBadRequestNoExtraMessage() throws IOException {
}

@Test
public void generateProjectNoContent() throws IOException {
public void generateProjectNoContent() throws Exception {
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
Expand All @@ -110,7 +108,7 @@ public void generateProjectNoContent() throws IOException {
}

@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();
Expand All @@ -120,7 +118,7 @@ public void loadMetadataBadRequest() throws IOException {
}

@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);
Expand All @@ -132,7 +130,7 @@ public void loadMetadataInvalidJson() throws IOException {
}

@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);
Expand All @@ -143,7 +141,7 @@ public void loadMetadataNoContent() throws IOException {
}

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")
Expand Down
Loading

0 comments on commit cc7c2eb

Please sign in to comment.