Change relevant Assert calls to throw IllegalStateException

Change certain Assert class from `assert...` to `assertState`
so that a more appropriate `IllegalStateException` is thrown.

Fixes gh-43779
This commit is contained in:
Phillip Webb
2025-01-10 23:01:17 -08:00
parent 29baaf32e6
commit f08188d5cf
76 changed files with 201 additions and 188 deletions

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.
@@ -65,7 +65,7 @@ class BuilderBuildpack implements Buildpack {
.of(unambiguous ? reference.getSubReference(PREFIX) : reference.toString());
BuildpackMetadata buildpackMetadata = findBuildpackMetadata(context, builderReference);
if (unambiguous) {
Assert.isTrue(buildpackMetadata != null, () -> "Buildpack '" + reference + "' not found in builder");
Assert.state(buildpackMetadata != null, () -> "Buildpack '" + reference + "' not found in builder");
}
return (buildpackMetadata != null) ? new BuilderBuildpack(buildpackMetadata) : null;
}

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.
@@ -158,7 +158,7 @@ class BuilderMetadata extends MappedObject {
static BuilderMetadata fromImageConfig(ImageConfig imageConfig) throws IOException {
Assert.notNull(imageConfig, "ImageConfig must not be null");
String json = imageConfig.getLabels().get(LABEL_NAME);
Assert.notNull(json, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
Assert.state(json != null, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
+ StringUtils.collectionToCommaDelimitedString(imageConfig.getLabels().keySet()) + "'");
return fromJson(json);
}

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.
@@ -77,7 +77,7 @@ final class BuildpackLayersMetadata extends MappedObject {
static BuildpackLayersMetadata fromImageConfig(ImageConfig imageConfig) throws IOException {
Assert.notNull(imageConfig, "ImageConfig must not be null");
String json = imageConfig.getLabels().get(LABEL_NAME);
Assert.notNull(json, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
Assert.state(json != null, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
+ StringUtils.collectionToCommaDelimitedString(imageConfig.getLabels().keySet()) + "'");
return fromJson(json);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@@ -94,7 +94,7 @@ final class BuildpackMetadata extends MappedObject {
static BuildpackMetadata fromImageConfig(ImageConfig imageConfig) throws IOException {
Assert.notNull(imageConfig, "ImageConfig must not be null");
String json = imageConfig.getLabels().get(LABEL_NAME);
Assert.notNull(json, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
Assert.state(json != null, () -> "No '" + LABEL_NAME + "' label found in image config labels '"
+ StringUtils.collectionToCommaDelimitedString(imageConfig.getLabels().keySet()) + "'");
return fromJson(json);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2021 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.
@@ -56,7 +56,7 @@ final class DirectoryBuildpack implements Buildpack {
private BuildpackCoordinates findBuildpackCoordinates(Path path) {
Path buildpackToml = path.resolve("buildpack.toml");
Assert.isTrue(Files.exists(buildpackToml),
Assert.state(Files.exists(buildpackToml),
() -> "Buildpack descriptor 'buildpack.toml' is required in buildpack '" + path + "'");
try {
try (InputStream inputStream = Files.newInputStream(buildpackToml)) {

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.
@@ -127,7 +127,8 @@ class ExportedImageTar implements Closeable {
entry = tar.getNextEntry();
}
Assert.state(index != null || manifest != null,
"Exported image '%s' does not contain 'index.json' or 'manifest.json'".formatted(reference));
() -> "Exported image '%s' does not contain 'index.json' or 'manifest.json'"
.formatted(reference));
return (index != null) ? new IndexLayerArchiveFactory(tarFile, index)
: new ManifestLayerArchiveFactory(tarFile, manifest);
}

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.
@@ -445,7 +445,7 @@ final class PemPrivateKeyParser {
public static final String PBES2_ALGORITHM = "PBES2";
static PKCS8EncodedKeySpec decrypt(byte[] bytes, String password) {
Assert.notNull(password, "Password is required for an encrypted private key");
Assert.state(password != null, "Password is required for an encrypted private key");
try {
EncryptedPrivateKeyInfo keyInfo = new EncryptedPrivateKeyInfo(bytes);
AlgorithmParameters algorithmParameters = keyInfo.getAlgParameters();

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.
@@ -35,6 +35,7 @@ import org.springframework.boot.buildpack.platform.docker.configuration.DockerHo
import org.springframework.boot.buildpack.platform.docker.configuration.ResolvedDockerHost;
import org.springframework.boot.buildpack.platform.docker.ssl.SslContextFactory;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* {@link HttpClientTransport} that talks to a remote Docker.
@@ -85,8 +86,8 @@ final class RemoteHttpClientTransport extends HttpClientTransport {
private static TlsSocketStrategy getTlsSocketStrategy(DockerHost host, SslContextFactory sslContextFactory) {
String directory = host.getCertificatePath();
Assert.hasText(directory,
() -> "Docker host TLS verification requires trust material location to be specified with certificate path");
Assert.state(StringUtils.hasText(directory),
"Docker host TLS verification requires trust material location to be specified with certificate path");
SSLContext sslContext = sslContextFactory.forDirectory(directory);
return new DefaultClientTlsStrategy(sslContext);
}

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.
@@ -27,7 +27,7 @@ import org.springframework.boot.buildpack.platform.docker.type.Layer;
import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -86,7 +86,7 @@ class BuilderBuildpackTests extends AbstractJsonTests {
@Test
void resolveWhenFullyQualifiedBuildpackWithVersionNotInBuilderThrowsException() {
BuildpackReference reference = BuildpackReference.of("urn:cnb:builder:example/buildpack1@1.2.3");
assertThatIllegalArgumentException().isThrownBy(() -> BuilderBuildpack.resolve(this.resolverContext, reference))
assertThatIllegalStateException().isThrownBy(() -> BuilderBuildpack.resolve(this.resolverContext, reference))
.withMessageContaining("'urn:cnb:builder:example/buildpack1@1.2.3'")
.withMessageContaining("not found in builder");
}
@@ -94,7 +94,7 @@ class BuilderBuildpackTests extends AbstractJsonTests {
@Test
void resolveWhenFullyQualifiedBuildpackWithoutVersionNotInBuilderThrowsException() {
BuildpackReference reference = BuildpackReference.of("urn:cnb:builder:example/buildpack1");
assertThatIllegalArgumentException().isThrownBy(() -> BuilderBuildpack.resolve(this.resolverContext, reference))
assertThatIllegalStateException().isThrownBy(() -> BuilderBuildpack.resolve(this.resolverContext, reference))
.withMessageContaining("'urn:cnb:builder:example/buildpack1'")
.withMessageContaining("not found in builder");
}

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.
@@ -28,6 +28,7 @@ import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -105,7 +106,7 @@ class BuilderMetadataTests extends AbstractJsonTests {
ImageConfig imageConfig = mock(ImageConfig.class);
given(image.getConfig()).willReturn(imageConfig);
given(imageConfig.getLabels()).willReturn(Collections.singletonMap("alpha", "a"));
assertThatIllegalArgumentException().isThrownBy(() -> BuilderMetadata.fromImage(image))
assertThatIllegalStateException().isThrownBy(() -> BuilderMetadata.fromImage(image))
.withMessage("No 'io.buildpacks.builder.metadata' label found in image config labels 'alpha'");
}

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.
@@ -517,7 +517,7 @@ class BuilderTests {
Builder builder = new Builder(BuildLog.to(out), docker, null);
BuildpackReference reference = BuildpackReference.of("urn:cnb:builder:example/buildpack@1.2.3");
BuildRequest request = getTestRequest().withBuildpacks(reference);
assertThatIllegalArgumentException().isThrownBy(() -> builder.build(request))
assertThatIllegalStateException().isThrownBy(() -> builder.build(request))
.withMessageContaining("'urn:cnb:builder:example/buildpack@1.2.3'")
.withMessageContaining("not found in builder");
}

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.
@@ -27,6 +27,7 @@ import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -70,7 +71,7 @@ class BuildpackLayersMetadataTests extends AbstractJsonTests {
ImageConfig imageConfig = mock(ImageConfig.class);
given(image.getConfig()).willReturn(imageConfig);
given(imageConfig.getLabels()).willReturn(Collections.singletonMap("alpha", "a"));
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackLayersMetadata.fromImage(image))
assertThatIllegalStateException().isThrownBy(() -> BuildpackLayersMetadata.fromImage(image))
.withMessage("No 'io.buildpacks.buildpack.layers' label found in image config labels 'alpha'");
}

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.
@@ -27,6 +27,7 @@ import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -64,7 +65,7 @@ class BuildpackMetadataTests extends AbstractJsonTests {
ImageConfig imageConfig = mock(ImageConfig.class);
given(image.getConfig()).willReturn(imageConfig);
given(imageConfig.getLabels()).willReturn(Collections.singletonMap("alpha", "a"));
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackMetadata.fromImage(image))
assertThatIllegalStateException().isThrownBy(() -> BuildpackMetadata.fromImage(image))
.withMessage("No 'io.buildpacks.buildpackage.metadata' label found in image config labels 'alpha'");
}

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.
@@ -37,7 +37,7 @@ import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
@@ -89,8 +89,7 @@ class DirectoryBuildpackTests {
void resolveWhenDirectoryWithoutBuildpackTomlThrowsException() throws Exception {
Files.createDirectories(this.buildpackDir.toPath());
BuildpackReference reference = BuildpackReference.of(this.buildpackDir.toString());
assertThatIllegalArgumentException()
.isThrownBy(() -> DirectoryBuildpack.resolve(this.resolverContext, reference))
assertThatIllegalStateException().isThrownBy(() -> DirectoryBuildpack.resolve(this.resolverContext, reference))
.withMessageContaining("Buildpack descriptor 'buildpack.toml' is required")
.withMessageContaining(this.buildpackDir.getAbsolutePath());
}

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,6 +43,7 @@ import org.springframework.boot.buildpack.platform.json.AbstractJsonTests;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.ArgumentMatchers.any;
@@ -156,7 +157,7 @@ class ImageBuildpackTests extends AbstractJsonTests {
BuildpackResolverContext resolverContext = mock(BuildpackResolverContext.class);
given(resolverContext.fetchImage(any(), any())).willReturn(image);
BuildpackReference reference = BuildpackReference.of("docker://example/buildpack1:latest");
assertThatIllegalArgumentException().isThrownBy(() -> ImageBuildpack.resolve(resolverContext, reference))
assertThatIllegalStateException().isThrownBy(() -> ImageBuildpack.resolve(resolverContext, reference))
.withMessageContaining("No 'io.buildpacks.buildpackage.metadata' label found");
}

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.
@@ -28,7 +28,7 @@ import org.springframework.boot.buildpack.platform.docker.configuration.Resolved
import org.springframework.boot.buildpack.platform.docker.ssl.SslContextFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -92,7 +92,7 @@ class RemoteHttpClientTransportTests {
void createIfPossibleWhenTlsVerifyWithMissingCertPathThrowsException() {
ResolvedDockerHost dockerHost = ResolvedDockerHost
.from(DockerHostConfiguration.forAddress("tcp://192.168.1.2:2376", true, null));
assertThatIllegalArgumentException().isThrownBy(() -> RemoteHttpClientTransport.createIfPossible(dockerHost))
assertThatIllegalStateException().isThrownBy(() -> RemoteHttpClientTransport.createIfPossible(dockerHost))
.withMessageContaining("Docker host TLS verification requires trust material");
}