Home / Class/ LocationResourceLoaderTests Class — spring-boot Architecture

LocationResourceLoaderTests Class — spring-boot Architecture

Architecture documentation for the LocationResourceLoaderTests class in LocationResourceLoaderTests.java from the spring-boot codebase.

Entity Profile

Source Code

core/spring-boot/src/test/java/org/springframework/boot/context/config/LocationResourceLoaderTests.java lines 38–151

class LocationResourceLoaderTests {

	private final LocationResourceLoader loader = new LocationResourceLoader(new DefaultResourceLoader());

	@TempDir
	@SuppressWarnings("NullAway.Init")
	File temp;

	@Test
	void isPatternWhenHasAsteriskReturnsTrue() {
		assertThat(this.loader.isPattern("spring/*/boot")).isTrue();
	}

	@Test
	void isPatternWhenNoAsteriskReturnsFalse() {
		assertThat(this.loader.isPattern("spring/boot")).isFalse();
	}

	@Test
	void getResourceWhenPatternThrowsException() {
		assertThatIllegalStateException().isThrownBy(() -> this.loader.getResource("spring/boot/*"))
			.withMessage("Location 'spring/boot/*' must not be a pattern");
	}

	@Test
	void getResourceReturnsResource() throws Exception {
		File file = new File(this.temp, "file");
		FileCopyUtils.copy("test".getBytes(), file);
		Resource resource = this.loader.getResource(file.toURI().toString());
		assertThat(resource.getInputStream()).hasContent("test");
	}

	@Test
	void getResourceWhenNotUrlReturnsResource() throws Exception {
		File file = new File(this.temp, "file");
		FileCopyUtils.copy("test".getBytes(), file);
		Resource resource = this.loader.getResource(file.getAbsolutePath());
		assertThat(resource.getInputStream()).hasContent("test");
	}

	@Test
	void getResourceWhenNonCleanPathReturnsResource() throws Exception {
		File file = new File(this.temp, "file");
		FileCopyUtils.copy("test".getBytes(), file);
		Resource resource = this.loader.getResource(this.temp.getAbsolutePath() + "/spring/../file");
		assertThat(resource.getInputStream()).hasContent("test");
	}

	@Test
	void getResourcesWhenNotPatternThrowsException() {
		assertThatIllegalStateException().isThrownBy(() -> this.loader.getResources("spring/boot", ResourceType.FILE))
			.withMessage("Location 'spring/boot' must be a pattern");
	}

	@Test
	void getResourcesWhenLocationStartsWithClasspathWildcardThrowsException() {
		assertThatIllegalStateException()
			.isThrownBy(() -> this.loader.getResources("classpath*:spring/boot/*/", ResourceType.FILE))
			.withMessage("Location 'classpath*:spring/boot/*/' cannot use classpath wildcards");
	}

	@Test
	void getResourcesWhenLocationContainsMultipleWildcardsThrowsException() {
		assertThatIllegalStateException()
			.isThrownBy(() -> this.loader.getResources("spring/*/boot/*/", ResourceType.FILE))
			.withMessage("Location 'spring/*/boot/*/' cannot contain multiple wildcards");
	}

	@Test
	void getResourcesWhenPatternDoesNotEndWithAsteriskSlashThrowsException() {
		assertThatIllegalStateException().isThrownBy(() -> this.loader.getResources("spring/boot/*", ResourceType.FILE))
			.withMessage("Location 'spring/boot/*' must end with '*/'");
	}

	@Test
	void getFileResourceReturnsResources() throws Exception {
		createTree();
		Resource[] resources = this.loader.getResources(this.temp.getAbsolutePath() + "/*/file", ResourceType.FILE);
		assertThat(resources).hasSize(2);
		assertThat(resources[0].getInputStream()).hasContent("a");
		assertThat(resources[1].getInputStream()).hasContent("b");
	}

	@Test
	void getDirectoryResourceReturnsResources() throws Exception {
		createTree();
		Resource[] resources = this.loader.getResources(this.temp.getAbsolutePath() + "/*/", ResourceType.DIRECTORY);
		assertThat(resources).hasSize(2);
		assertThat(resources[0].getFilename()).isEqualTo("a");
		assertThat(resources[1].getFilename()).isEqualTo("b");
	}

	@Test
	void getResourcesWhenHasHiddenDirectoriesFiltersResults() throws IOException {
		createTree();
		File hiddenDirectory = new File(this.temp, "..a");
		hiddenDirectory.mkdirs();
		FileCopyUtils.copy("h".getBytes(), new File(hiddenDirectory, "file"));
		Resource[] resources = this.loader.getResources(this.temp.getAbsolutePath() + "/*/file", ResourceType.FILE);
		assertThat(resources).hasSize(2);
		assertThat(resources[0].getInputStream()).hasContent("a");
		assertThat(resources[1].getInputStream()).hasContent("b");
	}

	private void createTree() throws IOException {
		File directoryA = new File(this.temp, "a");
		File directoryB = new File(this.temp, "b");
		directoryA.mkdirs();
		directoryB.mkdirs();
		FileCopyUtils.copy("a".getBytes(), new File(directoryA, "file"));
		FileCopyUtils.copy("b".getBytes(), new File(directoryB, "file"));
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free