mirror of
https://gitee.com/dcren/initializr.git
synced 2026-09-19 14:27:41 +08:00
Move to assertJ
This commit is contained in:
+3
-6
@@ -18,8 +18,7 @@ package io.spring.initializr.test.generator;
|
|||||||
|
|
||||||
import io.spring.initializr.generator.ProjectRequest;
|
import io.spring.initializr.generator.ProjectRequest;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Very simple assertions for the gradle build.
|
* Very simple assertions for the gradle build.
|
||||||
@@ -63,14 +62,12 @@ public class GradleBuildAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public GradleBuildAssert contains(String expression) {
|
public GradleBuildAssert contains(String expression) {
|
||||||
assertTrue(expression + " has not been found in gradle build " + content,
|
assertThat(this.content).contains(expression);
|
||||||
content.contains(expression));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GradleBuildAssert doesNotContain(String expression) {
|
public GradleBuildAssert doesNotContain(String expression) {
|
||||||
assertFalse(expression + " is not expected in gradle build " + content,
|
assertThat(this.content).doesNotContain(expression);
|
||||||
content.contains(expression));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+38
-51
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.Node;
|
||||||
import org.w3c.dom.NodeList;
|
import org.w3c.dom.NodeList;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XPath assertions that are specific to a standard Maven POM.
|
* XPath assertions that are specific to a standard Maven POM.
|
||||||
@@ -88,7 +85,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasGroupId(String groupId) {
|
public PomAssert hasGroupId(String groupId) {
|
||||||
try {
|
try {
|
||||||
assertEquals(groupId, eng.evaluate(createRootNodeXPath("groupId"), doc));
|
assertThat(eng.evaluate(createRootNodeXPath("groupId"), doc))
|
||||||
|
.isEqualTo(groupId);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -98,8 +96,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasArtifactId(String artifactId) {
|
public PomAssert hasArtifactId(String artifactId) {
|
||||||
try {
|
try {
|
||||||
assertEquals(artifactId,
|
assertThat(eng.evaluate(createRootNodeXPath("artifactId"), doc))
|
||||||
eng.evaluate(createRootNodeXPath("artifactId"), doc));
|
.isEqualTo(artifactId);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -109,7 +107,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasVersion(String version) {
|
public PomAssert hasVersion(String version) {
|
||||||
try {
|
try {
|
||||||
assertEquals(version, eng.evaluate(createRootNodeXPath("version"), doc));
|
assertThat(eng.evaluate(createRootNodeXPath("version"), doc))
|
||||||
|
.isEqualTo(version);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -119,7 +118,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasPackaging(String packaging) {
|
public PomAssert hasPackaging(String packaging) {
|
||||||
try {
|
try {
|
||||||
assertEquals(packaging, eng.evaluate(createRootNodeXPath("packaging"), doc));
|
assertThat(eng.evaluate(createRootNodeXPath("packaging"), doc))
|
||||||
|
.isEqualTo(packaging);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -129,7 +129,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasName(String name) {
|
public PomAssert hasName(String name) {
|
||||||
try {
|
try {
|
||||||
assertEquals(name, eng.evaluate(createRootNodeXPath("name"), doc));
|
assertThat(eng.evaluate(createRootNodeXPath("name"), doc))
|
||||||
|
.isEqualTo(name);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -139,8 +140,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasDescription(String description) {
|
public PomAssert hasDescription(String description) {
|
||||||
try {
|
try {
|
||||||
assertEquals(description,
|
assertThat(eng.evaluate(createRootNodeXPath("description"), doc))
|
||||||
eng.evaluate(createRootNodeXPath("description"), doc));
|
.isEqualTo(description);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -150,8 +151,8 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasJavaVersion(String javaVersion) {
|
public PomAssert hasJavaVersion(String javaVersion) {
|
||||||
try {
|
try {
|
||||||
assertEquals(javaVersion,
|
assertThat(eng.evaluate(createPropertyNodeXpath("java.version"), doc))
|
||||||
eng.evaluate(createPropertyNodeXpath("java.version"), doc));
|
.isEqualTo(javaVersion);
|
||||||
}
|
}
|
||||||
catch (XpathException e) {
|
catch (XpathException e) {
|
||||||
throw new IllegalStateException("Cannot find path", e);
|
throw new IllegalStateException("Cannot find path", e);
|
||||||
@@ -160,20 +161,18 @@ public class PomAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasProperty(String name, String value) {
|
public PomAssert hasProperty(String name, String value) {
|
||||||
assertTrue("No property " + name + " found", properties.containsKey(name));
|
assertThat(this.properties).containsKeys(name);
|
||||||
assertEquals("Wrong value for property " + name, value, properties.get(name));
|
assertThat(this.properties).containsEntry(name, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasNoProperty(String name) {
|
public PomAssert hasNoProperty(String name) {
|
||||||
assertFalse("No property " + name + " should have been found",
|
assertThat(this.properties).doesNotContainKeys(name);
|
||||||
properties.containsKey(name));
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasDependenciesCount(int count) {
|
public PomAssert hasDependenciesCount(int count) {
|
||||||
assertEquals("Wrong number of declared dependencies --> " + dependencies.keySet(),
|
assertThat(this.dependencies).hasSize(count);
|
||||||
count, dependencies.size());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,9 +204,9 @@ public class PomAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasParent(String groupId, String artifactId, String version) {
|
public PomAssert hasParent(String groupId, String artifactId, String version) {
|
||||||
assertEquals(groupId, this.parentPom.getGroupId());
|
assertThat(this.parentPom.getGroupId()).isEqualTo(groupId);
|
||||||
assertEquals(artifactId, this.parentPom.getArtifactId());
|
assertThat(this.parentPom.getArtifactId()).isEqualTo(artifactId);
|
||||||
assertEquals(version, this.parentPom.getVersion());
|
assertThat(this.parentPom.getVersion()).isEqualTo(version);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,35 +217,30 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasDependency(Dependency expected) {
|
public PomAssert hasDependency(Dependency expected) {
|
||||||
String id = generateDependencyId(expected.getGroupId(), expected.getArtifactId());
|
String id = generateDependencyId(expected.getGroupId(), expected.getArtifactId());
|
||||||
|
assertThat(this.dependencies).containsKey(id);
|
||||||
Dependency dependency = dependencies.get(id);
|
Dependency dependency = dependencies.get(id);
|
||||||
assertNotNull("No dependency found with '" + id + "' --> " + dependencies.keySet(),
|
|
||||||
dependency);
|
|
||||||
if (expected.getVersion() != null) {
|
if (expected.getVersion() != null) {
|
||||||
assertEquals("Wrong version for " + dependency, expected.getVersion(),
|
assertThat(dependency.getVersion()).isEqualTo(expected.getVersion());
|
||||||
dependency.getVersion());
|
|
||||||
}
|
}
|
||||||
if (expected.getScope() != null) {
|
if (expected.getScope() != null) {
|
||||||
assertEquals("Wrong scope for " + dependency, expected.getScope(),
|
assertThat(dependency.getScope()).isEqualTo(expected.getScope());
|
||||||
dependency.getScope());
|
|
||||||
}
|
}
|
||||||
if (expected.getType() != null) {
|
if (expected.getType() != null) {
|
||||||
assertEquals("Wrong type for " + dependency, expected.getType(),
|
assertThat(dependency.getType()).isEqualTo(expected.getType());
|
||||||
dependency.getType());
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasBom(String groupId, String artifactId, String version) {
|
public PomAssert hasBom(String groupId, String artifactId, String version) {
|
||||||
String id = generateBomId(groupId, artifactId);
|
String id = generateBomId(groupId, artifactId);
|
||||||
|
assertThat(this.boms).containsKey(id);
|
||||||
BillOfMaterials bom = boms.get(id);
|
BillOfMaterials bom = boms.get(id);
|
||||||
assertNotNull("No BOM found with '" + id + "' --> " + boms.keySet(), bom);
|
assertThat(bom.getVersion()).isEqualTo(version);
|
||||||
assertEquals("Wrong version for " + bom, version, bom.getVersion());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasBomsCount(int count) {
|
public PomAssert hasBomsCount(int count) {
|
||||||
assertEquals("Wrong number of declared boms -->" + boms.keySet(), count,
|
assertThat(this.boms).hasSize(count);
|
||||||
boms.size());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,32 +265,27 @@ public class PomAssert {
|
|||||||
|
|
||||||
public PomAssert hasRepository(String id, String name, String url,
|
public PomAssert hasRepository(String id, String name, String url,
|
||||||
Boolean snapshotsEnabled) {
|
Boolean snapshotsEnabled) {
|
||||||
|
assertThat(this.repositories).containsKeys(id);
|
||||||
Repository repository = repositories.get(id);
|
Repository repository = repositories.get(id);
|
||||||
assertNotNull(
|
|
||||||
"No repository found with '" + id + "' --> " + repositories.keySet(),
|
|
||||||
repository);
|
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
assertEquals("Wrong name for " + repository, name, repository.getName());
|
assertThat(repository.getName()).isEqualTo(name);
|
||||||
}
|
}
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
try {
|
try {
|
||||||
assertEquals("Wrong url for " + repository, new URL(url),
|
assertThat(repository.getUrl()).isEqualTo(new URL(url));
|
||||||
repository.getUrl());
|
|
||||||
}
|
}
|
||||||
catch (MalformedURLException e) {
|
catch (MalformedURLException e) {
|
||||||
throw new IllegalArgumentException("Cannot parse URL", e);
|
throw new IllegalArgumentException("Cannot parse URL", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (snapshotsEnabled) {
|
if (snapshotsEnabled) {
|
||||||
assertEquals("Wrong snapshots enabled flag for " + repository,
|
assertThat(repository.isSnapshotsEnabled()).isEqualTo(snapshotsEnabled);
|
||||||
snapshotsEnabled, repository.isSnapshotsEnabled());
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PomAssert hasRepositoriesCount(int count) {
|
public PomAssert hasRepositoriesCount(int count) {
|
||||||
assertEquals("Wrong number of declared repositories -->" + repositories.keySet(),
|
assertThat(this.repositories).hasSize(count);
|
||||||
count, repositories.size());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,8 +383,7 @@ public class PomAssert {
|
|||||||
dependency.setType(type.item(0).getTextContent());
|
dependency.setType(type.item(0).getTextContent());
|
||||||
}
|
}
|
||||||
String id = dependency.generateId();
|
String id = dependency.generateId();
|
||||||
assertFalse("Duplicate dependency with id " + id,
|
assertThat(dependencies).doesNotContainKeys(id);
|
||||||
dependencies.containsKey(id));
|
|
||||||
dependencies.put(id, dependency);
|
dependencies.put(id, dependency);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -434,7 +422,7 @@ public class PomAssert {
|
|||||||
bom.setVersion(version.item(0).getTextContent());
|
bom.setVersion(version.item(0).getTextContent());
|
||||||
}
|
}
|
||||||
String id = generateBomId(bom.getGroupId(), bom.getArtifactId());
|
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);
|
boms.put(id, bom);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -481,8 +469,7 @@ public class PomAssert {
|
|||||||
"true".equals(snapshotsEnabled.item(0).getTextContent()));
|
"true".equals(snapshotsEnabled.item(0).getTextContent()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assertFalse("Duplicate Repository with id " + id,
|
assertThat(this.repositories).doesNotContainKeys(id);
|
||||||
repositories.containsKey(id));
|
|
||||||
repositories.put(id, repository);
|
repositories.put(id, repository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-16
@@ -27,8 +27,7 @@ import org.springframework.core.io.support.PropertiesLoaderUtils;
|
|||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Various project based assertions.
|
* Various project based assertions.
|
||||||
@@ -71,9 +70,9 @@ public class ProjectAssert {
|
|||||||
*/
|
*/
|
||||||
public ProjectAssert hasBaseDir(String name) {
|
public ProjectAssert hasBaseDir(String name) {
|
||||||
File projectDir = file(name);
|
File projectDir = file(name);
|
||||||
assertTrue("No directory " + name + " found in " + dir.getAbsolutePath(),
|
assertThat(projectDir).describedAs("No directory %s found in %s", name,
|
||||||
projectDir.exists());
|
dir.getAbsolutePath()).exists();
|
||||||
assertTrue(name + " is not a directory", projectDir.isDirectory());
|
assertThat(projectDir).isDirectory();
|
||||||
// Replacing the root dir so that other assertions match the root
|
// Replacing the root dir so that other assertions match the root
|
||||||
return new ProjectAssert(projectDir);
|
return new ProjectAssert(projectDir);
|
||||||
}
|
}
|
||||||
@@ -149,8 +148,7 @@ public class ProjectAssert {
|
|||||||
Properties properties = properties(
|
Properties properties = properties(
|
||||||
"gradle/wrapper/gradle-wrapper.properties");
|
"gradle/wrapper/gradle-wrapper.properties");
|
||||||
String distributionUrl = properties.getProperty("distributionUrl");
|
String distributionUrl = properties.getProperty("distributionUrl");
|
||||||
assertTrue("Wrong gradle version for project " + distributionUrl,
|
assertThat(distributionUrl).contains(version);
|
||||||
distributionUrl.contains(version));
|
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -246,15 +244,8 @@ public class ProjectAssert {
|
|||||||
|
|
||||||
public ProjectAssert assertFile(String localPath, boolean exist) {
|
public ProjectAssert assertFile(String localPath, boolean exist) {
|
||||||
File candidate = file(localPath);
|
File candidate = file(localPath);
|
||||||
assertEquals("Invalid presence (\"" + exist + "\") for " + localPath, exist,
|
assertThat(candidate.exists()).describedAs("Invalid presence (%s) exist for %s",
|
||||||
candidate.exists());
|
exist, localPath).isEqualTo(exist);
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ProjectAssert assertExecutableFile(String localPath, boolean executable) {
|
|
||||||
File candidate = file(localPath);
|
|
||||||
assertEquals("Invalid (\"" + executable + "\") for " + localPath, executable,
|
|
||||||
candidate.exists() && candidate.canExecute());
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-14
@@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.core.io.Resource;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Source code assertions.
|
* Source code assertions.
|
||||||
@@ -46,8 +44,8 @@ public class SourceCodeAssert {
|
|||||||
try (InputStream stream = expected.getInputStream()) {
|
try (InputStream stream = expected.getInputStream()) {
|
||||||
String expectedContent = StreamUtils.copyToString(stream,
|
String expectedContent = StreamUtils.copyToString(stream,
|
||||||
Charset.forName("UTF-8"));
|
Charset.forName("UTF-8"));
|
||||||
assertEquals("Unexpected content for " + name,
|
assertThat(content).describedAs("Content for %s", this.name)
|
||||||
expectedContent.replaceAll("\r\n", "\n"), content);
|
.isEqualTo(expectedContent.replaceAll("\r\n", "\n"));
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
throw new IllegalStateException("Cannot read file", e);
|
throw new IllegalStateException("Cannot read file", e);
|
||||||
@@ -70,18 +68,14 @@ public class SourceCodeAssert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public SourceCodeAssert contains(String... expressions) {
|
public SourceCodeAssert contains(String... expressions) {
|
||||||
for (String expression : expressions) {
|
assertThat(this.content).describedAs("Content for %s", this.name)
|
||||||
assertTrue(expression + " has not been found in source code '" + name + "'",
|
.contains(expressions);
|
||||||
content.contains(expression));
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SourceCodeAssert doesNotContain(String... expressions) {
|
public SourceCodeAssert doesNotContain(String... expressions) {
|
||||||
for (String expression : expressions) {
|
assertThat(this.content).describedAs("Content for %s", this.name)
|
||||||
assertFalse(expression + " should not have been found in source code '" + name
|
.doesNotContain(expressions);
|
||||||
+ "'", content.contains(expression));
|
|
||||||
}
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user