Add support for working with resources in tests

Closes gh-44444
This commit is contained in:
Andy Wilkinson
2025-02-24 10:22:58 +00:00
parent abf320d273
commit 3acea583ad
25 changed files with 1397 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithPackageResources} on a class.
*
* @author Andy Wilkinson
*/
@WithPackageResources({ "resource-1.txt", "resource-2.txt", "sub/resource-3.txt" })
class OnClassWithPackageResourcesTests {
@Test
void whenWithPackageResourcesIsUsedOnAClassThenResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("resource-1.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("one");
assertThat(new ClassPathResource("resource-2.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("two");
assertThat(new ClassPathResource("sub/resource-3.txt").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("three");
}
}

View File

@@ -0,0 +1,89 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithResource} when used on a class.
*
* @author Andy Wilkinson
*/
@WithResource(name = "on-class", content = "class content")
class OnClassWithResourceTests {
@Test
void whenWithResourceIsUsedOnAClassThenResourceIsAvailable() throws IOException {
assertThat(new ClassPathResource("on-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("class content");
}
@Test
@WithResource(name = "method-resource", content = "method")
void whenWithResourceIsUsedOnClassAndMethodThenBothResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("on-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("class content");
assertThat(new ClassPathResource("method-resource").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method");
}
@Test
@WithResource(name = "method-resource-1", content = "method-1")
@WithResource(name = "method-resource-2", content = "method-2")
void whenWithResourceIsUsedOnClassAndRepeatedOnMethodThenAllResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("on-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("class content");
assertThat(new ClassPathResource("method-resource-1").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method-1");
assertThat(new ClassPathResource("method-resource-2").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method-2");
}
@Nested
class NestedTests {
@Test
void whenWithResourceIsUsedOnEnclosingClassThenResourceIsAvailable() throws IOException {
assertThat(new ClassPathResource("on-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("class content");
}
}
@Nested
@WithResource(name = "on-nested-class", content = "nested class content")
class WithResourceNestedTests {
@Test
void whenWithResourceIsUsedOnEnclosingClassAndClassThenBothResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("on-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("class content");
assertThat(new ClassPathResource("on-nested-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("nested class content");
}
}
}

View File

@@ -0,0 +1,43 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithPackageResources} when used on a super-class.
*
* @author Andy Wilkinson
*/
class OnSuperClassWithPackageResourcesTests extends WithPackageResourcesClass {
@Test
void whenWithPackageResourcesIsUsedOnASuperClassThenResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("resource-1.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("one");
assertThat(new ClassPathResource("resource-2.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("two");
assertThat(new ClassPathResource("sub/resource-3.txt").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("three");
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithResource} when used on a super-class.
*
* @author Andy Wilkinson
*/
class OnSuperClassWithResourceTests extends WithResourceClass {
@Test
void whenWithResourceIsUsedOnASuperClassThenResourceIsAvailable() throws IOException {
assertThat(new ClassPathResource("on-super-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("super-class content");
}
@Test
@WithResource(name = "method-resource", content = "method")
void whenWithResourceIsUsedOnASuperClassAndMethodThenBothResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("on-super-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("super-class content");
assertThat(new ClassPathResource("method-resource").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method");
}
@Test
@WithResource(name = "method-resource-1", content = "method-1")
@WithResource(name = "method-resource-2", content = "method-2")
void whenWithResourceIsUsedOnASuperClassAndRepeatedOnMethodThenAllResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("on-super-class").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("super-class content");
assertThat(new ClassPathResource("method-resource-1").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method-1");
assertThat(new ClassPathResource("method-resource-2").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("method-2");
}
}

View File

@@ -0,0 +1,132 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
/**
* Tests for {@link Resources}.
*
* @author Andy Wilkinson
*/
class ResourcesTests {
@TempDir
private Path root;
@Test
void whenAddResourceThenResourceIsCreated() {
new Resources(this.root).addResource("test", "test-content");
assertThat(this.root.resolve("test")).hasContent("test-content");
}
@Test
void whenAddResourceHasContentReferencingResourceRootThenResourceIsCreatedWithReferenceToRoot() {
new Resources(this.root).addResource("test", "*** ${resourceRoot} ***");
assertThat(this.root.resolve("test")).hasContent("*** " + this.root + " ***");
}
@Test
void whenAddResourceWithPathThenResourceIsCreated() {
new Resources(this.root).addResource("a/b/c/test", "test-content");
assertThat(this.root.resolve("a/b/c/test")).hasContent("test-content");
}
@Test
void whenAddResourceAndResourceAlreadyExistsThenResourcesIsOverwritten() {
Resources resources = new Resources(this.root);
resources.addResource("a/b/c/test", "original-content");
resources.addResource("a/b/c/test", "new-content");
assertThat(this.root.resolve("a/b/c/test")).hasContent("new-content");
}
@Test
void whenAddPackageThenNamedResourcesFromPackageAreCreated() {
new Resources(this.root).addPackage(getClass().getPackage(),
new String[] { "resource-1.txt", "sub/resource-3.txt" });
assertThat(this.root.resolve("resource-1.txt")).hasContent("one");
assertThat(this.root.resolve("resource-2.txt")).doesNotExist();
assertThat(this.root.resolve("sub/resource-3.txt")).hasContent("three");
}
@Test
void whenAddResourceAndDeleteThenResourceDoesNotExist() {
Resources resources = new Resources(this.root);
resources.addResource("test", "test-content");
assertThat(this.root.resolve("test")).hasContent("test-content");
resources.delete();
assertThat(this.root.resolve("test")).doesNotExist();
}
@Test
void whenAddPackageAndDeleteThenResourcesDoNotExist() {
Resources resources = new Resources(this.root);
resources.addPackage(getClass().getPackage(),
new String[] { "resource-1.txt", "resource-2.txt", "sub/resource-3.txt" });
assertThat(this.root.resolve("resource-1.txt")).hasContent("one");
assertThat(this.root.resolve("resource-2.txt")).hasContent("two");
assertThat(this.root.resolve("sub/resource-3.txt")).hasContent("three");
resources.delete();
assertThat(this.root.resolve("resource-1.txt")).doesNotExist();
assertThat(this.root.resolve("resource-2.txt")).doesNotExist();
assertThat(this.root.resolve("sub/resource-3.txt")).doesNotExist();
assertThat(this.root.resolve("sub")).doesNotExist();
}
@Test
void whenAddDirectoryThenDirectoryIsCreated() {
Resources resources = new Resources(this.root);
resources.addDirectory("dir");
assertThat(this.root.resolve("dir")).isDirectory();
}
@Test
void whenAddDirectoryWithPathThenDirectoryIsCreated() {
Resources resources = new Resources(this.root);
resources.addDirectory("one/two/three/dir");
assertThat(this.root.resolve("one/two/three/dir")).isDirectory();
}
@Test
void whenAddDirectoryAndDirectoryAlreadyExistsThenDoesNotThrow() {
Resources resources = new Resources(this.root);
resources.addDirectory("one/two/three/dir");
resources.addDirectory("one/two/three/dir");
assertThat(this.root.resolve("one/two/three/dir")).isDirectory();
}
@Test
void whenAddDirectoryAndResourceAlreadyExistsThenIllegalStateExceptionIsThrown() {
Resources resources = new Resources(this.root);
resources.addResource("one/two/three/", "content");
assertThatIllegalStateException().isThrownBy(() -> resources.addDirectory("one/two/three"));
}
@Test
void whenAddResourceAndDirectoryAlreadyExistsThenIllegalStateExceptionIsThrown() {
Resources resources = new Resources(this.root);
resources.addDirectory("one/two/three");
assertThatIllegalStateException().isThrownBy(() -> resources.addResource("one/two/three", "content"));
}
}

View File

@@ -0,0 +1,27 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
/**
* Class to test the use of {@link WithPackageResources} on a super-class.
*
* @author Andy Wilkinson
*/
@WithPackageResources({ "resource-1.txt", "resource-2.txt", "sub/resource-3.txt" })
class WithPackageResourcesClass {
}

View File

@@ -0,0 +1,53 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithPackageResources}.
*
* @author Andy Wilkinson
*/
class WithPackageResourcesTests {
@Test
@WithPackageResources({ "resource-1.txt", "resource-2.txt", "sub/resource-3.txt" })
void whenWithPackageResourcesIsUsedOnAMethodThenResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("resource-1.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("one");
assertThat(new ClassPathResource("resource-2.txt").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("two");
assertThat(new ClassPathResource("sub/resource-3.txt").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("three");
}
@Test
@WithPackageResources("sub/resource-3.txt")
void whenWithPackageResourcesOnlyIncludesSomeResourcesThenOnlyIncludedResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("resource-1.txt").exists()).isFalse();
assertThat(new ClassPathResource("resource-2.txt").exists()).isFalse();
assertThat(new ClassPathResource("sub/resource-3.txt").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("three");
}
}

View File

@@ -0,0 +1,27 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
/**
* Class to test the use of {@link WithResource} on a super-class
*
* @author Andy Wilkinson
*/
@WithResource(name = "on-super-class", content = "super-class content")
class WithResourceClass {
}

View File

@@ -0,0 +1,56 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithResourceDirectory}.
*
* @author Andy Wilkinson
*/
class WithResourceDirectoryTests {
@Test
@WithResourceDirectory("test")
void whenWithResourceDirectoryIsUsedOnAMethodThenDirectoryIsCreated() throws IOException {
assertThat(new ClassPathResource("test").getFile()).isDirectory();
}
@Test
@WithResourceDirectory("com/example/nested")
void whenWithResourceDirectoryNamesANestedDirectoryThenDirectoryIsCreated() throws IOException {
assertThat(new ClassPathResource("com/example/nested").getFile()).isDirectory();
}
@Test
@WithResourceDirectory("1")
@WithResourceDirectory("2")
@WithResourceDirectory("3")
void whenWithResourceDirectoryIsRepeatedOnAMethodThenAllResourceDirectoriesAreCreated() throws IOException {
assertThat(new ClassPathResource("1").getFile()).isDirectory();
assertThat(new ClassPathResource("2").getFile()).isDirectory();
assertThat(new ClassPathResource("3").getFile()).isDirectory();
}
}

View File

@@ -0,0 +1,100 @@
/*
* 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.
* You may obtain a copy of the License at
*
* https://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.testsupport.classpath.resources;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link WithResource}.
*
* @author Andy Wilkinson
*/
class WithResourceTests {
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailable() throws IOException {
assertThat(new ClassPathResource("test").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailableFromFileResourcesRoot(@ResourcesRoot File root) {
assertThat(new File(root, "test")).hasContent("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailableFromPathResourcesRoot(@ResourcesRoot Path root) {
assertThat(root.resolve("test")).hasContent("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailableFromPathResourcePath(
@ResourcePath("test") Path resource) {
assertThat(resource).hasContent("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailableFromFileResourcePath(
@ResourcePath("test") File resource) {
assertThat(resource).hasContent("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceIsAvailableFromStringResourcePath(
@ResourcePath("test") String resource) {
assertThat(new File(resource)).hasContent("content");
}
@Test
@WithResource(name = "test", content = "content")
void whenWithResourceIsUsedOnAMethodThenResourceContentIsAvailableAsAString(
@ResourceContent("test") String content) {
assertThat(content).isEqualTo("content");
}
@Test
@WithResource(name = "com/example/test-resource", content = "content")
void whenWithResourceNameIncludesADirectoryThenResourceIsAvailable() throws IOException {
assertThat(new ClassPathResource("com/example/test-resource").getContentAsString(StandardCharsets.UTF_8))
.isEqualTo("content");
}
@Test
@WithResource(name = "1", content = "one")
@WithResource(name = "2", content = "two")
@WithResource(name = "3", content = "three")
void whenWithResourceIsRepeatedOnAMethodThenAllResourcesAreAvailable() throws IOException {
assertThat(new ClassPathResource("1").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("one");
assertThat(new ClassPathResource("2").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("two");
assertThat(new ClassPathResource("3").getContentAsString(StandardCharsets.UTF_8)).isEqualTo("three");
}
}