From ad785527f122ecd5142608b148bd39b6f942ac02 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 29 Mar 2018 10:59:40 +0200 Subject: [PATCH] Move to assertJ --- .../test/generator/GradleBuildAssert.java | 9 +- .../initializr/test/generator/PomAssert.java | 89 ++++++++----------- .../test/generator/ProjectAssert.java | 23 ++--- .../test/generator/SourceCodeAssert.java | 22 ++--- 4 files changed, 56 insertions(+), 87 deletions(-) diff --git a/initializr-generator/src/test/java/io/spring/initializr/test/generator/GradleBuildAssert.java b/initializr-generator/src/test/java/io/spring/initializr/test/generator/GradleBuildAssert.java index 3f06a836..413ef29a 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/test/generator/GradleBuildAssert.java +++ b/initializr-generator/src/test/java/io/spring/initializr/test/generator/GradleBuildAssert.java @@ -18,8 +18,7 @@ package io.spring.initializr.test.generator; import io.spring.initializr.generator.ProjectRequest; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Very simple assertions for the gradle build. @@ -63,14 +62,12 @@ public class GradleBuildAssert { } public GradleBuildAssert contains(String expression) { - assertTrue(expression + " has not been found in gradle build " + content, - content.contains(expression)); + assertThat(this.content).contains(expression); return this; } public GradleBuildAssert doesNotContain(String expression) { - assertFalse(expression + " is not expected in gradle build " + content, - content.contains(expression)); + assertThat(this.content).doesNotContain(expression); return this; } diff --git a/initializr-generator/src/test/java/io/spring/initializr/test/generator/PomAssert.java b/initializr-generator/src/test/java/io/spring/initializr/test/generator/PomAssert.java index 4040f0b5..42ee528c 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/test/generator/PomAssert.java +++ b/initializr-generator/src/test/java/io/spring/initializr/test/generator/PomAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,10 +37,7 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; -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; /** * XPath assertions that are specific to a standard Maven POM. @@ -88,7 +85,8 @@ public class PomAssert { public PomAssert hasGroupId(String groupId) { try { - assertEquals(groupId, eng.evaluate(createRootNodeXPath("groupId"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("groupId"), doc)) + .isEqualTo(groupId); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -98,8 +96,8 @@ public class PomAssert { public PomAssert hasArtifactId(String artifactId) { try { - assertEquals(artifactId, - eng.evaluate(createRootNodeXPath("artifactId"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("artifactId"), doc)) + .isEqualTo(artifactId); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -109,7 +107,8 @@ public class PomAssert { public PomAssert hasVersion(String version) { try { - assertEquals(version, eng.evaluate(createRootNodeXPath("version"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("version"), doc)) + .isEqualTo(version); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -119,7 +118,8 @@ public class PomAssert { public PomAssert hasPackaging(String packaging) { try { - assertEquals(packaging, eng.evaluate(createRootNodeXPath("packaging"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("packaging"), doc)) + .isEqualTo(packaging); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -129,7 +129,8 @@ public class PomAssert { public PomAssert hasName(String name) { try { - assertEquals(name, eng.evaluate(createRootNodeXPath("name"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("name"), doc)) + .isEqualTo(name); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -139,8 +140,8 @@ public class PomAssert { public PomAssert hasDescription(String description) { try { - assertEquals(description, - eng.evaluate(createRootNodeXPath("description"), doc)); + assertThat(eng.evaluate(createRootNodeXPath("description"), doc)) + .isEqualTo(description); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -150,8 +151,8 @@ public class PomAssert { public PomAssert hasJavaVersion(String javaVersion) { try { - assertEquals(javaVersion, - eng.evaluate(createPropertyNodeXpath("java.version"), doc)); + assertThat(eng.evaluate(createPropertyNodeXpath("java.version"), doc)) + .isEqualTo(javaVersion); } catch (XpathException e) { throw new IllegalStateException("Cannot find path", e); @@ -160,20 +161,18 @@ public class PomAssert { } public PomAssert hasProperty(String name, String value) { - assertTrue("No property " + name + " found", properties.containsKey(name)); - assertEquals("Wrong value for property " + name, value, properties.get(name)); + assertThat(this.properties).containsKeys(name); + assertThat(this.properties).containsEntry(name, value); return this; } public PomAssert hasNoProperty(String name) { - assertFalse("No property " + name + " should have been found", - properties.containsKey(name)); + assertThat(this.properties).doesNotContainKeys(name); return this; } public PomAssert hasDependenciesCount(int count) { - assertEquals("Wrong number of declared dependencies --> " + dependencies.keySet(), - count, dependencies.size()); + assertThat(this.dependencies).hasSize(count); return this; } @@ -205,9 +204,9 @@ public class PomAssert { } public PomAssert hasParent(String groupId, String artifactId, String version) { - assertEquals(groupId, this.parentPom.getGroupId()); - assertEquals(artifactId, this.parentPom.getArtifactId()); - assertEquals(version, this.parentPom.getVersion()); + assertThat(this.parentPom.getGroupId()).isEqualTo(groupId); + assertThat(this.parentPom.getArtifactId()).isEqualTo(artifactId); + assertThat(this.parentPom.getVersion()).isEqualTo(version); return this; } @@ -218,35 +217,30 @@ public class PomAssert { public PomAssert hasDependency(Dependency expected) { String id = generateDependencyId(expected.getGroupId(), expected.getArtifactId()); + assertThat(this.dependencies).containsKey(id); Dependency dependency = dependencies.get(id); - assertNotNull("No dependency found with '" + id + "' --> " + dependencies.keySet(), - dependency); if (expected.getVersion() != null) { - assertEquals("Wrong version for " + dependency, expected.getVersion(), - dependency.getVersion()); + assertThat(dependency.getVersion()).isEqualTo(expected.getVersion()); } if (expected.getScope() != null) { - assertEquals("Wrong scope for " + dependency, expected.getScope(), - dependency.getScope()); + assertThat(dependency.getScope()).isEqualTo(expected.getScope()); } if (expected.getType() != null) { - assertEquals("Wrong type for " + dependency, expected.getType(), - dependency.getType()); + assertThat(dependency.getType()).isEqualTo(expected.getType()); } return this; } public PomAssert hasBom(String groupId, String artifactId, String version) { String id = generateBomId(groupId, artifactId); + assertThat(this.boms).containsKey(id); BillOfMaterials bom = boms.get(id); - assertNotNull("No BOM found with '" + id + "' --> " + boms.keySet(), bom); - assertEquals("Wrong version for " + bom, version, bom.getVersion()); + assertThat(bom.getVersion()).isEqualTo(version); return this; } public PomAssert hasBomsCount(int count) { - assertEquals("Wrong number of declared boms -->" + boms.keySet(), count, - boms.size()); + assertThat(this.boms).hasSize(count); return this; } @@ -271,32 +265,27 @@ public class PomAssert { public PomAssert hasRepository(String id, String name, String url, Boolean snapshotsEnabled) { + assertThat(this.repositories).containsKeys(id); Repository repository = repositories.get(id); - assertNotNull( - "No repository found with '" + id + "' --> " + repositories.keySet(), - repository); if (name != null) { - assertEquals("Wrong name for " + repository, name, repository.getName()); + assertThat(repository.getName()).isEqualTo(name); } if (url != null) { try { - assertEquals("Wrong url for " + repository, new URL(url), - repository.getUrl()); + assertThat(repository.getUrl()).isEqualTo(new URL(url)); } catch (MalformedURLException e) { throw new IllegalArgumentException("Cannot parse URL", e); } } if (snapshotsEnabled) { - assertEquals("Wrong snapshots enabled flag for " + repository, - snapshotsEnabled, repository.isSnapshotsEnabled()); + assertThat(repository.isSnapshotsEnabled()).isEqualTo(snapshotsEnabled); } return this; } public PomAssert hasRepositoriesCount(int count) { - assertEquals("Wrong number of declared repositories -->" + repositories.keySet(), - count, repositories.size()); + assertThat(this.repositories).hasSize(count); return this; } @@ -394,8 +383,7 @@ public class PomAssert { dependency.setType(type.item(0).getTextContent()); } String id = dependency.generateId(); - assertFalse("Duplicate dependency with id " + id, - dependencies.containsKey(id)); + assertThat(dependencies).doesNotContainKeys(id); dependencies.put(id, dependency); } } @@ -434,7 +422,7 @@ public class PomAssert { bom.setVersion(version.item(0).getTextContent()); } String id = generateBomId(bom.getGroupId(), bom.getArtifactId()); - assertFalse("Duplicate BOM with id " + id, boms.containsKey(id)); + assertThat(this.boms).doesNotContainKeys(id); boms.put(id, bom); } } @@ -481,8 +469,7 @@ public class PomAssert { "true".equals(snapshotsEnabled.item(0).getTextContent())); } } - assertFalse("Duplicate Repository with id " + id, - repositories.containsKey(id)); + assertThat(this.repositories).doesNotContainKeys(id); repositories.put(id, repository); } } diff --git a/initializr-generator/src/test/java/io/spring/initializr/test/generator/ProjectAssert.java b/initializr-generator/src/test/java/io/spring/initializr/test/generator/ProjectAssert.java index 4fac7396..50e1b736 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/test/generator/ProjectAssert.java +++ b/initializr-generator/src/test/java/io/spring/initializr/test/generator/ProjectAssert.java @@ -27,8 +27,7 @@ import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.util.StreamUtils; import org.springframework.util.StringUtils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Various project based assertions. @@ -71,9 +70,9 @@ public class ProjectAssert { */ public ProjectAssert hasBaseDir(String name) { File projectDir = file(name); - assertTrue("No directory " + name + " found in " + dir.getAbsolutePath(), - projectDir.exists()); - assertTrue(name + " is not a directory", projectDir.isDirectory()); + assertThat(projectDir).describedAs("No directory %s found in %s", name, + dir.getAbsolutePath()).exists(); + assertThat(projectDir).isDirectory(); // Replacing the root dir so that other assertions match the root return new ProjectAssert(projectDir); } @@ -149,8 +148,7 @@ public class ProjectAssert { Properties properties = properties( "gradle/wrapper/gradle-wrapper.properties"); String distributionUrl = properties.getProperty("distributionUrl"); - assertTrue("Wrong gradle version for project " + distributionUrl, - distributionUrl.contains(version)); + assertThat(distributionUrl).contains(version); } return this; } @@ -246,15 +244,8 @@ public class ProjectAssert { public ProjectAssert assertFile(String localPath, boolean exist) { File candidate = file(localPath); - assertEquals("Invalid presence (\"" + exist + "\") for " + localPath, exist, - candidate.exists()); - return this; - } - - public ProjectAssert assertExecutableFile(String localPath, boolean executable) { - File candidate = file(localPath); - assertEquals("Invalid (\"" + executable + "\") for " + localPath, executable, - candidate.exists() && candidate.canExecute()); + assertThat(candidate.exists()).describedAs("Invalid presence (%s) exist for %s", + exist, localPath).isEqualTo(exist); return this; } diff --git a/initializr-generator/src/test/java/io/spring/initializr/test/generator/SourceCodeAssert.java b/initializr-generator/src/test/java/io/spring/initializr/test/generator/SourceCodeAssert.java index 47dea60e..0e8b049e 100644 --- a/initializr-generator/src/test/java/io/spring/initializr/test/generator/SourceCodeAssert.java +++ b/initializr-generator/src/test/java/io/spring/initializr/test/generator/SourceCodeAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,9 +23,7 @@ import java.nio.charset.Charset; import org.springframework.core.io.Resource; import org.springframework.util.StreamUtils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Source code assertions. @@ -46,8 +44,8 @@ public class SourceCodeAssert { try (InputStream stream = expected.getInputStream()) { String expectedContent = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); - assertEquals("Unexpected content for " + name, - expectedContent.replaceAll("\r\n", "\n"), content); + assertThat(content).describedAs("Content for %s", this.name) + .isEqualTo(expectedContent.replaceAll("\r\n", "\n")); } catch (IOException e) { throw new IllegalStateException("Cannot read file", e); @@ -70,18 +68,14 @@ public class SourceCodeAssert { } public SourceCodeAssert contains(String... expressions) { - for (String expression : expressions) { - assertTrue(expression + " has not been found in source code '" + name + "'", - content.contains(expression)); - } + assertThat(this.content).describedAs("Content for %s", this.name) + .contains(expressions); return this; } public SourceCodeAssert doesNotContain(String... expressions) { - for (String expression : expressions) { - assertFalse(expression + " should not have been found in source code '" + name - + "'", content.contains(expression)); - } + assertThat(this.content).describedAs("Content for %s", this.name) + .doesNotContain(expressions); return this; }