diff --git a/spring-boot-cli/pom.xml b/spring-boot-cli/pom.xml index e0ef4f4ac8c..b341437191b 100644 --- a/spring-boot-cli/pom.xml +++ b/spring-boot-cli/pom.xml @@ -38,6 +38,10 @@ org.springframework.boot spring-boot-loader-tools + + com.vaadin.external.google + android-json + jline jline @@ -54,10 +58,6 @@ org.springframework spring-core - - org.json - json - org.apache.maven maven-aether-provider diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java index b3d9058a1f3..c85683229ae 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrService.java @@ -233,7 +233,8 @@ class InitializrService { return null; } - private JSONObject getContentAsJson(HttpEntity entity) throws IOException { + private JSONObject getContentAsJson(HttpEntity entity) + throws IOException, JSONException { return new JSONObject(getContent(entity)); } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java index 898269570dc..7615f88e74e 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java @@ -23,6 +23,7 @@ import java.util.Iterator; import java.util.Map; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; /** @@ -58,8 +59,9 @@ class InitializrServiceMetadata { /** * Creates a new instance using the specified root {@link JSONObject}. * @param root the root JSONObject + * @throws JSONException on JSON parsing failure */ - InitializrServiceMetadata(JSONObject root) { + InitializrServiceMetadata(JSONObject root) throws JSONException { this.dependencies = parseDependencies(root); this.projectTypes = parseProjectTypes(root); this.defaults = Collections.unmodifiableMap(parseDefaults(root)); @@ -124,7 +126,8 @@ class InitializrServiceMetadata { return this.defaults; } - private Map parseDependencies(JSONObject root) { + private Map parseDependencies(JSONObject root) + throws JSONException { Map result = new HashMap(); if (!root.has(DEPENDENCIES_EL)) { return result; @@ -138,7 +141,8 @@ class InitializrServiceMetadata { return result; } - private MetadataHolder parseProjectTypes(JSONObject root) { + private MetadataHolder parseProjectTypes(JSONObject root) + throws JSONException { MetadataHolder result = new MetadataHolder(); if (!root.has(TYPE_EL)) { return result; @@ -158,7 +162,7 @@ class InitializrServiceMetadata { return result; } - private Map parseDefaults(JSONObject root) { + private Map parseDefaults(JSONObject root) throws JSONException { Map result = new HashMap(); Iterator keys = root.keys(); while (keys.hasNext()) { @@ -174,7 +178,8 @@ class InitializrServiceMetadata { return result; } - private void parseGroup(JSONObject group, Map dependencies) { + private void parseGroup(JSONObject group, Map dependencies) + throws JSONException { if (group.has(VALUES_EL)) { JSONArray content = group.getJSONArray(VALUES_EL); for (int i = 0; i < content.length(); i++) { @@ -184,14 +189,15 @@ class InitializrServiceMetadata { } } - private Dependency parseDependency(JSONObject object) { + private Dependency parseDependency(JSONObject object) throws JSONException { String id = getStringValue(object, ID_ATTRIBUTE, null); String name = getStringValue(object, NAME_ATTRIBUTE, null); String description = getStringValue(object, DESCRIPTION_ATTRIBUTE, null); return new Dependency(id, name, description); } - private ProjectType parseType(JSONObject object, String defaultId) { + private ProjectType parseType(JSONObject object, String defaultId) + throws JSONException { String id = getStringValue(object, ID_ATTRIBUTE, null); String name = getStringValue(object, NAME_ATTRIBUTE, null); String action = getStringValue(object, ACTION_ATTRIBUTE, null); @@ -204,14 +210,15 @@ class InitializrServiceMetadata { return new ProjectType(id, name, action, defaultType, tags); } - private String getStringValue(JSONObject object, String name, String defaultValue) { + private String getStringValue(JSONObject object, String name, String defaultValue) + throws JSONException { return object.has(name) ? object.getString(name) : defaultValue; } - private Map parseStringItems(JSONObject json) { + private Map parseStringItems(JSONObject json) throws JSONException { Map result = new HashMap(); - 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); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java index bb8fc93bf76..9887b4f3451 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java @@ -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) { diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java index 825c4edde25..122cc3d4c54 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java @@ -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(); diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java index 7a4f69197e6..94ba131bdc0 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceTests.java @@ -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") diff --git a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java index 027e81731ee..7f134f77390 100644 --- a/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java +++ b/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java @@ -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"); diff --git a/spring-boot-parent/pom.xml b/spring-boot-parent/pom.xml index da776890615..89a17dc253c 100644 --- a/spring-boot-parent/pom.xml +++ b/spring-boot-parent/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.springframework.boot @@ -57,6 +58,11 @@ okhttp 3.4.1 + + com.vaadin.external.google + android-json + 0.0.20131108.vaadin1 + io.netty netty-all diff --git a/spring-boot-tools/spring-boot-configuration-metadata/pom.xml b/spring-boot-tools/spring-boot-configuration-metadata/pom.xml index 6374a429cd4..cfd2f20e525 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/pom.xml +++ b/spring-boot-tools/spring-boot-configuration-metadata/pom.xml @@ -20,8 +20,8 @@ - org.json - json + com.vaadin.external.google + android-json diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java index f268a26dccb..6ea6fde2406 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilder.java @@ -23,8 +23,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.json.JSONException; - /** * Load a {@link ConfigurationMetadataRepository} from the content of arbitrary * resource(s). @@ -104,13 +102,8 @@ public final class ConfigurationMetadataRepositoryJsonBuilder { RawConfigurationMetadata metadata = this.reader.read(in, charset); return create(metadata); } - catch (IOException ex) { - throw new IllegalArgumentException( - "Failed to read configuration " + "metadata", ex); - } - catch (JSONException ex) { - throw new IllegalArgumentException( - "Invalid configuration " + "metadata document", ex); + catch (Exception ex) { + throw new IllegalStateException("Failed to read configuration metadata", ex); } } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java index 97b33969f24..5c491b53173 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/JsonReader.java @@ -41,14 +41,26 @@ class JsonReader { public RawConfigurationMetadata read(InputStream in, Charset charset) throws IOException { - JSONObject json = readJson(in, charset); - List groups = parseAllSources(json); - List items = parseAllItems(json); - List hints = parseAllHints(json); - return new RawConfigurationMetadata(groups, items, hints); + try { + JSONObject json = readJson(in, charset); + List groups = parseAllSources(json); + List items = parseAllItems(json); + List hints = parseAllHints(json); + 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 parseAllSources(JSONObject root) { + private List parseAllSources(JSONObject root) + throws Exception { List result = new ArrayList(); if (!root.has("groups")) { return result; @@ -61,7 +73,8 @@ class JsonReader { return result; } - private List parseAllItems(JSONObject root) { + private List parseAllItems(JSONObject root) + throws Exception { List result = new ArrayList(); if (!root.has("properties")) { return result; @@ -74,7 +87,8 @@ class JsonReader { return result; } - private List parseAllHints(JSONObject root) { + private List parseAllHints(JSONObject root) + throws Exception { List result = new ArrayList(); if (!root.has("hints")) { return result; @@ -87,7 +101,7 @@ class JsonReader { return result; } - private ConfigurationMetadataSource parseSource(JSONObject json) { + private ConfigurationMetadataSource parseSource(JSONObject json) throws Exception { ConfigurationMetadataSource source = new ConfigurationMetadataSource(); source.setGroupId(json.getString("name")); source.setType(json.optString("type", null)); @@ -100,7 +114,7 @@ class JsonReader { return source; } - private ConfigurationMetadataItem parseItem(JSONObject json) { + private ConfigurationMetadataItem parseItem(JSONObject json) throws Exception { ConfigurationMetadataItem item = new ConfigurationMetadataItem(); item.setId(json.getString("name")); item.setType(json.optString("type", null)); @@ -115,7 +129,7 @@ class JsonReader { return item; } - private ConfigurationMetadataHint parseHint(JSONObject json) { + private ConfigurationMetadataHint parseHint(JSONObject json) throws Exception { ConfigurationMetadataHint hint = new ConfigurationMetadataHint(); hint.setId(json.getString("name")); if (json.has("values")) { @@ -152,7 +166,7 @@ class JsonReader { return hint; } - private Deprecation parseDeprecation(JSONObject object) { + private Deprecation parseDeprecation(JSONObject object) throws Exception { if (object.has("deprecation")) { JSONObject deprecationJsonObject = object.getJSONObject("deprecation"); Deprecation deprecation = new Deprecation(); @@ -164,7 +178,7 @@ class JsonReader { return (object.optBoolean("deprecated") ? new Deprecation() : null); } - private Object readItemValue(Object value) { + private Object readItemValue(Object value) throws Exception { if (value instanceof JSONArray) { JSONArray array = (JSONArray) value; Object[] content = new Object[array.length()]; @@ -176,7 +190,7 @@ class JsonReader { return value; } - private JSONObject readJson(InputStream in, Charset charset) throws IOException { + private JSONObject readJson(InputStream in, Charset charset) throws Exception { try { StringBuilder out = new StringBuilder(); InputStreamReader reader = new InputStreamReader(in, charset); diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java index 488eab2bc06..1783abbae38 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/JsonReaderTests.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.nio.charset.Charset; import java.util.List; +import org.hamcrest.CoreMatchers; import org.json.JSONException; import org.junit.Test; @@ -45,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { @Test public void invalidMetadata() throws IOException { - this.thrown.expect(JSONException.class); + this.thrown.expectCause(CoreMatchers.instanceOf(JSONException.class)); readFor("invalid"); } diff --git a/spring-boot-tools/spring-boot-configuration-processor/pom.xml b/spring-boot-tools/spring-boot-configuration-processor/pom.xml index 21d0fca3d3d..431280f3ad7 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/pom.xml +++ b/spring-boot-tools/spring-boot-configuration-processor/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 org.springframework.boot @@ -18,10 +19,11 @@ ${basedir}/../.. - - - org.json - json + + + com.vaadin.external.google + android-json + true @@ -29,10 +31,6 @@ lombok test - - org.springframework - spring-core - @@ -44,6 +42,34 @@ none + + org.apache.maven.plugins + maven-shade-plugin + + + package + + shade + + + + + com.vaadin.external.google:android-json + + + + + org.json + org.springframework.boot.configurationprocessor.json + + + false + true + true + + + + diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java index 8ee56e1d780..02e22941e8d 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessor.java @@ -17,7 +17,6 @@ package org.springframework.boot.configurationprocessor; import java.io.FileNotFoundException; -import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.LinkedHashMap; @@ -134,7 +133,12 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor } } if (roundEnv.processingOver()) { - writeMetaData(); + try { + writeMetaData(); + } + catch (Exception ex) { + throw new IllegalStateException("Failed to write metadata", ex); + } } return false; } @@ -390,16 +394,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor return values; } - protected ConfigurationMetadata writeMetaData() { + protected ConfigurationMetadata writeMetaData() throws Exception { ConfigurationMetadata metadata = this.metadataCollector.getMetadata(); metadata = mergeAdditionalMetadata(metadata); if (!metadata.getItems().isEmpty()) { - try { - this.metadataStore.writeMetadata(metadata); - } - catch (IOException ex) { - throw new IllegalStateException("Failed to write metadata", ex); - } + this.metadataStore.writeMetadata(metadata); return metadata; } return null; diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java index a78569ddafe..62d3896e4f2 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/MetadataStore.java @@ -27,8 +27,6 @@ import javax.tools.Diagnostic; import javax.tools.FileObject; import javax.tools.StandardLocation; -import org.json.JSONException; - import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException; import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller; @@ -87,7 +85,7 @@ public class MetadataStore { catch (IOException ex) { return null; } - catch (JSONException ex) { + catch (Exception ex) { throw new InvalidConfigurationMetadataException( "Invalid additional meta-data in '" + METADATA_PATH + "': " + ex.getMessage(), diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java index be735020dc2..2144292fd0e 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadata.java @@ -20,15 +20,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.ListIterator; +import java.util.Map; 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. * @@ -46,18 +43,18 @@ public class ConfigurationMetadata { SEPARATORS = Collections.unmodifiableSet(new HashSet(chars)); } - private final MultiValueMap items; + private final Map> items; - private final MultiValueMap hints; + private final Map> hints; public ConfigurationMetadata() { - this.items = new LinkedMultiValueMap(); - this.hints = new LinkedMultiValueMap(); + this.items = new LinkedHashMap>(); + this.hints = new LinkedHashMap>(); } public ConfigurationMetadata(ConfigurationMetadata metadata) { - this.items = new LinkedMultiValueMap(metadata.items); - this.hints = new LinkedMultiValueMap(metadata.hints); + this.items = new LinkedHashMap>(metadata.items); + this.hints = new LinkedHashMap>(metadata.hints); } /** @@ -65,7 +62,7 @@ public class ConfigurationMetadata { * @param itemMetadata the meta-data to add */ public void add(ItemMetadata itemMetadata) { - this.items.add(itemMetadata.getName(), itemMetadata); + add(this.items, itemMetadata.getName(), itemMetadata); } /** @@ -73,7 +70,7 @@ public class ConfigurationMetadata { * @param itemHint the item hint to add */ public void add(ItemHint itemHint) { - this.hints.add(itemHint.getName(), itemHint); + add(this.hints, itemHint.getName(), itemHint); } /** @@ -131,13 +128,22 @@ public class ConfigurationMetadata { } } else { - this.items.add(metadata.getName(), metadata); + add(this.items, metadata.getName(), metadata); } } + private void add(Map> map, K key, V value) { + List values = map.get(key); + if (values == null) { + values = new ArrayList(); + map.put(key, values); + } + values.add(value); + } + private ItemMetadata findMatchingItemMetadata(ItemMetadata metadata) { List candidates = this.items.get(metadata.getName()); - if (CollectionUtils.isEmpty(candidates)) { + if (candidates == null || candidates.isEmpty()) { return null; } ListIterator it = candidates.listIterator(); @@ -150,14 +156,20 @@ public class ConfigurationMetadata { return candidates.get(0); } for (ItemMetadata candidate : candidates) { - if (ObjectUtils.nullSafeEquals(candidate.getSourceType(), - metadata.getSourceType())) { + if (nullSafeEquals(candidate.getSourceType(), metadata.getSourceType())) { return candidate; } } 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) { String nestedPrefix = (prefix == null ? "" : prefix); String dashedName = toDashedCase(name); @@ -185,8 +197,7 @@ public class ConfigurationMetadata { return dashed.toString().toLowerCase(); } - private static > List flattenValues( - MultiValueMap map) { + private static > List flattenValues(Map> map) { List content = new ArrayList(); for (List values : map.values()) { content.addAll(values); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JSONOrderedObject.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JSONOrderedObject.java new file mode 100644 index 00000000000..a0ee208001b --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JSONOrderedObject.java @@ -0,0 +1,48 @@ +/* + * 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 keys = new LinkedHashSet(); + + @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(); + } + +} diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java new file mode 100644 index 00000000000..e92b542a606 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonConverter.java @@ -0,0 +1,161 @@ +/* + * 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 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 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; + } + +} diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java index 13bc23e4473..836cbce4161 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshaller.java @@ -20,18 +20,14 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.lang.reflect.Array; import java.nio.charset.Charset; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; -import java.util.LinkedHashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import org.json.JSONArray; -import org.json.JSONException; import org.json.JSONObject; import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; @@ -51,136 +47,26 @@ public class JsonMarshaller { public void write(ConfigurationMetadata metadata, OutputStream outputStream) throws IOException { - JSONObject object = new JSONOrderedObject(); - object.put("groups", toJsonArray(metadata, ItemType.GROUP)); - object.put("properties", toJsonArray(metadata, ItemType.PROPERTY)); - object.put("hints", toJsonArray(metadata.getHints())); - outputStream.write(object.toString(2).getBytes(UTF_8)); - } - - private JSONArray toJsonArray(ConfigurationMetadata metadata, ItemType itemType) { - JSONArray jsonArray = new JSONArray(); - for (ItemMetadata item : metadata.getItems()) { - if (item.isOfItemType(itemType)) { - jsonArray.put(toJsonObject(item)); + try { + JSONObject object = new JSONOrderedObject(); + JsonConverter converter = new JsonConverter(); + object.put("groups", converter.toJsonArray(metadata, ItemType.GROUP)); + object.put("properties", converter.toJsonArray(metadata, ItemType.PROPERTY)); + object.put("hints", converter.toJsonArray(metadata.getHints())); + outputStream.write(object.toString(2).getBytes(UTF_8)); + } + catch (Exception ex) { + if (ex instanceof IOException) { + throw (IOException) ex; } - } - return jsonArray; - } - - private JSONArray toJsonArray(Collection 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 (ex instanceof RuntimeException) { + throw (RuntimeException) ex; } - 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; - } - - 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 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) { - 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 { + public ConfigurationMetadata read(InputStream inputStream) throws Exception { ConfigurationMetadata metadata = new ConfigurationMetadata(); JSONObject object = new JSONObject(toString(inputStream)); JSONArray groups = object.optJSONArray("groups"); @@ -205,7 +91,8 @@ public class JsonMarshaller { return metadata; } - private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType) { + private ItemMetadata toItemMetadata(JSONObject object, ItemType itemType) + throws Exception { String name = object.getString("name"); String type = object.optString("type", null); String description = object.optString("description", null); @@ -217,7 +104,7 @@ public class JsonMarshaller { description, defaultValue, deprecation); } - private ItemDeprecation toItemDeprecation(JSONObject object) { + private ItemDeprecation toItemDeprecation(JSONObject object) throws Exception { if (object.has("deprecation")) { JSONObject deprecationJsonObject = object.getJSONObject("deprecation"); ItemDeprecation deprecation = new ItemDeprecation(); @@ -229,7 +116,7 @@ public class JsonMarshaller { return (object.optBoolean("deprecated") ? new ItemDeprecation() : null); } - private ItemHint toItemHint(JSONObject object) { + private ItemHint toItemHint(JSONObject object) throws Exception { String name = object.getString("name"); List values = new ArrayList(); if (object.has("values")) { @@ -248,19 +135,19 @@ public class JsonMarshaller { 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")); String description = object.optString("description", null); 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"); Map parameters = new HashMap(); if (object.has("parameters")) { JSONObject parametersObject = object.getJSONObject("parameters"); - for (Object k : parametersObject.keySet()) { - String key = (String) k; + for (Iterator iterator = parametersObject.keys(); iterator.hasNext();) { + String key = (String) iterator.next(); Object value = readItemValue(parametersObject.get(key)); parameters.put(key, value); } @@ -268,7 +155,7 @@ public class JsonMarshaller { return new ItemHint.ValueProvider(name, parameters); } - private Object readItemValue(Object value) { + private Object readItemValue(Object value) throws Exception { if (value instanceof JSONArray) { JSONArray array = (JSONArray) value; Object[] content = new Object[array.length()]; @@ -291,25 +178,4 @@ public class JsonMarshaller { return out.toString(); } - /** - * Extension to {@link JSONObject} that remembers the order of inserts. - */ - @SuppressWarnings("rawtypes") - private static class JSONOrderedObject extends JSONObject { - - private Set keys = new LinkedHashSet(); - - @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; - } - - } - } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java index 69ba7d4ba07..aa371a7a20e 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataAnnotationProcessorTests.java @@ -34,6 +34,7 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; import org.springframework.boot.configurationprocessor.metadata.ItemHint; 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.FooProperties; import org.springframework.boot.configurationsample.incremental.RenamedBarProperties; @@ -630,8 +631,9 @@ public class ConfigurationMetadataAnnotationProcessorTests { JSONObject additionalMetadata = new JSONObject(); additionalMetadata.put("properties", properties); FileWriter writer = new FileWriter(additionalMetadataFile); - additionalMetadata.write(writer); + writer.append(additionalMetadata.toString(2)); writer.flush(); + writer.close(); ConfigurationMetadata metadata = compile(SimpleProperties.class); assertThat(metadata).has(Metadata.withProperty("simple.comparator")); assertThat(metadata).has(Metadata.withProperty("foo", String.class) @@ -724,23 +726,28 @@ public class ConfigurationMetadataAnnotationProcessorTests { return processor.getMetadata(); } - private void writeAdditionalMetadata(ItemMetadata... metadata) throws IOException { + private void writeAdditionalMetadata(ItemMetadata... metadata) throws Exception { + TestJsonConverter converter = new TestJsonConverter(); File additionalMetadataFile = createAdditionalMetadataFile(); 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); } - private void writeAdditionalHints(ItemHint... hints) throws IOException { + private void writeAdditionalHints(ItemHint... hints) throws Exception { + TestJsonConverter converter = new TestJsonConverter(); File additionalMetadataFile = createAdditionalMetadataFile(); JSONObject additionalMetadata = new JSONObject(); - additionalMetadata.put("hints", hints); + additionalMetadata.put("hints", converter.toJsonArray(Arrays.asList(hints))); writeMetadata(additionalMetadataFile, additionalMetadata); } - private void writePropertyDeprecation(ItemMetadata... items) throws IOException { + private void writePropertyDeprecation(ItemMetadata... items) throws Exception { File additionalMetadataFile = createAdditionalMetadataFile(); - JSONArray propertiesArray = new JSONArray(); for (ItemMetadata item : items) { JSONObject jsonObject = new JSONObject(); @@ -776,11 +783,10 @@ public class ConfigurationMetadataAnnotationProcessorTests { return additionalMetadataFile; } - private void writeMetadata(File metadataFile, JSONObject metadata) - throws IOException { + private void writeMetadata(File metadataFile, JSONObject metadata) throws Exception { FileWriter writer = new FileWriter(metadataFile); try { - metadata.write(writer); + writer.append(metadata.toString(2)); } finally { writer.close(); diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java index b0ff80fd51f..12738c3082b 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java @@ -70,7 +70,7 @@ public class TestConfigurationMetadataAnnotationProcessor } @Override - protected ConfigurationMetadata writeMetaData() { + protected ConfigurationMetadata writeMetaData() throws Exception { super.writeMetaData(); try { File metadataFile = new File(this.outputLocation, diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java index 280f191accc..cc9f98c17ee 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java @@ -18,7 +18,6 @@ package org.springframework.boot.configurationprocessor.metadata; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.Collections; @@ -38,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class JsonMarshallerTests { @Test - public void marshallAndUnmarshal() throws IOException { + public void marshallAndUnmarshal() throws Exception { ConfigurationMetadata metadata = new ConfigurationMetadata(); metadata.add(ItemMetadata.newProperty("a", "b", StringBuffer.class.getName(), InputStream.class.getName(), "sourceMethod", "desc", "x", diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java new file mode 100644 index 00000000000..70167a65d33 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/TestJsonConverter.java @@ -0,0 +1,26 @@ +/* + * 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 { + +}