This commit is contained in:
Phillip Webb
2015-07-13 11:05:46 -07:00
parent 6fdcdd888b
commit 9ebe15232e
24 changed files with 514 additions and 309 deletions

View File

@@ -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();
}
}

View File

@@ -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<String, ?> 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));
}
}
}

View File

@@ -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));
}
}

View File

@@ -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<ConfigurationMetadataItem> 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);
}