Home / Class/ ConfigTreePropertySourceTests Class — spring-boot Architecture

ConfigTreePropertySourceTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/env/ConfigTreePropertySourceTests.java lines 49–306

class ConfigTreePropertySourceTests {

	@TempDir
	@SuppressWarnings("NullAway.Init")
	Path directory;

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenNameIsNullThrowsException() {
		assertThatIllegalArgumentException().isThrownBy(() -> new ConfigTreePropertySource(null, this.directory))
			.withMessageContaining("name must contain");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenSourceIsNullThrowsException() {
		assertThatIllegalArgumentException().isThrownBy(() -> new ConfigTreePropertySource("test", null))
			.withMessage("Property source must not be null");
	}

	@Test
	void createWhenSourceDoesNotExistThrowsException() {
		Path missing = this.directory.resolve("missing");
		assertThatIllegalArgumentException().isThrownBy(() -> new ConfigTreePropertySource("test", missing))
			.withMessage("'sourceDirectory' [" + missing + "] must exist");
	}

	@Test
	void createWhenSourceIsFileThrowsException() throws Exception {
		Path file = this.directory.resolve("file");
		FileCopyUtils.copy("test".getBytes(StandardCharsets.UTF_8), file.toFile());
		assertThatIllegalArgumentException().isThrownBy(() -> new ConfigTreePropertySource("test", file))
			.withMessage("'sourceDirectory' [" + file + "] must be a directory");
	}

	@Test
	void getPropertyNamesFromFlatReturnsPropertyNames() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		assertThat(propertySource.getPropertyNames()).containsExactly("a", "b", "c", "one");
	}

	@Test
	void getPropertyNamesFromNestedReturnsPropertyNames() throws Exception {
		ConfigTreePropertySource propertySource = getNestedPropertySource();
		assertThat(propertySource.getPropertyNames()).containsExactly("c", "fa.a", "fa.b", "fb.a", "fb.fa.a");
	}

	@Test
	void getPropertyNamesFromNestedWithSymlinkInPathReturnsPropertyNames() throws Exception {
		addNested();
		Path symlinkTempDir = Files.createSymbolicLink(this.directory.resolveSibling("symlinkTempDir"), this.directory);
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", symlinkTempDir);
		Files.delete(symlinkTempDir);
		assertThat(propertySource.getPropertyNames()).containsExactly("c", "fa.a", "fa.b", "fb.a", "fb.fa.a");
	}

	@Test
	void getPropertyNamesFromFlatWithSymlinksIgnoresHiddenFiles() throws Exception {
		ConfigTreePropertySource propertySource = getSymlinkedFlatPropertySource();
		assertThat(propertySource.getPropertyNames()).containsExactly("a", "b", "c");
	}

	@Test
	void getPropertyNamesFromNestedWithSymlinksIgnoresHiddenFiles() throws Exception {
		ConfigTreePropertySource propertySource = getSymlinkedNestedPropertySource();
		assertThat(propertySource.getPropertyNames()).containsExactly("aa", "ab", "baa", "c");
	}

