Home / Class/ MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests Class — spring-boot Architecture

MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests.java lines 45–161

class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {

	private final MockEnvironment environment = new MockEnvironment();

	@Test
	void analyzeWhenEnvironmentIsNullShouldReturnNull() {
		MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
		FailureAnalysis failureAnalysis = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(null)
			.analyze(failure);
		assertThat(failureAnalysis).isNull();
	}

	@Test
	void analyzeWhenNotAllPropertiesAreInTheEnvironmentShouldReturnNull() {
		MapPropertySource source = new MapPropertySource("test", Collections.singletonMap("com.example.a", "alpha"));
		this.environment.getPropertySources().addFirst(OriginCapablePropertySource.get(source));
		MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
		FailureAnalysis analysis = performAnalysis(failure);
		assertThat(analysis).isNull();
	}

	@Test
	void analyzeWhenAllConfiguredPropertiesAreInTheEnvironmentShouldReturnAnalysis() {
		Map<String, Object> properties = new HashMap<>();
		properties.put("com.example.a", "alpha");
		properties.put("com.example.b", "bravo");
		MapPropertySource source = new MapPropertySource("test", properties);
		this.environment.getPropertySources().addFirst(OriginCapablePropertySource.get(source));
		MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
		FailureAnalysis analysis = performAnalysis(failure);
		assertThat(analysis).isNotNull();
		assertThat(analysis.getAction()).isEqualTo(
				"Update your configuration so that only one of the mutually exclusive properties is configured.");
		assertThat(analysis.getDescription()).contains(String.format(
				"The following configuration properties are mutually exclusive:%n%n\tcom.example.a%n\tcom.example.b%n"))
			.contains(
					String.format("However, more than one of those properties has been configured at the same time:%n%n"
							+ "\tcom.example.a (originating from 'TestOrigin test')%n"
							+ "\tcom.example.b (originating from 'TestOrigin test')%n"));
	}

	@Test
	void analyzeWhenPropertyIsInMultiplePropertySourcesShouldListEachSourceInAnalysis() {
		Map<String, Object> properties = new LinkedHashMap<>();
		properties.put("com.example.a", "alpha");
		properties.put("com.example.b", "bravo");
		this.environment.getPropertySources()
			.addFirst(OriginCapablePropertySource.get(new MapPropertySource("test-one", properties)));
		this.environment.getPropertySources()
			.addLast(OriginCapablePropertySource.get(new MapPropertySource("test-two", properties)));
		MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
				new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
		FailureAnalysis analysis = performAnalysis(failure);
		assertThat(analysis).isNotNull();
		assertThat(analysis.getAction()).isEqualTo(
				"Update your configuration so that only one of the mutually exclusive properties is configured.");
		assertThat(analysis.getDescription()).contains(String.format(
				"The following configuration properties are mutually exclusive:%n%n\tcom.example.a%n\tcom.example.b%n"))
			.contains(
					String.format("However, more than one of those properties has been configured at the same time:%n%n"
							+ "\tcom.example.a (originating from 'TestOrigin test-one')%n"
							+ "\tcom.example.a (originating from 'TestOrigin test-two')%n"
							+ "\tcom.example.b (originating from 'TestOrigin test-one')%n"
							+ "\tcom.example.b (originating from 'TestOrigin test-two')%n"));
	}

	private @Nullable FailureAnalysis performAnalysis(MutuallyExclusiveConfigurationPropertiesException failure) {
		MutuallyExclusiveConfigurationPropertiesFailureAnalyzer analyzer = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(
				this.environment);
		return analyzer.analyze(failure);
	}

	static class OriginCapablePropertySource<T> extends EnumerablePropertySource<T> implements OriginLookup<String> {

		private final EnumerablePropertySource<T> propertySource;

		OriginCapablePropertySource(EnumerablePropertySource<T> propertySource) {
			super(propertySource.getName(), propertySource.getSource());
			this.propertySource = propertySource;
		}

		@Override
		public @Nullable Object getProperty(String name) {
			return this.propertySource.getProperty(name);
		}

		@Override
		public String[] getPropertyNames() {
			return this.propertySource.getPropertyNames();
		}

		@Override
		public Origin getOrigin(String name) {
			return new Origin() {

				@Override
				public String toString() {
					return "TestOrigin " + getName();
				}

			};
		}

		static <T> OriginCapablePropertySource<T> get(EnumerablePropertySource<T> propertySource) {
			return new OriginCapablePropertySource<>(propertySource);
		}

	}

}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free