Add access to short deprecation reason

Closes gh-11770
This commit is contained in:
Stephane Nicoll 2018-01-25 12:47:54 +01:00
parent 4c23afdcd8
commit 43bac617d4
7 changed files with 117 additions and 92 deletions

View File

@ -76,8 +76,8 @@ class PropertiesMigrationReport {
+ "supported was found in the environment:%n%n"));
append(report, content,
(metadata) -> "Reason: "
+ (StringUtils.hasText(metadata.getDeprecation().getReason())
? metadata.getDeprecation().getReason() : "none"));
+ (StringUtils.hasText(metadata.getDeprecation().getShortReason())
? metadata.getDeprecation().getShortReason() : "none"));
report.append(String.format("%n"));
report.append("Please refer to the migration guide or reference guide for "
+ "potential alternatives.");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -32,6 +32,8 @@ public class Deprecation implements Serializable {
private String reason;
private String shortReason;
private String replacement;
/**
@ -49,6 +51,7 @@ public class Deprecation implements Serializable {
/**
* A reason why the related property is deprecated, if any. Can be multi-lines.
* @return the deprecation reason
* @see #getShortReason()
*/
public String getReason() {
return this.reason;
@ -58,6 +61,20 @@ public class Deprecation implements Serializable {
this.reason = reason;
}
/**
* A single-line, single-sentence reason why the related property is deprecated, if
* any.
* @return the short deprecation reason
* @see #getShortReason()
*/
public String getShortReason() {
return this.shortReason;
}
public void setShortReason(String shortReason) {
this.shortReason = shortReason;
}
/**
* The full name of the property that replaces the related deprecated property, if
* any.

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -37,7 +37,7 @@ class JsonReader {
private static final int BUFFER_SIZE = 4096;
private final DescriptionExtractor descriptionExtractor = new DescriptionExtractor();
private final SentenceExtractor sentenceExtractor = new SentenceExtractor();
public RawConfigurationMetadata read(InputStream in, Charset charset)
throws IOException {
@ -108,7 +108,7 @@ class JsonReader {
String description = json.optString("description", null);
source.setDescription(description);
source.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
this.sentenceExtractor.getFirstSentence(description));
source.setSourceType(json.optString("sourceType", null));
source.setSourceMethod(json.optString("sourceMethod", null));
return source;
@ -121,7 +121,7 @@ class JsonReader {
String description = json.optString("description", null);
item.setDescription(description);
item.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
this.sentenceExtractor.getFirstSentence(description));
item.setDefaultValue(readItemValue(json.opt("defaultValue")));
item.setDeprecation(parseDeprecation(json));
item.setSourceType(json.optString("sourceType", null));
@ -141,7 +141,7 @@ class JsonReader {
String description = value.optString("description", null);
valueHint.setDescription(description);
valueHint.setShortDescription(
this.descriptionExtractor.getShortDescription(description));
this.sentenceExtractor.getFirstSentence(description));
hint.getValueHints().add(valueHint);
}
}
@ -172,7 +172,10 @@ class JsonReader {
Deprecation deprecation = new Deprecation();
deprecation.setLevel(parseDeprecationLevel(
deprecationJsonObject.optString("level", null)));
deprecation.setReason(deprecationJsonObject.optString("reason", null));
String reason = deprecationJsonObject.optString("reason", null);
deprecation.setReason(reason);
deprecation.setShortReason(
this.sentenceExtractor.getFirstSentence(reason));
deprecation
.setReplacement(deprecationJsonObject.optString("replacement", null));
return deprecation;

View File

@ -20,26 +20,26 @@ import java.text.BreakIterator;
import java.util.Locale;
/**
* Utility to extract a description.
* Utility to extract the first sentence of a text.
*
* @author Stephane Nicoll
*/
class DescriptionExtractor {
class SentenceExtractor {
public String getShortDescription(String description) {
if (description == null) {
public String getFirstSentence(String text) {
if (text == null) {
return null;
}
int dot = description.indexOf('.');
int dot = text.indexOf('.');
if (dot != -1) {
BreakIterator breakIterator = BreakIterator.getSentenceInstance(Locale.US);
breakIterator.setText(description);
String text = description
breakIterator.setText(text);
String sentence = text
.substring(breakIterator.first(), breakIterator.next()).trim();
return removeSpaceBetweenLine(text);
return removeSpaceBetweenLine(sentence);
}
else {
String[] lines = description.split(System.lineSeparator());
String[] lines = text.split(System.lineSeparator());
return lines[0].trim();
}
}

View File

@ -1,73 +0,0 @@
/*
* Copyright 2012-2018 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.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DescriptionExtractor}.
*
* @author Stephane Nicoll
*/
public class DescriptionExtractorTests {
private static final String NEW_LINE = System.lineSeparator();
private DescriptionExtractor extractor = new DescriptionExtractor();
@Test
public void extractShortDescription() {
String description = this.extractor
.getShortDescription("My short " + "description. More stuff.");
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNewLineBeforeDot() {
String description = this.extractor.getShortDescription(
"My short" + NEW_LINE + "description." + NEW_LINE + "More stuff.");
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNewLineBeforeDotWithSpaces() {
String description = this.extractor.getShortDescription(
"My short " + NEW_LINE + " description. " + NEW_LINE + "More stuff.");
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNoDot() {
String description = this.extractor.getShortDescription("My short description");
assertThat(description).isEqualTo("My short description");
}
@Test
public void extractShortDescriptionNoDotMultipleLines() {
String description = this.extractor
.getShortDescription("My short description " + NEW_LINE + " More stuff");
assertThat(description).isEqualTo("My short description");
}
@Test
public void extractShortDescriptionNull() {
assertThat(this.extractor.getShortDescription(null)).isEqualTo(null);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 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.
@ -156,6 +156,8 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
assertThat(item.isDeprecated()).isTrue();
assertThat(item.getDeprecation().getReason())
.isEqualTo("Server namespace has moved to spring.server");
assertThat(item.getDeprecation().getShortReason())
.isEqualTo("Server namespace has moved to spring.server");
assertThat(item.getDeprecation().getReplacement())
.isEqualTo("server.spring.port");
assertThat(item.getDeprecation().getLevel()).isEqualTo(Deprecation.Level.WARNING);
@ -165,6 +167,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
null);
assertThat(item2.isDeprecated()).isTrue();
assertThat(item2.getDeprecation().getReason()).isNull();
assertThat(item2.getDeprecation().getShortReason()).isNull();
assertThat(item2.getDeprecation().getReplacement()).isNull();
assertThat(item.getDeprecation().getLevel()).isEqualTo(Deprecation.Level.WARNING);
@ -179,6 +182,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
null);
assertThat(item4.isDeprecated()).isTrue();
assertThat(item4.getDeprecation().getReason()).isNull();
assertThat(item2.getDeprecation().getShortReason()).isNull();
assertThat(item4.getDeprecation().getReplacement())
.isEqualTo("spring.server.name");
assertThat(item4.getDeprecation().getLevel()).isEqualTo(Deprecation.Level.ERROR);
@ -188,6 +192,7 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
null);
assertThat(item5.isDeprecated()).isTrue();
assertThat(item5.getDeprecation().getReason()).isNull();
assertThat(item2.getDeprecation().getShortReason()).isNull();
assertThat(item5.getDeprecation().getReplacement())
.isEqualTo("spring.server.name");
assertThat(item5.getDeprecation().getLevel())

View File

@ -0,0 +1,73 @@
/*
* Copyright 2012-2018 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.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SentenceExtractor}.
*
* @author Stephane Nicoll
*/
public class SentenceExtractorTests {
private static final String NEW_LINE = System.lineSeparator();
private SentenceExtractor extractor = new SentenceExtractor();
@Test
public void extractFirstSentence() {
String sentence = this.extractor
.getFirstSentence("My short " + "description. More stuff.");
assertThat(sentence).isEqualTo("My short description.");
}
@Test
public void extractFirstSentenceNewLineBeforeDot() {
String sentence = this.extractor.getFirstSentence(
"My short" + NEW_LINE + "description." + NEW_LINE + "More stuff.");
assertThat(sentence).isEqualTo("My short description.");
}
@Test
public void extractFirstSentenceNewLineBeforeDotWithSpaces() {
String sentence = this.extractor.getFirstSentence(
"My short " + NEW_LINE + " description. " + NEW_LINE + "More stuff.");
assertThat(sentence).isEqualTo("My short description.");
}
@Test
public void extractFirstSentenceNoDot() {
String sentence = this.extractor.getFirstSentence("My short description");
assertThat(sentence).isEqualTo("My short description");
}
@Test
public void extractFirstSentenceNoDotMultipleLines() {
String sentence = this.extractor
.getFirstSentence("My short description " + NEW_LINE + " More stuff");
assertThat(sentence).isEqualTo("My short description");
}
@Test
public void extractFirstSentenceNull() {
assertThat(this.extractor.getFirstSentence(null)).isEqualTo(null);
}
}