Use AssertJ in spring-boot-tools

See gh-5083
This commit is contained in:
Phillip Webb
2016-02-06 14:53:00 -08:00
parent 7f9358f4d8
commit 00cfe1d054
37 changed files with 1326 additions and 1402 deletions

View File

@@ -25,8 +25,7 @@ import org.junit.rules.ExpectedException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Base for configuration meta-data tests.
@@ -40,25 +39,25 @@ public abstract class AbstractConfigurationMetadataTests {
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());
assertThat(actual).isNotNull();
assertThat(actual.getGroupId()).isEqualTo(groupId);
assertThat(actual.getType()).isEqualTo(type);
assertThat(actual.getSourceType()).isEqualTo(sourceType);
}
protected void assertProperty(ConfigurationMetadataProperty actual, String id,
String name, Class<?> type, Object defaultValue) {
assertNotNull(actual);
assertEquals(id, actual.getId());
assertEquals(name, actual.getName());
assertThat(actual).isNotNull();
assertThat(actual.getId()).isEqualTo(id);
assertThat(actual.getName()).isEqualTo(name);
String typeName = type != null ? type.getName() : null;
assertEquals(typeName, actual.getType());
assertEquals(defaultValue, actual.getDefaultValue());
assertThat(actual.getType()).isEqualTo(typeName);
assertThat(actual.getDefaultValue()).isEqualTo(defaultValue);
}
protected void assertItem(ConfigurationMetadataItem actual, String sourceType) {
assertNotNull(actual);
assertEquals(sourceType, actual.getSourceType());
assertThat(actual).isNotNull();
assertThat(actual.getSourceType()).isEqualTo(sourceType);
}
protected InputStream getInputStreamFor(String name) throws IOException {

View File

@@ -22,9 +22,7 @@ import java.util.Map;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ConfigurationMetadataRepository}.
@@ -47,10 +45,10 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
.create(foo).build();
validateFoo(repo);
assertEquals(1, repo.getAllGroups().size());
assertThat(repo.getAllGroups()).hasSize(1);
contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description",
"spring.foo.counter");
assertEquals(3, repo.getAllProperties().size());
assertThat(repo.getAllProperties()).hasSize(3);
}
finally {
foo.close();
@@ -66,11 +64,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
.create(foo, bar).build();
validateFoo(repo);
validateBar(repo);
assertEquals(2, repo.getAllGroups().size());
assertThat(repo.getAllGroups()).hasSize(2);
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());
assertThat(repo.getAllProperties()).hasSize(6);
}
finally {
foo.close();
@@ -86,11 +84,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
.create(foo, root).build();
validateFoo(repo);
assertEquals(2, repo.getAllGroups().size());
assertThat(repo.getAllGroups()).hasSize(2);
contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description",
"spring.foo.counter", "spring.root.name", "spring.root2.name");
assertEquals(5, repo.getAllProperties().size());
assertThat(repo.getAllProperties()).hasSize(5);
}
finally {
foo.close();
@@ -105,17 +103,17 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
try {
ConfigurationMetadataRepository repo = ConfigurationMetadataRepositoryJsonBuilder
.create(foo, foo2).build();
assertEquals(1, repo.getAllGroups().size());
assertThat(repo.getAllGroups()).hasSize(1);
ConfigurationMetadataGroup group = repo.getAllGroups().get("spring.foo");
contains(group.getSources(), "org.acme.Foo", "org.acme.Foo2",
"org.springframework.boot.FooProperties");
assertEquals(3, group.getSources().size());
assertThat(group.getSources()).hasSize(3);
contains(group.getProperties(), "spring.foo.name", "spring.foo.description",
"spring.foo.counter", "spring.foo.enabled", "spring.foo.type");
assertEquals(5, group.getProperties().size());
assertThat(group.getProperties()).hasSize(5);
contains(repo.getAllProperties(), "spring.foo.name", "spring.foo.description",
"spring.foo.counter", "spring.foo.enabled", "spring.foo.type");
assertEquals(5, repo.getAllProperties().size());
assertThat(repo.getAllProperties()).hasSize(5);
}
finally {
foo.close();
@@ -138,11 +136,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
validateFoo(secondRepo);
validateBar(secondRepo);
// first repo not impacted by second build
assertNotEquals(firstRepo, secondRepo);
assertEquals(1, firstRepo.getAllGroups().size());
assertEquals(3, firstRepo.getAllProperties().size());
assertEquals(2, secondRepo.getAllGroups().size());
assertEquals(6, secondRepo.getAllProperties().size());
assertThat(secondRepo).isNotEqualTo(firstRepo);
assertThat(firstRepo.getAllGroups()).hasSize(1);
assertThat(firstRepo.getAllProperties()).hasSize(3);
assertThat(secondRepo.getAllGroups()).hasSize(2);
assertThat(secondRepo.getAllProperties()).hasSize(6);
}
finally {
foo.close();
@@ -156,11 +154,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
"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());
assertThat(source.getProperties()).hasSize(2);
ConfigurationMetadataSource source2 = group.getSources()
.get("org.springframework.boot.FooProperties");
contains(source2.getProperties(), "spring.foo.name", "spring.foo.counter");
assertEquals(2, source2.getProperties().size());
assertThat(source2.getProperties()).hasSize(2);
validatePropertyHints(repo.getAllProperties().get("spring.foo.name"), 0, 0);
validatePropertyHints(repo.getAllProperties().get("spring.foo.description"), 0,
0);
@@ -173,11 +171,11 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
"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());
assertThat(source.getProperties()).hasSize(2);
ConfigurationMetadataSource source2 = group.getSources()
.get("org.springframework.boot.BarProperties");
contains(source2.getProperties(), "spring.bar.name", "spring.bar.counter");
assertEquals(2, source2.getProperties().size());
assertThat(source2.getProperties()).hasSize(2);
validatePropertyHints(repo.getAllProperties().get("spring.bar.name"), 0, 0);
validatePropertyHints(repo.getAllProperties().get("spring.bar.description"), 2,
2);
@@ -186,14 +184,13 @@ public class ConfigurationMetadataRepositoryJsonBuilderTests
private void validatePropertyHints(ConfigurationMetadataProperty property,
int valueHints, int valueProviders) {
assertEquals(valueHints, property.getValueHints().size());
assertEquals(valueProviders, property.getValueHints().size());
assertThat(property.getValueHints().size()).isEqualTo(valueHints);
assertThat(property.getValueHints().size()).isEqualTo(valueProviders);
}
private void contains(Map<String, ?> source, String... keys) {
for (String key : keys) {
assertTrue("Item '" + key + "' not found. Got " + source.keySet(),
source.containsKey(key));
assertThat(source).containsKey(key);
}
}

View File

@@ -18,7 +18,7 @@ package org.springframework.boot.configurationmetadata;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link DescriptionExtractor}.
@@ -35,39 +35,39 @@ public class DescriptionExtractorTests {
public void extractShortDescription() {
String description = this.extractor
.getShortDescription("My short " + "description. More stuff.");
assertEquals("My short description.", description);
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNewLineBeforeDot() {
String description = this.extractor.getShortDescription(
"My short" + NEW_LINE + "description." + NEW_LINE + "More stuff.");
assertEquals("My short description.", description);
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNewLineBeforeDotWithSpaces() {
String description = this.extractor.getShortDescription(
"My short " + NEW_LINE + " description. " + NEW_LINE + "More stuff.");
assertEquals("My short description.", description);
assertThat(description).isEqualTo("My short description.");
}
@Test
public void extractShortDescriptionNoDot() {
String description = this.extractor.getShortDescription("My short description");
assertEquals("My short description", description);
assertThat(description).isEqualTo("My short description");
}
@Test
public void extractShortDescriptionNoDotMultipleLines() {
String description = this.extractor
.getShortDescription("My short description " + NEW_LINE + " More stuff");
assertEquals("My short description", description);
assertThat(description).isEqualTo("My short description");
}
@Test
public void extractShortDescriptionNull() {
assertEquals(null, this.extractor.getShortDescription(null));
assertThat(this.extractor.getShortDescription(null)).isEqualTo(null);
}
}

View File

@@ -23,10 +23,7 @@ import java.util.List;
import org.json.JSONException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link JsonReader}
@@ -42,8 +39,8 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
@Test
public void emptyMetadata() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("empty");
assertEquals(0, rawMetadata.getSources().size());
assertEquals(0, rawMetadata.getItems().size());
assertThat(rawMetadata.getSources()).isEmpty();
assertThat(rawMetadata.getItems()).isEmpty();
}
@Test
@@ -56,17 +53,17 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
public void simpleMetadata() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("foo");
List<ConfigurationMetadataSource> sources = rawMetadata.getSources();
assertEquals(2, sources.size());
assertThat(sources).hasSize(2);
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertEquals(4, items.size());
assertThat(items).hasSize(4);
List<ConfigurationMetadataHint> hints = rawMetadata.getHints();
assertEquals(1, hints.size());
assertThat(hints).hasSize(1);
ConfigurationMetadataSource source = sources.get(0);
assertSource(source, "spring.foo", "org.acme.Foo", "org.acme.config.FooApp");
assertEquals("foo()", source.getSourceMethod());
assertEquals("This is Foo.", source.getDescription());
assertEquals("This is Foo.", source.getShortDescription());
assertThat(source.getSourceMethod()).isEqualTo("foo()");
assertThat(source.getDescription()).isEqualTo("This is Foo.");
assertThat(source.getShortDescription()).isEqualTo("This is Foo.");
ConfigurationMetadataItem item = items.get(0);
assertProperty(item, "spring.foo.name", "name", String.class, null);
@@ -74,61 +71,62 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
ConfigurationMetadataItem item2 = items.get(1);
assertProperty(item2, "spring.foo.description", "description", String.class,
"FooBar");
assertEquals("Foo description.", item2.getDescription());
assertEquals("Foo description.", item2.getShortDescription());
assertNull(item2.getSourceMethod());
assertThat(item2.getDescription()).isEqualTo("Foo description.");
assertThat(item2.getShortDescription()).isEqualTo("Foo description.");
assertThat(item2.getSourceMethod()).isNull();
assertItem(item2, "org.acme.Foo");
ConfigurationMetadataHint hint = hints.get(0);
assertEquals("spring.foo.counter", hint.getId());
assertEquals(1, hint.getValueHints().size());
assertThat(hint.getId()).isEqualTo("spring.foo.counter");
assertThat(hint.getValueHints()).hasSize(1);
ValueHint valueHint = hint.getValueHints().get(0);
assertEquals(42, valueHint.getValue());
assertEquals("Because that's the answer to any question, choose it. \nReally.",
valueHint.getDescription());
assertEquals("Because that's the answer to any question, choose it.",
valueHint.getShortDescription());
assertEquals(1, hint.getValueProviders().size());
assertThat(valueHint.getValue()).isEqualTo(42);
assertThat(valueHint.getDescription()).isEqualTo(
"Because that's the answer to any question, choose it. \nReally.");
assertThat(valueHint.getShortDescription())
.isEqualTo("Because that's the answer to any question, choose it.");
assertThat(hint.getValueProviders()).hasSize(1);
ValueProvider valueProvider = hint.getValueProviders().get(0);
assertEquals("handle-as", valueProvider.getName());
assertEquals(1, valueProvider.getParameters().size());
assertEquals(Integer.class.getName(),
valueProvider.getParameters().get("target"));
assertThat(valueProvider.getName()).isEqualTo("handle-as");
assertThat(valueProvider.getParameters()).hasSize(1);
assertThat(valueProvider.getParameters().get("target"))
.isEqualTo(Integer.class.getName());
}
@Test
public void metadataHints() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("bar");
List<ConfigurationMetadataHint> hints = rawMetadata.getHints();
assertEquals(1, hints.size());
assertThat(hints).hasSize(1);
ConfigurationMetadataHint hint = hints.get(0);
assertEquals("spring.bar.description", hint.getId());
assertEquals(2, hint.getValueHints().size());
assertThat(hint.getId()).isEqualTo("spring.bar.description");
assertThat(hint.getValueHints()).hasSize(2);
ValueHint valueHint = hint.getValueHints().get(0);
assertEquals("one", valueHint.getValue());
assertEquals("One.", valueHint.getDescription());
assertThat(valueHint.getValue()).isEqualTo("one");
assertThat(valueHint.getDescription()).isEqualTo("One.");
ValueHint valueHint2 = hint.getValueHints().get(1);
assertEquals("two", valueHint2.getValue());
assertEquals(null, valueHint2.getDescription());
assertThat(valueHint2.getValue()).isEqualTo("two");
assertThat(valueHint2.getDescription()).isEqualTo(null);
assertEquals(2, hint.getValueProviders().size());
assertThat(hint.getValueProviders()).hasSize(2);
ValueProvider valueProvider = hint.getValueProviders().get(0);
assertEquals("handle-as", valueProvider.getName());
assertEquals(1, valueProvider.getParameters().size());
assertEquals(String.class.getName(), valueProvider.getParameters().get("target"));
assertThat(valueProvider.getName()).isEqualTo("handle-as");
assertThat(valueProvider.getParameters()).hasSize(1);
assertThat(valueProvider.getParameters().get("target"))
.isEqualTo(String.class.getName());
ValueProvider valueProvider2 = hint.getValueProviders().get(1);
assertEquals("any", valueProvider2.getName());
assertEquals(0, valueProvider2.getParameters().size());
assertThat(valueProvider2.getName()).isEqualTo("any");
assertThat(valueProvider2.getParameters()).isEmpty();
}
@Test
public void rootMetadata() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("root");
List<ConfigurationMetadataSource> sources = rawMetadata.getSources();
assertEquals(0, sources.size());
assertThat(sources).isEmpty();
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertEquals(2, items.size());
assertThat(items).hasSize(2);
ConfigurationMetadataItem item = items.get(0);
assertProperty(item, "spring.root.name", "spring.root.name", String.class, null);
}
@@ -137,27 +135,28 @@ public class JsonReaderTests extends AbstractConfigurationMetadataTests {
public void deprecatedMetadata() throws IOException {
RawConfigurationMetadata rawMetadata = readFor("deprecated");
List<ConfigurationMetadataItem> items = rawMetadata.getItems();
assertEquals(3, items.size());
assertThat(items).hasSize(3);
ConfigurationMetadataItem item = items.get(0);
assertProperty(item, "server.port", "server.port", Integer.class, null);
assertTrue(item.isDeprecated());
assertEquals("Server namespace has moved to spring.server",
item.getDeprecation().getReason());
assertEquals("server.spring.port", item.getDeprecation().getReplacement());
assertThat(item.isDeprecated()).isTrue();
assertThat(item.getDeprecation().getReason())
.isEqualTo("Server namespace has moved to spring.server");
assertThat(item.getDeprecation().getReplacement())
.isEqualTo("server.spring.port");
ConfigurationMetadataItem item2 = items.get(1);
assertProperty(item2, "server.cluster-name", "server.cluster-name", String.class,
null);
assertTrue(item2.isDeprecated());
assertEquals(null, item2.getDeprecation().getReason());
assertEquals(null, item2.getDeprecation().getReplacement());
assertThat(item2.isDeprecated()).isTrue();
assertThat(item2.getDeprecation().getReason()).isEqualTo(null);
assertThat(item2.getDeprecation().getReplacement()).isEqualTo(null);
ConfigurationMetadataItem item3 = items.get(2);
assertProperty(item3, "spring.server.name", "spring.server.name", String.class,
null);
assertFalse(item3.isDeprecated());
assertEquals(null, item3.getDeprecation());
assertThat(item3.isDeprecated()).isFalse();
assertThat(item3.getDeprecation()).isEqualTo(null);
}
RawConfigurationMetadata readFor(String path) throws IOException {