	@Test
	void getPropertyNamesWhenLowercaseReturnsPropertyNames() throws Exception {
		addProperty("SpRiNg", "boot");
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.USE_LOWERCASE_NAMES);
		assertThat(propertySource.getPropertyNames()).containsExactly("spring");
	}

	@Test
	void getPropertyFromFlatReturnsFileContent() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		assertThat(propertySource.getProperty("b")).hasToString("B");
	}

	@Test
	void getPropertyFromFlatWhenMissingReturnsNull() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		assertThat(propertySource.getProperty("missing")).isNull();
	}

	@Test
	void getPropertyFromFlatWhenFileDeletedThrowsException() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		Path b = this.directory.resolve("b");
		Files.delete(b);
		assertThatIllegalStateException().isThrownBy(() -> {
			Value property = propertySource.getProperty("b");
			assertThat(property).isNotNull();
			property.toString();
		}).withMessage("The property file '" + b + "' no longer exists");
	}

	@Test
	void getOriginFromFlatReturnsOrigin() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		TextResourceOrigin origin = (TextResourceOrigin) propertySource.getOrigin("b");
		assertThat(origin).isNotNull();
		Resource resource = origin.getResource();
		assertThat(resource).isNotNull();
		assertThat(resource.getFile()).isEqualTo(this.directory.resolve("b").toFile());
		Location location = origin.getLocation();
		assertThat(location).isNotNull();
		assertThat(location.getLine()).isZero();
		assertThat(location.getColumn()).isZero();
	}

	@Test
	void getOriginFromFlatWhenMissingReturnsNull() throws Exception {
		ConfigTreePropertySource propertySource = getFlatPropertySource();
		assertThat(propertySource.getOrigin("missing")).isNull();
	}

	@Test
	void getPropertyViaEnvironmentSupportsConversion() throws Exception {
		StandardEnvironment environment = new StandardEnvironment();
		ConversionService conversionService = ApplicationConversionService.getSharedInstance();
		environment.setConversionService((ConfigurableConversionService) conversionService);
		environment.getPropertySources().addFirst(getFlatPropertySource());
		assertThat(environment.getProperty("a")).isEqualTo("A");
		assertThat(environment.getProperty("b")).isEqualTo("B");
		InputStreamSource c = environment.getProperty("c", InputStreamSource.class);
		assertThat(c).isNotNull();
		assertThat(c.getInputStream()).hasContent("C");
		assertThat(environment.getProperty("c", byte[].class)).contains('C');
		assertThat(environment.getProperty("one", Integer.class)).isOne();
	}

	@Test
	void getPropertyFromNestedReturnsFileContent() throws Exception {
		ConfigTreePropertySource propertySource = getNestedPropertySource();
		assertThat(propertySource.getProperty("fb.fa.a")).hasToString("BAA");
	}

	@Test
	void getPropertyWhenNotAlwaysReadIgnoresUpdates() throws Exception {
		ConfigTreePropertySource propertySource = getNestedPropertySource();
		Value v1 = propertySource.getProperty("fa.b");
		Value v2 = propertySource.getProperty("fa.b");
		assertThat(v1).isNotNull();
		assertThat(v1).isSameAs(v2);
		assertThat(v1).hasToString("AB");
		assertThat(FileCopyUtils.copyToByteArray(v1.getInputStream())).containsExactly('A', 'B');
		addProperty("fa/b", "XX");
		assertThat(v1).hasToString("AB");
		assertThat(FileCopyUtils.copyToByteArray(v1.getInputStream())).containsExactly('A', 'B');
	}

	@Test
	void getPropertyWhenAlwaysReadReflectsUpdates() throws Exception {
		addNested();
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.ALWAYS_READ);
		Value v1 = propertySource.getProperty("fa.b");
		Value v2 = propertySource.getProperty("fa.b");
		assertThat(v1).isNotNull();
		assertThat(v1).isNotSameAs(v2);
		assertThat(v1).hasToString("AB");
		assertThat(FileCopyUtils.copyToByteArray(v1.getInputStream())).containsExactly('A', 'B');
		addProperty("fa/b", "XX");
		assertThat(v1).hasToString("XX");
		assertThat(FileCopyUtils.copyToByteArray(v1.getInputStream())).containsExactly('X', 'X');
		assertThat(propertySource.getProperty("fa.b")).hasToString("XX");
	}

	@Test
	void getPropertyWhenLowercaseReturnsValue() throws Exception {
		addProperty("SpRiNg", "boot");
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.USE_LOWERCASE_NAMES);
		assertThat(propertySource.getProperty("spring")).hasToString("boot");
	}

	@Test
	void getPropertyAsStringWhenMultiLinePropertyReturnsNonTrimmed() throws Exception {
		addProperty("a", "a\nb\n");
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.AUTO_TRIM_TRAILING_NEW_LINE);
		assertThat(propertySource.getProperty("a")).hasToString("a\nb\n");
	}

	@Test
	void getPropertyAsStringWhenPropertyEndsWithNewLineReturnsTrimmed() throws Exception {
		addProperty("a", "a\n");
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.AUTO_TRIM_TRAILING_NEW_LINE);
		assertThat(propertySource.getProperty("a")).hasToString("a");
	}

	@Test
	void getPropertyAsStringWhenPropertyEndsWithWindowsNewLineReturnsTrimmed() throws Exception {
		addProperty("a", "a\r\n");
		ConfigTreePropertySource propertySource = new ConfigTreePropertySource("test", this.directory,
				Option.AUTO_TRIM_TRAILING_NEW_LINE);
		assertThat(propertySource.getProperty("a")).hasToString("a");
	}

	private ConfigTreePropertySource getFlatPropertySource() throws IOException {
		addProperty("a", "A");
		addProperty("b", "B");
		addProperty("c", "C");
		addProperty("one", "1");
		return new ConfigTreePropertySource("test", this.directory);
	}

	private ConfigTreePropertySource getSymlinkedFlatPropertySource() throws IOException {
		addProperty("..hidden-a", "A");
		addProperty("..hidden-b", "B");
		addProperty("..hidden-c", "C");
		createSymbolicLink("a", "..hidden-a");
		createSymbolicLink("b", "..hidden-b");
		createSymbolicLink("c", "..hidden-c");
		return new ConfigTreePropertySource("test", this.directory);
	}

	private ConfigTreePropertySource getNestedPropertySource() throws IOException {
		addNested();
		return new ConfigTreePropertySource("test", this.directory);
	}

	private void addNested() throws IOException {
		addProperty("fa/a", "AA");
		addProperty("fa/b", "AB");
		addProperty("fb/a", "BA");
		addProperty("fb/fa/a", "BAA");
		addProperty("c", "C");
	}

	private ConfigTreePropertySource getSymlinkedNestedPropertySource() throws IOException {
		addProperty("..hidden-a/a", "AA");
		addProperty("..hidden-a/b", "AB");
		addProperty("..hidden-b/fa/a", "BAA");
		addProperty("c", "C");
		createSymbolicLink("aa", "..hidden-a/a");
		createSymbolicLink("ab", "..hidden-a/b");
		createSymbolicLink("baa", "..hidden-b/fa/a");
		return new ConfigTreePropertySource("test", this.directory);
	}

	private void addProperty(String path, String value) throws IOException {
		File file = this.directory.resolve(path).toFile();
		file.getParentFile().mkdirs();
		FileCopyUtils.copy(value.getBytes(StandardCharsets.UTF_8), file);
	}

	private void createSymbolicLink(String link, String target) throws IOException {
		Files.createSymbolicLink(this.directory.resolve(link).toAbsolutePath(),
				this.directory.resolve(target).toAbsolutePath());
	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free