Use consistent exception messages in Assert calls

Update `Assert` calls to consistently use messages of the form
"'item' must [not] ...".

Closes gh-43780
This commit is contained in:
Phillip Webb
2025-01-09 15:33:44 -08:00
parent f08188d5cf
commit a49719d73e
559 changed files with 2001 additions and 2003 deletions

View File

@@ -129,9 +129,9 @@ public final class DockerComposeFile {
* @return the Docker Compose file
*/
public static DockerComposeFile of(File file) {
Assert.notNull(file, "File must not be null");
Assert.isTrue(file.exists(), () -> "Docker Compose file '%s' does not exist".formatted(file));
Assert.isTrue(file.isFile(), () -> "Docker compose file '%s' is not a file".formatted(file));
Assert.notNull(file, "'file' must not be null");
Assert.isTrue(file.exists(), () -> "'file' [%s] must exist".formatted(file));
Assert.isTrue(file.isFile(), () -> "'file' [%s] must be a normal file".formatted(file));
return new DockerComposeFile(Collections.singletonList(file));
}
@@ -142,11 +142,11 @@ public final class DockerComposeFile {
* @since 3.4.0
*/
public static DockerComposeFile of(Collection<? extends File> files) {
Assert.notNull(files, "Files must not be null");
Assert.notNull(files, "'files' must not be null");
for (File file : files) {
Assert.notNull(file, "File must not be null");
Assert.isTrue(file.exists(), () -> "Docker Compose file '%s' does not exist".formatted(file));
Assert.isTrue(file.isFile(), () -> "Docker compose file '%s' is not a file".formatted(file));
Assert.notNull(file, "'files' must not contain null elements");
Assert.isTrue(file.exists(), () -> "'files' content [%s] must exist".formatted(file));
Assert.isTrue(file.isFile(), () -> "'files' content [%s] must be a normal file".formatted(file));
}
return new DockerComposeFile(List.copyOf(files));
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -39,7 +39,7 @@ class ImageName {
private final String string;
ImageName(String domain, String path) {
Assert.hasText(path, "Path must not be empty");
Assert.hasText(path, "'path' must not be empty");
this.domain = getDomainOrDefault(domain);
this.name = getNameWithDefaultPath(this.domain, path);
this.string = this.domain + "/" + this.name;
@@ -114,13 +114,12 @@ class ImageName {
}
static ImageName of(String value) {
Assert.hasText(value, "Value must not be empty");
Assert.hasText(value, "'value' must not be empty");
String domain = parseDomain(value);
String path = (domain != null) ? value.substring(domain.length() + 1) : value;
Assert.isTrue(Regex.PATH.matcher(path).matches(),
() -> "Unable to parse name \"" + value + "\". "
+ "Image name must be in the form '[domainHost:port/][path/]name', "
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
() -> "'value' path must contain an image reference in the form '[domainHost:port/][path/]name' "
+ "(with 'path' and 'name' containing only [a-z0-9][.][_][-]) [" + value + "]");
return new ImageName(domain, path);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -39,7 +39,7 @@ public final class ImageReference {
private final String string;
private ImageReference(ImageName name, String tag, String digest) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(name, "'name' must not be null");
this.name = name;
this.tag = tag;
this.digest = digest;
@@ -136,7 +136,7 @@ public final class ImageReference {
* @return an {@link ImageReference} instance
*/
public static ImageReference of(String value) {
Assert.hasText(value, "Value must not be null");
Assert.hasText(value, "'value' must not be null");
String domain = ImageName.parseDomain(value);
String path = (domain != null) ? value.substring(domain.length() + 1) : value;
String digest = null;
@@ -162,9 +162,9 @@ public final class ImageReference {
}
}
Assert.isTrue(Regex.PATH.matcher(path).matches(),
() -> "Unable to parse image reference \"" + value + "\". "
+ "Image reference must be in the form '[domainHost:port/][path/]name[:tag][@digest]', "
+ "with 'path' and 'name' containing only [a-z0-9][.][_][-]");
() -> "'value' path must contain an image reference in the form "
+ "'[domainHost:port/][path/]name[:tag][@digest] "
+ "(with 'path' and 'name' containing only [a-z0-9][.][_][-]) [" + value + "]");
ImageName name = new ImageName(domain, path);
return new ImageReference(name, tag, digest);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -35,7 +35,7 @@ class ConnectionNamePredicate implements Predicate<DockerComposeConnectionSource
private final Set<String> required;
ConnectionNamePredicate(String... required) {
Assert.notEmpty(required, "Required must not be empty");
Assert.notEmpty(required, "'required' must not be empty");
this.required = Arrays.stream(required).map(this::asCanonicalName).collect(Collectors.toSet());
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -111,7 +111,7 @@ public abstract class DockerComposeConnectionDetailsFactory<D extends Connection
* @param runningService the source {@link RunningService}
*/
protected DockerComposeConnectionDetails(RunningService runningService) {
Assert.notNull(runningService, "RunningService must not be null");
Assert.notNull(runningService, "'runningService' must not be null");
this.origin = Origin.from(runningService);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -42,7 +42,7 @@ public class JdbcUrlBuilder {
* @param containerPort the source container port
*/
public JdbcUrlBuilder(String driverProtocol, int containerPort) {
Assert.notNull(driverProtocol, "DriverProtocol must not be null");
Assert.notNull(driverProtocol, "'driverProtocol' must not be null");
this.driverProtocol = driverProtocol;
this.containerPort = containerPort;
}
@@ -67,7 +67,7 @@ public class JdbcUrlBuilder {
}
private String urlFor(RunningService service, String database) {
Assert.notNull(service, "Service must not be null");
Assert.notNull(service, "'service' must not be null");
StringBuilder url = new StringBuilder("jdbc:%s://%s:%d".formatted(this.driverProtocol, service.host(),
service.ports().get(this.containerPort)));
if (StringUtils.hasLength(database)) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -51,14 +51,14 @@ public class ConnectionFactoryOptionsBuilder {
* @param containerPort the source container port
*/
public ConnectionFactoryOptionsBuilder(String driver, int containerPort) {
Assert.notNull(driver, "Driver must not be null");
Assert.notNull(driver, "'driver' must not be null");
this.driver = driver;
this.sourcePort = containerPort;
}
public ConnectionFactoryOptions build(RunningService service, String database, String user, String password) {
Assert.notNull(service, "Service must not be null");
Assert.notNull(database, "Database must not be null");
Assert.notNull(service, "'service' must not be null");
Assert.notNull(database, "'database' must not be null");
ConnectionFactoryOptions.Builder builder = ConnectionFactoryOptions.builder()
.option(ConnectionFactoryOptions.DRIVER, this.driver)
.option(ConnectionFactoryOptions.HOST, service.host())
@@ -91,7 +91,7 @@ public class ConnectionFactoryOptionsBuilder {
Map<String, String> result = new LinkedHashMap<>();
for (String parameter : StringUtils.commaDelimitedListToStringArray(parameters)) {
String[] parts = parameter.split("=");
Assert.state(parts.length == 2, () -> "Unable to parse parameter '%s'".formatted(parameter));
Assert.state(parts.length == 2, () -> "'parameters' [%s] must cotain parsable value".formatted(parameter));
result.put(parts[0], parts[1]);
}
return Collections.unmodifiableMap(result);

View File

@@ -129,20 +129,20 @@ class DockerComposeFileTests {
@Test
void ofWhenFileIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.of((File) null))
.withMessage("File must not be null");
.withMessage("'file' must not be null");
}
@Test
void ofWhenFileDoesNotExistThrowsException() {
File file = new File(this.temp, "missing");
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.of(file))
.withMessageEndingWith("does not exist");
.withMessageEndingWith("must exist");
}
@Test
void ofWhenFileIsNotFileThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> DockerComposeFile.of(this.temp))
.withMessageEndingWith("is not a file");
.withMessageEndingWith("must be a normal file");
}
private DockerComposeFile createComposeFile(String name) throws IOException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 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.
@@ -118,25 +118,26 @@ class ImageNameTests {
@Test
void ofWhenNameIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageName.of(null))
.withMessage("Value must not be empty");
.withMessage("'value' must not be empty");
}
@Test
void ofWhenNameIsEmptyThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageName.of("")).withMessage("Value must not be empty");
assertThatIllegalArgumentException().isThrownBy(() -> ImageName.of(""))
.withMessage("'value' must not be empty");
}
@Test
void ofWhenContainsUppercaseThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageName.of("Test"))
.withMessageContaining("Unable to parse name")
.withMessageContaining("must contain an image reference")
.withMessageContaining("Test");
}
@Test
void ofWhenNameIncludesTagThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageName.of("ubuntu:latest"))
.withMessageContaining("Unable to parse name")
.withMessageContaining("must contain an image reference")
.withMessageContaining(":latest");
}
@@ -144,7 +145,7 @@ class ImageNameTests {
void ofWhenNameIncludeDigestThrowsException() {
assertThatIllegalArgumentException().isThrownBy(
() -> ImageName.of("ubuntu@sha256:47bfdb88c3ae13e488167607973b7688f69d9e8c142c2045af343ec199649c09"))
.withMessageContaining("Unable to parse name")
.withMessageContaining("must contain an image reference")
.withMessageContaining("@sha256:47b");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -165,7 +165,7 @@ class ImageReferenceTests {
assertThatIllegalArgumentException()
.isThrownBy(() -> ImageReference
.of("registry.example.com/example/example-app:1.6.0-dev.2.uncommitted+wip.foo.c75795d"))
.withMessageContaining("Unable to parse image reference");
.withMessageContaining("must contain an image reference");
}
@Test
@@ -173,7 +173,7 @@ class ImageReferenceTests {
void ofWhenImageNameIsVeryLongAndHasIllegalCharacterThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> ImageReference
.of("docker.io/library/this-image-has-a-long-name-with-an-invalid-tag-which-is-at-danger-of-catastrophic-backtracking:1.0.0+1234"))
.withMessageContaining("Unable to parse image reference");
.withMessageContaining("must contain an image reference");
}
@Test

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 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.
@@ -43,7 +43,7 @@ class JdbcUrlBuilderTests {
@Test
void createWhenDriverProtocolIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> new JdbcUrlBuilder(null, 123))
.withMessage("DriverProtocol must not be null");
.withMessage("'driverProtocol' must not be null");
}
@Test
@@ -84,7 +84,7 @@ class JdbcUrlBuilderTests {
@Test
void buildWhenServiceIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.build(null, "mydb"))
.withMessage("Service must not be null");
.withMessage("'service' must not be null");
}
private RunningService mockService(int mappedPort) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,7 +46,7 @@ class ConnectionFactoryOptionsBuilderTests {
@Test
void createWhenDriverProtocolIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> new JdbcUrlBuilder(null, 123))
.withMessage("DriverProtocol must not be null");
.withMessage("'driverProtocol' must not be null");
}
@Test
@@ -81,14 +81,14 @@ class ConnectionFactoryOptionsBuilderTests {
@Test
void buildWhenServiceIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> this.builder.build(null, "mydb", "user", "pass"))
.withMessage("Service must not be null");
.withMessage("'service' must not be null");
}
@Test
void buildWhenDatabaseIsNullThrowsException() {
assertThatIllegalArgumentException()
.isThrownBy(() -> this.builder.build(mockService(456), null, "user", "pass"))
.withMessage("Database must not be null");
.withMessage("'database' must not be null");
}
private RunningService mockService(int mappedPort) {