From 9ebe15232e20e61edc91089dd0a88d3bcf5f7524 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 13 Jul 2015 11:05:46 -0700 Subject: [PATCH] Polish --- .../MetricExportAutoConfiguration.java | 18 +-- .../metrics/export/MetricExportersTests.java | 6 +- .../SessionAutoConfigurationTests.java | 2 + .../pom.xml | 1 - .../ConfigurationMetadataGroup.java | 33 +++-- .../ConfigurationMetadataHint.java | 22 ++- .../ConfigurationMetadataItem.java | 20 +-- .../ConfigurationMetadataProperty.java | 61 +++++--- .../ConfigurationMetadataRepository.java | 9 +- ...gurationMetadataRepositoryJsonBuilder.java | 131 +++++++++++------- .../ConfigurationMetadataSource.java | 27 ++-- .../DescriptionExtractor.java | 58 ++++++++ .../configurationmetadata/JsonReader.java | 47 ++----- .../RawConfigurationMetadata.java | 4 +- ...SimpleConfigurationMetadataRepository.java | 33 +++-- .../boot/configurationmetadata/ValueHint.java | 29 +++- .../configurationmetadata/ValueProvider.java | 32 ++++- .../AbstractConfigurationMetadataTests.java | 15 +- ...ionMetadataRepositoryJsonBuilderTests.java | 88 ++++++------ .../DescriptionExtractorTests.java | 68 +++++++++ .../JsonReaderTests.java | 43 +----- .../metadata/ItemHint.java | 4 +- .../metadata/JsonMarshaller.java | 63 +++++---- ...ationMetadataAnnotationProcessorTests.java | 9 +- 24 files changed, 514 insertions(+), 309 deletions(-) create mode 100644 spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/DescriptionExtractor.java create mode 100644 spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/DescriptionExtractorTests.java diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java index c34377a6ba..2380b2c1a6 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/MetricExportAutoConfiguration.java @@ -79,23 +79,17 @@ public class MetricExportAutoConfiguration { reader = new CompositeMetricReader( this.readers.toArray(new MetricReader[this.readers.size()])); } - MetricExporters exporters = null; + if (reader == null && this.exporters.isEmpty()) { + return new NoOpSchedulingConfigurer(); + } + MetricExporters exporters = new MetricExporters(this.properties); if (reader != null) { - exporters = new MetricExporters(this.properties); writers.putAll(this.writers); exporters.setReader(reader); exporters.setWriters(writers); } - if (!this.exporters.isEmpty()) { - if (exporters==null) { - exporters = new MetricExporters(this.properties); - } - exporters.setExporters(this.exporters); - } - if (exporters!=null) { - return exporters; - } - return new NoOpSchedulingConfigurer(); + exporters.setExporters(this.exporters); + return exporters; } @Configuration diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java index da552be611..ce484672e5 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/export/MetricExportersTests.java @@ -16,9 +16,6 @@ package org.springframework.boot.actuate.metrics.export; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -29,6 +26,9 @@ import org.springframework.boot.actuate.metrics.reader.MetricReader; import org.springframework.boot.actuate.metrics.writer.MetricWriter; import org.springframework.scheduling.config.ScheduledTaskRegistrar; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + /** * Tests for {@link MetricExporters}. * diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index d3a8a2e47f..c167e9e1d1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -32,6 +32,8 @@ import org.springframework.context.annotation.Configuration; import static org.junit.Assert.assertNotNull; /** + * Tests for {@link SessionAutoConfiguration}. + * * @author Dave Syer * @since 1.3.0 */ diff --git a/spring-boot-tools/spring-boot-configuration-metadata/pom.xml b/spring-boot-tools/spring-boot-configuration-metadata/pom.xml index 0449ea90ab..f060c66b56 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/pom.xml +++ b/spring-boot-tools/spring-boot-configuration-metadata/pom.xml @@ -24,6 +24,5 @@ org.json json - diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataGroup.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataGroup.java index bc6009d9fa..2f68d08e8e 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataGroup.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataGroup.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -20,10 +20,10 @@ import java.util.HashMap; import java.util.Map; /** - * Gather a collection of {@link ConfigurationMetadataProperty properties} that - * are sharing a {@link #getId() common prefix}. Provide access to all the - * {@link ConfigurationMetadataSource sources} that have contributed properties - * to the group. + * Gather a collection of {@link ConfigurationMetadataProperty properties} that are + * sharing a {@link #getId() common prefix}. Provide access to all the + * {@link ConfigurationMetadataSource sources} that have contributed properties to the + * group. * * @author Stephane Nicoll * @since 1.3.0 @@ -32,27 +32,27 @@ public class ConfigurationMetadataGroup { private final String id; - private final Map sources = - new HashMap(); + private final Map sources = new HashMap(); - private final Map properties = - new HashMap(); + private final Map properties = new HashMap(); public ConfigurationMetadataGroup(String id) { this.id = id; } /** - * Return the id of the group, used as a common prefix for all properties - * associated to it. + * Return the id of the group, used as a common prefix for all properties associated + * to it. + * @return the id of the group */ public String getId() { return this.id; } /** - * Return the {@link ConfigurationMetadataSource sources} defining - * the properties of this group. + * Return the {@link ConfigurationMetadataSource sources} defining the properties of + * this group. + * @return the sources of the group */ public Map getSources() { return this.sources; @@ -60,8 +60,11 @@ public class ConfigurationMetadataGroup { /** * Return the {@link ConfigurationMetadataProperty properties} defined in this group. - *

A property may appear more than once for a given source, potentially with conflicting - * type or documentation. This is a "merged" view of the properties of this group. + *

+ * A property may appear more than once for a given source, potentially with + * conflicting type or documentation. This is a "merged" view of the properties of + * this group. + * @return the properties of the group * @see ConfigurationMetadataSource#getProperties() */ public Map getProperties() { diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataHint.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataHint.java index ac0475fbaf..cef75b9c02 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataHint.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataHint.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012-2015 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.configurationmetadata; import java.util.ArrayList; @@ -18,7 +34,7 @@ class ConfigurationMetadataHint { private final List valueProviders = new ArrayList(); public String getId() { - return id; + return this.id; } public void setId(String id) { @@ -26,11 +42,11 @@ class ConfigurationMetadataHint { } public List getValueHints() { - return valueHints; + return this.valueHints; } public List getValueProviders() { - return valueProviders; + return this.valueProviders; } } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataItem.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataItem.java index cdc21a178b..0d7de9dc68 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataItem.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataItem.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -17,8 +17,8 @@ package org.springframework.boot.configurationmetadata; /** - * An extension of {@link ConfigurationMetadataProperty} that provides the - * a reference to its source. + * An extension of {@link ConfigurationMetadataProperty} that provides the a reference to + * its source. * * @author Stephane Nicoll * @since 1.3.0 @@ -30,9 +30,10 @@ class ConfigurationMetadataItem extends ConfigurationMetadataProperty { private String sourceMethod; /** - * The class name of the source that contributed this property. For example, if the property - * was from a class annotated with {@code @ConfigurationProperties} this attribute would - * contain the fully qualified name of that class. + * The class name of the source that contributed this property. For example, if the + * property was from a class annotated with {@code @ConfigurationProperties} this + * attribute would contain the fully qualified name of that class. + * @return the source type */ public String getSourceType() { return this.sourceType; @@ -43,9 +44,10 @@ class ConfigurationMetadataItem extends ConfigurationMetadataProperty { } /** - * The full name of the method (including parenthesis and argument types) that contributed this - * property. For example, the name of a getter in a {@code @ConfigurationProperties} annotated - * class. + * The full name of the method (including parenthesis and argument types) that + * contributed this property. For example, the name of a getter in a + * {@code @ConfigurationProperties} annotated class. + * @return the source method */ public String getSourceMethod() { return this.sourceMethod; diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataProperty.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataProperty.java index 45c134fb6b..4579cdc987 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataProperty.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataProperty.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -20,10 +20,10 @@ import java.util.ArrayList; import java.util.List; /** - * Define a configuration property. Each property is fully identified by - * its {@link #getId() id} who is composed of a namespace prefix (the - * {@link ConfigurationMetadataGroup#getId() group id}), if any and the - * {@link #getName() name} of the property. + * Define a configuration property. Each property is fully identified by its + * {@link #getId() id} who is composed of a namespace prefix (the + * {@link ConfigurationMetadataGroup#getId() group id}), if any and the {@link #getName() + * name} of the property. * * @author Stephane Nicoll * @since 1.3.0 @@ -49,7 +49,9 @@ public class ConfigurationMetadataProperty { private boolean deprecated; /** - * The full identifier of the property, in lowercase dashed form (e.g. my.group.simple-property) + * The full identifier of the property, in lowercase dashed form (e.g. + * my.group.simple-property) + * @return the property id */ public String getId() { return this.id; @@ -60,8 +62,9 @@ public class ConfigurationMetadataProperty { } /** - * The name of the property, in lowercase dashed form (e.g. simple-property). If this item - * does not belong to any group, the id is returned. + * The name of the property, in lowercase dashed form (e.g. simple-property). If this + * item does not belong to any group, the id is returned. + * @return the property name */ public String getName() { return this.name; @@ -72,13 +75,18 @@ public class ConfigurationMetadataProperty { } /** - * The class name of the data type of the property. For example, {@code java.lang.String}. - *

For consistency, the type of a primitive is specified using its wrapper counterpart, - * i.e. {@code boolean} becomes {@code java.lang.Boolean}. If the type holds generic - * information, these are provided as well, i.e. a {@code HashMap} of String to Integer - * would be defined as {@code java.util.HashMap}. - *

Note that this class may be a complex type that gets converted from a String as values - * are bound. + * The class name of the data type of the property. For example, + * {@code java.lang.String}. + *

+ * For consistency, the type of a primitive is specified using its wrapper + * counterpart, i.e. {@code boolean} becomes {@code java.lang.Boolean}. If the type + * holds generic information, these are provided as well, i.e. a {@code HashMap} of + * String to Integer would be defined as + * {@code java.util.HashMap}. + *

+ * Note that this class may be a complex type that gets converted from a String as + * values are bound. + * @return the property type */ public String getType() { return this.type; @@ -90,6 +98,7 @@ public class ConfigurationMetadataProperty { /** * A description of the property, if any. Can be multi-lines. + * @return the property description * @see #getShortDescription() */ public String getDescription() { @@ -102,10 +111,11 @@ public class ConfigurationMetadataProperty { /** * A single-line, single-sentence description of this property, if any. + * @return the property short description * @see #getDescription() */ public String getShortDescription() { - return shortDescription; + return this.shortDescription; } public void setShortDescription(String shortDescription) { @@ -114,6 +124,7 @@ public class ConfigurationMetadataProperty { /** * The default value, if any. + * @return the default value */ public Object getDefaultValue() { return this.defaultValue; @@ -124,24 +135,28 @@ public class ConfigurationMetadataProperty { } /** - * The list of well-defined values, if any. If no extra {@link ValueProvider provider} is - * specified, these values are to be considered a closed-set of the available values - * for this item. + * The list of well-defined values, if any. If no extra {@link ValueProvider provider} + * is specified, these values are to be considered a closed-set of the available + * values for this item. + * @return the value hints */ public List getValueHints() { - return valueHints; + return this.valueHints; } /** - * The value providers that are applicable to this item. Only one {@link ValueProvider} is - * enabled for an item: the first in the list that is supported should be used. + * The value providers that are applicable to this item. Only one + * {@link ValueProvider} is enabled for an item: the first in the list that is + * supported should be used. + * @return the value providers */ public List getValueProviders() { - return valueProviders; + return this.valueProviders; } /** * Specify if the property is deprecated. + * @return if the property is deprecated */ public boolean isDeprecated() { return this.deprecated; diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepository.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepository.java index 42db7f099c..95122ac2a1 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepository.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -27,19 +27,20 @@ import java.util.Map; public interface ConfigurationMetadataRepository { /** - * Defines the name of the "root" group, that is the group that - * gathers all the properties that aren't attached to a specific - * group. + * Defines the name of the "root" group, that is the group that gathers all the + * properties that aren't attached to a specific group. */ String ROOT_GROUP = "_ROOT_GROUP_"; /** * Return the groups, indexed by id. + * @return all configuration meta-data groups */ Map getAllGroups(); /** * Return the properties, indexed by id. + * @return all configuration meta-data properties */ Map getAllProperties(); 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 5d13fc066b..5fd28e2931 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -26,7 +26,8 @@ import java.util.Map; import org.json.JSONException; /** - * Load a {@link ConfigurationMetadataRepository} from the content of arbitrary resource(s). + * Load a {@link ConfigurationMetadataRepository} from the content of arbitrary + * resource(s). * * @author Stephane Nicoll * @since 1.3.0 @@ -39,75 +40,74 @@ public class ConfigurationMetadataRepositoryJsonBuilder { private final JsonReader reader = new JsonReader(); - private final List repositories - = new ArrayList(); - - - /** - * Create a new builder instance using {@link #UTF_8} as the default charset. - */ - public static ConfigurationMetadataRepositoryJsonBuilder create() { - return create(UTF_8); - } - - /** - * Create a new builder instance using the specified default {@link Charset}. - */ - public static ConfigurationMetadataRepositoryJsonBuilder create(Charset defaultCharset) { - return new ConfigurationMetadataRepositoryJsonBuilder(defaultCharset); - } + private final List repositories = new ArrayList(); private ConfigurationMetadataRepositoryJsonBuilder(Charset defaultCharset) { this.defaultCharset = defaultCharset; } /** - * Add the content of a {@link ConfigurationMetadataRepository} defined by the specified - * {@link InputStream} json document using the default charset. If this metadata - * repository holds items that were loaded previously, these are ignored. - *

Leave the stream open when done. + * Add the content of a {@link ConfigurationMetadataRepository} defined by the + * specified {@link InputStream} json document using the default charset. If this + * metadata repository holds items that were loaded previously, these are ignored. + *

+ * Leaves the stream open when done. + * @param inputStream the source input stream + * @return this builder + * @throws IOException */ - public ConfigurationMetadataRepositoryJsonBuilder withJsonResource(InputStream in) - throws IOException { - return withJsonResource(in, defaultCharset); + public ConfigurationMetadataRepositoryJsonBuilder withJsonResource( + InputStream inputStream) throws IOException { + return withJsonResource(inputStream, this.defaultCharset); } /** - * Add the content of a {@link ConfigurationMetadataRepository} defined by the specified - * {@link InputStream} json document using the specified {@link Charset}. If this metadata - * repository holds items that were loaded previously, these are ignored. - *

Leave the stream open when done. + * Add the content of a {@link ConfigurationMetadataRepository} defined by the + * specified {@link InputStream} json document using the specified {@link Charset}. If + * this metadata repository holds items that were loaded previously, these are + * ignored. + *

+ * Leaves the stream open when done. + * @param inputstream the source input stream + * @param charset the charset of the input + * @return this builder + * @throws IOException */ - public ConfigurationMetadataRepositoryJsonBuilder withJsonResource(InputStream inputstream, Charset charset) - throws IOException { + public ConfigurationMetadataRepositoryJsonBuilder withJsonResource( + InputStream inputstream, Charset charset) throws IOException { if (inputstream == null) { throw new IllegalArgumentException("InputStream must not be null."); } - repositories.add(add(inputstream, charset)); + this.repositories.add(add(inputstream, charset)); return this; } /** - * Build a {@link ConfigurationMetadataRepository} with the current state of this builder. + * Build a {@link ConfigurationMetadataRepository} with the current state of this + * builder. + * @return this builder */ public ConfigurationMetadataRepository build() { SimpleConfigurationMetadataRepository result = new SimpleConfigurationMetadataRepository(); - for (SimpleConfigurationMetadataRepository repository : repositories) { + for (SimpleConfigurationMetadataRepository repository : this.repositories) { result.include(repository); } return result; } - private SimpleConfigurationMetadataRepository add(InputStream in, Charset charset) throws IOException { + private SimpleConfigurationMetadataRepository add(InputStream in, Charset charset) + throws IOException { try { RawConfigurationMetadata metadata = this.reader.read(in, charset); return create(metadata); } - catch (IOException e) { - throw new IllegalArgumentException("Failed to read configuration metadata", e); + catch (IOException ex) { + throw new IllegalArgumentException("Failed to read configuration " + + "metadata", ex); } - catch (JSONException e) { - throw new IllegalArgumentException("Invalid configuration metadata document", e); + catch (JSONException ex) { + throw new IllegalArgumentException("Invalid configuration " + + "metadata document", ex); } } @@ -115,14 +115,11 @@ public class ConfigurationMetadataRepositoryJsonBuilder { SimpleConfigurationMetadataRepository repository = new SimpleConfigurationMetadataRepository(); repository.add(metadata.getSources()); for (ConfigurationMetadataItem item : metadata.getItems()) { - ConfigurationMetadataSource source = null; - String sourceType = item.getSourceType(); - if (sourceType != null) { - source = metadata.getSource(sourceType); - } + ConfigurationMetadataSource source = getSource(metadata, item); repository.add(item, source); } - Map allProperties = repository.getAllProperties(); + Map allProperties = repository + .getAllProperties(); for (ConfigurationMetadataHint hint : metadata.getHints()) { ConfigurationMetadataProperty property = allProperties.get(hint.getId()); if (property != null) { @@ -133,5 +130,45 @@ public class ConfigurationMetadataRepositoryJsonBuilder { return repository; } -} + private ConfigurationMetadataSource getSource(RawConfigurationMetadata metadata, + ConfigurationMetadataItem item) { + if (item.getSourceType() != null) { + return metadata.getSource(item.getSourceType()); + } + return null; + } + /** + * Create a new builder instance using {@link #UTF_8} as the default charset and the + * specified json resource. + * @param inputStreams the source input streams + * @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance. + * @throws IOException on error + */ + public static ConfigurationMetadataRepositoryJsonBuilder create( + InputStream... inputStreams) throws IOException { + ConfigurationMetadataRepositoryJsonBuilder builder = create(); + for (InputStream inputStream : inputStreams) { + builder = builder.withJsonResource(inputStream); + } + return builder; + } + + /** + * Create a new builder instance using {@link #UTF_8} as the default charset. + * @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance. + */ + public static ConfigurationMetadataRepositoryJsonBuilder create() { + return create(UTF_8); + } + + /** + * Create a new builder instance using the specified default {@link Charset}. + * @param defaultCharset the default charset to use + * @return a new {@link ConfigurationMetadataRepositoryJsonBuilder} instance. + */ + public static ConfigurationMetadataRepositoryJsonBuilder create(Charset defaultCharset) { + return new ConfigurationMetadataRepositoryJsonBuilder(defaultCharset); + } + +} diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataSource.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataSource.java index 2fa3fc4593..8dad107056 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataSource.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ConfigurationMetadataSource.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -20,8 +20,8 @@ import java.util.HashMap; import java.util.Map; /** - * A source of configuration metadata. Also defines where the source is declared, - * for instance if it is defined as a {@code @Bean}. + * A source of configuration metadata. Also defines where the source is declared, for + * instance if it is defined as a {@code @Bean}. * * @author Stephane Nicoll * @since 1.3.0 @@ -40,11 +40,11 @@ public class ConfigurationMetadataSource { private String sourceMethod; - private final Map properties - = new HashMap(); + private final Map properties = new HashMap(); /** * The identifier of the group to which this source is associated + * @return the group id */ public String getGroupId() { return this.groupId; @@ -55,9 +55,9 @@ public class ConfigurationMetadataSource { } /** - * The type of the source. Usually this is the fully qualified name of a - * class that defines configuration items. This class may or may not be - * available at runtime. + * The type of the source. Usually this is the fully qualified name of a class that + * defines configuration items. This class may or may not be available at runtime. + * @return the type */ public String getType() { return this.type; @@ -69,6 +69,7 @@ public class ConfigurationMetadataSource { /** * A description of this source, if any. Can be multi-lines. + * @return the description * @see #getShortDescription() */ public String getDescription() { @@ -81,10 +82,11 @@ public class ConfigurationMetadataSource { /** * A single-line, single-sentence description of this source, if any. + * @return the short description * @see #getDescription() */ public String getShortDescription() { - return shortDescription; + return this.shortDescription; } public void setShortDescription(String shortDescription) { @@ -92,8 +94,9 @@ public class ConfigurationMetadataSource { } /** - * The type where this source is defined. This can be identical - * to the {@link #getType() type} if the source is self-defined. + * The type where this source is defined. This can be identical to the + * {@link #getType() type} if the source is self-defined. + * @return the source type */ public String getSourceType() { return this.sourceType; @@ -105,6 +108,7 @@ public class ConfigurationMetadataSource { /** * The method name that defines this source, if any. + * @return the source method */ public String getSourceMethod() { return this.sourceMethod; @@ -116,6 +120,7 @@ public class ConfigurationMetadataSource { /** * Return the properties defined by this source. + * @return the properties */ public Map getProperties() { return this.properties; diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/DescriptionExtractor.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/DescriptionExtractor.java new file mode 100644 index 0000000000..d59745ce38 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/DescriptionExtractor.java @@ -0,0 +1,58 @@ +/* + * Copyright 2012-2015 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.configurationmetadata; + +import java.text.BreakIterator; +import java.util.Locale; + +/** + * Utility to extract a descriptions. + * + * @author Stephane Nicoll + */ +class DescriptionExtractor { + + private static final String NEW_LINE = System.getProperty("line.separator"); + + public String getShortDescription(String description) { + if (description == null) { + return null; + } + int dot = description.indexOf("."); + if (dot != -1) { + BreakIterator breakIterator = BreakIterator.getSentenceInstance(Locale.US); + breakIterator.setText(description); + String text = description.substring(breakIterator.first(), + breakIterator.next()).trim(); + return removeSpaceBetweenLine(text); + } + else { + String[] lines = description.split(NEW_LINE); + return lines[0].trim(); + } + } + + private String removeSpaceBetweenLine(String text) { + String[] lines = text.split(NEW_LINE); + StringBuilder sb = new StringBuilder(); + for (String line : lines) { + sb.append(line.trim()).append(" "); + } + return sb.toString().trim(); + } + +} 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 3921b4201c..17867aa687 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -20,11 +20,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; -import java.text.BreakIterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.Locale; import org.json.JSONArray; import org.json.JSONObject; @@ -39,9 +37,10 @@ class JsonReader { private static final int BUFFER_SIZE = 4096; - private static final String NEW_LINE = System.getProperty("line.separator"); + private final DescriptionExtractor descriptionExtractor = new DescriptionExtractor(); - public RawConfigurationMetadata read(InputStream in, Charset charset) throws IOException { + public RawConfigurationMetadata read(InputStream in, Charset charset) + throws IOException { JSONObject json = readJson(in, charset); List groups = parseAllSources(json); List items = parseAllItems(json); @@ -94,7 +93,8 @@ class JsonReader { source.setType(json.optString("type", null)); String description = json.optString("description", null); source.setDescription(description); - source.setShortDescription(extractShortDescription(description)); + source.setShortDescription(this.descriptionExtractor + .getShortDescription(description)); source.setSourceType(json.optString("sourceType", null)); source.setSourceMethod(json.optString("sourceMethod", null)); return source; @@ -106,7 +106,8 @@ class JsonReader { item.setType(json.optString("type", null)); String description = json.optString("description", null); item.setDescription(description); - item.setShortDescription(extractShortDescription(description)); + item.setShortDescription(this.descriptionExtractor + .getShortDescription(description)); item.setDefaultValue(readItemValue(json.opt("defaultValue"))); item.setDeprecated(json.optBoolean("deprecated", false)); item.setSourceType(json.optString("sourceType", null)); @@ -125,7 +126,8 @@ class JsonReader { valueHint.setValue(readItemValue(value.get("value"))); String description = value.optString("description", null); valueHint.setDescription(description); - valueHint.setShortDescription(extractShortDescription(description)); + valueHint.setShortDescription(this.descriptionExtractor + .getShortDescription(description)); hint.getValueHints().add(valueHint); } } @@ -140,7 +142,8 @@ class JsonReader { Iterator keys = parameters.keys(); while (keys.hasNext()) { String key = (String) keys.next(); - valueProvider.getParameters().put(key, readItemValue(parameters.get(key))); + valueProvider.getParameters().put(key, + readItemValue(parameters.get(key))); } } hint.getValueProviders().add(valueProvider); @@ -161,32 +164,6 @@ class JsonReader { return value; } - static String extractShortDescription(String description) { - if (description == null) { - return null; - } - int dot = description.indexOf("."); - if (dot != -1) { - BreakIterator breakIterator = BreakIterator.getSentenceInstance(Locale.US); - breakIterator.setText(description); - String text = description.substring(breakIterator.first(), breakIterator.next()).trim(); - return removeSpaceBetweenLine(text); - } - else { - String[] lines = description.split(NEW_LINE); - return lines[0].trim(); - } - } - - private static String removeSpaceBetweenLine(String text) { - String[] lines = text.split(NEW_LINE); - StringBuilder sb = new StringBuilder(); - for (String line : lines) { - sb.append(line.trim()).append(" "); - } - return sb.toString().trim(); - } - private JSONObject readJson(InputStream in, Charset charset) throws IOException { try { StringBuilder out = new StringBuilder(); 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 0f3aa6ba01..7c148f6510 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-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -61,7 +61,7 @@ class RawConfigurationMetadata { } public List getHints() { - return hints; + return this.hints; } /** diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java index b8fbae69fb..8e807b5041 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/SimpleConfigurationMetadataRepository.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -27,7 +27,8 @@ import java.util.Map; * @author Stephane Nicoll * @since 1.3.0 */ -public class SimpleConfigurationMetadataRepository implements ConfigurationMetadataRepository { +public class SimpleConfigurationMetadataRepository implements + ConfigurationMetadataRepository { private final Map allGroups = new HashMap(); @@ -47,6 +48,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad /** * Register the specified {@link ConfigurationMetadataSource sources}. + * @param sources the sources to add */ public void add(Collection sources) { for (ConfigurationMetadataSource source : sources) { @@ -64,19 +66,22 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad } /** - * Add a {@link ConfigurationMetadataProperty} with the {@link ConfigurationMetadataSource source} - * that defines it, if any. + * Add a {@link ConfigurationMetadataProperty} with the + * {@link ConfigurationMetadataSource source} that defines it, if any. + * @param property the property to add + * @param source the source */ - public void add(ConfigurationMetadataProperty property, ConfigurationMetadataSource source) { + public void add(ConfigurationMetadataProperty property, + ConfigurationMetadataSource source) { if (source != null) { putIfAbsent(source.getProperties(), property.getId(), property); } putIfAbsent(getGroup(source).getProperties(), property.getId(), property); } - /** * Merge the content of the specified repository to this repository. + * @param repository the repository to include */ public void include(ConfigurationMetadataRepository repository) { for (ConfigurationMetadataGroup group : repository.getAllGroups().values()) { @@ -86,12 +91,16 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad } else { // Merge properties - for (Map.Entry entry : group.getProperties().entrySet()) { - putIfAbsent(existingGroup.getProperties(), entry.getKey(), entry.getValue()); + for (Map.Entry entry : group + .getProperties().entrySet()) { + putIfAbsent(existingGroup.getProperties(), entry.getKey(), + entry.getValue()); } // Merge sources - for (Map.Entry entry : group.getSources().entrySet()) { - putIfAbsent(existingGroup.getSources(), entry.getKey(), entry.getValue()); + for (Map.Entry entry : group + .getSources().entrySet()) { + putIfAbsent(existingGroup.getSources(), entry.getKey(), + entry.getValue()); } } } @@ -107,9 +116,7 @@ public class SimpleConfigurationMetadataRepository implements ConfigurationMetad } return rootGroup; } - else { - return this.allGroups.get(source.getGroupId()); - } + return this.allGroups.get(source.getGroupId()); } private void putIfAbsent(Map map, String key, V value) { diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueHint.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueHint.java index bb457a9375..8217c2a66a 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueHint.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueHint.java @@ -1,8 +1,24 @@ +/* + * Copyright 2012-2015 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.configurationmetadata; /** - * Hint for a value a given property may have. Provide the value and - * an optional description. + * Hint for a value a given property may have. Provide the value and an optional + * description. * * @author Stephane Nicoll * @since 1.3.0 @@ -17,9 +33,10 @@ public class ValueHint { /** * Return the hint value. + * @return the value */ public Object getValue() { - return value; + return this.value; } public void setValue(Object value) { @@ -28,10 +45,11 @@ public class ValueHint { /** * A description of this value, if any. Can be multi-lines. + * @return the description * @see #getShortDescription() */ public String getDescription() { - return description; + return this.description; } public void setDescription(String description) { @@ -40,10 +58,11 @@ public class ValueHint { /** * A single-line, single-sentence description of this hint, if any. + * @return the short description * @see #getDescription() */ public String getShortDescription() { - return shortDescription; + return this.shortDescription; } public void setShortDescription(String shortDescription) { diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueProvider.java b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueProvider.java index 2475931e2d..1de23f3ad3 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueProvider.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/main/java/org/springframework/boot/configurationmetadata/ValueProvider.java @@ -1,3 +1,19 @@ +/* + * Copyright 2012-2015 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.configurationmetadata; import java.util.LinkedHashMap; @@ -6,9 +22,9 @@ import java.util.Map; /** * Define a component that is able to provide the values of a property. *

- * Each provider is defined by a {@code name} and can have an arbitrary - * number of {@code parameters}. The available providers are defined in - * the Spring Boot documentation. + * Each provider is defined by a {@code name} and can have an arbitrary number of + * {@code parameters}. The available providers are defined in the Spring Boot + * documentation. * * @author Stephane Nicoll * @since 1.3.0 @@ -21,9 +37,10 @@ public class ValueProvider { /** * Return the name of the provider. + * @return the name */ public String getName() { - return name; + return this.name; } public void setName(String name) { @@ -32,15 +49,16 @@ public class ValueProvider { /** * Return the parameters. + * @return the parameters */ public Map getParameters() { - return parameters; + return this.parameters; } @Override public String toString() { - return "ValueProvider{" + "name='" + this.name + ", parameters=" + this.parameters + - '}'; + return "ValueProvider{" + "name='" + this.name + ", parameters=" + + this.parameters + '}'; } } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/AbstractConfigurationMetadataTests.java b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/AbstractConfigurationMetadataTests.java index 9ca2bcb1dc..0f0c6c4062 100644 --- a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/AbstractConfigurationMetadataTests.java +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/AbstractConfigurationMetadataTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -21,7 +21,6 @@ import java.io.InputStream; import org.junit.Rule; import org.junit.rules.ExpectedException; - import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -29,6 +28,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; /** + * Base for configuration meta-data tests. * * @author Stephane Nicoll */ @@ -37,15 +37,16 @@ public abstract class AbstractConfigurationMetadataTests { @Rule public final ExpectedException thrown = ExpectedException.none(); - protected void assertSource(ConfigurationMetadataSource actual, String groupId, String type, String sourceType) { + protected void assertSource(ConfigurationMetadataSource actual, String groupId, + String type, String sourceType) { assertNotNull(actual); assertEquals(groupId, actual.getGroupId()); assertEquals(type, actual.getType()); assertEquals(sourceType, actual.getSourceType()); } - protected void assertProperty(ConfigurationMetadataProperty actual, String id, String name, - Class type, Object defaultValue) { + protected void assertProperty(ConfigurationMetadataProperty actual, String id, + String name, Class type, Object defaultValue) { assertNotNull(actual); assertEquals(id, actual.getId()); assertEquals(name, actual.getName()); @@ -60,7 +61,9 @@ public abstract class AbstractConfigurationMetadataTests { } protected InputStream getInputStreamFor(String name) throws IOException { - Resource r = new ClassPathResource("metadata/configuration-metadata-" + name + ".json"); + Resource r = new ClassPathResource("metadata/configuration-metadata-" + name + + ".json"); return r.getInputStream(); } + } 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 b9fe5090a8..7880a19455 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-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -31,12 +31,12 @@ import static org.junit.Assert.assertTrue; * * @author Stephane Nicoll */ -public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractConfigurationMetadataTests { - +public class ConfigurationMetadataRepositoryJsonBuilderTests extends + AbstractConfigurationMetadataTests { @Test public void nullResource() throws IOException { - thrown.expect(IllegalArgumentException.class); + this.thrown.expect(IllegalArgumentException.class); ConfigurationMetadataRepositoryJsonBuilder.create().withJsonResource(null); } @@ -44,13 +44,12 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon public void simpleRepository() throws IOException { InputStream foo = getInputStreamFor("foo"); try { - ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create() - .withJsonResource(foo) - .build(); + ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder + .create(foo).build(); validateFoo(repo); assertEquals(1, repo.getAllGroups().size()); - - contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter"); + contains(repo.getAllProperties(), "spring.foo.name", + "spring.foo.description", "spring.foo.counter"); assertEquals(3, repo.getAllProperties().size()); } finally { @@ -63,16 +62,14 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon InputStream foo = getInputStreamFor("foo"); InputStream bar = getInputStreamFor("bar"); try { - ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create() - .withJsonResource(foo) - .withJsonResource(bar) - .build(); + ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder + .create(foo, bar).build(); validateFoo(repo); validateBar(repo); assertEquals(2, repo.getAllGroups().size()); - - contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter", - "spring.bar.name", "spring.bar.description", "spring.bar.counter"); + contains(repo.getAllProperties(), "spring.foo.name", + "spring.foo.description", "spring.foo.counter", "spring.bar.name", + "spring.bar.description", "spring.bar.counter"); assertEquals(6, repo.getAllProperties().size()); } finally { @@ -86,15 +83,14 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon InputStream foo = getInputStreamFor("foo"); InputStream root = getInputStreamFor("root"); try { - ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create() - .withJsonResource(foo) - .withJsonResource(root) - .build(); + ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder + .create(foo, root).build(); validateFoo(repo); assertEquals(2, repo.getAllGroups().size()); - contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter", - "spring.root.name", "spring.root2.name"); + contains(repo.getAllProperties(), "spring.foo.name", + "spring.foo.description", "spring.foo.counter", "spring.root.name", + "spring.root2.name"); assertEquals(5, repo.getAllProperties().size()); } finally { @@ -108,19 +104,19 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon InputStream foo = getInputStreamFor("foo"); InputStream foo2 = getInputStreamFor("foo2"); try { - ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder.create() - .withJsonResource(foo) - .withJsonResource(foo2) - .build(); + ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder + .create(foo, foo2).build(); assertEquals(1, repo.getAllGroups().size()); ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo"); - contains(group.getSources(), "org.acme.Foo", "org.acme.Foo2", "org.springframework.boot.FooProperties"); + contains(group.getSources(), "org.acme.Foo", "org.acme.Foo2", + "org.springframework.boot.FooProperties"); assertEquals(3, group.getSources().size()); - contains(group.getProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter", - "spring.foo.enabled", "spring.foo.type"); + contains(group.getProperties(), "spring.foo.name", "spring.foo.description", + "spring.foo.counter", "spring.foo.enabled", "spring.foo.type"); assertEquals(5, group.getProperties().size()); - contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description", "spring.foo.counter", - "spring.foo.enabled", "spring.foo.type"); + contains(repo.getAllProperties(), "spring.foo.name", + "spring.foo.description", "spring.foo.counter", "spring.foo.enabled", + "spring.foo.type"); assertEquals(5, repo.getAllProperties().size()); } finally { @@ -134,18 +130,15 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon InputStream foo = getInputStreamFor("foo"); InputStream bar = getInputStreamFor("bar"); try { - ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder.create(); - ConfigurationMetadataRepository firstRepo = builder - .withJsonResource(foo) + ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder + .create(); + ConfigurationMetadataRepository firstRepo = builder.withJsonResource(foo) .build(); validateFoo(firstRepo); - - ConfigurationMetadataRepository secondRepo = builder - .withJsonResource(bar) + ConfigurationMetadataRepository secondRepo = builder.withJsonResource(bar) .build(); validateFoo(secondRepo); validateBar(secondRepo); - // first repo not impacted by second build assertNotEquals(firstRepo, secondRepo); assertEquals(1, firstRepo.getAllGroups().size()); @@ -161,11 +154,13 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon private void validateFoo(ConfigurationMetadataRepository repo) { ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo"); - contains(group.getSources(), "org.acme.Foo", "org.springframework.boot.FooProperties"); + contains(group.getSources(), "org.acme.Foo", + "org.springframework.boot.FooProperties"); ConfigurationMetadataSource source = group.getSources().get("org.acme.Foo"); contains(source.getProperties(), "spring.foo.name", "spring.foo.description"); assertEquals(2, source.getProperties().size()); - ConfigurationMetadataSource source2 = group.getSources().get("org.springframework.boot.FooProperties"); + ConfigurationMetadataSource source2 = group.getSources().get( + "org.springframework.boot.FooProperties"); contains(source2.getProperties(), "spring.foo.name", "spring.foo.counter"); assertEquals(2, source2.getProperties().size()); validatePropertyHints(repo.getAllProperties().get("spring.foo.name"), 0, 0); @@ -175,11 +170,13 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon private void validateBar(ConfigurationMetadataRepository repo) { ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.bar"); - contains(group.getSources(), "org.acme.Bar", "org.springframework.boot.BarProperties"); + contains(group.getSources(), "org.acme.Bar", + "org.springframework.boot.BarProperties"); ConfigurationMetadataSource source = group.getSources().get("org.acme.Bar"); contains(source.getProperties(), "spring.bar.name", "spring.bar.description"); assertEquals(2, source.getProperties().size()); - ConfigurationMetadataSource source2 = group.getSources().get("org.springframework.boot.BarProperties"); + ConfigurationMetadataSource source2 = group.getSources().get( + "org.springframework.boot.BarProperties"); contains(source2.getProperties(), "spring.bar.name", "spring.bar.counter"); assertEquals(2, source2.getProperties().size()); validatePropertyHints(repo.getAllProperties().get("spring.bar.name"), 0, 0); @@ -187,14 +184,17 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests extends AbstractCon validatePropertyHints(repo.getAllProperties().get("spring.bar.counter"), 0, 0); } - private void validatePropertyHints(ConfigurationMetadataProperty property, int valueHints, int valueProviders) { + private void validatePropertyHints(ConfigurationMetadataProperty property, + int valueHints, int valueProviders) { assertEquals(valueHints, property.getValueHints().size()); assertEquals(valueProviders, property.getValueHints().size()); } private void contains(Map source, String... keys) { for (String key : keys) { - assertTrue("Item '" + key + "' not found. Got " + source.keySet(), source.containsKey(key)); + assertTrue("Item '" + key + "' not found. Got " + source.keySet(), + source.containsKey(key)); } } + } diff --git a/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/DescriptionExtractorTests.java b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/DescriptionExtractorTests.java new file mode 100644 index 0000000000..ab5e698398 --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-metadata/src/test/java/org/springframework/boot/configurationmetadata/DescriptionExtractorTests.java @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2015 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.configurationmetadata; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Tests for {@link DescriptionExtractor}. + * + * @author Stephane Nicoll + */ +public class DescriptionExtractorTests { + + private DescriptionExtractor extractor = new DescriptionExtractor(); + + @Test + public void extractShortDescription() { + assertEquals("My short description.", + this.extractor.getShortDescription("My short description. More stuff.")); + } + + @Test + public void extractShortDescriptionNewLineBeforeDot() { + assertEquals("My short description.", + this.extractor.getShortDescription("My short\ndescription.\nMore stuff.")); + } + + @Test + public void extractShortDescriptionNewLineBeforeDotWithSpaces() { + assertEquals("My short description.", + this.extractor + .getShortDescription("My short \n description. \nMore stuff.")); + } + + @Test + public void extractShortDescriptionNoDot() { + assertEquals("My short description", + this.extractor.getShortDescription("My short description")); + } + + @Test + public void extractShortDescriptionNoDotMultipleLines() { + assertEquals("My short description", + this.extractor.getShortDescription("My short description \n More stuff")); + } + + @Test + public void extractShortDescriptionNull() { + assertEquals(null, this.extractor.getShortDescription(null)); + } + +} 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 539b30e9b2..c099c17fb7 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2015 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. @@ -46,7 +46,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { @Test public void invalidMetadata() throws IOException { - thrown.expect(JSONException.class); + this.thrown.expect(JSONException.class); readFor("invalid"); } @@ -70,7 +70,8 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { assertProperty(item, "spring.foo.name", "name", String.class, null); assertItem(item, "org.acme.Foo"); ConfigurationMetadataItem item2 = items.get(1); - assertProperty(item2, "spring.foo.description", "description", String.class, "FooBar"); + assertProperty(item2, "spring.foo.description", "description", String.class, + "FooBar"); assertEquals("Foo description.", item2.getDescription()); assertEquals("Foo description.", item2.getShortDescription()); assertNull(item2.getSourceMethod()); @@ -125,46 +126,10 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests { assertEquals(0, sources.size()); List items = rawMetadata.getItems(); assertEquals(2, items.size()); - ConfigurationMetadataItem item = items.get(0); assertProperty(item, "spring.root.name", "spring.root.name", String.class, null); } - @Test - public void extractShortDescription() { - assertEquals("My short description.", JsonReader.extractShortDescription( - "My short description. More stuff.")); - } - - @Test - public void extractShortDescriptionNewLineBeforeDot() { - assertEquals("My short description.", JsonReader.extractShortDescription( - "My short\ndescription.\nMore stuff.")); - } - - @Test - public void extractShortDescriptionNewLineBeforeDotWithSpaces() { - assertEquals("My short description.", JsonReader.extractShortDescription( - "My short \n description. \nMore stuff.")); - } - - @Test - public void extractShortDescriptionNoDot() { - assertEquals("My short description", JsonReader.extractShortDescription( - "My short description")); - } - - @Test - public void extractShortDescriptionNoDotMultipleLines() { - assertEquals("My short description", JsonReader.extractShortDescription( - "My short description \n More stuff")); - } - - @Test - public void extractShortDescriptionNull() { - assertEquals(null, JsonReader.extractShortDescription(null)); - } - RawConfigurationMetadata readFor(String path) throws IOException { return this.reader.read(getInputStreamFor(path), DEFAULT_CHARSET); } diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemHint.java b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemHint.java index 492902b091..3eccec00b1 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemHint.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/metadata/ItemHint.java @@ -136,8 +136,8 @@ public class ItemHint implements Comparable { @Override public String toString() { - return "ValueProvider{" + "name='" + this.name + "', parameters=" + this.parameters - + '}'; + return "ValueProvider{" + "name='" + this.name + "', parameters=" + + this.parameters + '}'; } } 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 c76c1c3eac..6649f20093 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 @@ -96,37 +96,50 @@ public class JsonMarshaller { JSONObject jsonObject = new JSONOrderedObject(); jsonObject.put("name", hint.getName()); if (!hint.getValues().isEmpty()) { - JSONArray valuesArray = new JSONArray(); - for (ItemHint.ValueHint valueHint : hint.getValues()) { - JSONObject valueObject = new JSONOrderedObject(); - putHintValue(valueObject, valueHint.getValue()); - putIfPresent(valueObject, "description", valueHint.getDescription()); - valuesArray.put(valueObject); - } - jsonObject.put("values", valuesArray); + jsonObject.put("values", getItemHintValues(hint)); } if (!hint.getProviders().isEmpty()) { - JSONArray providersArray = new JSONArray(); - for (ItemHint.ValueProvider valueProvider : hint.getProviders()) { - JSONObject valueProviderObject = new JSONOrderedObject(); - valueProviderObject.put("name", valueProvider.getName()); - if (valueProvider.getParameters() != null - && !valueProvider.getParameters().isEmpty()) { - JSONObject parametersObject = new JSONOrderedObject(); - for (Map.Entry entry : valueProvider.getParameters() - .entrySet()) { - parametersObject.put(entry.getKey(), - extractItemValue(entry.getValue())); - } - valueProviderObject.put("parameters", parametersObject); - } - providersArray.put(valueProviderObject); - } - jsonObject.put("providers", providersArray); + 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); 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 d2b6663181..dd793ce8c0 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 @@ -366,7 +366,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { writeAdditionalHints(ItemHint.newHint("simple.the-name", new ItemHint.ValueHint( "boot", "Bla bla"), new ItemHint.ValueHint("spring", null))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, + assertThat( + metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") @@ -381,7 +382,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { writeAdditionalHints(ItemHint.newHint("simple.theName", new ItemHint.ValueHint( "boot", "Bla bla"))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, + assertThat( + metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.") @@ -398,7 +400,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { . singletonMap("target", "org.foo")), new ItemHint.ValueProvider("second", null)))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, + assertThat( + metadata, containsProperty("simple.the-name", String.class) .fromSource(SimpleProperties.class) .withDescription("The name of this simple properties.")