From 00cfe1d054d62b9713e3ea151c7efe38b466fe00 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 6 Feb 2016 14:53:00 -0800 Subject: [PATCH] Use AssertJ in spring-boot-tools See gh-5083 --- .../AbstractConfigurationMetadataTests.java | 25 +- ...ionMetadataRepositoryJsonBuilderTests.java | 49 +- .../DescriptionExtractorTests.java | 14 +- .../JsonReaderTests.java | 103 +++-- ...ationMetadataAnnotationProcessorTests.java | 430 +++++++++--------- .../ConfigurationMetadataMatchers.java | 416 ----------------- .../boot/configurationprocessor/Metadata.java | 401 ++++++++++++++++ ...figurationMetadataAnnotationProcessor.java | 2 + .../AbstractFieldValuesProcessorTests.java | 73 ++- .../metadata/ConfigurationMetadataTests.java | 22 +- .../metadata/JsonMarshallerTests.java | 39 +- .../tools/DefaultLaunchScriptTests.java | 24 +- .../boot/loader/tools/FileUtilsTests.java | 25 +- .../boot/loader/tools/JvmUtilsTests.java | 8 +- .../boot/loader/tools/LayoutsTests.java | 71 ++- .../loader/tools/MainClassFinderTests.java | 21 +- .../boot/loader/tools/RepackagerTests.java | 126 +++-- .../ExecutableArchiveLauncherTests.java | 7 +- .../InputArgumentsJavaAgentDetectorTests.java | 25 +- .../loader/LaunchedURLClassLoaderTests.java | 25 +- .../boot/loader/PropertiesLauncherTests.java | 63 ++- .../boot/loader/WarLauncherTests.java | 27 +- .../loader/archive/ExplodedArchiveTests.java | 46 +- .../loader/archive/JarFileArchiveTests.java | 28 +- .../data/ByteArrayRandomAccessDataTests.java | 21 +- .../data/RandomAccessDataFileTests.java | 74 ++- .../boot/loader/jar/AsciiBytesTests.java | 76 ++-- .../jar/CentralDirectoryParserTests.java | 31 +- .../boot/loader/jar/JarFileTests.java | 223 +++++---- .../loader/util/SystemPropertyUtilsTests.java | 16 +- .../boot/maven/ArtifactsLibrariesTests.java | 15 +- .../boot/maven/DependencyFilterMojoTests.java | 11 +- .../boot/maven/ExcludeFilterTests.java | 29 +- .../boot/maven/IncludeFilterTests.java | 23 +- ...ertiesMergingResourceTransformerTests.java | 15 +- .../boot/maven/RunArgumentsTests.java | 35 +- .../springframework/boot/maven/Verify.java | 87 ++-- 37 files changed, 1325 insertions(+), 1401 deletions(-) delete mode 100644 spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java create mode 100644 spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/Metadata.java 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 1af015327f..afd39de0fb 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 @@ -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 { 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 1107e51ce9..068412e8a1 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 @@ -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 source, String... keys) { for (String key : keys) { - assertTrue("Item '" + key + "' not found. Got " + source.keySet(), - source.containsKey(key)); + assertThat(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 index e1ba457684..e355e48b6b 100644 --- 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 @@ -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); } } 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 ce75549d21..51b0f94678 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 @@ -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 sources = rawMetadata.getSources(); - assertEquals(2, sources.size()); + assertThat(sources).hasSize(2); List items = rawMetadata.getItems(); - assertEquals(4, items.size()); + assertThat(items).hasSize(4); List 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 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 sources = rawMetadata.getSources(); - assertEquals(0, sources.size()); + assertThat(sources).isEmpty(); List 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 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 { 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 99fa18ad0d..e19c71eaa5 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 @@ -63,16 +63,7 @@ import org.springframework.boot.configurationsample.specific.InnerClassRootConfi import org.springframework.boot.configurationsample.specific.SimplePojo; import org.springframework.util.FileCopyUtils; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsHint; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link ConfigurationMetadataAnnotationProcessor}. @@ -100,80 +91,88 @@ public class ConfigurationMetadataAnnotationProcessorTests { @Test public void notAnnotated() throws Exception { ConfigurationMetadata metadata = compile(NotAnnotated.class); - assertThat("No config metadata file should have been generated when " - + "no metadata is discovered", metadata.getItems(), empty()); + assertThat(metadata.getItems()).isEmpty(); } @Test public void simpleProperties() throws Exception { ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsGroup("simple").fromSource(SimpleProperties.class)); - assertThat(metadata, - containsProperty("simple.the-name", String.class) - .fromSource(SimpleProperties.class) - .withDescription("The name of this simple properties.") - .withDefaultValue(is("boot")).withDeprecation(null, null)); - assertThat(metadata, - containsProperty("simple.flag", Boolean.class) - .fromSource(SimpleProperties.class) - .withDescription("A simple flag.").withDeprecation(null, null)); - assertThat(metadata, containsProperty("simple.comparator")); - assertThat(metadata, not(containsProperty("simple.counter"))); - assertThat(metadata, not(containsProperty("simple.size"))); + assertThat(metadata) + .has(Metadata.withGroup("simple").fromSource(SimpleProperties.class)); + assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) + .fromSource(SimpleProperties.class) + .withDescription("The name of this simple properties.") + .withDefaultValue("boot").withDeprecation(null, null)); + assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class) + .fromSource(SimpleProperties.class).withDescription("A simple flag.") + .withDeprecation(null, null)); + assertThat(metadata).has(Metadata.withProperty("simple.comparator")); + assertThat(metadata).doesNotHave(Metadata.withProperty("simple.counter")); + assertThat(metadata).doesNotHave(Metadata.withProperty("simple.size")); } @Test public void simplePrefixValueProperties() throws Exception { ConfigurationMetadata metadata = compile(SimplePrefixValueProperties.class); - assertThat(metadata, - containsGroup("simple").fromSource(SimplePrefixValueProperties.class)); - assertThat(metadata, containsProperty("simple.name", String.class) + assertThat(metadata).has(Metadata.withGroup("simple") + .fromSource(SimplePrefixValueProperties.class)); + assertThat(metadata).has(Metadata.withProperty("simple.name", String.class) .fromSource(SimplePrefixValueProperties.class)); } @Test public void simpleTypeProperties() throws Exception { ConfigurationMetadata metadata = compile(SimpleTypeProperties.class); - assertThat(metadata, - containsGroup("simple.type").fromSource(SimpleTypeProperties.class)); - assertThat(metadata, containsProperty("simple.type.my-string", String.class)); - assertThat(metadata, containsProperty("simple.type.my-byte", Byte.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-byte", Byte.class)); - assertThat(metadata, containsProperty("simple.type.my-char", Character.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-char", Character.class)); - assertThat(metadata, containsProperty("simple.type.my-boolean", Boolean.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-boolean", Boolean.class)); - assertThat(metadata, containsProperty("simple.type.my-short", Short.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-short", Short.class)); - assertThat(metadata, containsProperty("simple.type.my-integer", Integer.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-integer", Integer.class)); - assertThat(metadata, containsProperty("simple.type.my-long", Long.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-long", Long.class)); - assertThat(metadata, containsProperty("simple.type.my-double", Double.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-double", Double.class)); - assertThat(metadata, containsProperty("simple.type.my-float", Float.class)); - assertThat(metadata, - containsProperty("simple.type.my-primitive-float", Float.class)); - assertThat(metadata.getItems().size(), equalTo(18)); + assertThat(metadata).has( + Metadata.withGroup("simple.type").fromSource(SimpleTypeProperties.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-string", String.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-byte", Byte.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-primitive-byte", Byte.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-char", Character.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-char", Character.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-boolean", Boolean.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-boolean", Boolean.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-short", Short.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-short", Short.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-integer", Integer.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-integer", Integer.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-long", Long.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-primitive-long", Long.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-double", Double.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-double", Double.class)); + assertThat(metadata) + .has(Metadata.withProperty("simple.type.my-float", Float.class)); + assertThat(metadata).has( + Metadata.withProperty("simple.type.my-primitive-float", Float.class)); + assertThat(metadata.getItems().size()).isEqualTo(18); } @Test public void hierarchicalProperties() throws Exception { ConfigurationMetadata metadata = compile(HierarchicalProperties.class); - assertThat(metadata, - containsGroup("hierarchical").fromSource(HierarchicalProperties.class)); - assertThat(metadata, containsProperty("hierarchical.first", String.class) + assertThat(metadata).has(Metadata.withGroup("hierarchical") .fromSource(HierarchicalProperties.class)); - assertThat(metadata, containsProperty("hierarchical.second", String.class) + assertThat(metadata).has(Metadata.withProperty("hierarchical.first", String.class) .fromSource(HierarchicalProperties.class)); - assertThat(metadata, containsProperty("hierarchical.third", String.class) + assertThat(metadata) + .has(Metadata.withProperty("hierarchical.second", String.class) + .fromSource(HierarchicalProperties.class)); + assertThat(metadata).has(Metadata.withProperty("hierarchical.third", String.class) .fromSource(HierarchicalProperties.class)); } @@ -182,152 +181,157 @@ public class ConfigurationMetadataAnnotationProcessorTests { public void deprecatedProperties() throws Exception { Class type = org.springframework.boot.configurationsample.simple.DeprecatedProperties.class; ConfigurationMetadata metadata = compile(type); - assertThat(metadata, containsGroup("deprecated").fromSource(type)); - assertThat(metadata, containsProperty("deprecated.name", String.class) - .fromSource(type).withDeprecation(null, null)); - assertThat(metadata, containsProperty("deprecated.description", String.class) + assertThat(metadata).has(Metadata.withGroup("deprecated").fromSource(type)); + assertThat(metadata).has(Metadata.withProperty("deprecated.name", String.class) .fromSource(type).withDeprecation(null, null)); + assertThat(metadata) + .has(Metadata.withProperty("deprecated.description", String.class) + .fromSource(type).withDeprecation(null, null)); } @Test public void singleDeprecatedProperty() throws Exception { Class type = DeprecatedSingleProperty.class; ConfigurationMetadata metadata = compile(type); - assertThat(metadata, containsGroup("singledeprecated").fromSource(type)); - assertThat(metadata, containsProperty("singledeprecated.new-name", String.class) - .fromSource(type)); - assertThat(metadata, - containsProperty("singledeprecated.name", String.class).fromSource(type) - .withDeprecation("renamed", "singledeprecated.new-name")); + assertThat(metadata).has(Metadata.withGroup("singledeprecated").fromSource(type)); + assertThat(metadata) + .has(Metadata.withProperty("singledeprecated.new-name", String.class) + .fromSource(type)); + assertThat(metadata).has(Metadata + .withProperty("singledeprecated.name", String.class).fromSource(type) + .withDeprecation("renamed", "singledeprecated.new-name")); } @Test public void deprecatedOnUnrelatedSetter() throws Exception { Class type = DeprecatedUnrelatedMethodPojo.class; ConfigurationMetadata metadata = compile(type); - assertThat(metadata, containsGroup("not.deprecated").fromSource(type)); - assertThat(metadata, containsProperty("not.deprecated.counter", Integer.class) - .withNoDeprecation().fromSource(type)); - assertThat(metadata, containsProperty("not.deprecated.flag", Boolean.class) - .withNoDeprecation().fromSource(type)); + assertThat(metadata).has(Metadata.withGroup("not.deprecated").fromSource(type)); + assertThat(metadata) + .has(Metadata.withProperty("not.deprecated.counter", Integer.class) + .withNoDeprecation().fromSource(type)); + assertThat(metadata) + .has(Metadata.withProperty("not.deprecated.flag", Boolean.class) + .withNoDeprecation().fromSource(type)); } @Test public void boxingOnSetter() throws IOException { Class type = BoxingPojo.class; ConfigurationMetadata metadata = compile(type); - assertThat(metadata, containsGroup("boxing").fromSource(type)); - assertThat(metadata, - containsProperty("boxing.flag", Boolean.class).fromSource(type)); - assertThat(metadata, - containsProperty("boxing.counter", Integer.class).fromSource(type)); + assertThat(metadata).has(Metadata.withGroup("boxing").fromSource(type)); + assertThat(metadata).has( + Metadata.withProperty("boxing.flag", Boolean.class).fromSource(type)); + assertThat(metadata).has( + Metadata.withProperty("boxing.counter", Integer.class).fromSource(type)); } @Test public void parseCollectionConfig() throws Exception { ConfigurationMetadata metadata = compile(SimpleCollectionProperties.class); // getter and setter - assertThat(metadata, containsProperty("collection.integers-to-names", + assertThat(metadata).has(Metadata.withProperty("collection.integers-to-names", "java.util.Map")); - assertThat(metadata, containsProperty("collection.longs", + assertThat(metadata).has(Metadata.withProperty("collection.longs", "java.util.Collection")); - assertThat(metadata, - containsProperty("collection.floats", "java.util.List")); + assertThat(metadata).has(Metadata.withProperty("collection.floats", + "java.util.List")); // getter only - assertThat(metadata, containsProperty("collection.names-to-integers", + assertThat(metadata).has(Metadata.withProperty("collection.names-to-integers", "java.util.Map")); - assertThat(metadata, containsProperty("collection.bytes", + assertThat(metadata).has(Metadata.withProperty("collection.bytes", "java.util.Collection")); - assertThat(metadata, containsProperty("collection.doubles", + assertThat(metadata).has(Metadata.withProperty("collection.doubles", "java.util.List")); } @Test public void simpleMethodConfig() throws Exception { ConfigurationMetadata metadata = compile(SimpleMethodConfig.class); - assertThat(metadata, containsGroup("foo").fromSource(SimpleMethodConfig.class)); - assertThat(metadata, containsProperty("foo.name", String.class) + assertThat(metadata) + .has(Metadata.withGroup("foo").fromSource(SimpleMethodConfig.class)); + assertThat(metadata).has(Metadata.withProperty("foo.name", String.class) .fromSource(SimpleMethodConfig.Foo.class)); - assertThat(metadata, containsProperty("foo.flag", Boolean.class) + assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class) .fromSource(SimpleMethodConfig.Foo.class)); } @Test public void invalidMethodConfig() throws Exception { ConfigurationMetadata metadata = compile(InvalidMethodConfig.class); - assertThat(metadata, containsProperty("something.name", String.class) + assertThat(metadata).has(Metadata.withProperty("something.name", String.class) .fromSource(InvalidMethodConfig.class)); - assertThat(metadata, not(containsProperty("invalid.name"))); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("invalid.name")); } @Test public void methodAndClassConfig() throws Exception { ConfigurationMetadata metadata = compile(MethodAndClassConfig.class); - assertThat(metadata, containsProperty("conflict.name", String.class) + assertThat(metadata).has(Metadata.withProperty("conflict.name", String.class) .fromSource(MethodAndClassConfig.Foo.class)); - assertThat(metadata, containsProperty("conflict.flag", Boolean.class) + assertThat(metadata).has(Metadata.withProperty("conflict.flag", Boolean.class) .fromSource(MethodAndClassConfig.Foo.class)); - assertThat(metadata, containsProperty("conflict.value", String.class) + assertThat(metadata).has(Metadata.withProperty("conflict.value", String.class) .fromSource(MethodAndClassConfig.class)); } @Test public void emptyTypeMethodConfig() throws Exception { ConfigurationMetadata metadata = compile(EmptyTypeMethodConfig.class); - assertThat(metadata, not(containsProperty("something.foo"))); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("something.foo")); } @Test public void innerClassRootConfig() throws Exception { ConfigurationMetadata metadata = compile(InnerClassRootConfig.class); - assertThat(metadata, containsProperty("config.name")); + assertThat(metadata).has(Metadata.withProperty("config.name")); } @Test public void innerClassProperties() throws Exception { ConfigurationMetadata metadata = compile(InnerClassProperties.class); - assertThat(metadata, - containsGroup("config").fromSource(InnerClassProperties.class)); - assertThat(metadata, - containsGroup("config.first").ofType(InnerClassProperties.Foo.class) + assertThat(metadata) + .has(Metadata.withGroup("config").fromSource(InnerClassProperties.class)); + assertThat(metadata).has( + Metadata.withGroup("config.first").ofType(InnerClassProperties.Foo.class) .fromSource(InnerClassProperties.class)); - assertThat(metadata, containsProperty("config.first.name")); - assertThat(metadata, containsProperty("config.first.bar.name")); - assertThat(metadata, - containsGroup("config.the-second", InnerClassProperties.Foo.class) + assertThat(metadata).has(Metadata.withProperty("config.first.name")); + assertThat(metadata).has(Metadata.withProperty("config.first.bar.name")); + assertThat(metadata).has( + Metadata.withGroup("config.the-second", InnerClassProperties.Foo.class) .fromSource(InnerClassProperties.class)); - assertThat(metadata, containsProperty("config.the-second.name")); - assertThat(metadata, containsProperty("config.the-second.bar.name")); - assertThat(metadata, containsGroup("config.third").ofType(SimplePojo.class) - .fromSource(InnerClassProperties.class)); - assertThat(metadata, containsProperty("config.third.value")); - assertThat(metadata, containsProperty("config.fourth")); - assertThat(metadata, not(containsGroup("config.fourth"))); + assertThat(metadata).has(Metadata.withProperty("config.the-second.name")); + assertThat(metadata).has(Metadata.withProperty("config.the-second.bar.name")); + assertThat(metadata).has(Metadata.withGroup("config.third") + .ofType(SimplePojo.class).fromSource(InnerClassProperties.class)); + assertThat(metadata).has(Metadata.withProperty("config.third.value")); + assertThat(metadata).has(Metadata.withProperty("config.fourth")); + assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth")); } @Test public void innerClassAnnotatedGetterConfig() throws Exception { ConfigurationMetadata metadata = compile(InnerClassAnnotatedGetterConfig.class); - assertThat(metadata, containsProperty("specific.value")); - assertThat(metadata, containsProperty("foo.name")); - assertThat(metadata, not(containsProperty("specific.foo"))); + assertThat(metadata).has(Metadata.withProperty("specific.value")); + assertThat(metadata).has(Metadata.withProperty("foo.name")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("specific.foo")); } @Test public void builderPojo() throws IOException { ConfigurationMetadata metadata = compile(BuilderPojo.class); - assertThat(metadata, containsProperty("builder.name")); + assertThat(metadata).has(Metadata.withProperty("builder.name")); } @Test public void excludedTypesPojo() throws IOException { ConfigurationMetadata metadata = compile(ExcludedTypesPojo.class); - assertThat(metadata, containsProperty("excluded.name")); - assertThat(metadata, not(containsProperty("excluded.class-loader"))); - assertThat(metadata, not(containsProperty("excluded.data-source"))); - assertThat(metadata, not(containsProperty("excluded.print-writer"))); - assertThat(metadata, not(containsProperty("excluded.writer"))); - assertThat(metadata, not(containsProperty("excluded.writer-array"))); + assertThat(metadata).has(Metadata.withProperty("excluded.name")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.class-loader")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.data-source")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.print-writer")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.writer")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("excluded.writer-array")); } @Test @@ -352,28 +356,29 @@ public class ConfigurationMetadataAnnotationProcessorTests { @Test public void lombokInnerClassProperties() throws Exception { ConfigurationMetadata metadata = compile(LombokInnerClassProperties.class); - assertThat(metadata, - containsGroup("config").fromSource(LombokInnerClassProperties.class)); - assertThat(metadata, - containsGroup("config.first").ofType(LombokInnerClassProperties.Foo.class) + assertThat(metadata).has(Metadata.withGroup("config") + .fromSource(LombokInnerClassProperties.class)); + assertThat(metadata).has(Metadata.withGroup("config.first") + .ofType(LombokInnerClassProperties.Foo.class) + .fromSource(LombokInnerClassProperties.class)); + assertThat(metadata).has(Metadata.withProperty("config.first.name")); + assertThat(metadata).has(Metadata.withProperty("config.first.bar.name")); + assertThat(metadata).has( + Metadata.withGroup("config.second", LombokInnerClassProperties.Foo.class) .fromSource(LombokInnerClassProperties.class)); - assertThat(metadata, containsProperty("config.first.name")); - assertThat(metadata, containsProperty("config.first.bar.name")); - assertThat(metadata, - containsGroup("config.second", LombokInnerClassProperties.Foo.class) + assertThat(metadata).has(Metadata.withProperty("config.second.name")); + assertThat(metadata).has(Metadata.withProperty("config.second.bar.name")); + assertThat(metadata) + .has(Metadata.withGroup("config.third").ofType(SimpleLombokPojo.class) .fromSource(LombokInnerClassProperties.class)); - assertThat(metadata, containsProperty("config.second.name")); - assertThat(metadata, containsProperty("config.second.bar.name")); - assertThat(metadata, containsGroup("config.third").ofType(SimpleLombokPojo.class) - .fromSource(LombokInnerClassProperties.class)); // For some reason the annotation processor resolves a type for SimpleLombokPojo // that is resolved (compiled) and the source annotations are gone. Because we // don't see the @Data annotation anymore, no field is harvested. What is crazy is // that a sample project works fine so this seem to be related to the unit test // environment for some reason. assertThat(metadata, // containsProperty("config.third.value")); - assertThat(metadata, containsProperty("config.fourth")); - assertThat(metadata, not(containsGroup("config.fourth"))); + assertThat(metadata).has(Metadata.withProperty("config.fourth")); + assertThat(metadata).isNotEqualTo(Metadata.withGroup("config.fourth")); } @Test @@ -382,8 +387,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { AdditionalMetadata.class.getName(), null, null, null, null); writeAdditionalMetadata(property); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsProperty("simple.comparator")); - assertThat(metadata, containsProperty("foo", String.class) + assertThat(metadata).has(Metadata.withProperty("simple.comparator")); + assertThat(metadata).has(Metadata.withProperty("foo", String.class) .fromSource(AdditionalMetadata.class)); } @@ -393,10 +398,10 @@ public class ConfigurationMetadataAnnotationProcessorTests { null, null, true, null); writeAdditionalMetadata(property); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsProperty("simple.flag", Boolean.class) + assertThat(metadata).has(Metadata.withProperty("simple.flag", Boolean.class) .fromSource(SimpleProperties.class).withDescription("A simple flag.") - .withDeprecation(null, null).withDefaultValue(is(true))); - assertThat(metadata.getItems().size(), is(4)); + .withDeprecation(null, null).withDefaultValue(true)); + assertThat(metadata.getItems()).hasSize(4); } @Test @@ -405,11 +410,11 @@ public class ConfigurationMetadataAnnotationProcessorTests { null, null, "A nice comparator.", null, null); writeAdditionalMetadata(property); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, - containsProperty("simple.comparator", "java.util.Comparator") + assertThat(metadata) + .has(Metadata.withProperty("simple.comparator", "java.util.Comparator") .fromSource(SimpleProperties.class) .withDescription("A nice comparator.")); - assertThat(metadata.getItems().size(), is(4)); + assertThat(metadata.getItems()).hasSize(4); } @Test @@ -419,11 +424,11 @@ public class ConfigurationMetadataAnnotationProcessorTests { new ItemDeprecation("Don't use this.", "simple.complex-comparator")); writeAdditionalMetadata(property); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, - containsProperty("simple.comparator", "java.util.Comparator") + assertThat(metadata) + .has(Metadata.withProperty("simple.comparator", "java.util.Comparator") .fromSource(SimpleProperties.class) .withDeprecation("Don't use this.", "simple.complex-comparator")); - assertThat(metadata.getItems().size(), is(4)); + assertThat(metadata.getItems()).hasSize(4); } @Test @@ -433,11 +438,11 @@ public class ConfigurationMetadataAnnotationProcessorTests { new ItemDeprecation("Don't use this.", "single.name")); writeAdditionalMetadata(property); ConfigurationMetadata metadata = compile(DeprecatedSingleProperty.class); - assertThat(metadata, - containsProperty("singledeprecated.name", String.class.getName()) + assertThat(metadata).has( + Metadata.withProperty("singledeprecated.name", String.class.getName()) .fromSource(DeprecatedSingleProperty.class) .withDeprecation("Don't use this.", "single.name")); - assertThat(metadata.getItems().size(), is(3)); + assertThat(metadata.getItems()).hasSize(3); } @Test @@ -456,12 +461,11 @@ public class ConfigurationMetadataAnnotationProcessorTests { new ItemHint.ValueHint("boot", "Bla bla"), new ItemHint.ValueHint("spring", null))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, - containsProperty("simple.the-name", String.class) - .fromSource(SimpleProperties.class) - .withDescription("The name of this simple properties.") - .withDefaultValue(is("boot")).withDeprecation(null, null)); - assertThat(metadata, containsHint("simple.the-name") + assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) + .fromSource(SimpleProperties.class) + .withDescription("The name of this simple properties.") + .withDefaultValue("boot").withDeprecation(null, null)); + assertThat(metadata).has(Metadata.withHint("simple.the-name") .withValue(0, "boot", "Bla bla").withValue(1, "spring", null)); } @@ -470,13 +474,12 @@ public class ConfigurationMetadataAnnotationProcessorTests { writeAdditionalHints(ItemHint.newHint("simple.theName", new ItemHint.ValueHint("boot", "Bla bla"))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, - containsProperty("simple.the-name", String.class) - .fromSource(SimpleProperties.class) - .withDescription("The name of this simple properties.") - .withDefaultValue(is("boot")).withDeprecation(null, null)); - assertThat(metadata, - containsHint("simple.the-name").withValue(0, "boot", "Bla bla")); + assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) + .fromSource(SimpleProperties.class) + .withDescription("The name of this simple properties.") + .withDefaultValue("boot").withDeprecation(null, null)); + assertThat(metadata).has( + Metadata.withHint("simple.the-name").withValue(0, "boot", "Bla bla")); } @Test @@ -489,12 +492,11 @@ public class ConfigurationMetadataAnnotationProcessorTests { "org.foo")), new ItemHint.ValueProvider("second", null)))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, - containsProperty("simple.the-name", String.class) - .fromSource(SimpleProperties.class) - .withDescription("The name of this simple properties.") - .withDefaultValue(is("boot")).withDeprecation(null, null)); - assertThat(metadata, containsHint("simple.the-name") + assertThat(metadata).has(Metadata.withProperty("simple.the-name", String.class) + .fromSource(SimpleProperties.class) + .withDescription("The name of this simple properties.") + .withDefaultValue("boot").withDeprecation(null, null)); + assertThat(metadata).has(Metadata.withHint("simple.the-name") .withProvider("first", "target", "org.foo").withProvider("second")); } @@ -504,7 +506,7 @@ public class ConfigurationMetadataAnnotationProcessorTests { "java.lang.String", null, null, null, null, new ItemDeprecation("Lame name.", "simple.the-name"))); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsProperty("simple.wrong-name", String.class) + assertThat(metadata).has(Metadata.withProperty("simple.wrong-name", String.class) .withDeprecation("Lame name.", "simple.the-name")); } @@ -527,8 +529,8 @@ public class ConfigurationMetadataAnnotationProcessorTests { additionalMetadata.write(writer); writer.flush(); ConfigurationMetadata metadata = compile(SimpleProperties.class); - assertThat(metadata, containsProperty("simple.comparator")); - assertThat(metadata, containsProperty("foo", String.class) + assertThat(metadata).has(Metadata.withProperty("simple.comparator")); + assertThat(metadata).has(Metadata.withProperty("foo", String.class) .fromSource(AdditionalMetadata.class)); } @@ -536,29 +538,29 @@ public class ConfigurationMetadataAnnotationProcessorTests { public void incrementalBuild() throws Exception { TestProject project = new TestProject(this.temporaryFolder, FooProperties.class, BarProperties.class); - assertFalse(project.getOutputFile(MetadataStore.METADATA_PATH).exists()); + assertThat(project.getOutputFile(MetadataStore.METADATA_PATH).exists()).isFalse(); ConfigurationMetadata metadata = project.fullBuild(); - assertTrue(project.getOutputFile(MetadataStore.METADATA_PATH).exists()); - assertThat(metadata, - containsProperty("foo.counter").fromSource(FooProperties.class)); - assertThat(metadata, - containsProperty("bar.counter").fromSource(BarProperties.class)); + assertThat(project.getOutputFile(MetadataStore.METADATA_PATH).exists()).isTrue(); + assertThat(metadata).has( + Metadata.withProperty("foo.counter").fromSource(FooProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("bar.counter").fromSource(BarProperties.class)); metadata = project.incrementalBuild(BarProperties.class); - assertThat(metadata, - containsProperty("foo.counter").fromSource(FooProperties.class)); - assertThat(metadata, - containsProperty("bar.counter").fromSource(BarProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("foo.counter").fromSource(FooProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("bar.counter").fromSource(BarProperties.class)); project.addSourceCode(BarProperties.class, BarProperties.class.getResourceAsStream("BarProperties.snippet")); metadata = project.incrementalBuild(BarProperties.class); - assertThat(metadata, containsProperty("bar.extra")); - assertThat(metadata, containsProperty("foo.counter")); - assertThat(metadata, containsProperty("bar.counter")); + assertThat(metadata).has(Metadata.withProperty("bar.extra")); + assertThat(metadata).has(Metadata.withProperty("foo.counter")); + assertThat(metadata).has(Metadata.withProperty("bar.counter")); project.revert(BarProperties.class); metadata = project.incrementalBuild(BarProperties.class); - assertThat(metadata, not(containsProperty("bar.extra"))); - assertThat(metadata, containsProperty("foo.counter")); - assertThat(metadata, containsProperty("bar.counter")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.extra")); + assertThat(metadata).has(Metadata.withProperty("foo.counter")); + assertThat(metadata).has(Metadata.withProperty("bar.counter")); } @Test @@ -566,13 +568,13 @@ public class ConfigurationMetadataAnnotationProcessorTests { TestProject project = new TestProject(this.temporaryFolder, FooProperties.class, BarProperties.class); ConfigurationMetadata metadata = project.fullBuild(); - assertThat(metadata, containsProperty("foo.counter")); - assertThat(metadata, containsProperty("bar.counter")); + assertThat(metadata).has(Metadata.withProperty("foo.counter")); + assertThat(metadata).has(Metadata.withProperty("bar.counter")); project.replaceText(BarProperties.class, "@ConfigurationProperties", "//@ConfigurationProperties"); metadata = project.incrementalBuild(BarProperties.class); - assertThat(metadata, containsProperty("foo.counter")); - assertThat(metadata, not(containsProperty("bar.counter"))); + assertThat(metadata).has(Metadata.withProperty("foo.counter")); + assertThat(metadata).isNotEqualTo(Metadata.withProperty("bar.counter")); } @Test @@ -580,35 +582,35 @@ public class ConfigurationMetadataAnnotationProcessorTests { TestProject project = new TestProject(this.temporaryFolder, FooProperties.class, BarProperties.class); ConfigurationMetadata metadata = project.fullBuild(); - assertThat(metadata, - containsProperty("foo.counter").fromSource(FooProperties.class)); - assertThat(metadata, - containsProperty("bar.counter").fromSource(BarProperties.class)); - assertThat(metadata, not( - containsProperty("bar.counter").fromSource(RenamedBarProperties.class))); + assertThat(metadata).has( + Metadata.withProperty("foo.counter").fromSource(FooProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("bar.counter").fromSource(BarProperties.class)); + assertThat(metadata).doesNotHave(Metadata.withProperty("bar.counter") + .fromSource(RenamedBarProperties.class)); project.delete(BarProperties.class); project.add(RenamedBarProperties.class); metadata = project.incrementalBuild(RenamedBarProperties.class); - assertThat(metadata, - containsProperty("foo.counter").fromSource(FooProperties.class)); - assertThat(metadata, - not(containsProperty("bar.counter").fromSource(BarProperties.class))); - assertThat(metadata, - containsProperty("bar.counter").fromSource(RenamedBarProperties.class)); + assertThat(metadata).has( + Metadata.withProperty("foo.counter").fromSource(FooProperties.class)); + assertThat(metadata).doesNotHave( + Metadata.withProperty("bar.counter").fromSource(BarProperties.class)); + assertThat(metadata).has(Metadata.withProperty("bar.counter") + .fromSource(RenamedBarProperties.class)); } private void assertSimpleLombokProperties(ConfigurationMetadata metadata, Class source, String prefix) { - assertThat(metadata, containsGroup(prefix).fromSource(source)); - assertThat(metadata, not(containsProperty(prefix + ".id"))); - assertThat(metadata, containsProperty(prefix + ".name", String.class) + assertThat(metadata).has(Metadata.withGroup(prefix).fromSource(source)); + assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".id")); + assertThat(metadata).has(Metadata.withProperty(prefix + ".name", String.class) .fromSource(source).withDescription("Name description.")); - assertThat(metadata, containsProperty(prefix + ".description")); - assertThat(metadata, containsProperty(prefix + ".counter")); - assertThat(metadata, containsProperty(prefix + ".number").fromSource(source) - .withDefaultValue(is(0)).withDeprecation(null, null)); - assertThat(metadata, containsProperty(prefix + ".items")); - assertThat(metadata, not(containsProperty(prefix + ".ignored"))); + assertThat(metadata).has(Metadata.withProperty(prefix + ".description")); + assertThat(metadata).has(Metadata.withProperty(prefix + ".counter")); + assertThat(metadata).has(Metadata.withProperty(prefix + ".number") + .fromSource(source).withDefaultValue(0).withDeprecation(null, null)); + assertThat(metadata).has(Metadata.withProperty(prefix + ".items")); + assertThat(metadata).doesNotHave(Metadata.withProperty(prefix + ".ignored")); } private ConfigurationMetadata compile(Class... types) throws IOException { diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java deleted file mode 100644 index d630059a44..0000000000 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/ConfigurationMetadataMatchers.java +++ /dev/null @@ -1,416 +0,0 @@ -/* - * 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.configurationprocessor; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.collection.IsMapContaining; - -import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; -import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; -import org.springframework.boot.configurationprocessor.metadata.ItemHint; -import org.springframework.boot.configurationprocessor.metadata.ItemMetadata; -import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; - -/** - * Hamcrest {@link Matcher} to help test {@link ConfigurationMetadata}. - * - * @author Phillip Webb - * @author Stephane Nicoll - */ -public final class ConfigurationMetadataMatchers { - - private ConfigurationMetadataMatchers() { - } - - public static ContainsItemMatcher containsGroup(String name) { - return new ContainsItemMatcher(ItemType.GROUP, name); - } - - public static ContainsItemMatcher containsGroup(String name, Class type) { - return new ContainsItemMatcher(ItemType.GROUP, name).ofType(type); - } - - public static ContainsItemMatcher containsGroup(String name, String type) { - return new ContainsItemMatcher(ItemType.GROUP, name).ofType(type); - } - - public static ContainsItemMatcher containsProperty(String name) { - return new ContainsItemMatcher(ItemType.PROPERTY, name); - } - - public static ContainsItemMatcher containsProperty(String name, Class type) { - return new ContainsItemMatcher(ItemType.PROPERTY, name).ofType(type); - } - - public static ContainsItemMatcher containsProperty(String name, String type) { - return new ContainsItemMatcher(ItemType.PROPERTY, name).ofType(type); - } - - public static ContainsHintMatcher containsHint(String name) { - return new ContainsHintMatcher(name); - } - - public static class ContainsItemMatcher extends BaseMatcher { - - private final ItemType itemType; - - private final String name; - - private final String type; - - private final Class sourceType; - - private final String description; - - private final Matcher defaultValue; - - private final ItemDeprecation deprecation; - - public ContainsItemMatcher(ItemType itemType, String name) { - this(itemType, name, null, null, null, null, null); - } - - public ContainsItemMatcher(ItemType itemType, String name, String type, - Class sourceType, String description, Matcher defaultValue, - ItemDeprecation deprecation) { - this.itemType = itemType; - this.name = name; - this.type = type; - this.sourceType = sourceType; - this.description = description; - this.defaultValue = defaultValue; - this.deprecation = deprecation; - } - - @Override - public boolean matches(Object item) { - ConfigurationMetadata metadata = (ConfigurationMetadata) item; - ItemMetadata itemMetadata = getFirstItemWithName(metadata, this.name); - if (itemMetadata == null) { - return false; - } - if (this.type != null && !this.type.equals(itemMetadata.getType())) { - return false; - } - if (this.sourceType != null - && !this.sourceType.getName().equals(itemMetadata.getSourceType())) { - return false; - } - if (this.defaultValue != null - && !this.defaultValue.matches(itemMetadata.getDefaultValue())) { - return false; - } - if (this.description != null - && !this.description.equals(itemMetadata.getDescription())) { - return false; - } - if (this.deprecation == null && itemMetadata.getDeprecation() != null) { - return false; - } - if (this.deprecation != null - && !this.deprecation.equals(itemMetadata.getDeprecation())) { - return false; - } - return true; - } - - @Override - public void describeMismatch(Object item, Description description) { - ConfigurationMetadata metadata = (ConfigurationMetadata) item; - ItemMetadata property = getFirstItemWithName(metadata, this.name); - if (property == null) { - description.appendText("missing " + this.itemType.toString().toLowerCase() - + " " + this.name); - } - else { - description - .appendText("was " + this.itemType.toString().toLowerCase() + " ") - .appendValue(property); - } - } - - @Override - public void describeTo(Description description) { - description.appendText("metadata containing " + this.name); - if (this.type != null) { - description.appendText(" dataType ").appendValue(this.type); - } - if (this.sourceType != null) { - description.appendText(" sourceType ").appendValue(this.sourceType); - } - if (this.defaultValue != null) { - description.appendText(" defaultValue ").appendValue(this.defaultValue); - } - if (this.description != null) { - description.appendText(" description ").appendValue(this.description); - } - if (this.deprecation != null) { - description.appendText(" deprecation ").appendValue(this.deprecation); - } - } - - public ContainsItemMatcher ofType(Class dataType) { - return new ContainsItemMatcher(this.itemType, this.name, dataType.getName(), - this.sourceType, this.description, this.defaultValue, - this.deprecation); - } - - public ContainsItemMatcher ofType(String dataType) { - return new ContainsItemMatcher(this.itemType, this.name, dataType, - this.sourceType, this.description, this.defaultValue, - this.deprecation); - } - - public ContainsItemMatcher fromSource(Class sourceType) { - return new ContainsItemMatcher(this.itemType, this.name, this.type, - sourceType, this.description, this.defaultValue, this.deprecation); - } - - public ContainsItemMatcher withDescription(String description) { - return new ContainsItemMatcher(this.itemType, this.name, this.type, - this.sourceType, description, this.defaultValue, this.deprecation); - } - - public ContainsItemMatcher withDefaultValue(Matcher defaultValue) { - return new ContainsItemMatcher(this.itemType, this.name, this.type, - this.sourceType, this.description, defaultValue, this.deprecation); - } - - public ContainsItemMatcher withDeprecation(String reason, String replacement) { - return new ContainsItemMatcher(this.itemType, this.name, this.type, - this.sourceType, this.description, this.defaultValue, - new ItemDeprecation(reason, replacement)); - } - - public ContainsItemMatcher withNoDeprecation() { - return new ContainsItemMatcher(this.itemType, this.name, this.type, - this.sourceType, this.description, this.defaultValue, null); - } - - private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata, - String name) { - for (ItemMetadata item : metadata.getItems()) { - if (item.isOfItemType(this.itemType) && name.equals(item.getName())) { - return item; - } - } - return null; - } - - } - - public static class ContainsHintMatcher extends BaseMatcher { - - private final String name; - - private final List values; - - private final List providers; - - public ContainsHintMatcher(String name) { - this(name, new ArrayList(), - new ArrayList()); - } - - public ContainsHintMatcher(String name, List values, - List providers) { - this.name = name; - this.values = values; - this.providers = providers; - } - - @Override - public boolean matches(Object item) { - ConfigurationMetadata metadata = (ConfigurationMetadata) item; - ItemHint itemHint = getFirstHintWithName(metadata, this.name); - if (itemHint == null) { - return false; - } - for (ValueHintMatcher value : this.values) { - if (!value.matches(itemHint)) { - return false; - } - } - for (ValueProviderMatcher provider : this.providers) { - if (!provider.matches(itemHint)) { - return false; - } - } - return true; - } - - @Override - public void describeMismatch(Object item, Description description) { - ConfigurationMetadata metadata = (ConfigurationMetadata) item; - ItemHint itemHint = getFirstHintWithName(metadata, this.name); - if (itemHint == null) { - description.appendText("missing hint " + this.name); - } - else { - description.appendText("was hint ").appendValue(itemHint); - } - } - - @Override - public void describeTo(Description description) { - description.appendText("hints for " + this.name); - if (this.values != null) { - description.appendText(" values ").appendValue(this.values); - } - if (this.providers != null) { - description.appendText(" providers ").appendValue(this.providers); - } - } - - public ContainsHintMatcher withValue(int index, Object value, - String description) { - List values = new ArrayList(this.values); - values.add(new ValueHintMatcher(index, value, description)); - return new ContainsHintMatcher(this.name, values, this.providers); - } - - public ContainsHintMatcher withProvider(int index, String provider, - Map parameters) { - List providers = new ArrayList( - this.providers); - providers.add(new ValueProviderMatcher(index, provider, parameters)); - return new ContainsHintMatcher(this.name, this.values, providers); - } - - public ContainsHintMatcher withProvider(String provider, String key, - Object value) { - return withProvider(this.providers.size(), provider, - Collections.singletonMap(key, value)); - } - - public ContainsHintMatcher withProvider(String provider) { - return withProvider(this.providers.size(), provider, null); - } - - private ItemHint getFirstHintWithName(ConfigurationMetadata metadata, - String name) { - for (ItemHint hint : metadata.getHints()) { - if (name.equals(hint.getName())) { - return hint; - } - } - return null; - } - - } - - public static class ValueHintMatcher extends BaseMatcher { - - private final int index; - - private final Object value; - - private final String description; - - public ValueHintMatcher(int index, Object value, String description) { - this.index = index; - this.value = value; - this.description = description; - } - - @Override - public boolean matches(Object item) { - ItemHint hint = (ItemHint) item; - if (this.index + 1 > hint.getValues().size()) { - return false; - } - ItemHint.ValueHint valueHint = hint.getValues().get(this.index); - if (this.value != null && !this.value.equals(valueHint.getValue())) { - return false; - } - if (this.description != null - && !this.description.equals(valueHint.getDescription())) { - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("value hint at index '" + this.index + "'"); - if (this.value != null) { - description.appendText(" value ").appendValue(this.value); - } - if (this.description != null) { - description.appendText(" description ").appendValue(this.description); - } - } - - } - - public static class ValueProviderMatcher extends BaseMatcher { - - private final int index; - - private final String name; - - private final Map parameters; - - public ValueProviderMatcher(int index, String name, - Map parameters) { - this.index = index; - this.name = name; - this.parameters = parameters; - } - - @Override - public boolean matches(Object item) { - ItemHint hint = (ItemHint) item; - if (this.index + 1 > hint.getProviders().size()) { - return false; - } - ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index); - if (this.name != null && !this.name.equals(valueProvider.getName())) { - return false; - } - if (this.parameters != null) { - for (Map.Entry entry : this.parameters.entrySet()) { - if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue()) - .matches(valueProvider.getParameters())) { - return false; - } - } - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("value provider "); - if (this.name != null) { - description.appendText(" name ").appendValue(this.name); - } - if (this.parameters != null) { - description.appendText(" parameters ").appendValue(this.parameters); - } - } - - } - -} diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/Metadata.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/Metadata.java new file mode 100644 index 0000000000..4dba5fa12d --- /dev/null +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/Metadata.java @@ -0,0 +1,401 @@ +/* + * 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.configurationprocessor; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.assertj.core.api.Condition; +import org.hamcrest.collection.IsMapContaining; + +import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata; +import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation; +import org.springframework.boot.configurationprocessor.metadata.ItemHint; +import org.springframework.boot.configurationprocessor.metadata.ItemMetadata; +import org.springframework.boot.configurationprocessor.metadata.ItemMetadata.ItemType; +import org.springframework.util.ObjectUtils; + +/** + * AssertJ {@link Condition} to help test {@link ConfigurationMetadata}. + * + * @author Phillip Webb + * @author Stephane Nicoll + */ +public final class Metadata { + + private Metadata() { + } + + public static MetadataItemCondition withGroup(String name) { + return new MetadataItemCondition(ItemType.GROUP, name); + } + + public static MetadataItemCondition withGroup(String name, Class type) { + return new MetadataItemCondition(ItemType.GROUP, name).ofType(type); + } + + public static MetadataItemCondition withGroup(String name, String type) { + return new MetadataItemCondition(ItemType.GROUP, name).ofType(type); + } + + public static MetadataItemCondition withProperty(String name) { + return new MetadataItemCondition(ItemType.PROPERTY, name); + } + + public static MetadataItemCondition withProperty(String name, Class type) { + return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type); + } + + public static MetadataItemCondition withProperty(String name, String type) { + return new MetadataItemCondition(ItemType.PROPERTY, name).ofType(type); + } + + public static MetadataHintCondition withHint(String name) { + return new MetadataHintCondition(name); + } + + public static class MetadataItemCondition extends Condition { + + private final ItemType itemType; + + private final String name; + + private final String type; + + private final Class sourceType; + + private final String description; + + private final Object defaultValue; + + private final ItemDeprecation deprecation; + + public MetadataItemCondition(ItemType itemType, String name) { + this(itemType, name, null, null, null, null, null); + } + + public MetadataItemCondition(ItemType itemType, String name, String type, + Class sourceType, String description, Object defaultValue, + ItemDeprecation deprecation) { + this.itemType = itemType; + this.name = name; + this.type = type; + this.sourceType = sourceType; + this.description = description; + this.defaultValue = defaultValue; + this.deprecation = deprecation; + describedAs(createDescription()); + } + + private String createDescription() { + StringBuilder description = new StringBuilder(); + description.append("an item named '" + this.name + "'"); + if (this.type != null) { + description.append(" with dataType:").append(this.type); + } + if (this.sourceType != null) { + description.append(" with sourceType:").append(this.sourceType); + } + if (this.defaultValue != null) { + description.append(" with defaultValue:").append(this.defaultValue); + } + if (this.description != null) { + description.append(" with description:").append(this.description); + } + if (this.deprecation != null) { + description.append(" with deprecation:").append(this.deprecation); + } + return description.toString(); + } + + @Override + public boolean matches(ConfigurationMetadata value) { + ItemMetadata itemMetadata = getFirstItemWithName(value, this.name); + if (itemMetadata == null) { + return false; + } + if (this.type != null && !this.type.equals(itemMetadata.getType())) { + return false; + } + if (this.sourceType != null + && !this.sourceType.getName().equals(itemMetadata.getSourceType())) { + return false; + } + if (this.defaultValue != null && !ObjectUtils + .nullSafeEquals(this.defaultValue, itemMetadata.getDefaultValue())) { + return false; + } + if (this.description != null + && !this.description.equals(itemMetadata.getDescription())) { + return false; + } + if (this.deprecation == null && itemMetadata.getDeprecation() != null) { + return false; + } + if (this.deprecation != null + && !this.deprecation.equals(itemMetadata.getDeprecation())) { + return false; + } + return true; + } + + public MetadataItemCondition ofType(Class dataType) { + return new MetadataItemCondition(this.itemType, this.name, dataType.getName(), + this.sourceType, this.description, this.defaultValue, + this.deprecation); + } + + public MetadataItemCondition ofType(String dataType) { + return new MetadataItemCondition(this.itemType, this.name, dataType, + this.sourceType, this.description, this.defaultValue, + this.deprecation); + } + + public MetadataItemCondition fromSource(Class sourceType) { + return new MetadataItemCondition(this.itemType, this.name, this.type, + sourceType, this.description, this.defaultValue, this.deprecation); + } + + public MetadataItemCondition withDescription(String description) { + return new MetadataItemCondition(this.itemType, this.name, this.type, + this.sourceType, description, this.defaultValue, this.deprecation); + } + + public MetadataItemCondition withDefaultValue(Object defaultValue) { + return new MetadataItemCondition(this.itemType, this.name, this.type, + this.sourceType, this.description, defaultValue, this.deprecation); + } + + public MetadataItemCondition withDeprecation(String reason, String replacement) { + return new MetadataItemCondition(this.itemType, this.name, this.type, + this.sourceType, this.description, this.defaultValue, + new ItemDeprecation(reason, replacement)); + } + + public MetadataItemCondition withNoDeprecation() { + return new MetadataItemCondition(this.itemType, this.name, this.type, + this.sourceType, this.description, this.defaultValue, null); + } + + private ItemMetadata getFirstItemWithName(ConfigurationMetadata metadata, + String name) { + for (ItemMetadata item : metadata.getItems()) { + if (item.isOfItemType(this.itemType) && name.equals(item.getName())) { + return item; + } + } + return null; + } + + } + + public static class MetadataHintCondition extends Condition { + + private final String name; + + private final List valueConditions; + + private final List providerConditions; + + public MetadataHintCondition(String name) { + this.name = name; + this.valueConditions = Collections.emptyList(); + this.providerConditions = Collections.emptyList(); + } + + public MetadataHintCondition(String name, + List valueConditions, + List providerConditions) { + this.name = name; + this.valueConditions = valueConditions; + this.providerConditions = providerConditions; + describedAs(createDescription()); + } + + private String createDescription() { + StringBuilder description = new StringBuilder(); + description.append("a hints name '" + this.name + "'"); + if (!this.valueConditions.isEmpty()) { + description.append(" with values:").append(this.valueConditions); + } + if (!this.providerConditions.isEmpty()) { + description.append(" with providers:").append(this.providerConditions); + } + return description.toString(); + } + + @Override + public boolean matches(ConfigurationMetadata metadata) { + ItemHint itemHint = getFirstHintWithName(metadata, this.name); + if (itemHint == null) { + return false; + } + return matches(itemHint, this.valueConditions) + && matches(itemHint, this.providerConditions); + } + + private boolean matches(ItemHint itemHint, + List> conditions) { + for (Condition condition : conditions) { + if (!condition.matches(itemHint)) { + return false; + } + } + return true; + } + + private ItemHint getFirstHintWithName(ConfigurationMetadata metadata, + String name) { + for (ItemHint hint : metadata.getHints()) { + if (name.equals(hint.getName())) { + return hint; + } + } + return null; + } + + public MetadataHintCondition withValue(int index, Object value, + String description) { + return new MetadataHintCondition(this.name, + add(this.valueConditions, + new ItemHintValueCondition(index, value, description)), + this.providerConditions); + } + + public MetadataHintCondition withProvider(String provider) { + return withProvider(this.providerConditions.size(), provider, null); + } + + public MetadataHintCondition withProvider(String provider, String key, + Object value) { + return withProvider(this.providerConditions.size(), provider, + Collections.singletonMap(key, value)); + } + + public MetadataHintCondition withProvider(int index, String provider, + Map parameters) { + return new MetadataHintCondition(this.name, this.valueConditions, + add(this.providerConditions, + new ItemHintProviderCondition(index, provider, parameters))); + } + + private List add(List items, T item) { + List result = new ArrayList(items); + result.add(item); + return result; + } + + } + + private static class ItemHintValueCondition extends Condition { + + private final int index; + + private final Object value; + + private final String description; + + ItemHintValueCondition(int index, Object value, String description) { + this.index = index; + this.value = value; + this.description = description; + describedAs(createDescription()); + } + + private String createDescription() { + StringBuilder description = new StringBuilder(); + description.append("value hint at index '" + this.index + "'"); + if (this.value != null) { + description.append(" with value:").append(this.value); + } + if (this.description != null) { + description.append(" with description:").append(this.description); + } + return description.toString(); + } + + @Override + public boolean matches(ItemHint value) { + if (this.index + 1 > value.getValues().size()) { + return false; + } + ItemHint.ValueHint valueHint = value.getValues().get(this.index); + if (this.value != null && !this.value.equals(valueHint.getValue())) { + return false; + } + if (this.description != null + && !this.description.equals(valueHint.getDescription())) { + return false; + } + return true; + } + + } + + private static class ItemHintProviderCondition extends Condition { + + private final int index; + + private final String name; + + private final Map parameters; + + ItemHintProviderCondition(int index, String name, + Map parameters) { + this.index = index; + this.name = name; + this.parameters = parameters; + describedAs(createDescription()); + } + + public String createDescription() { + StringBuilder description = new StringBuilder(); + description.append("value provider"); + if (this.name != null) { + description.append(" with name:").append(this.name); + } + if (this.parameters != null) { + description.append(" with parameters:").append(this.parameters); + } + return description.toString(); + } + + @Override + public boolean matches(ItemHint hint) { + if (this.index + 1 > hint.getProviders().size()) { + return false; + } + ItemHint.ValueProvider valueProvider = hint.getProviders().get(this.index); + if (this.name != null && !this.name.equals(valueProvider.getName())) { + return false; + } + if (this.parameters != null) { + for (Map.Entry entry : this.parameters.entrySet()) { + if (!IsMapContaining.hasEntry(entry.getKey(), entry.getValue()) + .matches(valueProvider.getParameters())) { + return false; + } + } + } + return true; + } + + } + +} diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java index 698bf65018..6247f3e7da 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/TestConfigurationMetadataAnnotationProcessor.java @@ -28,6 +28,8 @@ import org.springframework.boot.configurationprocessor.metadata.ConfigurationMet import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller; /** + * Test {@link ConfigurationMetadataAnnotationProcessor}. + * * @author Stephane Nicoll * @author Phillip Webb * @author Andy Wilkinson diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java index 5f749fd4f9..f7b9f635fc 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/fieldvalues/AbstractFieldValuesProcessorTests.java @@ -29,7 +29,6 @@ import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; import javax.lang.model.element.TypeElement; -import org.hamcrest.Matcher; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; @@ -37,9 +36,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.configurationprocessor.TestCompiler; import org.springframework.boot.configurationsample.fieldvalues.FieldValues; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Abstract base class for {@link FieldValuesParser} tests. @@ -60,43 +57,37 @@ public abstract class AbstractFieldValuesProcessorTests { TestCompiler compiler = new TestCompiler(this.temporaryFolder); compiler.getTask(FieldValues.class).call(processor); Map values = processor.getValues(); - assertThat(values.get("string"), equalToObject("1")); - assertThat(values.get("stringNone"), nullValue()); - assertThat(values.get("stringConst"), equalToObject("c")); - assertThat(values.get("bool"), equalToObject(true)); - assertThat(values.get("boolNone"), equalToObject(false)); - assertThat(values.get("boolConst"), equalToObject(true)); - assertThat(values.get("boolObject"), equalToObject(true)); - assertThat(values.get("boolObjectNone"), nullValue()); - assertThat(values.get("boolObjectConst"), equalToObject(true)); - assertThat(values.get("integer"), equalToObject(1)); - assertThat(values.get("integerNone"), equalToObject(0)); - assertThat(values.get("integerConst"), equalToObject(2)); - assertThat(values.get("integerObject"), equalToObject(3)); - assertThat(values.get("integerObjectNone"), nullValue()); - assertThat(values.get("integerObjectConst"), equalToObject(4)); - assertThat(values.get("charset"), equalToObject("US-ASCII")); - assertThat(values.get("charsetConst"), equalToObject("UTF-8")); - assertThat(values.get("mimeType"), equalToObject("text/html")); - assertThat(values.get("mimeTypeConst"), equalToObject("text/plain")); - assertThat(values.get("object"), equalToObject(123)); - assertThat(values.get("objectNone"), nullValue()); - assertThat(values.get("objectConst"), equalToObject("c")); - assertThat(values.get("objectInstance"), nullValue()); - assertThat(values.get("stringArray"), - equalToObject(new Object[] { "FOO", "BAR" })); - assertThat(values.get("stringArrayNone"), nullValue()); - assertThat(values.get("stringEmptyArray"), equalToObject(new Object[0])); - assertThat(values.get("stringArrayConst"), - equalToObject(new Object[] { "OK", "KO" })); - assertThat(values.get("stringArrayConstElements"), - equalToObject(new Object[] { "c" })); - assertThat(values.get("integerArray"), equalToObject(new Object[] { 42, 24 })); - assertThat(values.get("unknownArray"), nullValue()); - } - - private Matcher equalToObject(Object object) { - return equalTo(object); + assertThat(values.get("string")).isEqualTo("1"); + assertThat(values.get("stringNone")).isNull(); + assertThat(values.get("stringConst")).isEqualTo("c"); + assertThat(values.get("bool")).isEqualTo(true); + assertThat(values.get("boolNone")).isEqualTo(false); + assertThat(values.get("boolConst")).isEqualTo(true); + assertThat(values.get("boolObject")).isEqualTo(true); + assertThat(values.get("boolObjectNone")).isNull(); + assertThat(values.get("boolObjectConst")).isEqualTo(true); + assertThat(values.get("integer")).isEqualTo(1); + assertThat(values.get("integerNone")).isEqualTo(0); + assertThat(values.get("integerConst")).isEqualTo(2); + assertThat(values.get("integerObject")).isEqualTo(3); + assertThat(values.get("integerObjectNone")).isNull(); + assertThat(values.get("integerObjectConst")).isEqualTo(4); + assertThat(values.get("charset")).isEqualTo("US-ASCII"); + assertThat(values.get("charsetConst")).isEqualTo("UTF-8"); + assertThat(values.get("mimeType")).isEqualTo("text/html"); + assertThat(values.get("mimeTypeConst")).isEqualTo("text/plain"); + assertThat(values.get("object")).isEqualTo(123); + assertThat(values.get("objectNone")).isNull(); + assertThat(values.get("objectConst")).isEqualTo("c"); + assertThat(values.get("objectInstance")).isNull(); + assertThat(values.get("stringArray")).isEqualTo(new Object[] { "FOO", "BAR" }); + assertThat(values.get("stringArrayNone")).isNull(); + assertThat(values.get("stringEmptyArray")).isEqualTo(new Object[0]); + assertThat(values.get("stringArrayConst")).isEqualTo(new Object[] { "OK", "KO" }); + assertThat(values.get("stringArrayConstElements")) + .isEqualTo(new Object[] { "c" }); + assertThat(values.get("integerArray")).isEqualTo(new Object[] { 42, 24 }); + assertThat(values.get("unknownArray")).isNull(); } @SupportedAnnotationTypes({ diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java index 3f60e7406e..395f3fd577 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/ConfigurationMetadataTests.java @@ -18,8 +18,7 @@ package org.springframework.boot.configurationprocessor.metadata; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link ConfigurationMetadata}. @@ -30,43 +29,44 @@ public class ConfigurationMetadataTests { @Test public void toDashedCaseCamelCase() { - assertThat(toDashedCase("simpleCamelCase"), is("simple-camel-case")); + assertThat(toDashedCase("simpleCamelCase")).isEqualTo("simple-camel-case"); } @Test public void toDashedCaseWordsUnderscore() { - assertThat(toDashedCase("Word_With_underscore"), is("word_with_underscore")); + assertThat(toDashedCase("Word_With_underscore")) + .isEqualTo("word_with_underscore"); } @Test public void toDashedCaseWordsSeveralUnderscores() { - assertThat(toDashedCase("Word___With__underscore"), - is("word___with__underscore")); + assertThat(toDashedCase("Word___With__underscore")) + .isEqualTo("word___with__underscore"); } @Test public void toDashedCaseLowerCaseUnderscore() { - assertThat(toDashedCase("lower_underscore"), is("lower_underscore")); + assertThat(toDashedCase("lower_underscore")).isEqualTo("lower_underscore"); } @Test public void toDashedCaseUpperUnderscore() { - assertThat(toDashedCase("UPPER_UNDERSCORE"), is("upper_underscore")); + assertThat(toDashedCase("UPPER_UNDERSCORE")).isEqualTo("upper_underscore"); } @Test public void toDashedCaseMultipleUnderscores() { - assertThat(toDashedCase("super___crazy"), is("super___crazy")); + assertThat(toDashedCase("super___crazy")).isEqualTo("super___crazy"); } @Test public void toDashedCaseUppercase() { - assertThat(toDashedCase("UPPERCASE"), is("uppercase")); + assertThat(toDashedCase("UPPERCASE")).isEqualTo("uppercase"); } @Test public void toDashedCaseLowercase() { - assertThat(toDashedCase("lowercase"), is("lowercase")); + assertThat(toDashedCase("lowercase")).isEqualTo("lowercase"); } private String toDashedCase(String name) { diff --git a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java index 820794eb16..bc63a345da 100644 --- a/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java +++ b/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/JsonMarshallerTests.java @@ -25,11 +25,9 @@ import java.util.Collections; import org.junit.Test; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsGroup; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsHint; -import static org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.containsProperty; +import org.springframework.boot.configurationprocessor.Metadata; + +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link JsonMarshaller}. @@ -70,22 +68,21 @@ public class JsonMarshallerTests { marshaller.write(metadata, outputStream); ConfigurationMetadata read = marshaller .read(new ByteArrayInputStream(outputStream.toByteArray())); - assertThat(read, - containsProperty("a.b", StringBuffer.class).fromSource(InputStream.class) - .withDescription("desc").withDefaultValue(is("x")) - .withDeprecation("Deprecation comment", "b.c.d")); - assertThat(read, containsProperty("b.c.d")); - assertThat(read, containsProperty("c").withDefaultValue(is(123))); - assertThat(read, containsProperty("d").withDefaultValue(is(true))); - assertThat(read, - containsProperty("e").withDefaultValue(is(new String[] { "y", "n" }))); - assertThat(read, containsProperty("f") - .withDefaultValue(is(new boolean[] { true, false }))); - assertThat(read, containsGroup("d")); - assertThat(read, containsHint("a.b")); - assertThat(read, - containsHint("c").withValue(0, 123, "hey").withValue(1, 456, null)); - assertThat(read, containsHint("d").withProvider("first", "target", "foo") + assertThat(read).has(Metadata.withProperty("a.b", StringBuffer.class) + .fromSource(InputStream.class).withDescription("desc") + .withDefaultValue("x").withDeprecation("Deprecation comment", "b.c.d")); + assertThat(read).has(Metadata.withProperty("b.c.d")); + assertThat(read).has(Metadata.withProperty("c").withDefaultValue(123)); + assertThat(read).has(Metadata.withProperty("d").withDefaultValue(true)); + assertThat(read).has( + Metadata.withProperty("e").withDefaultValue(new String[] { "y", "n" })); + assertThat(read).has(Metadata.withProperty("f") + .withDefaultValue(new Object[] { true, false })); + assertThat(read).has(Metadata.withGroup("d")); + assertThat(read).has(Metadata.withHint("a.b")); + assertThat(read).has( + Metadata.withHint("c").withValue(0, 123, "hey").withValue(1, 456, null)); + assertThat(read).has(Metadata.withHint("d").withProvider("first", "target", "foo") .withProvider("second")); } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java index fec531b74e..71c54eef50 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/DefaultLaunchScriptTests.java @@ -26,9 +26,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.util.FileCopyUtils; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link DefaultLaunchScript}. @@ -45,7 +43,7 @@ public class DefaultLaunchScriptTests { public void loadsDefaultScript() throws Exception { DefaultLaunchScript script = new DefaultLaunchScript(null, null); String content = new String(script.toByteArray()); - assertThat(content, containsString("Spring Boot Startup Script")); + assertThat(content).contains("Spring Boot Startup Script"); } @Test @@ -82,14 +80,14 @@ public class DefaultLaunchScriptTests { public void defaultForUseStartStopDaemonIsTrue() throws Exception { DefaultLaunchScript script = new DefaultLaunchScript(null, null); String content = new String(script.toByteArray()); - assertThat(content, containsString("USE_START_STOP_DAEMON=\"true\"")); + assertThat(content).contains("USE_START_STOP_DAEMON=\"true\""); } @Test public void defaultForModeIsAuto() throws Exception { DefaultLaunchScript script = new DefaultLaunchScript(null, null); String content = new String(script.toByteArray()); - assertThat(content, containsString("MODE=\"auto\"")); + assertThat(content).contains("MODE=\"auto\""); } @Test @@ -98,7 +96,7 @@ public class DefaultLaunchScriptTests { FileCopyUtils.copy("ABC".getBytes(), file); DefaultLaunchScript script = new DefaultLaunchScript(file, null); String content = new String(script.toByteArray()); - assertThat(content, equalTo("ABC")); + assertThat(content).isEqualTo("ABC"); } @Test @@ -108,7 +106,7 @@ public class DefaultLaunchScriptTests { DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:e", "b:o")); String content = new String(script.toByteArray()); - assertThat(content, equalTo("hello")); + assertThat(content).isEqualTo("hello"); } @Test @@ -118,7 +116,7 @@ public class DefaultLaunchScriptTests { DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:e", "b:o")); String content = new String(script.toByteArray()); - assertThat(content, equalTo("hel\nlo")); + assertThat(content).isEqualTo("hel\nlo"); } @Test @@ -127,7 +125,7 @@ public class DefaultLaunchScriptTests { FileCopyUtils.copy("h{{a:e}}ll{{b:o}}".getBytes(), file); DefaultLaunchScript script = new DefaultLaunchScript(file, null); String content = new String(script.toByteArray()); - assertThat(content, equalTo("hello")); + assertThat(content).isEqualTo("hello"); } @Test @@ -137,7 +135,7 @@ public class DefaultLaunchScriptTests { DefaultLaunchScript script = new DefaultLaunchScript(file, createProperties("a:a")); String content = new String(script.toByteArray()); - assertThat(content, equalTo("hallo")); + assertThat(content).isEqualTo("hallo"); } @Test @@ -146,14 +144,14 @@ public class DefaultLaunchScriptTests { FileCopyUtils.copy("h{{a}}ll{{b}}".getBytes(), file); DefaultLaunchScript script = new DefaultLaunchScript(file, null); String content = new String(script.toByteArray()); - assertThat(content, equalTo("h{{a}}ll{{b}}")); + assertThat(content).isEqualTo("h{{a}}ll{{b}}"); } private void assertThatPlaceholderCanBeReplaced(String placeholder) throws Exception { DefaultLaunchScript script = new DefaultLaunchScript(null, createProperties(placeholder + ":__test__")); String content = new String(script.toByteArray()); - assertThat(content, containsString("__test__")); + assertThat(content).contains("__test__"); } private Map createProperties(String... pairs) { diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java index 25a09229c0..3525323b8f 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/FileUtilsTests.java @@ -28,10 +28,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.util.FileSystemUtils; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link FileUtils}. @@ -65,31 +62,31 @@ public class FileUtilsTests { new File(this.originDirectory, "logback.xml").createNewFile(); FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory); - assertFalse(file.exists()); + assertThat(file.exists()).isFalse(); } @Test public void nestedDuplicateFile() throws IOException { - assertTrue(new File(this.outputDirectory, "sub").mkdirs()); - assertTrue(new File(this.originDirectory, "sub").mkdirs()); + assertThat(new File(this.outputDirectory, "sub").mkdirs()).isTrue(); + assertThat(new File(this.originDirectory, "sub").mkdirs()).isTrue(); File file = new File(this.outputDirectory, "sub/logback.xml"); file.createNewFile(); new File(this.originDirectory, "sub/logback.xml").createNewFile(); FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory); - assertFalse(file.exists()); + assertThat(file.exists()).isFalse(); } @Test public void nestedNonDuplicateFile() throws IOException { - assertTrue(new File(this.outputDirectory, "sub").mkdirs()); - assertTrue(new File(this.originDirectory, "sub").mkdirs()); + assertThat(new File(this.outputDirectory, "sub").mkdirs()).isTrue(); + assertThat(new File(this.originDirectory, "sub").mkdirs()).isTrue(); File file = new File(this.outputDirectory, "sub/logback.xml"); file.createNewFile(); new File(this.originDirectory, "sub/different.xml").createNewFile(); FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory); - assertTrue(file.exists()); + assertThat(file.exists()).isTrue(); } @Test @@ -99,7 +96,7 @@ public class FileUtilsTests { new File(this.originDirectory, "different.xml").createNewFile(); FileUtils.removeDuplicatesFromOutputDirectory(this.outputDirectory, this.originDirectory); - assertTrue(file.exists()); + assertThat(file.exists()).isTrue(); } @Test @@ -112,8 +109,8 @@ public class FileUtilsTests { finally { outputStream.close(); } - assertThat(FileUtils.sha1Hash(file), - equalTo("7037807198c22a7d2b0807371d763779a84fdfcf")); + assertThat(FileUtils.sha1Hash(file)) + .isEqualTo("7037807198c22a7d2b0807371d763779a84fdfcf"); } } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/JvmUtilsTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/JvmUtilsTests.java index 8bcb4e054f..f4e321f0bf 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/JvmUtilsTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/JvmUtilsTests.java @@ -21,9 +21,7 @@ import java.net.URL; import org.junit.Test; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link JvmUtils}. @@ -36,8 +34,8 @@ public class JvmUtilsTests { public void getToolsJar() throws Exception { URL jarUrl = JvmUtils.getToolsJarUrl(); // System.out.println(jarUrl); - assertThat(jarUrl.toString(), endsWith(".jar")); - assertThat(new File(jarUrl.toURI()).exists(), equalTo(true)); + assertThat(jarUrl.toString()).endsWith(".jar"); + assertThat(new File(jarUrl.toURI()).exists()).isTrue(); } } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/LayoutsTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/LayoutsTests.java index 8fc7597924..42ee8fad23 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/LayoutsTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/LayoutsTests.java @@ -22,10 +22,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link Layouts}. @@ -40,18 +37,20 @@ public class LayoutsTests { @Test public void jarFile() throws Exception { - assertThat(Layouts.forFile(new File("test.jar")), instanceOf(Layouts.Jar.class)); - assertThat(Layouts.forFile(new File("test.JAR")), instanceOf(Layouts.Jar.class)); - assertThat(Layouts.forFile(new File("test.jAr")), instanceOf(Layouts.Jar.class)); - assertThat(Layouts.forFile(new File("te.st.jar")), instanceOf(Layouts.Jar.class)); + assertThat(Layouts.forFile(new File("test.jar"))).isInstanceOf(Layouts.Jar.class); + assertThat(Layouts.forFile(new File("test.JAR"))).isInstanceOf(Layouts.Jar.class); + assertThat(Layouts.forFile(new File("test.jAr"))).isInstanceOf(Layouts.Jar.class); + assertThat(Layouts.forFile(new File("te.st.jar"))) + .isInstanceOf(Layouts.Jar.class); } @Test public void warFile() throws Exception { - assertThat(Layouts.forFile(new File("test.war")), instanceOf(Layouts.War.class)); - assertThat(Layouts.forFile(new File("test.WAR")), instanceOf(Layouts.War.class)); - assertThat(Layouts.forFile(new File("test.wAr")), instanceOf(Layouts.War.class)); - assertThat(Layouts.forFile(new File("te.st.war")), instanceOf(Layouts.War.class)); + assertThat(Layouts.forFile(new File("test.war"))).isInstanceOf(Layouts.War.class); + assertThat(Layouts.forFile(new File("test.WAR"))).isInstanceOf(Layouts.War.class); + assertThat(Layouts.forFile(new File("test.wAr"))).isInstanceOf(Layouts.War.class); + assertThat(Layouts.forFile(new File("te.st.war"))) + .isInstanceOf(Layouts.War.class); } @Test @@ -64,40 +63,40 @@ public class LayoutsTests { @Test public void jarLayout() throws Exception { Layout layout = new Layouts.Jar(); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE), - equalTo("lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM), - equalTo("lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED), - equalTo("lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME), - equalTo("lib/")); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE)) + .isEqualTo("lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM)) + .isEqualTo("lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED)) + .isEqualTo("lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME)) + .isEqualTo("lib/"); } @Test public void warLayout() throws Exception { Layout layout = new Layouts.War(); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE), - equalTo("WEB-INF/lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM), - equalTo("WEB-INF/lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED), - equalTo("WEB-INF/lib-provided/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME), - equalTo("WEB-INF/lib/")); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE)) + .isEqualTo("WEB-INF/lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM)) + .isEqualTo("WEB-INF/lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED)) + .isEqualTo("WEB-INF/lib-provided/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME)) + .isEqualTo("WEB-INF/lib/"); } @Test public void moduleLayout() throws Exception { Layout layout = new Layouts.Module(); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE), - equalTo("lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED), - nullValue()); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME), - equalTo("lib/")); - assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM), - equalTo("lib/")); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.COMPILE)) + .isEqualTo("lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.PROVIDED)) + .isNull(); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.RUNTIME)) + .isEqualTo("lib/"); + assertThat(layout.getLibraryDestination("lib.jar", LibraryScope.CUSTOM)) + .isEqualTo("lib/"); } } diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/MainClassFinderTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/MainClassFinderTests.java index fe2062e943..9d7d196d29 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/MainClassFinderTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/MainClassFinderTests.java @@ -30,8 +30,7 @@ import org.springframework.boot.loader.tools.MainClassFinder.ClassNameCallback; import org.springframework.boot.loader.tools.sample.ClassWithMainMethod; import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link MainClassFinder}. @@ -58,7 +57,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("B.class", ClassWithMainMethod.class); this.testJarFile.addClass("A.class", ClassWithoutMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(), ""); - assertThat(actual, equalTo("B")); + assertThat(actual).isEqualTo("B"); } @Test @@ -67,7 +66,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/b/c/E.class", ClassWithoutMainMethod.class); this.testJarFile.addClass("a/b/F.class", ClassWithoutMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(), ""); - assertThat(actual, equalTo("a.b.c.D")); + assertThat(actual).isEqualTo("a.b.c.D"); } @Test @@ -75,7 +74,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(), ""); - assertThat(actual, equalTo("a.B")); + assertThat(actual).isEqualTo("a.B"); } @Test @@ -94,7 +93,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarFile(), "a/"); - assertThat(actual, equalTo("B")); + assertThat(actual).isEqualTo("B"); } @@ -103,7 +102,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("B.class", ClassWithMainMethod.class); this.testJarFile.addClass("A.class", ClassWithoutMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarSource()); - assertThat(actual, equalTo("B")); + assertThat(actual).isEqualTo("B"); } @Test @@ -112,7 +111,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/b/c/E.class", ClassWithoutMainMethod.class); this.testJarFile.addClass("a/b/F.class", ClassWithoutMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarSource()); - assertThat(actual, equalTo("a.b.c.D")); + assertThat(actual).isEqualTo("a.b.c.D"); } @Test @@ -120,7 +119,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/B.class", ClassWithMainMethod.class); this.testJarFile.addClass("a/b/c/E.class", ClassWithMainMethod.class); String actual = MainClassFinder.findMainClass(this.testJarFile.getJarSource()); - assertThat(actual, equalTo("a.B")); + assertThat(actual).isEqualTo("a.B"); } @Test @@ -141,7 +140,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/b/G.class", ClassWithMainMethod.class); ClassNameCollector callback = new ClassNameCollector(); MainClassFinder.doWithMainClasses(this.testJarFile.getJarSource(), callback); - assertThat(callback.getClassNames().toString(), equalTo("[a.b.G, a.b.c.D]")); + assertThat(callback.getClassNames().toString()).isEqualTo("[a.b.G, a.b.c.D]"); } @Test @@ -152,7 +151,7 @@ public class MainClassFinderTests { this.testJarFile.addClass("a/b/G.class", ClassWithMainMethod.class); ClassNameCollector callback = new ClassNameCollector(); MainClassFinder.doWithMainClasses(this.testJarFile.getJarFile(), "", callback); - assertThat(callback.getClassNames().toString(), equalTo("[a.b.G, a.b.c.D]")); + assertThat(callback.getClassNames().toString()).isEqualTo("[a.b.G, a.b.c.D]"); } private static class ClassNameCollector implements ClassNameCallback { diff --git a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java index 78ecccfc39..8d0e2707f4 100644 --- a/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java +++ b/spring-boot-tools/spring-boot-loader-tools/src/test/java/org/springframework/boot/loader/tools/RepackagerTests.java @@ -38,10 +38,7 @@ import org.springframework.boot.loader.tools.sample.ClassWithMainMethod; import org.springframework.boot.loader.tools.sample.ClassWithoutMainMethod; import org.springframework.util.FileCopyUtils; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; @@ -112,11 +109,11 @@ public class RepackagerTests { repackager.setMainClass("a.b.C"); repackager.repackage(NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo("org.springframework.boot.loader.JarLauncher")); - assertThat(actualManifest.getMainAttributes().getValue("Start-Class"), - equalTo("a.b.C")); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.JarLauncher"); + assertThat(actualManifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("a.b.C"); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -130,11 +127,11 @@ public class RepackagerTests { Repackager repackager = new Repackager(file); repackager.repackage(NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo("org.springframework.boot.loader.JarLauncher")); - assertThat(actualManifest.getMainAttributes().getValue("Start-Class"), - equalTo("a.b.C")); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.JarLauncher"); + assertThat(actualManifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("a.b.C"); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -144,11 +141,11 @@ public class RepackagerTests { Repackager repackager = new Repackager(file); repackager.repackage(NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo("org.springframework.boot.loader.JarLauncher")); - assertThat(actualManifest.getMainAttributes().getValue("Start-Class"), - equalTo("a.b.C")); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.JarLauncher"); + assertThat(actualManifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("a.b.C"); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -159,11 +156,11 @@ public class RepackagerTests { repackager.repackage(NO_LIBRARIES); repackager.repackage(NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo("org.springframework.boot.loader.JarLauncher")); - assertThat(actualManifest.getMainAttributes().getValue("Start-Class"), - equalTo("a.b.C")); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.JarLauncher"); + assertThat(actualManifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("a.b.C"); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -194,9 +191,9 @@ public class RepackagerTests { repackager.setLayout(new Layouts.None()); repackager.repackage(file, NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo("a.b.C")); - assertThat(hasLauncherClasses(file), equalTo(false)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("a.b.C"); + assertThat(hasLauncherClasses(file)).isFalse(); } @Test @@ -207,9 +204,9 @@ public class RepackagerTests { repackager.setLayout(new Layouts.None()); repackager.repackage(file, NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes().getValue("Main-Class"), - equalTo(null)); - assertThat(hasLauncherClasses(file), equalTo(false)); + assertThat(actualManifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo(null); + assertThat(hasLauncherClasses(file)).isFalse(); } @Test @@ -218,9 +215,8 @@ public class RepackagerTests { File file = this.testJarFile.getFile(); Repackager repackager = new Repackager(file); repackager.repackage(NO_LIBRARIES); - assertThat(new File(file.getParent(), file.getName() + ".original").exists(), - equalTo(true)); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(new File(file.getParent(), file.getName() + ".original")).exists(); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -230,9 +226,9 @@ public class RepackagerTests { Repackager repackager = new Repackager(file); repackager.setBackupSource(false); repackager.repackage(NO_LIBRARIES); - assertThat(new File(file.getParent(), file.getName() + ".original").exists(), - equalTo(false)); - assertThat(hasLauncherClasses(file), equalTo(true)); + assertThat(new File(file.getParent(), file.getName() + ".original")) + .doesNotExist(); + assertThat(hasLauncherClasses(file)).isTrue(); } @Test @@ -242,10 +238,10 @@ public class RepackagerTests { File dest = this.temporaryFolder.newFile("different.jar"); Repackager repackager = new Repackager(source); repackager.repackage(dest, NO_LIBRARIES); - assertThat(new File(source.getParent(), source.getName() + ".original").exists(), - equalTo(false)); - assertThat(hasLauncherClasses(source), equalTo(false)); - assertThat(hasLauncherClasses(dest), equalTo(true)); + assertThat(new File(source.getParent(), source.getName() + ".original")) + .doesNotExist(); + assertThat(hasLauncherClasses(source)).isFalse(); + assertThat(hasLauncherClasses(dest)).isTrue(); } @Test @@ -273,7 +269,7 @@ public class RepackagerTests { File dest = this.temporaryFolder.newFile("dest.jar"); dest.createNewFile(); repackager.repackage(dest, NO_LIBRARIES); - assertThat(hasLauncherClasses(dest), equalTo(true)); + assertThat(hasLauncherClasses(dest)).isTrue(); } @Test @@ -309,14 +305,14 @@ public class RepackagerTests { callback.library(new Library(libNonJarFile, LibraryScope.COMPILE)); } }); - assertThat(hasEntry(file, "lib/" + libJarFile.getName()), equalTo(true)); - assertThat(hasEntry(file, "lib/" + libJarFileToUnpack.getName()), equalTo(true)); - assertThat(hasEntry(file, "lib/" + libNonJarFile.getName()), equalTo(false)); + assertThat(hasEntry(file, "lib/" + libJarFile.getName())).isTrue(); + assertThat(hasEntry(file, "lib/" + libJarFileToUnpack.getName())).isTrue(); + assertThat(hasEntry(file, "lib/" + libNonJarFile.getName())).isFalse(); JarEntry entry = getEntry(file, "lib/" + libJarFile.getName()); - assertThat(entry.getTime(), equalTo(JAN_1_1985)); + assertThat(entry.getTime()).isEqualTo(JAN_1_1985); entry = getEntry(file, "lib/" + libJarFileToUnpack.getName()); - assertThat(entry.getComment(), startsWith("UNPACK:")); - assertThat(entry.getComment().length(), equalTo(47)); + assertThat(entry.getComment()).startsWith("UNPACK:"); + assertThat(entry.getComment().length()).isEqualTo(47); } @Test @@ -357,9 +353,9 @@ public class RepackagerTests { callback.library(new Library(libJarFile, scope)); } }); - assertThat(hasEntry(file, "test/" + libJarFile.getName()), equalTo(true)); - assertThat(getManifest(file).getMainAttributes().getValue("Main-Class"), - equalTo("testLauncher")); + assertThat(hasEntry(file, "test/" + libJarFile.getName())).isTrue(); + assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")) + .isEqualTo("testLauncher"); } @Test @@ -369,8 +365,8 @@ public class RepackagerTests { Repackager repackager = new Repackager(file); repackager.repackage(NO_LIBRARIES); Manifest actualManifest = getManifest(file); - assertThat(actualManifest.getMainAttributes() - .containsKey(new Attributes.Name("Spring-Boot-Version")), equalTo(true)); + assertThat(actualManifest.getMainAttributes()) + .containsKey(new Attributes.Name("Spring-Boot-Version")); } @Test @@ -399,10 +395,10 @@ public class RepackagerTests { }); JarFile jarFile = new JarFile(file); try { - assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getMethod(), - equalTo(ZipEntry.STORED)); - assertThat(jarFile.getEntry("test/nested.jar").getMethod(), - equalTo(ZipEntry.STORED)); + assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getMethod()) + .isEqualTo(ZipEntry.STORED); + assertThat(jarFile.getEntry("test/nested.jar").getMethod()) + .isEqualTo(ZipEntry.STORED); } finally { jarFile.close(); @@ -418,12 +414,12 @@ public class RepackagerTests { LaunchScript script = new MockLauncherScript("ABC"); repackager.repackage(dest, NO_LIBRARIES, script); byte[] bytes = FileCopyUtils.copyToByteArray(dest); - assertThat(new String(bytes), startsWith("ABC")); - assertThat(hasLauncherClasses(source), equalTo(false)); - assertThat(hasLauncherClasses(dest), equalTo(true)); + assertThat(new String(bytes)).startsWith("ABC"); + assertThat(hasLauncherClasses(source)).isFalse(); + assertThat(hasLauncherClasses(dest)).isTrue(); try { - assertThat(Files.getPosixFilePermissions(dest.toPath()), - hasItem(PosixFilePermission.OWNER_EXECUTE)); + assertThat(Files.getPosixFilePermissions(dest.toPath())) + .contains(PosixFilePermission.OWNER_EXECUTE); } catch (UnsupportedOperationException ex) { // Probably running the test on Windows @@ -450,8 +446,8 @@ public class RepackagerTests { }); JarFile jarFile = new JarFile(file); try { - assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getComment(), - startsWith("UNPACK:")); + assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getComment()) + .startsWith("UNPACK:"); } finally { jarFile.close(); @@ -482,8 +478,8 @@ public class RepackagerTests { }); JarFile jarFile = new JarFile(file); try { - assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getSize(), - equalTo(sourceLength)); + assertThat(jarFile.getEntry("lib/" + nestedFile.getName()).getSize()) + .isEqualTo(sourceLength); } finally { jarFile.close(); diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/ExecutableArchiveLauncherTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/ExecutableArchiveLauncherTests.java index 4d8846835e..cadfba4fe5 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/ExecutableArchiveLauncherTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/ExecutableArchiveLauncherTests.java @@ -28,8 +28,7 @@ import org.mockito.MockitoAnnotations; import org.springframework.boot.loader.archive.Archive.Entry; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; /** @@ -87,8 +86,8 @@ public class ExecutableArchiveLauncherTests { } private void assertClassLoaderUrls(ClassLoader classLoader, URL[] urls) { - assertTrue(classLoader instanceof URLClassLoader); - assertArrayEquals(urls, ((URLClassLoader) classLoader).getURLs()); + assertThat(classLoader).isInstanceOf(URLClassLoader.class); + assertThat(((URLClassLoader) classLoader).getURLs()).isEqualTo(urls); } private void doWithTccl(ClassLoader classLoader, Callable action) diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/InputArgumentsJavaAgentDetectorTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/InputArgumentsJavaAgentDetectorTests.java index f4d930a992..5ca75e65e0 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/InputArgumentsJavaAgentDetectorTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/InputArgumentsJavaAgentDetectorTests.java @@ -23,8 +23,7 @@ import java.util.Arrays; import org.junit.Test; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link InputArgumentsJavaAgentDetector} @@ -38,24 +37,25 @@ public class InputArgumentsJavaAgentDetectorTests { throws MalformedURLException, IOException { InputArgumentsJavaAgentDetector detector = new InputArgumentsJavaAgentDetector( Arrays.asList("-javaagent:my-agent.jar")); - assertFalse(detector.isJavaAgentJar( - new File("something-else.jar").getCanonicalFile().toURI().toURL())); + assertThat(detector.isJavaAgentJar( + new File("something-else.jar").getCanonicalFile().toURI().toURL())) + .isFalse(); } @Test public void singleJavaAgent() throws MalformedURLException, IOException { InputArgumentsJavaAgentDetector detector = new InputArgumentsJavaAgentDetector( Arrays.asList("-javaagent:my-agent.jar")); - assertTrue(detector.isJavaAgentJar( - new File("my-agent.jar").getCanonicalFile().toURI().toURL())); + assertThat(detector.isJavaAgentJar( + new File("my-agent.jar").getCanonicalFile().toURI().toURL())).isTrue(); } @Test public void singleJavaAgentWithOptions() throws MalformedURLException, IOException { InputArgumentsJavaAgentDetector detector = new InputArgumentsJavaAgentDetector( Arrays.asList("-javaagent:my-agent.jar=a=alpha,b=bravo")); - assertTrue(detector.isJavaAgentJar( - new File("my-agent.jar").getCanonicalFile().toURI().toURL())); + assertThat(detector.isJavaAgentJar( + new File("my-agent.jar").getCanonicalFile().toURI().toURL())).isTrue(); } @Test @@ -63,10 +63,11 @@ public class InputArgumentsJavaAgentDetectorTests { InputArgumentsJavaAgentDetector detector = new InputArgumentsJavaAgentDetector( Arrays.asList("-javaagent:my-agent.jar", "-javaagent:my-other-agent.jar")); - assertTrue(detector.isJavaAgentJar( - new File("my-agent.jar").getCanonicalFile().toURI().toURL())); - assertTrue(detector.isJavaAgentJar( - new File("my-other-agent.jar").getCanonicalFile().toURI().toURL())); + assertThat(detector.isJavaAgentJar( + new File("my-agent.jar").getCanonicalFile().toURI().toURL())).isTrue(); + assertThat(detector.isJavaAgentJar( + new File("my-other-agent.jar").getCanonicalFile().toURI().toURL())) + .isTrue(); } } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java index 498796e7d9..bcb9b997d6 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/LaunchedURLClassLoaderTests.java @@ -25,11 +25,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.loader.jar.JarFile; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link LaunchedURLClassLoader}. @@ -47,13 +43,13 @@ public class LaunchedURLClassLoaderTests { public void resolveResourceFromWindowsFilesystem() throws Exception { // This path is invalid - it should return null even on Windows. // A regular URLClassLoader will deal with it gracefully. - assertNull(getClass().getClassLoader() - .getResource("c:\\Users\\user\\bar.properties")); + assertThat(getClass().getClassLoader() + .getResource("c:\\Users\\user\\bar.properties")).isNull(); LaunchedURLClassLoader loader = new LaunchedURLClassLoader( new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader()); // So we should too... - assertNull(loader.getResource("c:\\Users\\user\\bar.properties")); + assertThat(loader.getResource("c:\\Users\\user\\bar.properties")).isNull(); } @Test @@ -61,7 +57,7 @@ public class LaunchedURLClassLoaderTests { LaunchedURLClassLoader loader = new LaunchedURLClassLoader( new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader()); - assertNotNull(loader.getResource("demo/Application.java")); + assertThat(loader.getResource("demo/Application.java")).isNotNull(); } @Test @@ -69,7 +65,8 @@ public class LaunchedURLClassLoaderTests { LaunchedURLClassLoader loader = new LaunchedURLClassLoader( new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader()); - assertTrue(loader.getResources("demo/Application.java").hasMoreElements()); + assertThat(loader.getResources("demo/Application.java").hasMoreElements()) + .isTrue(); } @Test @@ -77,7 +74,7 @@ public class LaunchedURLClassLoaderTests { LaunchedURLClassLoader loader = new LaunchedURLClassLoader( new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader()); - assertNotNull(loader.getResource("")); + assertThat(loader.getResource("")).isNotNull(); } @Test @@ -85,7 +82,7 @@ public class LaunchedURLClassLoaderTests { LaunchedURLClassLoader loader = new LaunchedURLClassLoader( new URL[] { new URL("jar:file:src/test/resources/jars/app.jar!/") }, getClass().getClassLoader()); - assertTrue(loader.getResources("").hasMoreElements()); + assertThat(loader.getResources("").hasMoreElements()).isTrue(); } @Test @@ -97,8 +94,8 @@ public class LaunchedURLClassLoaderTests { LaunchedURLClassLoader loader = new LaunchedURLClassLoader(new URL[] { url }, null); URL resource = loader.getResource("nested.jar!/3.dat"); - assertThat(resource.toString(), equalTo(url + "nested.jar!/3.dat")); - assertThat(resource.openConnection().getInputStream().read(), equalTo(3)); + assertThat(resource.toString()).isEqualTo(url + "nested.jar!/3.dat"); + assertThat(resource.openConnection().getInputStream().read()).isEqualTo(3); } } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java index bcaea1d186..d0fb1c4989 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/PropertiesLauncherTests.java @@ -34,13 +34,7 @@ import org.mockito.MockitoAnnotations; import org.springframework.boot.loader.archive.Archive; import org.springframework.test.util.ReflectionTestUtils; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.verify; @@ -78,31 +72,32 @@ public class PropertiesLauncherTests { @Test public void testDefaultHome() { PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals(new File(System.getProperty("loader.home")), - launcher.getHomeDirectory()); + assertThat(launcher.getHomeDirectory()) + .isEqualTo(new File(System.getProperty("loader.home"))); } @Test public void testUserSpecifiedMain() throws Exception { PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("demo.Application", launcher.getMainClass()); - assertNull(System.getProperty("loader.main")); + assertThat(launcher.getMainClass()).isEqualTo("demo.Application"); + assertThat(System.getProperty("loader.main")).isNull(); } @Test public void testUserSpecifiedConfigName() throws Exception { System.setProperty("loader.config.name", "foo"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("my.Application", launcher.getMainClass()); - assertEquals("[etc/]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(launcher.getMainClass()).isEqualTo("my.Application"); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[etc/]"); } @Test public void testUserSpecifiedDotPath() throws Exception { System.setProperty("loader.path", "."); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[.]", ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[.]"); } @Test @@ -110,8 +105,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.path", "jars/*"); System.setProperty("loader.main", "demo.Application"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[jars/]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[jars/]"); launcher.launch(new String[0]); waitFor("Hello World"); } @@ -121,8 +116,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.path", "jars/app.jar"); System.setProperty("loader.main", "demo.Application"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[jars/app.jar]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[jars/app.jar]"); launcher.launch(new String[0]); waitFor("Hello World"); } @@ -132,8 +127,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.path", "./jars/app.jar"); System.setProperty("loader.main", "demo.Application"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[jars/app.jar]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[jars/app.jar]"); launcher.launch(new String[0]); waitFor("Hello World"); } @@ -143,8 +138,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.path", "jars/app.jar"); System.setProperty("loader.classLoader", URLClassLoader.class.getName()); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[jars/app.jar]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[jars/app.jar]"); launcher.launch(new String[0]); waitFor("Hello World"); } @@ -154,8 +149,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.path", "more-jars/app.jar,jars/app.jar"); System.setProperty("loader.classLoader", URLClassLoader.class.getName()); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[more-jars/app.jar, jars/app.jar]", - ReflectionTestUtils.getField(launcher, "paths").toString()); + assertThat(ReflectionTestUtils.getField(launcher, "paths").toString()) + .isEqualTo("[more-jars/app.jar, jars/app.jar]"); launcher.launch(new String[0]); waitFor("Hello Other World"); } @@ -165,8 +160,8 @@ public class PropertiesLauncherTests { System.setProperty("loader.classLoader", TestLoader.class.getName()); PropertiesLauncher launcher = new PropertiesLauncher(); ClassLoader loader = launcher.createClassLoader(Collections.emptyList()); - assertNotNull(loader); - assertEquals(TestLoader.class.getName(), loader.getClass().getName()); + assertThat(loader).isNotNull(); + assertThat(loader.getClass().getName()).isEqualTo(TestLoader.class.getName()); } @Test @@ -175,28 +170,29 @@ public class PropertiesLauncherTests { System.setProperty("loader.config.name", "foo"); System.setProperty("loader.config.location", "classpath:bar.properties"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("my.BarApplication", launcher.getMainClass()); + assertThat(launcher.getMainClass()).isEqualTo("my.BarApplication"); } @Test public void testSystemPropertySpecifiedMain() throws Exception { System.setProperty("loader.main", "foo.Bar"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("foo.Bar", launcher.getMainClass()); + assertThat(launcher.getMainClass()).isEqualTo("foo.Bar"); } @Test public void testSystemPropertiesSet() throws Exception { System.setProperty("loader.system", "true"); new PropertiesLauncher(); - assertEquals("demo.Application", System.getProperty("loader.main")); + assertThat(System.getProperty("loader.main")).isEqualTo("demo.Application"); } @Test public void testArgsEnhanced() throws Exception { System.setProperty("loader.args", "foo"); PropertiesLauncher launcher = new PropertiesLauncher(); - assertEquals("[foo, bar]", Arrays.asList(launcher.getArgs("bar")).toString()); + assertThat(Arrays.asList(launcher.getArgs("bar")).toString()) + .isEqualTo("[foo, bar]"); } @Test @@ -208,8 +204,7 @@ public class PropertiesLauncherTests { } List nonAgentArchives = new PropertiesLauncher(this.javaAgentDetector) .getClassPathArchives(); - assertThat(nonAgentArchives.size(), - is(equalTo(allArchives.size() - parentUrls.length))); + assertThat(nonAgentArchives).hasSize(allArchives.size() - parentUrls.length); for (URL url : parentUrls) { verify(this.javaAgentDetector).isJavaAgentJar(url); } @@ -223,7 +218,7 @@ public class PropertiesLauncherTests { Thread.sleep(50L); timeout = this.output.toString().contains(value); } - assertTrue("Timed out waiting for (" + value + ")", timeout); + assertThat(timeout).as("Timed out waiting for (" + value + ")").isTrue(); } public static class TestLoader extends URLClassLoader { diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/WarLauncherTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/WarLauncherTests.java index 53cc6cd749..6d1e7ad0e5 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/WarLauncherTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/WarLauncherTests.java @@ -38,9 +38,7 @@ import org.springframework.boot.loader.archive.ExplodedArchive; import org.springframework.boot.loader.archive.JarFileArchive; import org.springframework.util.FileSystemUtils; -import static org.hamcrest.Matchers.hasItems; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link WarLauncher} @@ -61,28 +59,23 @@ public class WarLauncherTests { webInfLib.mkdirs(); File webInfLibFoo = new File(webInfLib, "foo.jar"); new JarOutputStream(new FileOutputStream(webInfLibFoo)).close(); - WarLauncher launcher = new WarLauncher(new ExplodedArchive(warRoot, true)); List archives = launcher.getClassPathArchives(); - assertEquals(2, archives.size()); - - assertThat(getUrls(archives), hasItems(webInfClasses.toURI().toURL(), - new URL("jar:" + webInfLibFoo.toURI().toURL() + "!/"))); + assertThat(archives).hasSize(2); + assertThat(getUrls(archives)).containsOnly(webInfClasses.toURI().toURL(), + new URL("jar:" + webInfLibFoo.toURI().toURL() + "!/")); } @Test public void archivedWarHasOnlyWebInfClassesAndContentsOfWebInfLibOnClasspath() throws Exception { File warRoot = createWarArchive(); - WarLauncher launcher = new WarLauncher(new JarFileArchive(warRoot)); List archives = launcher.getClassPathArchives(); - assertEquals(2, archives.size()); - - assertThat(getUrls(archives), - hasItems(new URL("jar:" + warRoot.toURI().toURL() - + "!/WEB-INF/classes!/"), - new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/lib/foo.jar!/"))); + assertThat(archives).hasSize(2); + assertThat(getUrls(archives)).containsOnly( + new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/classes!/"), + new URL("jar:" + warRoot.toURI().toURL() + "!/WEB-INF/lib/foo.jar!/")); } private Set getUrls(List archives) throws MalformedURLException { @@ -96,14 +89,11 @@ public class WarLauncherTests { private File createWarArchive() throws IOException, FileNotFoundException { File warRoot = new File("target/archive.war"); warRoot.delete(); - JarOutputStream jarOutputStream = new JarOutputStream( new FileOutputStream(warRoot)); - jarOutputStream.putNextEntry(new JarEntry("WEB-INF/")); jarOutputStream.putNextEntry(new JarEntry("WEB-INF/classes/")); jarOutputStream.putNextEntry(new JarEntry("WEB-INF/lib/")); - JarEntry webInfLibFoo = new JarEntry("WEB-INF/lib/foo.jar"); webInfLibFoo.setMethod(ZipEntry.STORED); ByteArrayOutputStream fooJarStream = new ByteArrayOutputStream(); @@ -114,7 +104,6 @@ public class WarLauncherTests { webInfLibFoo.setCrc(crc32.getValue()); jarOutputStream.putNextEntry(webInfLibFoo); jarOutputStream.write(fooJarStream.toByteArray()); - jarOutputStream.close(); return warRoot; } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java index 06a9ab5a08..bad8fef271 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/ExplodedArchiveTests.java @@ -38,10 +38,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.loader.TestJarCreator; import org.springframework.boot.loader.archive.Archive.Entry; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link ExplodedArchive}. @@ -93,29 +90,29 @@ public class ExplodedArchiveTests { @Test public void getManifest() throws Exception { - assertThat(this.archive.getManifest().getMainAttributes().getValue("Built-By"), - equalTo("j1")); + assertThat(this.archive.getManifest().getMainAttributes().getValue("Built-By")) + .isEqualTo("j1"); } @Test public void getEntries() throws Exception { Map entries = getEntriesMap(this.archive); - assertThat(entries.size(), equalTo(10)); + assertThat(entries.size()).isEqualTo(10); } @Test public void getUrl() throws Exception { URL url = this.archive.getUrl(); - assertThat(new File(URLDecoder.decode(url.getFile(), "UTF-8")), - equalTo(this.rootFolder)); + assertThat(new File(URLDecoder.decode(url.getFile(), "UTF-8"))) + .isEqualTo(this.rootFolder); } @Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); - assertThat(nested.getUrl().toString(), - equalTo("jar:" + this.rootFolder.toURI() + "nested.jar!/")); + assertThat(nested.getUrl().toString()) + .isEqualTo("jar:" + this.rootFolder.toURI() + "nested.jar!/"); } @Test @@ -123,43 +120,44 @@ public class ExplodedArchiveTests { Entry entry = getEntriesMap(this.archive).get("d/"); Archive nested = this.archive.getNestedArchive(entry); Map nestedEntries = getEntriesMap(nested); - assertThat(nestedEntries.size(), equalTo(1)); - assertThat(nested.getUrl().toString(), - equalTo("file:" + this.rootFolder.toURI().getPath() + "d/")); + assertThat(nestedEntries.size()).isEqualTo(1); + assertThat(nested.getUrl().toString()) + .isEqualTo("file:" + this.rootFolder.toURI().getPath() + "d/"); } @Test public void getNonRecursiveEntriesForRoot() throws Exception { ExplodedArchive archive = new ExplodedArchive(new File("/"), false); Map entries = getEntriesMap(archive); - assertThat(entries.size(), greaterThan(1)); + assertThat(entries.size()).isGreaterThan(1); } @Test public void getNonRecursiveManifest() throws Exception { ExplodedArchive archive = new ExplodedArchive( new File("src/test/resources/root")); - assertNotNull(archive.getManifest()); + assertThat(archive.getManifest()).isNotNull(); Map entries = getEntriesMap(archive); - assertThat(entries.size(), equalTo(4)); + assertThat(entries.size()).isEqualTo(4); } @Test public void getNonRecursiveManifestEvenIfNonRecursive() throws Exception { ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"), false); - assertNotNull(archive.getManifest()); + assertThat(archive.getManifest()).isNotNull(); Map entries = getEntriesMap(archive); - assertThat(entries.size(), equalTo(3)); + assertThat(entries.size()).isEqualTo(3); } @Test public void getResourceAsStream() throws Exception { ExplodedArchive archive = new ExplodedArchive( new File("src/test/resources/root")); - assertNotNull(archive.getManifest()); + assertThat(archive.getManifest()).isNotNull(); URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() }); - assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml")); + assertThat(loader.getResourceAsStream("META-INF/spring/application.xml")) + .isNotNull(); loader.close(); } @@ -167,9 +165,10 @@ public class ExplodedArchiveTests { public void getResourceAsStreamNonRecursive() throws Exception { ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"), false); - assertNotNull(archive.getManifest()); + assertThat(archive.getManifest()).isNotNull(); URLClassLoader loader = new URLClassLoader(new URL[] { archive.getUrl() }); - assertNotNull(loader.getResourceAsStream("META-INF/spring/application.xml")); + assertThat(loader.getResourceAsStream("META-INF/spring/application.xml")) + .isNotNull(); loader.close(); } @@ -180,4 +179,5 @@ public class ExplodedArchiveTests { } return entries; } + } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java index 88c4b30a89..47241af3af 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java @@ -29,12 +29,7 @@ import org.junit.rules.TemporaryFolder; import org.springframework.boot.loader.TestJarCreator; import org.springframework.boot.loader.archive.Archive.Entry; -import static org.hamcrest.Matchers.endsWith; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link JarFileArchive}. @@ -67,28 +62,28 @@ public class JarFileArchiveTests { @Test public void getManifest() throws Exception { - assertThat(this.archive.getManifest().getMainAttributes().getValue("Built-By"), - equalTo("j1")); + assertThat(this.archive.getManifest().getMainAttributes().getValue("Built-By")) + .isEqualTo("j1"); } @Test public void getEntries() throws Exception { Map entries = getEntriesMap(this.archive); - assertThat(entries.size(), equalTo(10)); + assertThat(entries.size()).isEqualTo(10); } @Test public void getUrl() throws Exception { URL url = this.archive.getUrl(); - assertThat(url.toString(), equalTo("jar:" + this.rootJarFileUrl + "!/")); + assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFileUrl + "!/"); } @Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); - assertThat(nested.getUrl().toString(), - equalTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/")); + assertThat(nested.getUrl().toString()) + .isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/"); } @Test @@ -96,8 +91,8 @@ public class JarFileArchiveTests { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); - assertThat(nested.getUrl().toString(), startsWith("file:")); - assertThat(nested.getUrl().toString(), endsWith("/nested.jar")); + assertThat(nested.getUrl().toString()).startsWith("file:"); + assertThat(nested.getUrl().toString()).endsWith("/nested.jar"); } @Test @@ -108,7 +103,7 @@ public class JarFileArchiveTests { setup(true); entry = getEntriesMap(this.archive).get("nested.jar"); URL secondNested = this.archive.getNestedArchive(entry).getUrl(); - assertThat(secondNested, is(not(equalTo(firstNested)))); + assertThat(secondNested).isNotEqualTo(firstNested); } @Test @@ -122,7 +117,7 @@ public class JarFileArchiveTests { .getNestedArchive( getEntriesMap(this.archive).get("another-nested.jar")) .getUrl().toURI()); - assertThat(nested.getParent(), is(equalTo(anotherNested.getParent()))); + assertThat(nested.getParent()).isEqualTo(anotherNested.getParent()); } private Map getEntriesMap(Archive archive) { @@ -132,4 +127,5 @@ public class JarFileArchiveTests { } return entries; } + } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/ByteArrayRandomAccessDataTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/ByteArrayRandomAccessDataTests.java index 793b6edb81..e2ae903fec 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/ByteArrayRandomAccessDataTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/ByteArrayRandomAccessDataTests.java @@ -16,13 +16,14 @@ package org.springframework.boot.loader.data; +import java.io.InputStream; + import org.junit.Test; import org.springframework.boot.loader.data.RandomAccessData.ResourceAccess; import org.springframework.util.FileCopyUtils; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link ByteArrayRandomAccessData}. @@ -35,9 +36,9 @@ public class ByteArrayRandomAccessDataTests { public void testGetInputStream() throws Exception { byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; RandomAccessData data = new ByteArrayRandomAccessData(bytes); - assertThat(FileCopyUtils.copyToByteArray( - data.getInputStream(ResourceAccess.PER_READ)), equalTo(bytes)); - assertThat(data.getSize(), equalTo((long) bytes.length)); + InputStream inputStream = data.getInputStream(ResourceAccess.PER_READ); + assertThat(FileCopyUtils.copyToByteArray(inputStream)).isEqualTo(bytes); + assertThat(data.getSize()).isEqualTo(bytes.length); } @Test @@ -45,10 +46,10 @@ public class ByteArrayRandomAccessDataTests { byte[] bytes = new byte[] { 0, 1, 2, 3, 4, 5 }; RandomAccessData data = new ByteArrayRandomAccessData(bytes); data = data.getSubsection(1, 4).getSubsection(1, 2); - assertThat( - FileCopyUtils - .copyToByteArray(data.getInputStream(ResourceAccess.PER_READ)), - equalTo(new byte[] { 2, 3 })); - assertThat(data.getSize(), equalTo(2L)); + InputStream inputStream = data.getInputStream(ResourceAccess.PER_READ); + assertThat(FileCopyUtils.copyToByteArray(inputStream)) + .isEqualTo(new byte[] { 2, 3 }); + assertThat(data.getSize()).isEqualTo(2L); } + } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/RandomAccessDataFileTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/RandomAccessDataFileTests.java index 95dfc74721..d9fc7ac10b 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/RandomAccessDataFileTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/data/RandomAccessDataFileTests.java @@ -29,7 +29,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; -import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; import org.junit.Rule; @@ -37,11 +36,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.springframework.boot.loader.ByteArrayStartsWith; import org.springframework.boot.loader.data.RandomAccessData.ResourceAccess; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link RandomAccessDataFile}. @@ -118,7 +115,7 @@ public class RandomAccessDataFileTests { @Test public void inputStreamRead() throws Exception { for (int i = 0; i <= 255; i++) { - assertThat(this.inputStream.read(), equalTo(i)); + assertThat(this.inputStream.read()).isEqualTo(i); } } @@ -140,8 +137,8 @@ public class RandomAccessDataFileTests { public void inputStreamReadBytes() throws Exception { byte[] b = new byte[256]; int amountRead = this.inputStream.read(b); - assertThat(b, equalTo(BYTES)); - assertThat(amountRead, equalTo(256)); + assertThat(b).isEqualTo(BYTES); + assertThat(amountRead).isEqualTo(256); } @Test @@ -149,54 +146,54 @@ public class RandomAccessDataFileTests { byte[] b = new byte[7]; this.inputStream.skip(1); int amountRead = this.inputStream.read(b, 2, 3); - assertThat(b, equalTo(new byte[] { 0, 0, 1, 2, 3, 0, 0 })); - assertThat(amountRead, equalTo(3)); + assertThat(b).isEqualTo(new byte[] { 0, 0, 1, 2, 3, 0, 0 }); + assertThat(amountRead).isEqualTo(3); } @Test public void inputStreamReadMoreBytesThanAvailable() throws Exception { byte[] b = new byte[257]; int amountRead = this.inputStream.read(b); - assertThat(b, startsWith(BYTES)); - assertThat(amountRead, equalTo(256)); + assertThat(b).startsWith(BYTES); + assertThat(amountRead).isEqualTo(256); } @Test public void inputStreamReadPastEnd() throws Exception { this.inputStream.skip(255); - assertThat(this.inputStream.read(), equalTo(0xFF)); - assertThat(this.inputStream.read(), equalTo(-1)); - assertThat(this.inputStream.read(), equalTo(-1)); + assertThat(this.inputStream.read()).isEqualTo(0xFF); + assertThat(this.inputStream.read()).isEqualTo(-1); + assertThat(this.inputStream.read()).isEqualTo(-1); } @Test public void inputStreamReadZeroLength() throws Exception { byte[] b = new byte[] { 0x0F }; int amountRead = this.inputStream.read(b, 0, 0); - assertThat(b, equalTo(new byte[] { 0x0F })); - assertThat(amountRead, equalTo(0)); - assertThat(this.inputStream.read(), equalTo(0)); + assertThat(b).isEqualTo(new byte[] { 0x0F }); + assertThat(amountRead).isEqualTo(0); + assertThat(this.inputStream.read()).isEqualTo(0); } @Test public void inputStreamSkip() throws Exception { long amountSkipped = this.inputStream.skip(4); - assertThat(this.inputStream.read(), equalTo(4)); - assertThat(amountSkipped, equalTo(4L)); + assertThat(this.inputStream.read()).isEqualTo(4); + assertThat(amountSkipped).isEqualTo(4L); } @Test public void inputStreamSkipMoreThanAvailable() throws Exception { long amountSkipped = this.inputStream.skip(257); - assertThat(this.inputStream.read(), equalTo(-1)); - assertThat(amountSkipped, equalTo(256L)); + assertThat(this.inputStream.read()).isEqualTo(-1); + assertThat(amountSkipped).isEqualTo(256L); } @Test public void inputStreamSkipPastEnd() throws Exception { this.inputStream.skip(256); long amountSkipped = this.inputStream.skip(1); - assertThat(amountSkipped, equalTo(0L)); + assertThat(amountSkipped).isEqualTo(0L); } @Test @@ -214,8 +211,8 @@ public class RandomAccessDataFileTests { @Test public void subsectionZeroLength() throws Exception { RandomAccessData subsection = this.file.getSubsection(0, 0); - assertThat(subsection.getInputStream(ResourceAccess.PER_READ).read(), - equalTo(-1)); + assertThat(subsection.getInputStream(ResourceAccess.PER_READ).read()) + .isEqualTo(-1); } @Test @@ -235,16 +232,17 @@ public class RandomAccessDataFileTests { @Test public void subsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 1); - assertThat(subsection.getInputStream(ResourceAccess.PER_READ).read(), equalTo(1)); + assertThat(subsection.getInputStream(ResourceAccess.PER_READ).read()) + .isEqualTo(1); } @Test public void inputStreamReadPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); - assertThat(inputStream.read(), equalTo(1)); - assertThat(inputStream.read(), equalTo(2)); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream.read()).isEqualTo(1); + assertThat(inputStream.read()).isEqualTo(2); + assertThat(inputStream.read()).isEqualTo(-1); } @Test @@ -253,26 +251,26 @@ public class RandomAccessDataFileTests { InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); byte[] b = new byte[3]; int amountRead = inputStream.read(b); - assertThat(b, equalTo(new byte[] { 1, 2, 0 })); - assertThat(amountRead, equalTo(2)); + assertThat(b).isEqualTo(new byte[] { 1, 2, 0 }); + assertThat(amountRead).isEqualTo(2); } @Test public void inputStreamSkipPastSubsection() throws Exception { RandomAccessData subsection = this.file.getSubsection(1, 2); InputStream inputStream = subsection.getInputStream(ResourceAccess.PER_READ); - assertThat(inputStream.skip(3), equalTo(2L)); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream.skip(3)).isEqualTo(2L); + assertThat(inputStream.read()).isEqualTo(-1); } @Test public void inputStreamSkipNegative() throws Exception { - assertThat(this.inputStream.skip(-1), equalTo(0L)); + assertThat(this.inputStream.skip(-1)).isEqualTo(0L); } @Test public void getFile() throws Exception { - assertThat(this.file.getFile(), equalTo(this.tempFile)); + assertThat(this.file.getFile()).isEqualTo(this.tempFile); } @Test @@ -294,7 +292,7 @@ public class RandomAccessDataFileTests { })); } for (Future future : results) { - assertThat(future.get(), equalTo(true)); + assertThat(future.get()).isTrue(); } } @@ -308,11 +306,7 @@ public class RandomAccessDataFileTests { Field filesField = filePool.getClass().getDeclaredField("files"); filesField.setAccessible(true); Queue queue = (Queue) filesField.get(filePool); - assertThat(queue.size(), equalTo(0)); - } - - private static Matcher startsWith(byte[] bytes) { - return new ByteArrayStartsWith(bytes); + assertThat(queue.size()).isEqualTo(0); } } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/AsciiBytesTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/AsciiBytesTests.java index 7fb75241d4..9453bdf43d 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/AsciiBytesTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/AsciiBytesTests.java @@ -20,9 +20,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link AsciiBytes}. @@ -37,27 +35,27 @@ public class AsciiBytesTests { @Test public void createFromBytes() throws Exception { AsciiBytes bytes = new AsciiBytes(new byte[] { 65, 66 }); - assertThat(bytes.toString(), equalTo("AB")); + assertThat(bytes.toString()).isEqualTo("AB"); } @Test public void createFromBytesWithOffset() throws Exception { AsciiBytes bytes = new AsciiBytes(new byte[] { 65, 66, 67, 68 }, 1, 2); - assertThat(bytes.toString(), equalTo("BC")); + assertThat(bytes.toString()).isEqualTo("BC"); } @Test public void createFromString() throws Exception { AsciiBytes bytes = new AsciiBytes("AB"); - assertThat(bytes.toString(), equalTo("AB")); + assertThat(bytes.toString()).isEqualTo("AB"); } @Test public void length() throws Exception { AsciiBytes b1 = new AsciiBytes(new byte[] { 65, 66 }); AsciiBytes b2 = new AsciiBytes(new byte[] { 65, 66, 67, 68 }, 1, 2); - assertThat(b1.length(), equalTo(2)); - assertThat(b2.length(), equalTo(2)); + assertThat(b1.length()).isEqualTo(2); + assertThat(b2.length()).isEqualTo(2); } @Test @@ -66,10 +64,10 @@ public class AsciiBytesTests { AsciiBytes ab = new AsciiBytes(new byte[] { 65, 66 }); AsciiBytes bc = new AsciiBytes(new byte[] { 65, 66, 67 }, 1, 2); AsciiBytes abcd = new AsciiBytes(new byte[] { 65, 66, 67, 68 }); - assertThat(abc.startsWith(abc), equalTo(true)); - assertThat(abc.startsWith(ab), equalTo(true)); - assertThat(abc.startsWith(bc), equalTo(false)); - assertThat(abc.startsWith(abcd), equalTo(false)); + assertThat(abc.startsWith(abc)).isTrue(); + assertThat(abc.startsWith(ab)).isTrue(); + assertThat(abc.startsWith(bc)).isFalse(); + assertThat(abc.startsWith(abcd)).isFalse(); } @Test @@ -78,20 +76,20 @@ public class AsciiBytesTests { AsciiBytes bc = new AsciiBytes(new byte[] { 65, 66, 67 }, 1, 2); AsciiBytes ab = new AsciiBytes(new byte[] { 65, 66 }); AsciiBytes aabc = new AsciiBytes(new byte[] { 65, 65, 66, 67 }); - assertThat(abc.endsWith(abc), equalTo(true)); - assertThat(abc.endsWith(bc), equalTo(true)); - assertThat(abc.endsWith(ab), equalTo(false)); - assertThat(abc.endsWith(aabc), equalTo(false)); + assertThat(abc.endsWith(abc)).isTrue(); + assertThat(abc.endsWith(bc)).isTrue(); + assertThat(abc.endsWith(ab)).isFalse(); + assertThat(abc.endsWith(aabc)).isFalse(); } @Test public void substringFromBeingIndex() throws Exception { AsciiBytes abcd = new AsciiBytes(new byte[] { 65, 66, 67, 68 }); - assertThat(abcd.substring(0).toString(), equalTo("ABCD")); - assertThat(abcd.substring(1).toString(), equalTo("BCD")); - assertThat(abcd.substring(2).toString(), equalTo("CD")); - assertThat(abcd.substring(3).toString(), equalTo("D")); - assertThat(abcd.substring(4).toString(), equalTo("")); + assertThat(abcd.substring(0).toString()).isEqualTo("ABCD"); + assertThat(abcd.substring(1).toString()).isEqualTo("BCD"); + assertThat(abcd.substring(2).toString()).isEqualTo("CD"); + assertThat(abcd.substring(3).toString()).isEqualTo("D"); + assertThat(abcd.substring(4).toString()).isEqualTo(""); this.thrown.expect(IndexOutOfBoundsException.class); abcd.substring(5); } @@ -99,10 +97,10 @@ public class AsciiBytesTests { @Test public void substring() throws Exception { AsciiBytes abcd = new AsciiBytes(new byte[] { 65, 66, 67, 68 }); - assertThat(abcd.substring(0, 4).toString(), equalTo("ABCD")); - assertThat(abcd.substring(1, 3).toString(), equalTo("BC")); - assertThat(abcd.substring(3, 4).toString(), equalTo("D")); - assertThat(abcd.substring(3, 3).toString(), equalTo("")); + assertThat(abcd.substring(0, 4).toString()).isEqualTo("ABCD"); + assertThat(abcd.substring(1, 3).toString()).isEqualTo("BC"); + assertThat(abcd.substring(3, 4).toString()).isEqualTo("D"); + assertThat(abcd.substring(3, 3).toString()).isEqualTo(""); this.thrown.expect(IndexOutOfBoundsException.class); abcd.substring(3, 5); } @@ -111,16 +109,16 @@ public class AsciiBytesTests { public void appendString() throws Exception { AsciiBytes bc = new AsciiBytes(new byte[] { 65, 66, 67, 68 }, 1, 2); AsciiBytes appended = bc.append("D"); - assertThat(bc.toString(), equalTo("BC")); - assertThat(appended.toString(), equalTo("BCD")); + assertThat(bc.toString()).isEqualTo("BC"); + assertThat(appended.toString()).isEqualTo("BCD"); } @Test public void appendBytes() throws Exception { AsciiBytes bc = new AsciiBytes(new byte[] { 65, 66, 67, 68 }, 1, 2); AsciiBytes appended = bc.append(new byte[] { 68 }); - assertThat(bc.toString(), equalTo("BC")); - assertThat(appended.toString(), equalTo("BCD")); + assertThat(bc.toString()).isEqualTo("BC"); + assertThat(appended.toString()).isEqualTo("BCD"); } @Test @@ -130,28 +128,28 @@ public class AsciiBytesTests { AsciiBytes bc_substring = new AsciiBytes(new byte[] { 65, 66, 67, 68 }) .substring(1, 3); AsciiBytes bc_string = new AsciiBytes("BC"); - assertThat(bc.hashCode(), equalTo(bc.hashCode())); - assertThat(bc.hashCode(), equalTo(bc_substring.hashCode())); - assertThat(bc.hashCode(), equalTo(bc_string.hashCode())); - assertThat(bc, equalTo(bc)); - assertThat(bc, equalTo(bc_substring)); - assertThat(bc, equalTo(bc_string)); - assertThat(bc.hashCode(), not(equalTo(abcd.hashCode()))); - assertThat(bc, not(equalTo(abcd))); + assertThat(bc.hashCode()).isEqualTo(bc.hashCode()); + assertThat(bc.hashCode()).isEqualTo(bc_substring.hashCode()); + assertThat(bc.hashCode()).isEqualTo(bc_string.hashCode()); + assertThat(bc).isEqualTo(bc); + assertThat(bc).isEqualTo(bc_substring); + assertThat(bc).isEqualTo(bc_string); + assertThat(bc.hashCode()).isNotEqualTo(abcd.hashCode()); + assertThat(bc).isNotEqualTo(abcd); } @Test public void hashCodeSameAsString() throws Exception { String s = "abcABC123xyz!"; AsciiBytes a = new AsciiBytes(s); - assertThat(s.hashCode(), equalTo(a.hashCode())); + assertThat(s.hashCode()).isEqualTo(a.hashCode()); } @Test public void hashCodeSameAsStringWithSpecial() throws Exception { String s = "special/\u00EB.dat"; AsciiBytes a = new AsciiBytes(s); - assertThat(s.hashCode(), equalTo(a.hashCode())); + assertThat(s.hashCode()).isEqualTo(a.hashCode()); } } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/CentralDirectoryParserTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/CentralDirectoryParserTests.java index 22b7aec18c..723e9afb9b 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/CentralDirectoryParserTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/CentralDirectoryParserTests.java @@ -31,8 +31,7 @@ import org.springframework.boot.loader.TestJarCreator; import org.springframework.boot.loader.data.RandomAccessData; import org.springframework.boot.loader.data.RandomAccessDataFile; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Mockito.atLeastOnce; @@ -62,7 +61,7 @@ public class CentralDirectoryParserTests { @Test public void visitsInOrder() throws Exception { - CentralDirectoryVisitor visitor = mock(CentralDirectoryVisitor.class); + CentralDirectoryVisitor visitor = mock(TestCentralDirectoryVisitor.class); CentralDirectoryParser parser = new CentralDirectoryParser(); parser.addVisitor(visitor); parser.parse(this.jarData, false); @@ -81,17 +80,17 @@ public class CentralDirectoryParserTests { parser.addVisitor(collector); parser.parse(this.jarData, false); Iterator headers = collector.getHeaders().iterator(); - assertThat(headers.next().getName().toString(), equalTo("META-INF/")); - assertThat(headers.next().getName().toString(), equalTo("META-INF/MANIFEST.MF")); - assertThat(headers.next().getName().toString(), equalTo("1.dat")); - assertThat(headers.next().getName().toString(), equalTo("2.dat")); - assertThat(headers.next().getName().toString(), equalTo("d/")); - assertThat(headers.next().getName().toString(), equalTo("d/9.dat")); - assertThat(headers.next().getName().toString(), equalTo("special/")); - assertThat(headers.next().getName().toString(), equalTo("special/\u00EB.dat")); - assertThat(headers.next().getName().toString(), equalTo("nested.jar")); - assertThat(headers.next().getName().toString(), equalTo("another-nested.jar")); - assertThat(headers.hasNext(), equalTo(false)); + assertThat(headers.next().getName().toString()).isEqualTo("META-INF/"); + assertThat(headers.next().getName().toString()).isEqualTo("META-INF/MANIFEST.MF"); + assertThat(headers.next().getName().toString()).isEqualTo("1.dat"); + assertThat(headers.next().getName().toString()).isEqualTo("2.dat"); + assertThat(headers.next().getName().toString()).isEqualTo("d/"); + assertThat(headers.next().getName().toString()).isEqualTo("d/9.dat"); + assertThat(headers.next().getName().toString()).isEqualTo("special/"); + assertThat(headers.next().getName().toString()).isEqualTo("special/\u00EB.dat"); + assertThat(headers.next().getName().toString()).isEqualTo("nested.jar"); + assertThat(headers.next().getName().toString()).isEqualTo("another-nested.jar"); + assertThat(headers.hasNext()).isFalse(); } private static class Collector implements CentralDirectoryVisitor { @@ -119,4 +118,8 @@ public class CentralDirectoryParserTests { } + public interface TestCentralDirectoryVisitor extends CentralDirectoryVisitor { + + } + } diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java index 20ec637fa2..85418c9cf7 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java @@ -41,14 +41,7 @@ import org.springframework.boot.loader.data.RandomAccessDataFile; import org.springframework.util.FileCopyUtils; import org.springframework.util.StreamUtils; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.greaterThan; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.notNullValue; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.sameInstance; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.verify; @@ -82,21 +75,21 @@ public class JarFileTests { // Sanity checks to see how the default jar file operates java.util.jar.JarFile jarFile = new java.util.jar.JarFile(this.rootJarFile); Enumeration entries = jarFile.entries(); - assertThat(entries.nextElement().getName(), equalTo("META-INF/")); - assertThat(entries.nextElement().getName(), equalTo("META-INF/MANIFEST.MF")); - assertThat(entries.nextElement().getName(), equalTo("1.dat")); - assertThat(entries.nextElement().getName(), equalTo("2.dat")); - assertThat(entries.nextElement().getName(), equalTo("d/")); - assertThat(entries.nextElement().getName(), equalTo("d/9.dat")); - assertThat(entries.nextElement().getName(), equalTo("special/")); - assertThat(entries.nextElement().getName(), equalTo("special/\u00EB.dat")); - assertThat(entries.nextElement().getName(), equalTo("nested.jar")); - assertThat(entries.nextElement().getName(), equalTo("another-nested.jar")); - assertThat(entries.hasMoreElements(), equalTo(false)); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/"); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/MANIFEST.MF"); + assertThat(entries.nextElement().getName()).isEqualTo("1.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("2.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("d/"); + assertThat(entries.nextElement().getName()).isEqualTo("d/9.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("special/"); + assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("nested.jar"); + assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar"); + assertThat(entries.hasMoreElements()).isFalse(); URL jarUrl = new URL("jar:" + this.rootJarFile.toURI() + "!/"); URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jarUrl }); - assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); - assertThat(urlClassLoader.getResource("d/9.dat"), notNullValue()); + assertThat(urlClassLoader.getResource("special/\u00EB.dat")).isNotNull(); + assertThat(urlClassLoader.getResource("d/9.dat")).isNotNull(); jarFile.close(); urlClassLoader.close(); } @@ -104,79 +97,79 @@ public class JarFileTests { @Test public void createFromFile() throws Exception { JarFile jarFile = new JarFile(this.rootJarFile); - assertThat(jarFile.getName(), notNullValue(String.class)); + assertThat(jarFile.getName()).isNotNull(); jarFile.close(); } @Test public void getManifest() throws Exception { - assertThat(this.jarFile.getManifest().getMainAttributes().getValue("Built-By"), - equalTo("j1")); + assertThat(this.jarFile.getManifest().getMainAttributes().getValue("Built-By")) + .isEqualTo("j1"); } @Test public void getManifestEntry() throws Exception { ZipEntry entry = this.jarFile.getJarEntry("META-INF/MANIFEST.MF"); Manifest manifest = new Manifest(this.jarFile.getInputStream(entry)); - assertThat(manifest.getMainAttributes().getValue("Built-By"), equalTo("j1")); + assertThat(manifest.getMainAttributes().getValue("Built-By")).isEqualTo("j1"); } @Test public void getEntries() throws Exception { Enumeration entries = this.jarFile.entries(); - assertThat(entries.nextElement().getName(), equalTo("META-INF/")); - assertThat(entries.nextElement().getName(), equalTo("META-INF/MANIFEST.MF")); - assertThat(entries.nextElement().getName(), equalTo("1.dat")); - assertThat(entries.nextElement().getName(), equalTo("2.dat")); - assertThat(entries.nextElement().getName(), equalTo("d/")); - assertThat(entries.nextElement().getName(), equalTo("d/9.dat")); - assertThat(entries.nextElement().getName(), equalTo("special/")); - assertThat(entries.nextElement().getName(), equalTo("special/\u00EB.dat")); - assertThat(entries.nextElement().getName(), equalTo("nested.jar")); - assertThat(entries.nextElement().getName(), equalTo("another-nested.jar")); - assertThat(entries.hasMoreElements(), equalTo(false)); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/"); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/MANIFEST.MF"); + assertThat(entries.nextElement().getName()).isEqualTo("1.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("2.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("d/"); + assertThat(entries.nextElement().getName()).isEqualTo("d/9.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("special/"); + assertThat(entries.nextElement().getName()).isEqualTo("special/\u00EB.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("nested.jar"); + assertThat(entries.nextElement().getName()).isEqualTo("another-nested.jar"); + assertThat(entries.hasMoreElements()).isFalse(); } @Test public void getSpecialResourceViaClassLoader() throws Exception { URLClassLoader urlClassLoader = new URLClassLoader( new URL[] { this.jarFile.getUrl() }); - assertThat(urlClassLoader.getResource("special/\u00EB.dat"), notNullValue()); + assertThat(urlClassLoader.getResource("special/\u00EB.dat")).isNotNull(); urlClassLoader.close(); } @Test public void getJarEntry() throws Exception { java.util.jar.JarEntry entry = this.jarFile.getJarEntry("1.dat"); - assertThat(entry, notNullValue(ZipEntry.class)); - assertThat(entry.getName(), equalTo("1.dat")); + assertThat(entry).isNotNull(); + assertThat(entry.getName()).isEqualTo("1.dat"); } @Test public void getInputStream() throws Exception { InputStream inputStream = this.jarFile .getInputStream(this.jarFile.getEntry("1.dat")); - assertThat(inputStream.available(), equalTo(1)); - assertThat(inputStream.read(), equalTo(1)); - assertThat(inputStream.available(), equalTo(0)); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream.available()).isEqualTo(1); + assertThat(inputStream.read()).isEqualTo(1); + assertThat(inputStream.available()).isEqualTo(0); + assertThat(inputStream.read()).isEqualTo(-1); } @Test public void getName() throws Exception { - assertThat(this.jarFile.getName(), equalTo(this.rootJarFile.getPath())); + assertThat(this.jarFile.getName()).isEqualTo(this.rootJarFile.getPath()); } @Test public void getSize() throws Exception { - assertThat(this.jarFile.size(), equalTo((int) this.rootJarFile.length())); + assertThat(this.jarFile.size()).isEqualTo((int) this.rootJarFile.length()); } @Test public void getEntryTime() throws Exception { java.util.jar.JarFile jdkJarFile = new java.util.jar.JarFile(this.rootJarFile); - assertThat(this.jarFile.getEntry("META-INF/MANIFEST.MF").getTime(), - equalTo(jdkJarFile.getEntry("META-INF/MANIFEST.MF").getTime())); + assertThat(this.jarFile.getEntry("META-INF/MANIFEST.MF").getTime()) + .isEqualTo(jdkJarFile.getEntry("META-INF/MANIFEST.MF").getTime()); jdkJarFile.close(); } @@ -192,36 +185,36 @@ public class JarFileTests { @Test public void getUrl() throws Exception { URL url = this.jarFile.getUrl(); - assertThat(url.toString(), equalTo("jar:" + this.rootJarFile.toURI() + "!/")); + assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/"); JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection(); - assertThat(jarURLConnection.getJarFile(), sameInstance(this.jarFile)); - assertThat(jarURLConnection.getJarEntry(), nullValue()); - assertThat(jarURLConnection.getContentLength(), greaterThan(1)); - assertThat(jarURLConnection.getContent(), sameInstance((Object) this.jarFile)); - assertThat(jarURLConnection.getContentType(), equalTo("x-java/jar")); - assertThat(jarURLConnection.getJarFileURL().toURI(), - equalTo(this.rootJarFile.toURI())); + assertThat(jarURLConnection.getJarFile()).isSameAs(this.jarFile); + assertThat(jarURLConnection.getJarEntry()).isNull(); + assertThat(jarURLConnection.getContentLength()).isGreaterThan(1); + assertThat(jarURLConnection.getContent()).isSameAs(this.jarFile); + assertThat(jarURLConnection.getContentType()).isEqualTo("x-java/jar"); + assertThat(jarURLConnection.getJarFileURL().toURI()) + .isEqualTo(this.rootJarFile.toURI()); } @Test public void createEntryUrl() throws Exception { URL url = new URL(this.jarFile.getUrl(), "1.dat"); - assertThat(url.toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/1.dat")); + assertThat(url.toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/1.dat"); JarURLConnection jarURLConnection = (JarURLConnection) url.openConnection(); - assertThat(jarURLConnection.getJarFile(), sameInstance(this.jarFile)); - assertThat(jarURLConnection.getJarEntry(), - sameInstance(this.jarFile.getJarEntry("1.dat"))); - assertThat(jarURLConnection.getContentLength(), equalTo(1)); - assertThat(jarURLConnection.getContent(), instanceOf(InputStream.class)); - assertThat(jarURLConnection.getContentType(), equalTo("content/unknown")); + assertThat(jarURLConnection.getJarFile()).isSameAs(this.jarFile); + assertThat(jarURLConnection.getJarEntry()) + .isSameAs(this.jarFile.getJarEntry("1.dat")); + assertThat(jarURLConnection.getContentLength()).isEqualTo(1); + assertThat(jarURLConnection.getContent()).isInstanceOf(InputStream.class); + assertThat(jarURLConnection.getContentType()).isEqualTo("content/unknown"); } @Test public void getMissingEntryUrl() throws Exception { URL url = new URL(this.jarFile.getUrl(), "missing.dat"); - assertThat(url.toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/missing.dat")); + assertThat(url.toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/missing.dat"); this.thrown.expect(FileNotFoundException.class); ((JarURLConnection) url.openConnection()).getJarEntry(); } @@ -239,8 +232,8 @@ public class JarFileTests { URL url = new URL(this.jarFile.getUrl(), "1.dat"); url.openConnection(); InputStream stream = url.openStream(); - assertThat(stream.read(), equalTo(1)); - assertThat(stream.read(), equalTo(-1)); + assertThat(stream.read()).isEqualTo(1); + assertThat(stream.read()).isEqualTo(-1); } @Test @@ -249,25 +242,25 @@ public class JarFileTests { .getNestedJarFile(this.jarFile.getEntry("nested.jar")); Enumeration entries = nestedJarFile.entries(); - assertThat(entries.nextElement().getName(), equalTo("META-INF/")); - assertThat(entries.nextElement().getName(), equalTo("META-INF/MANIFEST.MF")); - assertThat(entries.nextElement().getName(), equalTo("3.dat")); - assertThat(entries.nextElement().getName(), equalTo("4.dat")); - assertThat(entries.nextElement().getName(), equalTo("\u00E4.dat")); - assertThat(entries.hasMoreElements(), equalTo(false)); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/"); + assertThat(entries.nextElement().getName()).isEqualTo("META-INF/MANIFEST.MF"); + assertThat(entries.nextElement().getName()).isEqualTo("3.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("4.dat"); + assertThat(entries.nextElement().getName()).isEqualTo("\u00E4.dat"); + assertThat(entries.hasMoreElements()).isFalse(); InputStream inputStream = nestedJarFile .getInputStream(nestedJarFile.getEntry("3.dat")); - assertThat(inputStream.read(), equalTo(3)); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream.read()).isEqualTo(3); + assertThat(inputStream.read()).isEqualTo(-1); URL url = nestedJarFile.getUrl(); - assertThat(url.toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar!/")); + assertThat(url.toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar!/"); JarURLConnection conn = (JarURLConnection) url.openConnection(); - assertThat(conn.getJarFile(), sameInstance(nestedJarFile)); - assertThat(conn.getJarFileURL().toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar")); + assertThat(conn.getJarFile()).isSameAs(nestedJarFile); + assertThat(conn.getJarFileURL().toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar"); } @Test @@ -276,18 +269,18 @@ public class JarFileTests { .getNestedJarFile(this.jarFile.getEntry("d/")); Enumeration entries = nestedJarFile.entries(); - assertThat(entries.nextElement().getName(), equalTo("9.dat")); - assertThat(entries.hasMoreElements(), equalTo(false)); + assertThat(entries.nextElement().getName()).isEqualTo("9.dat"); + assertThat(entries.hasMoreElements()).isFalse(); InputStream inputStream = nestedJarFile .getInputStream(nestedJarFile.getEntry("9.dat")); - assertThat(inputStream.read(), equalTo(9)); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream.read()).isEqualTo(9); + assertThat(inputStream.read()).isEqualTo(-1); URL url = nestedJarFile.getUrl(); - assertThat(url.toString(), equalTo("jar:" + this.rootJarFile.toURI() + "!/d!/")); - assertThat(((JarURLConnection) url.openConnection()).getJarFile(), - sameInstance(nestedJarFile)); + assertThat(url.toString()).isEqualTo("jar:" + this.rootJarFile.toURI() + "!/d!/"); + assertThat(((JarURLConnection) url.openConnection()).getJarFile()) + .isSameAs(nestedJarFile); } @Test @@ -295,11 +288,11 @@ public class JarFileTests { JarFile nestedJarFile = this.jarFile .getNestedJarFile(this.jarFile.getEntry("nested.jar")); URL url = nestedJarFile.getJarEntry("3.dat").getUrl(); - assertThat(url.toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat")); + assertThat(url.toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat"); InputStream inputStream = url.openStream(); - assertThat(inputStream, notNullValue()); - assertThat(inputStream.read(), equalTo(3)); + assertThat(inputStream).isNotNull(); + assertThat(inputStream.read()).isEqualTo(3); } @Test @@ -307,15 +300,15 @@ public class JarFileTests { JarFile.registerUrlProtocolHandler(); String spec = "jar:" + this.rootJarFile.toURI() + "!/nested.jar!/3.dat"; URL url = new URL(spec); - assertThat(url.toString(), equalTo(spec)); + assertThat(url.toString()).isEqualTo(spec); InputStream inputStream = url.openStream(); - assertThat(inputStream, notNullValue()); - assertThat(inputStream.read(), equalTo(3)); + assertThat(inputStream).isNotNull(); + assertThat(inputStream.read()).isEqualTo(3); JarURLConnection connection = (JarURLConnection) url.openConnection(); - assertThat(connection.getURL().toString(), equalTo(spec)); - assertThat(connection.getJarFileURL().toString(), - equalTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar")); - assertThat(connection.getEntryName(), equalTo("3.dat")); + assertThat(connection.getURL().toString()).isEqualTo(spec); + assertThat(connection.getJarFileURL().toString()) + .isEqualTo("jar:" + this.rootJarFile.toURI() + "!/nested.jar"); + assertThat(connection.getEntryName()).isEqualTo("3.dat"); } @Test @@ -323,38 +316,37 @@ public class JarFileTests { JarFile.registerUrlProtocolHandler(); String spec = "jar:" + this.rootJarFile.toURI() + "!/2.dat"; URL url = new URL(spec); - assertThat(url.toString(), equalTo(spec)); + assertThat(url.toString()).isEqualTo(spec); InputStream inputStream = url.openStream(); - assertThat(inputStream, notNullValue()); - assertThat(inputStream.read(), equalTo(2)); + assertThat(inputStream).isNotNull(); + assertThat(inputStream.read()).isEqualTo(2); JarURLConnection connection = (JarURLConnection) url.openConnection(); - assertThat(connection.getURL().toString(), equalTo(spec)); - assertThat(connection.getJarFileURL().toURI(), equalTo(this.rootJarFile.toURI())); - assertThat(connection.getEntryName(), equalTo("2.dat")); + assertThat(connection.getURL().toString()).isEqualTo(spec); + assertThat(connection.getJarFileURL().toURI()) + .isEqualTo(this.rootJarFile.toURI()); + assertThat(connection.getEntryName()).isEqualTo("2.dat"); } @Test public void getDirectoryInputStream() throws Exception { InputStream inputStream = this.jarFile .getInputStream(this.jarFile.getEntry("d/")); - assertThat(inputStream, notNullValue()); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream).isNotNull(); + assertThat(inputStream.read()).isEqualTo(-1); } @Test public void getDirectoryInputStreamWithoutSlash() throws Exception { InputStream inputStream = this.jarFile.getInputStream(this.jarFile.getEntry("d")); - assertThat(inputStream, notNullValue()); - assertThat(inputStream.read(), equalTo(-1)); + assertThat(inputStream).isNotNull(); + assertThat(inputStream.read()).isEqualTo(-1); } @Test public void sensibleToString() throws Exception { - assertThat(this.jarFile.toString(), equalTo(this.rootJarFile.getPath())); - assertThat( - this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")) - .toString(), - equalTo(this.rootJarFile.getPath() + "!/nested.jar")); + assertThat(this.jarFile.toString()).isEqualTo(this.rootJarFile.getPath()); + assertThat(this.jarFile.getNestedJarFile(this.jarFile.getEntry("nested.jar")) + .toString()).isEqualTo(this.rootJarFile.getPath() + "!/nested.jar"); } @Test @@ -367,7 +359,7 @@ public class JarFileTests { signedJarFile = entry; } } - assertNotNull(signedJarFile); + assertThat(signedJarFile).isNotNull(); java.util.jar.JarFile jarFile = new JarFile(new File(signedJarFile)); jarFile.getManifest(); Enumeration jarEntries = jarFile.entries(); @@ -378,8 +370,7 @@ public class JarFileTests { inputStream.close(); if (!jarEntry.getName().startsWith("META-INF") && !jarEntry.isDirectory() && !jarEntry.getName().endsWith("TigerDigest.class")) { - assertNotNull("Missing cert " + jarEntry.getName(), - jarEntry.getCertificates()); + assertThat(jarEntry.getCertificates()).isNotNull(); } } jarFile.close(); diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java index 17111bc125..9a8e349257 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/util/SystemPropertyUtilsTests.java @@ -20,9 +20,11 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** + * Tests for {@link SystemPropertyUtils}. + * * @author Dave Syer */ public class SystemPropertyUtilsTests { @@ -39,23 +41,25 @@ public class SystemPropertyUtilsTests { @Test public void testVanillaPlaceholder() { - assertEquals("bar", SystemPropertyUtils.resolvePlaceholders("${foo}")); + assertThat(SystemPropertyUtils.resolvePlaceholders("${foo}")).isEqualTo("bar"); } @Test public void testDefaultValue() { - assertEquals("foo", SystemPropertyUtils.resolvePlaceholders("${bar:foo}")); + assertThat(SystemPropertyUtils.resolvePlaceholders("${bar:foo}")) + .isEqualTo("foo"); } @Test public void testNestedPlaceholder() { - assertEquals("foo", - SystemPropertyUtils.resolvePlaceholders("${bar:${spam:foo}}")); + assertThat(SystemPropertyUtils.resolvePlaceholders("${bar:${spam:foo}}")) + .isEqualTo("foo"); } @Test public void testEnvVar() { - assertEquals(System.getenv("LANG"), SystemPropertyUtils.getProperty("lang")); + assertThat(SystemPropertyUtils.getProperty("lang")) + .isEqualTo(System.getenv("LANG")); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java index b083d977ca..e95bbb8bf8 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ArtifactsLibrariesTests.java @@ -36,8 +36,7 @@ import org.springframework.boot.loader.tools.Library; import org.springframework.boot.loader.tools.LibraryCallback; import org.springframework.boot.loader.tools.LibraryScope; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -80,9 +79,9 @@ public class ArtifactsLibrariesTests { this.libs.doWithLibraries(this.callback); verify(this.callback).library(this.libraryCaptor.capture()); Library library = this.libraryCaptor.getValue(); - assertThat(library.getFile(), equalTo(this.file)); - assertThat(library.getScope(), equalTo(LibraryScope.COMPILE)); - assertThat(library.isUnpackRequired(), equalTo(false)); + assertThat(library.getFile()).isEqualTo(this.file); + assertThat(library.getScope()).isEqualTo(LibraryScope.COMPILE); + assertThat(library.isUnpackRequired()).isFalse(); } @Test @@ -98,7 +97,7 @@ public class ArtifactsLibrariesTests { mock(Log.class)); this.libs.doWithLibraries(this.callback); verify(this.callback).library(this.libraryCaptor.capture()); - assertThat(this.libraryCaptor.getValue().isUnpackRequired(), equalTo(true)); + assertThat(this.libraryCaptor.getValue().isUnpackRequired()).isTrue(); } @Test @@ -117,8 +116,8 @@ public class ArtifactsLibrariesTests { this.libs = new ArtifactsLibraries(this.artifacts, null, mock(Log.class)); this.libs.doWithLibraries(this.callback); verify(this.callback, times(2)).library(this.libraryCaptor.capture()); - assertThat(this.libraryCaptor.getAllValues().get(0).getName(), equalTo("g1-a")); - assertThat(this.libraryCaptor.getAllValues().get(1).getName(), equalTo("g2-a")); + assertThat(this.libraryCaptor.getAllValues().get(0).getName()).isEqualTo("g1-a"); + assertThat(this.libraryCaptor.getAllValues().get(1).getName()).isEqualTo("g2-a"); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DependencyFilterMojoTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DependencyFilterMojoTests.java index e637e61c0e..c5d5b23b2d 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DependencyFilterMojoTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/DependencyFilterMojoTests.java @@ -27,8 +27,7 @@ import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -48,8 +47,8 @@ public class DependencyFilterMojoTests { Set artifacts = mojo.filterDependencies( createArtifact("com.foo", "one"), createArtifact("com.foo", "two"), createArtifact("com.bar", "exclude-id"), artifact); - assertEquals("wrong filtering of artifacts", 1, artifacts.size()); - assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next()); + assertThat(artifacts).hasSize(1); + assertThat(artifacts.iterator().next()).isSameAs(artifact); } @Test @@ -61,8 +60,8 @@ public class DependencyFilterMojoTests { Set artifacts = mojo.filterDependencies( createArtifact("com.foo", "one"), createArtifact("com.foo", "two"), artifact); - assertEquals("wrong filtering of artifacts", 1, artifacts.size()); - assertSame("Wrong filtered artifact", artifact, artifacts.iterator().next()); + assertThat(artifacts).hasSize(1); + assertThat(artifacts.iterator().next()).isSameAs(artifact); } private Artifact createArtifact(String groupId, String artifactId) { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ExcludeFilterTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ExcludeFilterTests.java index 880e9c8b58..6fd6d8a362 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ExcludeFilterTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/ExcludeFilterTests.java @@ -25,8 +25,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -36,7 +35,7 @@ import static org.mockito.Mockito.mock; * @author Stephane Nicoll * @author David Turanski */ -@SuppressWarnings("rawtypes") +@SuppressWarnings({ "rawtypes", "unchecked" }) public class ExcludeFilterTests { @Test @@ -45,7 +44,7 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar"))); Set result = filter .filter(Collections.singleton(createArtifact("com.foo", "bar"))); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -54,8 +53,8 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar"))); Artifact artifact = createArtifact("com.baz", "bar"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -64,8 +63,8 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar"))); Artifact artifact = createArtifact("com.foo", "biz"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -74,7 +73,7 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar", "jdk5"))); Set result = filter .filter(Collections.singleton(createArtifact("com.foo", "bar", "jdk5"))); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -83,8 +82,8 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar", "jdk5"))); Artifact artifact = createArtifact("com.foo", "bar"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -93,8 +92,8 @@ public class ExcludeFilterTests { Arrays.asList(createExclude("com.foo", "bar", "jdk5"))); Artifact artifact = createArtifact("com.foo", "bar", "jdk6"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -108,8 +107,8 @@ public class ExcludeFilterTests { Artifact anotherAcme = createArtifact("org.acme", "another-app"); artifacts.add(anotherAcme); Set result = filter.filter(artifacts); - assertEquals("Two dependencies should have been filtered", 1, result.size()); - assertSame(anotherAcme, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(anotherAcme); } private Exclude createExclude(String groupId, String artifactId) { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/IncludeFilterTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/IncludeFilterTests.java index 3f99067ff2..cda8d6b987 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/IncludeFilterTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/IncludeFilterTests.java @@ -25,8 +25,7 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertSame; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; import static org.mockito.Mockito.mock; @@ -35,7 +34,7 @@ import static org.mockito.Mockito.mock; * * @author David Turanski */ -@SuppressWarnings("rawtypes") +@SuppressWarnings({ "rawtypes", "unchecked" }) public class IncludeFilterTests { @Test @@ -44,8 +43,8 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar"))); Artifact artifact = createArtifact("com.foo", "bar"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -54,7 +53,7 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar"))); Artifact artifact = createArtifact("com.baz", "bar"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -63,7 +62,7 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar"))); Artifact artifact = createArtifact("com.foo", "biz"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -72,8 +71,8 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar", "jdk5"))); Artifact artifact = createArtifact("com.foo", "bar", "jdk5"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should not have been filtered", 1, result.size()); - assertSame(artifact, result.iterator().next()); + assertThat(result).hasSize(1); + assertThat(result.iterator().next()).isSameAs(artifact); } @Test @@ -82,7 +81,7 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar", "jdk5"))); Artifact artifact = createArtifact("com.foo", "bar"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -91,7 +90,7 @@ public class IncludeFilterTests { Arrays.asList(createInclude("com.foo", "bar", "jdk5"))); Artifact artifact = createArtifact("com.foo", "bar", "jdk6"); Set result = filter.filter(Collections.singleton(artifact)); - assertEquals("Should have been filtered", 0, result.size()); + assertThat(result).isEmpty(); } @Test @@ -105,7 +104,7 @@ public class IncludeFilterTests { Artifact anotherAcme = createArtifact("org.acme", "another-app"); artifacts.add(anotherAcme); Set result = filter.filter(artifacts); - assertEquals("One dependency should have been filtered", 2, result.size()); + assertThat(result).hasSize(2); } private Include createInclude(String groupId, String artifactId) { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java index 469e887043..84ba464573 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/PropertiesMergingResourceTransformerTests.java @@ -22,10 +22,7 @@ import java.util.jar.JarOutputStream; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link PropertiesMergingResourceTransformer}. @@ -38,10 +35,10 @@ public class PropertiesMergingResourceTransformerTests { @Test public void testProcess() throws Exception { - assertFalse(this.transformer.hasTransformedResource()); + assertThat(this.transformer.hasTransformedResource()).isFalse(); this.transformer.processResource("foo", new ByteArrayInputStream("foo=bar".getBytes()), null); - assertTrue(this.transformer.hasTransformedResource()); + assertThat(this.transformer.hasTransformedResource()).isTrue(); } @Test @@ -50,7 +47,7 @@ public class PropertiesMergingResourceTransformerTests { new ByteArrayInputStream("foo=bar".getBytes()), null); this.transformer.processResource("bar", new ByteArrayInputStream("foo=spam".getBytes()), null); - assertEquals("bar,spam", this.transformer.getData().getProperty("foo")); + assertThat(this.transformer.getData().getProperty("foo")).isEqualTo("bar,spam"); } @Test @@ -63,8 +60,8 @@ public class PropertiesMergingResourceTransformerTests { this.transformer.modifyOutputStream(os); os.flush(); os.close(); - assertNotNull(out.toByteArray()); - assertTrue(out.toByteArray().length > 0); + assertThat(out.toByteArray()).isNotNull(); + assertThat(out.toByteArray().length > 0).isTrue(); } } diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java index 89a6e10798..e1f33f83bc 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/RunArgumentsTests.java @@ -18,8 +18,7 @@ package org.springframework.boot.maven; import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Tests for {@link RunArguments}. @@ -31,48 +30,48 @@ public class RunArgumentsTests { @Test public void parseNull() { String[] args = parseArgs(null); - assertNotNull(args); - assertEquals(0, args.length); + assertThat(args).isNotNull(); + assertThat(args.length).isEqualTo(0); } @Test public void parseEmpty() { String[] args = parseArgs(" "); - assertNotNull(args); - assertEquals(0, args.length); + assertThat(args).isNotNull(); + assertThat(args.length).isEqualTo(0); } @Test public void parseDebugFlags() { String[] args = parseArgs( "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"); - assertEquals(2, args.length); - assertEquals("-Xdebug", args[0]); - assertEquals("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005", - args[1]); + assertThat(args.length).isEqualTo(2); + assertThat(args[0]).isEqualTo("-Xdebug"); + assertThat(args[1]).isEqualTo( + "-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"); } @Test public void parseWithExtraSpaces() { String[] args = parseArgs(" -Dfoo=bar -Dfoo2=bar2 "); - assertEquals(2, args.length); - assertEquals("-Dfoo=bar", args[0]); - assertEquals("-Dfoo2=bar2", args[1]); + assertThat(args.length).isEqualTo(2); + assertThat(args[0]).isEqualTo("-Dfoo=bar"); + assertThat(args[1]).isEqualTo("-Dfoo2=bar2"); } @Test public void parseWithNewLinesAndTabs() { String[] args = parseArgs(" -Dfoo=bar \n" + "\t\t -Dfoo2=bar2 "); - assertEquals(2, args.length); - assertEquals("-Dfoo=bar", args[0]); - assertEquals("-Dfoo2=bar2", args[1]); + assertThat(args.length).isEqualTo(2); + assertThat(args[0]).isEqualTo("-Dfoo=bar"); + assertThat(args[1]).isEqualTo("-Dfoo2=bar2"); } @Test public void quoteHandledProperly() { String[] args = parseArgs("-Dvalue=\"My Value\" "); - assertEquals(1, args.length); - assertEquals("-Dvalue=My Value", args[0]); + assertThat(args.length).isEqualTo(1); + assertThat(args[0]).isEqualTo("-Dvalue=My Value"); } private String[] parseArgs(String args) { diff --git a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java index d44eb33221..351d20ae4e 100644 --- a/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java +++ b/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/Verify.java @@ -28,11 +28,7 @@ import java.util.zip.ZipFile; import org.springframework.util.FileCopyUtils; -import static org.hamcrest.Matchers.containsString; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Verification utility for use with maven-invoker-plugin verification scripts. @@ -107,13 +103,15 @@ public final class Verify { } public void assertHasNonUnpackEntry(String entryName) { - assertTrue("Entry starting with " + entryName + " was an UNPACK entry", - hasNonUnpackEntry(entryName)); + assertThat(hasNonUnpackEntry(entryName)) + .as("Entry starting with " + entryName + " was an UNPACK entry") + .isTrue(); } public void assertHasUnpackEntry(String entryName) { - assertTrue("Entry starting with " + entryName + " was not an UNPACK entry", - hasUnpackEntry(entryName)); + assertThat(hasUnpackEntry(entryName)) + .as("Entry starting with " + entryName + " was not an UNPACK entry") + .isTrue(); } private boolean hasNonUnpackEntry(String entryName) { @@ -167,22 +165,21 @@ public final class Verify { public void verify(boolean executable, String... scriptContents) throws Exception { - assertTrue("Archive missing", this.file.exists()); - assertTrue("Archive not a file", this.file.isFile()); + assertThat(this.file).exists().isFile(); if (scriptContents.length > 0 && executable) { String contents = new String(FileCopyUtils.copyToByteArray(this.file)); contents = contents.substring(0, contents .indexOf(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }))); for (String content : scriptContents) { - assertThat(contents, containsString(content)); + assertThat(contents).contains(content); } } if (!executable) { String contents = new String(FileCopyUtils.copyToByteArray(this.file)); - assertTrue("Is executable", contents - .startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 }))); + assertThat(contents).as("Is executable") + .startsWith(new String(new byte[] { 0x50, 0x4b, 0x03, 0x04 })); } ZipFile zipFile = new ZipFile(this.file); @@ -224,18 +221,21 @@ public final class Verify { verifier.assertHasEntryNameStartingWith("lib/spring-context"); verifier.assertHasEntryNameStartingWith("lib/spring-core"); verifier.assertHasEntryNameStartingWith("lib/javax.servlet-api-3"); - assertTrue("Unpacked launcher classes", verifier - .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")); - assertTrue("Own classes", - verifier.hasEntry("org/" + "test/SampleApplication.class")); + assertThat(verifier + .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")) + .as("Unpacked launcher classes").isTrue(); + assertThat(verifier.hasEntry("org/" + "test/SampleApplication.class")) + .as("Own classes").isTrue(); } @Override protected void verifyManifest(Manifest manifest) throws Exception { - assertEquals("org.springframework.boot.loader.JarLauncher", - manifest.getMainAttributes().getValue("Main-Class")); - assertEquals(this.main, manifest.getMainAttributes().getValue("Start-Class")); - assertEquals("Foo", manifest.getMainAttributes().getValue("Not-Used")); + assertThat(manifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.JarLauncher"); + assertThat(manifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo(this.main); + assertThat(manifest.getMainAttributes().getValue("Not-Used")) + .isEqualTo("Foo"); } } @@ -252,20 +252,23 @@ public final class Verify { verifier.assertHasEntryNameStartingWith("WEB-INF/lib/spring-core"); verifier.assertHasEntryNameStartingWith( "WEB-INF/lib-provided/javax.servlet-api-3"); - assertTrue("Unpacked launcher classes", verifier - .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")); - assertTrue("Own classes", verifier - .hasEntry("WEB-INF/classes/org/" + "test/SampleApplication.class")); - assertTrue("Web content", verifier.hasEntry("index.html")); + assertThat(verifier + .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")) + .as("Unpacked launcher classes").isTrue(); + assertThat(verifier + .hasEntry("WEB-INF/classes/org/" + "test/SampleApplication.class")) + .as("Own classes").isTrue(); + assertThat(verifier.hasEntry("index.html")).as("Web content").isTrue(); } @Override protected void verifyManifest(Manifest manifest) throws Exception { - assertEquals("org.springframework.boot.loader.WarLauncher", - manifest.getMainAttributes().getValue("Main-Class")); - assertEquals("org.test.SampleApplication", - manifest.getMainAttributes().getValue("Start-Class")); - assertEquals("Foo", manifest.getMainAttributes().getValue("Not-Used")); + assertThat(manifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.WarLauncher"); + assertThat(manifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("org.test.SampleApplication"); + assertThat(manifest.getMainAttributes().getValue("Not-Used")) + .isEqualTo("Foo"); } } @@ -277,11 +280,12 @@ public final class Verify { @Override protected void verifyManifest(Manifest manifest) throws Exception { - assertEquals("org.springframework.boot.loader.PropertiesLauncher", - manifest.getMainAttributes().getValue("Main-Class")); - assertEquals("org.test.SampleApplication", - manifest.getMainAttributes().getValue("Start-Class")); - assertEquals("Foo", manifest.getMainAttributes().getValue("Not-Used")); + assertThat(manifest.getMainAttributes().getValue("Main-Class")) + .isEqualTo("org.springframework.boot.loader.PropertiesLauncher"); + assertThat(manifest.getMainAttributes().getValue("Start-Class")) + .isEqualTo("org.test.SampleApplication"); + assertThat(manifest.getMainAttributes().getValue("Not-Used")) + .isEqualTo("Foo"); } } @@ -297,10 +301,11 @@ public final class Verify { verifier.assertHasEntryNameStartingWith("lib/spring-context"); verifier.assertHasEntryNameStartingWith("lib/spring-core"); verifier.assertHasNoEntryNameStartingWith("lib/javax.servlet-api-3"); - assertFalse("Unpacked launcher classes", verifier - .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")); - assertTrue("Own classes", - verifier.hasEntry("org/" + "test/SampleModule.class")); + assertThat(verifier + .hasEntry("org/" + "springframework/boot/loader/JarLauncher.class")) + .as("Unpacked launcher classes").isFalse(); + assertThat(verifier.hasEntry("org/" + "test/SampleModule.class")) + .as("Own classes").isTrue(); } @Override