Commit 4bfecc13 authored by Phillip Webb's avatar Phillip Webb

Merge branch '1.5.x'

parents 125d9d61 45d5b60f
...@@ -38,6 +38,10 @@ ...@@ -38,6 +38,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-loader-tools</artifactId> <artifactId>spring-boot-loader-tools</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</dependency>
<dependency> <dependency>
<groupId>jline</groupId> <groupId>jline</groupId>
<artifactId>jline</artifactId> <artifactId>jline</artifactId>
...@@ -54,10 +58,6 @@ ...@@ -54,10 +58,6 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId> <artifactId>spring-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.apache.maven</groupId> <groupId>org.apache.maven</groupId>
<artifactId>maven-aether-provider</artifactId> <artifactId>maven-aether-provider</artifactId>
......
...@@ -233,7 +233,8 @@ class InitializrService { ...@@ -233,7 +233,8 @@ class InitializrService {
return null; return null;
} }
private JSONObject getContentAsJson(HttpEntity entity) throws IOException { private JSONObject getContentAsJson(HttpEntity entity)
throws IOException, JSONException {
return new JSONObject(getContent(entity)); return new JSONObject(getContent(entity));
} }
......
...@@ -23,6 +23,7 @@ import java.util.Iterator; ...@@ -23,6 +23,7 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
/** /**
...@@ -58,8 +59,9 @@ class InitializrServiceMetadata { ...@@ -58,8 +59,9 @@ class InitializrServiceMetadata {
/** /**
* Creates a new instance using the specified root {@link JSONObject}. * Creates a new instance using the specified root {@link JSONObject}.
* @param root the root 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.dependencies = parseDependencies(root);
this.projectTypes = parseProjectTypes(root); this.projectTypes = parseProjectTypes(root);
this.defaults = Collections.unmodifiableMap(parseDefaults(root)); this.defaults = Collections.unmodifiableMap(parseDefaults(root));
...@@ -124,7 +126,8 @@ class InitializrServiceMetadata { ...@@ -124,7 +126,8 @@ class InitializrServiceMetadata {
return this.defaults; 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>(); Map<String, Dependency> result = new HashMap<String, Dependency>();
if (!root.has(DEPENDENCIES_EL)) { if (!root.has(DEPENDENCIES_EL)) {
return result; return result;
...@@ -138,7 +141,8 @@ class InitializrServiceMetadata { ...@@ -138,7 +141,8 @@ class InitializrServiceMetadata {
return result; 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>(); MetadataHolder<String, ProjectType> result = new MetadataHolder<String, ProjectType>();
if (!root.has(TYPE_EL)) { if (!root.has(TYPE_EL)) {
return result; return result;
...@@ -158,7 +162,7 @@ class InitializrServiceMetadata { ...@@ -158,7 +162,7 @@ class InitializrServiceMetadata {
return result; 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>(); Map<String, String> result = new HashMap<String, String>();
Iterator<?> keys = root.keys(); Iterator<?> keys = root.keys();
while (keys.hasNext()) { while (keys.hasNext()) {
...@@ -174,7 +178,8 @@ class InitializrServiceMetadata { ...@@ -174,7 +178,8 @@ class InitializrServiceMetadata {
return result; 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)) { if (group.has(VALUES_EL)) {
JSONArray content = group.getJSONArray(VALUES_EL); JSONArray content = group.getJSONArray(VALUES_EL);
for (int i = 0; i < content.length(); i++) { for (int i = 0; i < content.length(); i++) {
...@@ -184,14 +189,15 @@ class InitializrServiceMetadata { ...@@ -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 id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null); String name = getStringValue(object, NAME_ATTRIBUTE, null);
String description = getStringValue(object, DESCRIPTION_ATTRIBUTE, null); String description = getStringValue(object, DESCRIPTION_ATTRIBUTE, null);
return new Dependency(id, name, description); 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 id = getStringValue(object, ID_ATTRIBUTE, null);
String name = getStringValue(object, NAME_ATTRIBUTE, null); String name = getStringValue(object, NAME_ATTRIBUTE, null);
String action = getStringValue(object, ACTION_ATTRIBUTE, null); String action = getStringValue(object, ACTION_ATTRIBUTE, null);
...@@ -204,14 +210,15 @@ class InitializrServiceMetadata { ...@@ -204,14 +210,15 @@ class InitializrServiceMetadata {
return new ProjectType(id, name, action, defaultType, tags); 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; 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>(); Map<String, String> result = new HashMap<String, String>();
for (Object k : json.keySet()) { for (Iterator<?> iterator = json.keys(); iterator.hasNext();) {
String key = (String) k; String key = (String) iterator.next();
Object value = json.get(key); Object value = json.get(key);
if (value instanceof String) { if (value instanceof String) {
result.put(key, (String) value); result.put(key, (String) value);
......
...@@ -28,6 +28,7 @@ import org.apache.http.client.methods.HttpGet; ...@@ -28,6 +28,7 @@ import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicHeader;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.mockito.ArgumentMatcher; import org.mockito.ArgumentMatcher;
...@@ -95,7 +96,7 @@ public abstract class AbstractHttpClientMockTests { ...@@ -95,7 +96,7 @@ public abstract class AbstractHttpClientMockTests {
} }
protected void mockProjectGenerationError(int status, String message) protected void mockProjectGenerationError(int status, String message)
throws IOException { throws IOException, JSONException {
// Required for project generation as the metadata is read first // Required for project generation as the metadata is read first
mockSuccessfulMetadataGet(false); mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class); CloseableHttpResponse response = mock(CloseableHttpResponse.class);
...@@ -105,7 +106,8 @@ public abstract class AbstractHttpClientMockTests { ...@@ -105,7 +106,8 @@ public abstract class AbstractHttpClientMockTests {
given(this.http.execute(isA(HttpGet.class))).willReturn(response); 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); CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(), mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json"); "application/json");
...@@ -156,7 +158,7 @@ public abstract class AbstractHttpClientMockTests { ...@@ -156,7 +158,7 @@ public abstract class AbstractHttpClientMockTests {
return "attachment; filename=\"" + 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(); JSONObject json = new JSONObject();
json.put("status", status); json.put("status", status);
if (message != null) { if (message != null) {
......
...@@ -20,6 +20,7 @@ import java.io.IOException; ...@@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Test; import org.junit.Test;
...@@ -37,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -37,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class InitializrServiceMetadataTests { public class InitializrServiceMetadataTests {
@Test @Test
public void parseDefaults() { public void parseDefaults() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0"); InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDefaults().get("bootVersion")).isEqualTo("1.1.8.RELEASE"); assertThat(metadata.getDefaults().get("bootVersion")).isEqualTo("1.1.8.RELEASE");
assertThat(metadata.getDefaults().get("javaVersion")).isEqualTo("1.7"); assertThat(metadata.getDefaults().get("javaVersion")).isEqualTo("1.7");
...@@ -55,7 +56,7 @@ public class InitializrServiceMetadataTests { ...@@ -55,7 +56,7 @@ public class InitializrServiceMetadataTests {
} }
@Test @Test
public void parseDependencies() { public void parseDependencies() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0"); InitializrServiceMetadata metadata = createInstance("2.0.0");
assertThat(metadata.getDependencies()).hasSize(5); assertThat(metadata.getDependencies()).hasSize(5);
...@@ -70,7 +71,7 @@ public class InitializrServiceMetadataTests { ...@@ -70,7 +71,7 @@ public class InitializrServiceMetadataTests {
} }
@Test @Test
public void parseTypes() { public void parseTypes() throws Exception {
InitializrServiceMetadata metadata = createInstance("2.0.0"); InitializrServiceMetadata metadata = createInstance("2.0.0");
ProjectType projectType = metadata.getProjectTypes().get("maven-project"); ProjectType projectType = metadata.getProjectTypes().get("maven-project");
assertThat(projectType).isNotNull(); assertThat(projectType).isNotNull();
...@@ -78,7 +79,8 @@ public class InitializrServiceMetadataTests { ...@@ -78,7 +79,8 @@ public class InitializrServiceMetadataTests {
assertThat(projectType.getTags().get("format")).isEqualTo("project"); assertThat(projectType.getTags().get("format")).isEqualTo("project");
} }
private static InitializrServiceMetadata createInstance(String version) { private static InitializrServiceMetadata createInstance(String version)
throws JSONException {
try { try {
return new InitializrServiceMetadata(readJson(version)); return new InitializrServiceMetadata(readJson(version));
} }
...@@ -87,7 +89,7 @@ public class InitializrServiceMetadataTests { ...@@ -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( Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json"); "metadata/service-metadata-" + version + ".json");
InputStream stream = resource.getInputStream(); InputStream stream = resource.getInputStream();
......
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
package org.springframework.boot.cli.command.init; package org.springframework.boot.cli.command.init;
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.junit.Rule; import org.junit.Rule;
...@@ -42,14 +40,14 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -42,14 +40,14 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
private final InitializrService invoker = new InitializrService(this.http); private final InitializrService invoker = new InitializrService(this.http);
@Test @Test
public void loadMetadata() throws IOException { public void loadMetadata() throws Exception {
mockSuccessfulMetadataGet(false); mockSuccessfulMetadataGet(false);
InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar"); InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar");
assertThat(metadata).isNotNull(); assertThat(metadata).isNotNull();
} }
@Test @Test
public void generateSimpleProject() throws IOException { public void generateSimpleProject() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest( MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", "foo.zip"); "application/xml", "foo.zip");
...@@ -59,7 +57,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -59,7 +57,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void generateProjectCustomTargetFilename() throws IOException { public void generateProjectCustomTargetFilename() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
request.setOutput("bar.zip"); request.setOutput("bar.zip");
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest( MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
...@@ -69,7 +67,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -69,7 +67,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void generateProjectNoDefaultFileName() throws IOException { public void generateProjectNoDefaultFileName() throws Exception {
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest( MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null); "application/xml", null);
...@@ -78,7 +76,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -78,7 +76,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void generateProjectBadRequest() throws IOException { public void generateProjectBadRequest() throws Exception {
String jsonMessage = "Unknown dependency foo:bar"; String jsonMessage = "Unknown dependency foo:bar";
mockProjectGenerationError(400, jsonMessage); mockProjectGenerationError(400, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
...@@ -89,7 +87,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -89,7 +87,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void generateProjectBadRequestNoExtraMessage() throws IOException { public void generateProjectBadRequestNoExtraMessage() throws Exception {
mockProjectGenerationError(400, null); mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
this.thrown.expect(ReportableException.class); this.thrown.expect(ReportableException.class);
...@@ -98,7 +96,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -98,7 +96,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void generateProjectNoContent() throws IOException { public void generateProjectNoContent() throws Exception {
mockSuccessfulMetadataGet(false); mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class); CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500); mockStatus(response, 500);
...@@ -110,7 +108,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -110,7 +108,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void loadMetadataBadRequest() throws IOException { public void loadMetadataBadRequest() throws Exception {
String jsonMessage = "whatever error on the server"; String jsonMessage = "whatever error on the server";
mockMetadataGetError(500, jsonMessage); mockMetadataGetError(500, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest(); ProjectGenerationRequest request = new ProjectGenerationRequest();
...@@ -120,7 +118,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -120,7 +118,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void loadMetadataInvalidJson() throws IOException { public void loadMetadataInvalidJson() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class); CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json"); mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json");
mockStatus(response, 200); mockStatus(response, 200);
...@@ -132,7 +130,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -132,7 +130,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
@Test @Test
public void loadMetadataNoContent() throws IOException { public void loadMetadataNoContent() throws Exception {
CloseableHttpResponse response = mock(CloseableHttpResponse.class); CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500); mockStatus(response, 500);
given(this.http.execute(isA(HttpGet.class))).willReturn(response); given(this.http.execute(isA(HttpGet.class))).willReturn(response);
...@@ -143,7 +141,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests { ...@@ -143,7 +141,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
} }
private ProjectGenerationResponse generateProject(ProjectGenerationRequest request, private ProjectGenerationResponse generateProject(ProjectGenerationRequest request,
MockHttpProjectGenerationRequest mockRequest) throws IOException { MockHttpProjectGenerationRequest mockRequest) throws Exception {
mockSuccessfulProjectGeneration(mockRequest); mockSuccessfulProjectGeneration(mockRequest);
ProjectGenerationResponse entity = this.invoker.generate(request); ProjectGenerationResponse entity = this.invoker.generate(request);
assertThat(entity.getContent()).as("wrong body content") assertThat(entity.getContent()).as("wrong body content")
......
...@@ -23,6 +23,7 @@ import java.nio.charset.Charset; ...@@ -23,6 +23,7 @@ import java.nio.charset.Charset;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -169,7 +170,7 @@ public class ProjectGenerationRequestTests { ...@@ -169,7 +170,7 @@ public class ProjectGenerationRequestTests {
} }
@Test @Test
public void buildNoMatch() { public void buildNoMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata(); InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("does-not-exist", null); setBuildAndFormat("does-not-exist", null);
this.thrown.expect(ReportableException.class); this.thrown.expect(ReportableException.class);
...@@ -178,7 +179,7 @@ public class ProjectGenerationRequestTests { ...@@ -178,7 +179,7 @@ public class ProjectGenerationRequestTests {
} }
@Test @Test
public void buildMultipleMatch() { public void buildMultipleMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata("types-conflict"); InitializrServiceMetadata metadata = readMetadata("types-conflict");
setBuildAndFormat("gradle", null); setBuildAndFormat("gradle", null);
this.thrown.expect(ReportableException.class); this.thrown.expect(ReportableException.class);
...@@ -188,7 +189,7 @@ public class ProjectGenerationRequestTests { ...@@ -188,7 +189,7 @@ public class ProjectGenerationRequestTests {
} }
@Test @Test
public void buildOneMatch() { public void buildOneMatch() throws Exception {
InitializrServiceMetadata metadata = readMetadata(); InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", null); setBuildAndFormat("gradle", null);
assertThat(this.request.generateUrl(metadata)) assertThat(this.request.generateUrl(metadata))
...@@ -196,7 +197,7 @@ public class ProjectGenerationRequestTests { ...@@ -196,7 +197,7 @@ public class ProjectGenerationRequestTests {
} }
@Test @Test
public void typeAndBuildAndFormat() { public void typeAndBuildAndFormat() throws Exception {
InitializrServiceMetadata metadata = readMetadata(); InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", "project"); setBuildAndFormat("gradle", "project");
this.request.setType("maven-build"); this.request.setType("maven-build");
...@@ -205,14 +206,14 @@ public class ProjectGenerationRequestTests { ...@@ -205,14 +206,14 @@ public class ProjectGenerationRequestTests {
} }
@Test @Test
public void invalidType() throws URISyntaxException { public void invalidType() throws Exception {
this.request.setType("does-not-exist"); this.request.setType("does-not-exist");
this.thrown.expect(ReportableException.class); this.thrown.expect(ReportableException.class);
this.request.generateUrl(createDefaultMetadata()); this.request.generateUrl(createDefaultMetadata());
} }
@Test @Test
public void noTypeAndNoDefault() throws URISyntaxException { public void noTypeAndNoDefault() throws Exception {
this.thrown.expect(ReportableException.class); this.thrown.expect(ReportableException.class);
this.thrown.expectMessage("no default is defined"); this.thrown.expectMessage("no default is defined");
this.request.generateUrl(readMetadata("types-conflict")); this.request.generateUrl(readMetadata("types-conflict"));
...@@ -243,11 +244,12 @@ public class ProjectGenerationRequestTests { ...@@ -243,11 +244,12 @@ public class ProjectGenerationRequestTests {
return new InitializrServiceMetadata(projectType); return new InitializrServiceMetadata(projectType);
} }
private static InitializrServiceMetadata readMetadata() { private static InitializrServiceMetadata readMetadata() throws JSONException {
return readMetadata("2.0.0"); return readMetadata("2.0.0");
} }
private static InitializrServiceMetadata readMetadata(String version) { private static InitializrServiceMetadata readMetadata(String version)
throws JSONException {
try { try {
Resource resource = new ClassPathResource( Resource resource = new ClassPathResource(
"metadata/service-metadata-" + version + ".json"); "metadata/service-metadata-" + version + ".json");
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -57,6 +58,11 @@ ...@@ -57,6 +58,11 @@
<artifactId>okhttp</artifactId> <artifactId>okhttp</artifactId>
<version>3.4.1</version> <version>3.4.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
</dependency>
<dependency> <dependency>
<groupId>io.netty</groupId> <groupId>io.netty</groupId>
<artifactId>netty-all</artifactId> <artifactId>netty-all</artifactId>
......
...@@ -20,8 +20,8 @@ ...@@ -20,8 +20,8 @@
<dependencies> <dependencies>
<!-- Compile (should stick to the bare minimum) --> <!-- Compile (should stick to the bare minimum) -->
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>com.vaadin.external.google</groupId>
<artifactId>json</artifactId> <artifactId>android-json</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
...@@ -23,8 +23,6 @@ import java.util.ArrayList; ...@@ -23,8 +23,6 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.json.JSONException;
/** /**
* Load a {@link ConfigurationMetadataRepository} from the content of arbitrary * Load a {@link ConfigurationMetadataRepository} from the content of arbitrary
* resource(s). * resource(s).
...@@ -104,13 +102,8 @@ public final class ConfigurationMetadataRepositoryJsonBuilder { ...@@ -104,13 +102,8 @@ public final class ConfigurationMetadataRepositoryJsonBuilder {
RawConfigurationMetadata metadata = this.reader.read(in, charset); RawConfigurationMetadata metadata = this.reader.read(in, charset);
return create(metadata); return create(metadata);
} }
catch (IOException ex) { catch (Exception ex) {
throw new IllegalArgumentException( throw new IllegalStateException("Failed to read configuration metadata", ex);
"Failed to read configuration " + "metadata", ex);
}
catch (JSONException ex) {
throw new IllegalArgumentException(
"Invalid configuration " + "metadata document", ex);
} }
} }
......
...@@ -41,14 +41,26 @@ class JsonReader { ...@@ -41,14 +41,26 @@ class JsonReader {
public RawConfigurationMetadata read(InputStream in, Charset charset) public RawConfigurationMetadata read(InputStream in, Charset charset)
throws IOException { throws IOException {
try {
JSONObject json = readJson(in, charset); JSONObject json = readJson(in, charset);
List<ConfigurationMetadataSource> groups = parseAllSources(json); List<ConfigurationMetadataSource> groups = parseAllSources(json);
List<ConfigurationMetadataItem> items = parseAllItems(json); List<ConfigurationMetadataItem> items = parseAllItems(json);
List<ConfigurationMetadataHint> hints = parseAllHints(json); List<ConfigurationMetadataHint> hints = parseAllHints(json);
return new RawConfigurationMetadata(groups, items, hints); return new RawConfigurationMetadata(groups, items, hints);
} }
catch (Exception ex) {
if (ex instanceof IOException) {
throw (IOException) ex;
}
if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
}
throw new IllegalStateException(ex);
}
}
private List<ConfigurationMetadataSource> parseAllSources(JSONObject root) { private List<ConfigurationMetadataSource> parseAllSources(JSONObject root)
throws Exception {
List<ConfigurationMetadataSource> result = new ArrayList<ConfigurationMetadataSource>(); List<ConfigurationMetadataSource> result = new ArrayList<ConfigurationMetadataSource>();
if (!root.has("groups")) { if (!root.has("groups")) {
return result; return result;
...@@ -61,7 +73,8 @@ class JsonReader { ...@@ -61,7 +73,8 @@ class JsonReader {
return result; return result;
} }
private List<ConfigurationMetadataItem> parseAllItems(JSONObject root) { private List<ConfigurationMetadataItem> parseAllItems(JSONObject root)
throws Exception {
List<ConfigurationMetadataItem> result = new ArrayList<ConfigurationMetadataItem>(); List<ConfigurationMetadataItem> result = new ArrayList<ConfigurationMetadataItem>();
if (!root.has("properties")) { if (!root.has("properties")) {
return result; return result;
...@@ -74,7 +87,8 @@ class JsonReader { ...@@ -74,7 +87,8 @@ class JsonReader {
return result; return result;
} }
private List<ConfigurationMetadataHint> parseAllHints(JSONObject root) { private List<ConfigurationMetadataHint> parseAllHints(JSONObject root)
throws Exception {
List<ConfigurationMetadataHint> result = new ArrayList<ConfigurationMetadataHint>(); List<ConfigurationMetadataHint> result = new ArrayList<ConfigurationMetadataHint>();
if (!root.has("hints")) { if (!root.has("hints")) {
return result; return result;
...@@ -87,7 +101,7 @@ class JsonReader { ...@@ -87,7 +101,7 @@ class JsonReader {
return result; return result;
} }
private ConfigurationMetadataSource parseSource(JSONObject json) { private ConfigurationMetadataSource parseSource(JSONObject json) throws Exception {
ConfigurationMetadataSource source = new ConfigurationMetadataSource(); ConfigurationMetadataSource source = new ConfigurationMetadataSource();
source.setGroupId(json.getString("name")); source.setGroupId(json.getString("name"));
source.setType(json.optString("type", null)); source.setType(json.optString("type", null));
...@@ -100,7 +114,7 @@ class JsonReader { ...@@ -100,7 +114,7 @@ class JsonReader {
return source; return source;
} }
private ConfigurationMetadataItem parseItem(JSONObject json) { private ConfigurationMetadataItem parseItem(JSONObject json) throws Exception {
ConfigurationMetadataItem item = new ConfigurationMetadataItem(); ConfigurationMetadataItem item = new ConfigurationMetadataItem();
item.setId(json.getString("name")); item.setId(json.getString("name"));
item.setType(json.optString("type", null)); item.setType(json.optString("type", null));
...@@ -115,7 +129,7 @@ class JsonReader { ...@@ -115,7 +129,7 @@ class JsonReader {
return item; return item;
} }
private ConfigurationMetadataHint parseHint(JSONObject json) { private ConfigurationMetadataHint parseHint(JSONObject json) throws Exception {
ConfigurationMetadataHint hint = new ConfigurationMetadataHint(); ConfigurationMetadataHint hint = new ConfigurationMetadataHint();
hint.setId(json.getString("name")); hint.setId(json.getString("name"));
if (json.has("values")) { if (json.has("values")) {
...@@ -152,7 +166,7 @@ class JsonReader { ...@@ -152,7 +166,7 @@ class JsonReader {
return hint; return hint;
} }
private Deprecation parseDeprecation(JSONObject object) { private Deprecation parseDeprecation(JSONObject object) throws Exception {
if (object.has("deprecation")) { if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation"); JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
Deprecation deprecation = new Deprecation(); Deprecation deprecation = new Deprecation();
...@@ -164,7 +178,7 @@ class JsonReader { ...@@ -164,7 +178,7 @@ class JsonReader {
return (object.optBoolean("deprecated") ? new Deprecation() : null); return (object.optBoolean("deprecated") ? new Deprecation() : null);
} }
private Object readItemValue(Object value) { private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray) { if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value; JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()]; Object[] content = new Object[array.length()];
...@@ -176,7 +190,7 @@ class JsonReader { ...@@ -176,7 +190,7 @@ class JsonReader {
return value; return value;
} }
private JSONObject readJson(InputStream in, Charset charset) throws IOException { private JSONObject readJson(InputStream in, Charset charset) throws Exception {
try { try {
StringBuilder out = new StringBuilder(); StringBuilder out = new StringBuilder();
InputStreamReader reader = new InputStreamReader(in, charset); InputStreamReader reader = new InputStreamReader(in, charset);
......
...@@ -20,6 +20,7 @@ import java.io.IOException; ...@@ -20,6 +20,7 @@ import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
import org.hamcrest.CoreMatchers;
import org.json.JSONException; import org.json.JSONException;
import org.junit.Test; import org.junit.Test;
...@@ -45,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { ...@@ -45,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
@Test @Test
public void invalidMetadata() throws IOException { public void invalidMetadata() throws IOException {
this.thrown.expect(JSONException.class); this.thrown.expectCause(CoreMatchers.<Throwable>instanceOf(JSONException.class));
readFor("invalid"); readFor("invalid");
} }
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -18,10 +19,11 @@ ...@@ -18,10 +19,11 @@
<main.basedir>${basedir}/../..</main.basedir> <main.basedir>${basedir}/../..</main.basedir>
</properties> </properties>
<dependencies> <dependencies>
<!-- Compile (should stick to the bare minimum) --> <!-- Shaded -->
<dependency> <dependency>
<groupId>org.json</groupId> <groupId>com.vaadin.external.google</groupId>
<artifactId>json</artifactId> <artifactId>android-json</artifactId>
<optional>true</optional>
</dependency> </dependency>
<!-- Test --> <!-- Test -->
<dependency> <dependency>
...@@ -29,10 +31,6 @@ ...@@ -29,10 +31,6 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
...@@ -44,6 +42,34 @@ ...@@ -44,6 +42,34 @@
<proc>none</proc> <proc>none</proc>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.vaadin.external.google:android-json</artifact>
</filter>
</filters>
<relocations>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>org.springframework.boot.configurationprocessor.json</shadedPattern>
</relocation>
</relocations>
<createDependencyReducedPom>false</createDependencyReducedPom>
<createSourcesJar>true</createSourcesJar>
<shadeSourcesContent>true</shadeSourcesContent>
</configuration>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>
</project> </project>
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.springframework.boot.configurationprocessor; package org.springframework.boot.configurationprocessor;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
...@@ -134,8 +133,13 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor ...@@ -134,8 +133,13 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
} }
} }
if (roundEnv.processingOver()) { if (roundEnv.processingOver()) {
try {
writeMetaData(); writeMetaData();
} }
catch (Exception ex) {
throw new IllegalStateException("Failed to write metadata", ex);
}
}
return false; return false;
} }
...@@ -390,16 +394,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor ...@@ -390,16 +394,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
return values; return values;
} }
protected ConfigurationMetadata writeMetaData() { protected ConfigurationMetadata writeMetaData() throws Exception {
ConfigurationMetadata metadata = this.metadataCollector.getMetadata(); ConfigurationMetadata metadata = this.metadataCollector.getMetadata();
metadata = mergeAdditionalMetadata(metadata); metadata = mergeAdditionalMetadata(metadata);
if (!metadata.getItems().isEmpty()) { if (!metadata.getItems().isEmpty()) {
try {
this.metadataStore.writeMetadata(metadata); this.metadataStore.writeMetadata(metadata);
}
catch (IOException ex) {
throw new IllegalStateException("Failed to write metadata", ex);
}
return metadata; return metadata;
} }
return null; return null;
......
...@@ -27,8 +27,6 @@ import javax.tools.Diagnostic; ...@@ -27,8 +27,6 @@ import javax.tools.Diagnostic;
import javax.tools.FileObject; import javax.tools.FileObject;
import javax.tools.StandardLocation; import javax.tools.StandardLocation;
import org.json.JSONException;
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException; import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller; import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
...@@ -87,7 +85,7 @@ public class MetadataStore { ...@@ -87,7 +85,7 @@ public class MetadataStore {
catch (IOException ex) { catch (IOException ex) {
return null; return null;
} }
catch (JSONException ex) { catch (Exception ex) {
throw new InvalidConfigurationMetadataException( throw new InvalidConfigurationMetadataException(
"Invalid additional meta-data in '" + METADATA_PATH + "': " "Invalid additional meta-data in '" + METADATA_PATH + "': "
+ ex.getMessage(), + ex.getMessage(),
......
...@@ -20,15 +20,12 @@ import java.util.ArrayList; ...@@ -20,15 +20,12 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.ListIterator; import java.util.ListIterator;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
/** /**
* Configuration meta-data. * Configuration meta-data.
* *
...@@ -46,18 +43,18 @@ public class ConfigurationMetadata { ...@@ -46,18 +43,18 @@ public class ConfigurationMetadata {
SEPARATORS = Collections.unmodifiableSet(new HashSet<Character>(chars)); SEPARATORS = Collections.unmodifiableSet(new HashSet<Character>(chars));
} }
private final MultiValueMap<String, ItemMetadata> items; private final Map<String, List<ItemMetadata>> items;
private final MultiValueMap<String, ItemHint> hints; private final Map<String, List<ItemHint>> hints;
public ConfigurationMetadata() { public ConfigurationMetadata() {
this.items = new LinkedMultiValueMap<String, ItemMetadata>(); this.items = new LinkedHashMap<String, List<ItemMetadata>>();
this.hints = new LinkedMultiValueMap<String, ItemHint>(); this.hints = new LinkedHashMap<String, List<ItemHint>>();
} }
public ConfigurationMetadata(ConfigurationMetadata metadata) { public ConfigurationMetadata(ConfigurationMetadata metadata) {
this.items = new LinkedMultiValueMap<String, ItemMetadata>(metadata.items); this.items = new LinkedHashMap<String, List<ItemMetadata>>(metadata.items);
this.hints = new LinkedMultiValueMap<String, ItemHint>(metadata.hints); this.hints = new LinkedHashMap<String, List<ItemHint>>(metadata.hints);
} }
/** /**
...@@ -65,7 +62,7 @@ public class ConfigurationMetadata { ...@@ -65,7 +62,7 @@ public class ConfigurationMetadata {
* @param itemMetadata the meta-data to add * @param itemMetadata the meta-data to add
*/ */
public void add(ItemMetadata itemMetadata) { public void add(ItemMetadata itemMetadata) {
this.items.add(itemMetadata.getName(), itemMetadata); add(this.items, itemMetadata.getName(), itemMetadata);
} }
/** /**
...@@ -73,7 +70,7 @@ public class ConfigurationMetadata { ...@@ -73,7 +70,7 @@ public class ConfigurationMetadata {
* @param itemHint the item hint to add * @param itemHint the item hint to add
*/ */
public void add(ItemHint itemHint) { public void add(ItemHint itemHint) {
this.hints.add(itemHint.getName(), itemHint); add(this.hints, itemHint.getName(), itemHint);
} }
/** /**
...@@ -131,13 +128,22 @@ public class ConfigurationMetadata { ...@@ -131,13 +128,22 @@ public class ConfigurationMetadata {
} }
} }
else { else {
this.items.add(metadata.getName(), metadata); add(this.items, metadata.getName(), metadata);
}
}
private <K, V> void add(Map<K, List<V>> map, K key, V value) {
List<V> values = map.get(key);
if (values == null) {
values = new ArrayList<V>();
map.put(key, values);
} }
values.add(value);
} }
private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) { private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) {
List<ItemMetadata> candidates = this.items.get(metadata.getName()); List<ItemMetadata> candidates = this.items.get(metadata.getName());
if (CollectionUtils.isEmpty(candidates)) { if (candidates == null || candidates.isEmpty()) {
return null; return null;
} }
ListIterator<ItemMetadata> it = candidates.listIterator(); ListIterator<ItemMetadata> it = candidates.listIterator();
...@@ -150,14 +156,20 @@ public class ConfigurationMetadata { ...@@ -150,14 +156,20 @@ public class ConfigurationMetadata {
return candidates.get(0); return candidates.get(0);
} }
for (ItemMetadata candidate : candidates) { for (ItemMetadata candidate : candidates) {
if (ObjectUtils.nullSafeEquals(candidate.getSourceType(), if (nullSafeEquals(candidate.getSourceType(), metadata.getSourceType())) {
metadata.getSourceType())) {
return candidate; return candidate;
} }
} }
return null; return null;
} }
private boolean nullSafeEquals(Object o1, Object o2) {
if (o1 == o2) {
return true;
}
return o1 != null && o2 != null && o1.equals(o2);
}
public static String nestedPrefix(String prefix, String name) { public static String nestedPrefix(String prefix, String name) {
String nestedPrefix = (prefix == null ? "" : prefix); String nestedPrefix = (prefix == null ? "" : prefix);
String dashedName = toDashedCase(name); String dashedName = toDashedCase(name);
...@@ -185,8 +197,7 @@ public class ConfigurationMetadata { ...@@ -185,8 +197,7 @@ public class ConfigurationMetadata {
return dashed.toString().toLowerCase(); return dashed.toString().toLowerCase();
} }
private static <T extends Comparable<T>> List<T> flattenValues( private static <T extends Comparable<T>> List<T> flattenValues(Map<?, List<T>> map) {
MultiValueMap<?, T> map) {
List<T> content = new ArrayList<T>(); List<T> content = new ArrayList<T>();
for (List<T> values : map.values()) { for (List<T> values : map.values()) {
content.addAll(values); content.addAll(values);
......
/*
* Copyright 2012-2016 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 org.springframework.boot.configurationprocessor.metadata;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Extension to {@link JSONObject} that remembers the order of inserts.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
@SuppressWarnings("rawtypes")
class JSONOrderedObject extends JSONObject {
private Set<String> keys = new LinkedHashSet<String>();
@Override
public JSONObject put(String key, Object value) throws JSONException {
this.keys.add(key);
return super.put(key, value);
}
@Override
public Iterator keys() {
return this.keys.iterator();
}
}
/*
* Copyright 2012-2016 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 org.springframework.boot.configurationprocessor.metadata;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
/**
* Converter to change meta-data objects into JSON objects.
*
* @author Stephane Nicoll
* @author Phillip Webb
*/
class JsonConverter {
public JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType)
throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(itemType)) {
jsonArray.put(toJsonObject(item));
}
}
return jsonArray;
}
public JSONArray toJsonArray(Collection<ItemHint> hints) throws Exception {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}
public JSONObject toJsonObject(ItemMetadata item) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", item.getName());
putIfPresent(jsonObject, "type", item.getType());
putIfPresent(jsonObject, "description", item.getDescription());
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
Object defaultValue = item.getDefaultValue();
if (defaultValue != null) {
putDefaultValue(jsonObject, defaultValue);
}
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}
private JSONObject toJsonObject(ItemHint hint) throws Exception {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
jsonObject.put("values", getItemHintValues(hint));
}
if (!hint.getProviders().isEmpty()) {
jsonObject.put("providers", getItemHintProviders(hint));
}
return jsonObject;
}
private JSONArray getItemHintValues(ItemHint hint) throws Exception {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}
private JSONObject getItemHintValue(ItemHint.ValueHint value) throws Exception {
JSONObject result = new JSONOrderedObject();
putHintValue(result, value.getValue());
putIfPresent(result, "description", value.getDescription());
return result;
}
private JSONArray getItemHintProviders(ItemHint hint) throws Exception {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
}
return providers;
}
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider)
throws Exception {
JSONObject result = new JSONOrderedObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
JSONObject parameters = new JSONOrderedObject();
for (Map.Entry<String, Object> entry : provider.getParameters().entrySet()) {
parameters.put(entry.getKey(), extractItemValue(entry.getValue()));
}
result.put("parameters", parameters);
}
return result;
}
private void putIfPresent(JSONObject jsonObject, String name, Object value)
throws Exception {
if (value != null) {
jsonObject.put(name, value);
}
}
private void putHintValue(JSONObject jsonObject, Object value) throws Exception {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}
private void putDefaultValue(JSONObject jsonObject, Object value) throws Exception {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
}
private Object extractItemValue(Object value) {
Object defaultValue = value;
if (value.getClass().isArray()) {
JSONArray array = new JSONArray();
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
array.put(Array.get(value, i));
}
defaultValue = array;
}
return defaultValue;
}
}
...@@ -20,18 +20,14 @@ import java.io.IOException; ...@@ -20,18 +20,14 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.reflect.Array;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedHashSet; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType;
...@@ -51,136 +47,26 @@ public class JsonMarshaller { ...@@ -51,136 +47,26 @@ public class JsonMarshaller {
public void write(ConfigurationMetadata metadata, OutputStream outputStream) public void write(ConfigurationMetadata metadata, OutputStream outputStream)
throws IOException { throws IOException {
try {
JSONObject object = new JSONOrderedObject(); JSONObject object = new JSONOrderedObject();
object.put("groups", toJsonArray(metadata, ItemType.GROUP)); JsonConverter converter = new JsonConverter();
object.put("properties", toJsonArray(metadata, ItemType.PROPERTY)); object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP));
object.put("hints", toJsonArray(metadata.getHints())); object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY));
object.put("hints", converter.toJsonArray(metadata.getHints()));
outputStream.write(object.toString(2).getBytes(UTF_8)); outputStream.write(object.toString(2).getBytes(UTF_8));
} }
catch (Exception ex) {
private JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) { if (ex instanceof IOException) {
JSONArray jsonArray = new JSONArray(); throw (IOException) ex;
for (ItemMetadata item : metadata.getItems()) {
if (item.isOfItemType(itemType)) {
jsonArray.put(toJsonObject(item));
}
}
return jsonArray;
}
private JSONArray toJsonArray(Collection<ItemHint> hints) {
JSONArray jsonArray = new JSONArray();
for (ItemHint hint : hints) {
jsonArray.put(toJsonObject(hint));
}
return jsonArray;
}
private JSONObject toJsonObject(ItemMetadata item) {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", item.getName());
putIfPresent(jsonObject, "type", item.getType());
putIfPresent(jsonObject, "description", item.getDescription());
putIfPresent(jsonObject, "sourceType", item.getSourceType());
putIfPresent(jsonObject, "sourceMethod", item.getSourceMethod());
Object defaultValue = item.getDefaultValue();
if (defaultValue != null) {
putDefaultValue(jsonObject, defaultValue);
}
ItemDeprecation deprecation = item.getDeprecation();
if (deprecation != null) {
jsonObject.put("deprecated", true); // backward compatibility
JSONObject deprecationJsonObject = new JSONObject();
if (deprecation.getReason() != null) {
deprecationJsonObject.put("reason", deprecation.getReason());
}
if (deprecation.getReplacement() != null) {
deprecationJsonObject.put("replacement", deprecation.getReplacement());
}
jsonObject.put("deprecation", deprecationJsonObject);
}
return jsonObject;
}
private JSONObject toJsonObject(ItemHint hint) {
JSONObject jsonObject = new JSONOrderedObject();
jsonObject.put("name", hint.getName());
if (!hint.getValues().isEmpty()) {
jsonObject.put("values", getItemHintValues(hint));
}
if (!hint.getProviders().isEmpty()) {
jsonObject.put("providers", getItemHintProviders(hint));
}
return jsonObject;
}
private JSONArray getItemHintValues(ItemHint hint) {
JSONArray values = new JSONArray();
for (ItemHint.ValueHint value : hint.getValues()) {
values.put(getItemHintValue(value));
}
return values;
}
private JSONObject getItemHintValue(ItemHint.ValueHint value) {
JSONObject result = new JSONOrderedObject();
putHintValue(result, value.getValue());
putIfPresent(result, "description", value.getDescription());
return result;
}
private JSONArray getItemHintProviders(ItemHint hint) {
JSONArray providers = new JSONArray();
for (ItemHint.ValueProvider provider : hint.getProviders()) {
providers.put(getItemHintProvider(provider));
} }
return providers; if (ex instanceof RuntimeException) {
} throw (RuntimeException) ex;
private JSONObject getItemHintProvider(ItemHint.ValueProvider provider) {
JSONObject result = new JSONOrderedObject();
result.put("name", provider.getName());
if (provider.getParameters() != null && !provider.getParameters().isEmpty()) {
JSONObject parameters = new JSONOrderedObject();
for (Map.Entry<String, Object> entry : provider.getParameters().entrySet()) {
parameters.put(entry.getKey(), extractItemValue(entry.getValue()));
}
result.put("parameters", parameters);
}
return result;
}
private void putIfPresent(JSONObject jsonObject, String name, Object value) {
if (value != null) {
jsonObject.put(name, value);
} }
throw new IllegalStateException(ex);
} }
private void putHintValue(JSONObject jsonObject, Object value) {
Object hintValue = extractItemValue(value);
jsonObject.put("value", hintValue);
}
private void putDefaultValue(JSONObject jsonObject, Object value) {
Object defaultValue = extractItemValue(value);
jsonObject.put("defaultValue", defaultValue);
} }
private Object extractItemValue(Object value) { public ConfigurationMetadata read(InputStream inputStream) throws Exception {
Object defaultValue = value;
if (value.getClass().isArray()) {
JSONArray array = new JSONArray();
int length = Array.getLength(value);
for (int i = 0; i < length; i++) {
array.put(Array.get(value, i));
}
defaultValue = array;
}
return defaultValue;
}
public ConfigurationMetadata read(InputStream inputStream) throws IOException {
ConfigurationMetadata metadata = new ConfigurationMetadata(); ConfigurationMetadata metadata = new ConfigurationMetadata();
JSONObject object = new JSONObject(toString(inputStream)); JSONObject object = new JSONObject(toString(inputStream));
JSONArray groups = object.optJSONArray("groups"); JSONArray groups = object.optJSONArray("groups");
...@@ -205,7 +91,8 @@ public class JsonMarshaller { ...@@ -205,7 +91,8 @@ public class JsonMarshaller {
return metadata; return metadata;
} }
private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType) { private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType)
throws Exception {
String name = object.getString("name"); String name = object.getString("name");
String type = object.optString("type", null); String type = object.optString("type", null);
String description = object.optString("description", null); String description = object.optString("description", null);
...@@ -217,7 +104,7 @@ public class JsonMarshaller { ...@@ -217,7 +104,7 @@ public class JsonMarshaller {
description, defaultValue, deprecation); description, defaultValue, deprecation);
} }
private ItemDeprecation toItemDeprecation(JSONObject object) { private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception {
if (object.has("deprecation")) { if (object.has("deprecation")) {
JSONObject deprecationJsonObject = object.getJSONObject("deprecation"); JSONObject deprecationJsonObject = object.getJSONObject("deprecation");
ItemDeprecation deprecation = new ItemDeprecation(); ItemDeprecation deprecation = new ItemDeprecation();
...@@ -229,7 +116,7 @@ public class JsonMarshaller { ...@@ -229,7 +116,7 @@ public class JsonMarshaller {
return (object.optBoolean("deprecated") ? new ItemDeprecation() : null); return (object.optBoolean("deprecated") ? new ItemDeprecation() : null);
} }
private ItemHint toItemHint(JSONObject object) { private ItemHint toItemHint(JSONObject object) throws Exception {
String name = object.getString("name"); String name = object.getString("name");
List<ItemHint.ValueHint> values = new ArrayList<ItemHint.ValueHint>(); List<ItemHint.ValueHint> values = new ArrayList<ItemHint.ValueHint>();
if (object.has("values")) { if (object.has("values")) {
...@@ -248,19 +135,19 @@ public class JsonMarshaller { ...@@ -248,19 +135,19 @@ public class JsonMarshaller {
return new ItemHint(name, values, providers); return new ItemHint(name, values, providers);
} }
private ItemHint.ValueHint toValueHint(JSONObject object) { private ItemHint.ValueHint toValueHint(JSONObject object) throws Exception {
Object value = readItemValue(object.get("value")); Object value = readItemValue(object.get("value"));
String description = object.optString("description", null); String description = object.optString("description", null);
return new ItemHint.ValueHint(value, description); return new ItemHint.ValueHint(value, description);
} }
private ItemHint.ValueProvider toValueProvider(JSONObject object) { private ItemHint.ValueProvider toValueProvider(JSONObject object) throws Exception {
String name = object.getString("name"); String name = object.getString("name");
Map<String, Object> parameters = new HashMap<String, Object>(); Map<String, Object> parameters = new HashMap<String, Object>();
if (object.has("parameters")) { if (object.has("parameters")) {
JSONObject parametersObject = object.getJSONObject("parameters"); JSONObject parametersObject = object.getJSONObject("parameters");
for (Object k : parametersObject.keySet()) { for (Iterator<?> iterator = parametersObject.keys(); iterator.hasNext();) {
String key = (String) k; String key = (String) iterator.next();
Object value = readItemValue(parametersObject.get(key)); Object value = readItemValue(parametersObject.get(key));
parameters.put(key, value); parameters.put(key, value);
} }
...@@ -268,7 +155,7 @@ public class JsonMarshaller { ...@@ -268,7 +155,7 @@ public class JsonMarshaller {
return new ItemHint.ValueProvider(name, parameters); return new ItemHint.ValueProvider(name, parameters);
} }
private Object readItemValue(Object value) { private Object readItemValue(Object value) throws Exception {
if (value instanceof JSONArray) { if (value instanceof JSONArray) {
JSONArray array = (JSONArray) value; JSONArray array = (JSONArray) value;
Object[] content = new Object[array.length()]; Object[] content = new Object[array.length()];
...@@ -291,25 +178,4 @@ public class JsonMarshaller { ...@@ -291,25 +178,4 @@ public class JsonMarshaller {
return out.toString(); return out.toString();
} }
/**
* Extension to {@link JSONObject} that remembers the order of inserts.
*/
@SuppressWarnings("rawtypes")
private static class JSONOrderedObject extends JSONObject {
private Set<String> keys = new LinkedHashSet<String>();
@Override
public JSONObject put(String key, Object value) throws JSONException {
this.keys.add(key);
return super.put(key, value);
}
@Override
public Set keySet() {
return this.keys;
}
}
} }
...@@ -34,6 +34,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet ...@@ -34,6 +34,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet
import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemHint; import org.springframework.boot.configurationprocessor.metadata.ItemHint;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
import org.springframework.boot.configurationprocessor.metadata.TestJsonConverter;
import org.springframework.boot.configurationsample.incremental.BarProperties; import org.springframework.boot.configurationsample.incremental.BarProperties;
import org.springframework.boot.configurationsample.incremental.FooProperties; import org.springframework.boot.configurationsample.incremental.FooProperties;
import org.springframework.boot.configurationsample.incremental.RenamedBarProperties; import org.springframework.boot.configurationsample.incremental.RenamedBarProperties;
...@@ -630,8 +631,9 @@ public class ConfigurationMetadataAnnotationProcessorTests { ...@@ -630,8 +631,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
JSONObject additionalMetadata = new JSONObject(); JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("properties", properties); additionalMetadata.put("properties", properties);
FileWriter writer = new FileWriter(additionalMetadataFile); FileWriter writer = new FileWriter(additionalMetadataFile);
additionalMetadata.write(writer); writer.append(additionalMetadata.toString(2));
writer.flush(); writer.flush();
writer.close();
ConfigurationMetadata metadata = compile(SimpleProperties.class); ConfigurationMetadata metadata = compile(SimpleProperties.class);
assertThat(metadata).has(Metadata.withProperty("simple.comparator")); assertThat(metadata).has(Metadata.withProperty("simple.comparator"));
assertThat(metadata).has(Metadata.withProperty("foo", String.class) assertThat(metadata).has(Metadata.withProperty("foo", String.class)
...@@ -724,23 +726,28 @@ public class ConfigurationMetadataAnnotationProcessorTests { ...@@ -724,23 +726,28 @@ public class ConfigurationMetadataAnnotationProcessorTests {
return processor.getMetadata(); return processor.getMetadata();
} }
private void writeAdditionalMetadata(ItemMetadata... metadata) throws IOException { private void writeAdditionalMetadata(ItemMetadata... metadata) throws Exception {
TestJsonConverter converter = new TestJsonConverter();
File additionalMetadataFile = createAdditionalMetadataFile(); File additionalMetadataFile = createAdditionalMetadataFile();
JSONObject additionalMetadata = new JSONObject(); JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("properties", metadata); JSONArray properties = new JSONArray();
for (ItemMetadata itemMetadata : metadata) {
properties.put(converter.toJsonObject(itemMetadata));
}
additionalMetadata.put("properties", properties);
writeMetadata(additionalMetadataFile, additionalMetadata); writeMetadata(additionalMetadataFile, additionalMetadata);
} }
private void writeAdditionalHints(ItemHint... hints) throws IOException { private void writeAdditionalHints(ItemHint... hints) throws Exception {
TestJsonConverter converter = new TestJsonConverter();
File additionalMetadataFile = createAdditionalMetadataFile(); File additionalMetadataFile = createAdditionalMetadataFile();
JSONObject additionalMetadata = new JSONObject(); JSONObject additionalMetadata = new JSONObject();
additionalMetadata.put("hints", hints); additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints)));
writeMetadata(additionalMetadataFile, additionalMetadata); writeMetadata(additionalMetadataFile, additionalMetadata);
} }
private void writePropertyDeprecation(ItemMetadata... items) throws IOException { private void writePropertyDeprecation(ItemMetadata... items) throws Exception {
File additionalMetadataFile = createAdditionalMetadataFile(); File additionalMetadataFile = createAdditionalMetadataFile();
JSONArray propertiesArray = new JSONArray(); JSONArray propertiesArray = new JSONArray();
for (ItemMetadata item : items) { for (ItemMetadata item : items) {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
...@@ -776,11 +783,10 @@ public class ConfigurationMetadataAnnotationProcessorTests { ...@@ -776,11 +783,10 @@ public class ConfigurationMetadataAnnotationProcessorTests {
return additionalMetadataFile; return additionalMetadataFile;
} }
private void writeMetadata(File metadataFile, JSONObject metadata) private void writeMetadata(File metadataFile, JSONObject metadata) throws Exception {
throws IOException {
FileWriter writer = new FileWriter(metadataFile); FileWriter writer = new FileWriter(metadataFile);
try { try {
metadata.write(writer); writer.append(metadata.toString(2));
} }
finally { finally {
writer.close(); writer.close();
......
...@@ -70,7 +70,7 @@ public class TestConfigurationMetadataAnnotationProcessor ...@@ -70,7 +70,7 @@ public class TestConfigurationMetadataAnnotationProcessor
} }
@Override @Override
protected ConfigurationMetadata writeMetaData() { protected ConfigurationMetadata writeMetaData() throws Exception {
super.writeMetaData(); super.writeMetaData();
try { try {
File metadataFile = new File(this.outputLocation, File metadataFile = new File(this.outputLocation,
......
...@@ -18,7 +18,6 @@ package org.springframework.boot.configurationprocessor.metadata; ...@@ -18,7 +18,6 @@ package org.springframework.boot.configurationprocessor.metadata;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
...@@ -38,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -38,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
public class JsonMarshallerTests { public class JsonMarshallerTests {
@Test @Test
public void marshallAndUnmarshal() throws IOException { public void marshallAndUnmarshal() throws Exception {
ConfigurationMetadata metadata = new ConfigurationMetadata(); ConfigurationMetadata metadata = new ConfigurationMetadata();
metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(), metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(),
InputStream.class.getName(), "sourceMethod", "desc", "x", InputStream.class.getName(), "sourceMethod", "desc", "x",
......
/*
* Copyright 2012-2016 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 org.springframework.boot.configurationprocessor.metadata;
/**
* {@link JsonConverter} for use in tests.
*
* @author Phillip Webb
*/
public class TestJsonConverter extends JsonConverter {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment