From a83dae0e10fe353f7f1a8460241b08b8a31ca8b7 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 8 Mar 2016 09:45:35 +0100 Subject: [PATCH] Polish contribution Closes gh-5335 --- .../RawConfigurationMetadata.java | 12 +++++-- ...ionMetadataRepositoryJsonBuilderTests.java | 32 ++++++++++++++++++- .../JsonReaderTests.java | 11 ++++--- ... configuration-metadata-empty-groups.json} | 12 +++++++ 4 files changed, 58 insertions(+), 9 deletions(-) rename spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/{configuration-metadata-emptygroup.json => configuration-metadata-empty-groups.json} (53%) diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java index 0533776ffa5..ab4918cb796 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/RawConfigurationMetadata.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * 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. @@ -77,12 +77,18 @@ class RawConfigurationMetadata { } ConfigurationMetadataSource source = getSource(item.getSourceType()); if (source != null) { - String dottedPrefix = source.getGroupId() + "."; + String groupId = source.getGroupId(); + String dottedPrefix = groupId + "."; String id = item.getId(); - if (id.startsWith(dottedPrefix)) { + if (hasLength(groupId) && id.startsWith(dottedPrefix)) { String name = id.substring(dottedPrefix.length(), id.length()); item.setName(name); } } } + + private static boolean hasLength(String s) { + return (s != null && s.length() > 0); + } + } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilderTests.java b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilderTests.java index 1107e51ce98..4db6baaaedb 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilderTests.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepositoryJsonBuilderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2015 the original author or authors. + * 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. @@ -123,6 +123,22 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests } } + @Test + public void emptyGroups() throws IOException { + InputStream in = getInputStreamFor("empty-groups"); + try { + ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder + .create(in).build(); + validateEmptyGroup(repo); + assertEquals(1, repo.getAllGroups().size()); + contains(repo.getAllProperties(), "name", "title"); + assertEquals(2, repo.getAllProperties().size()); + } + finally { + in.close(); + } + } + @Test public void builderInstancesAreIsolated() throws IOException { InputStream foo = getInputStreamFor("foo"); @@ -184,6 +200,20 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests validatePropertyHints(repo.getAllProperties().get("spring.bar.counter"), 0, 0); } + private void validateEmptyGroup(ConfigurationMetadataRepository repo) { + ConfigurationMetadataGroup group = repo.getAllGroups().get(""); + contains(group.getSources(), "org.acme.Foo", "org.acme.Bar"); + ConfigurationMetadataSource source = group.getSources().get("org.acme.Foo"); + contains(source.getProperties(), "name"); + assertEquals(1, source.getProperties().size()); + ConfigurationMetadataSource source2 = group.getSources() + .get("org.acme.Bar"); + contains(source2.getProperties(), "title"); + assertEquals(1, source2.getProperties().size()); + validatePropertyHints(repo.getAllProperties().get("name"), 0, 0); + validatePropertyHints(repo.getAllProperties().get("title"), 0, 0); + } + private void validatePropertyHints(ConfigurationMetadataProperty property, int valueHints, int valueProviders) { assertEquals(valueHints, property.getValueHints().size()); 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 f286ab7fe8a..8170197bccb 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 @@ -54,13 +54,14 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { @Test public void emptyGroupName() throws IOException { - RawConfigurationMetadata rawMetadata = readFor("emptygroup"); + RawConfigurationMetadata rawMetadata = readFor("empty-groups"); List items = rawMetadata.getItems(); - assertEquals(1, items.size()); - - ConfigurationMetadataItem item = items.get(0); - assertProperty(item, "name", "name", String.class, null); + assertEquals(2, items.size()); + ConfigurationMetadataItem name = items.get(0); + assertProperty(name, "name", "name", String.class, null); + ConfigurationMetadataItem dotTitle = items.get(1); + assertProperty(dotTitle, "title", "title", String.class, null); } @Test diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json b/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-empty-groups.json similarity index 53% rename from spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json rename to spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-empty-groups.json index eb16f3cb449..14977b264cc 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-emptygroup.json +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/resources/metadata/configuration-metadata-empty-groups.json @@ -6,6 +6,13 @@ "sourceType": "org.acme.config.FooApp", "sourceMethod": "foo()", "description": "This is Foo." + }, + { + "name": "", + "type": "org.acme.Bar", + "sourceType": "org.acme.config.FooApp", + "sourceMethod": "bar()", + "description": "This is Bar." } ], "properties": [ @@ -13,6 +20,11 @@ "name": "name", "type": "java.lang.String", "sourceType": "org.acme.Foo" + }, + { + "name": "title", + "type": "java.lang.String", + "sourceType": "org.acme.Bar" } ] }