Home / Class/ TemplateAvailabilityProvidersTests Class — spring-boot Architecture

TemplateAvailabilityProvidersTests Class — spring-boot Architecture

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

Entity Profile

Source Code

core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/template/TemplateAvailabilityProvidersTests.java lines 50–230

@ExtendWith(MockitoExtension.class)
class TemplateAvailabilityProvidersTests {

	private TemplateAvailabilityProviders providers;

	@Mock
	@SuppressWarnings("NullAway.Init")
	private TemplateAvailabilityProvider provider;

	private final String view = "view";

	private final ClassLoader classLoader = getClass().getClassLoader();

	private final MockEnvironment environment = new MockEnvironment();

	@Mock
	@SuppressWarnings("NullAway.Init")
	private ResourceLoader resourceLoader;

	@BeforeEach
	void setup() {
		this.providers = new TemplateAvailabilityProviders(Collections.singleton(this.provider));
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenApplicationContextIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> new TemplateAvailabilityProviders((ApplicationContext) null))
			.withMessageContaining("'applicationContext' must not be null");
	}

	@Test
	@SuppressWarnings("rawtypes")
	@WithTestTemplateAvailabilityProvider
	void createWhenUsingApplicationContextShouldLoadProviders() {
		ApplicationContext applicationContext = mock(ApplicationContext.class);
		given(applicationContext.getClassLoader()).willReturn(Thread.currentThread().getContextClassLoader());
		TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(applicationContext);
		assertThat(providers.getProviders()).extracting((provider) -> (Class) provider.getClass())
			.containsExactly(TestTemplateAvailabilityProvider.class);
		then(applicationContext).should().getClassLoader();
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenClassLoaderIsNullShouldThrowException() {
		assertThatIllegalArgumentException().isThrownBy(() -> new TemplateAvailabilityProviders((ClassLoader) null))
			.withMessageContaining("'classLoader' must not be null");
	}

	@Test
	@WithTestTemplateAvailabilityProvider
	void createWhenUsingClassLoaderShouldLoadProviders() {
		TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(
				Thread.currentThread().getContextClassLoader());
		assertThat(providers.getProviders()).isNotEmpty();
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void createWhenProvidersIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> new TemplateAvailabilityProviders((Collection<TemplateAvailabilityProvider>) null))
			.withMessageContaining("'providers' must not be null");
	}

	@Test
	void createWhenUsingProvidersShouldUseProviders() {
		TemplateAvailabilityProviders providers = new TemplateAvailabilityProviders(
				Collections.singleton(this.provider));
		assertThat(providers.getProviders()).containsOnly(this.provider);
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void getProviderWhenApplicationContextIsNullShouldThrowException() {
		assertThatIllegalArgumentException().isThrownBy(() -> this.providers.getProvider(this.view, null))
			.withMessageContaining("'applicationContext' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void getProviderWhenViewIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> this.providers.getProvider(null, this.environment, this.classLoader, this.resourceLoader))
			.withMessageContaining("'view' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void getProviderWhenEnvironmentIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> this.providers.getProvider(this.view, null, this.classLoader, this.resourceLoader))
			.withMessageContaining("'environment' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void getProviderWhenClassLoaderIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> this.providers.getProvider(this.view, this.environment, null, this.resourceLoader))
			.withMessageContaining("'classLoader' must not be null");
	}

	@Test
	@SuppressWarnings("NullAway") // Test null check
	void getProviderWhenResourceLoaderIsNullShouldThrowException() {
		assertThatIllegalArgumentException()
			.isThrownBy(() -> this.providers.getProvider(this.view, this.environment, this.classLoader, null))
			.withMessageContaining("'resourceLoader' must not be null");
	}

	@Test
	void getProviderWhenNoneMatchShouldReturnNull() {
		TemplateAvailabilityProvider found = this.providers.getProvider(this.view, this.environment, this.classLoader,
				this.resourceLoader);
		assertThat(found).isNull();
		then(this.provider).should()
			.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
	}

	@Test
	void getProviderWhenMatchShouldReturnProvider() {
		given(this.provider.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader))
			.willReturn(true);
		TemplateAvailabilityProvider found = this.providers.getProvider(this.view, this.environment, this.classLoader,
				this.resourceLoader);
		assertThat(found).isSameAs(this.provider);

	}

	@Test
	void getProviderShouldCacheMatchResult() {
		given(this.provider.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader))
			.willReturn(true);
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		then(this.provider).should()
			.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
	}

	@Test
	void getProviderShouldCacheNoMatchResult() {
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		then(this.provider).should()
			.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
	}

	@Test
	void getProviderWhenCacheDisabledShouldNotUseCache() {
		given(this.provider.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader))
			.willReturn(true);
		this.environment.setProperty("spring.template.provider.cache", "false");
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		this.providers.getProvider(this.view, this.environment, this.classLoader, this.resourceLoader);
		then(this.provider).should(times(2))
			.isTemplateAvailable(this.view, this.environment, this.classLoader, this.resourceLoader);
	}

	@Target(ElementType.METHOD)
	@Retention(RetentionPolicy.RUNTIME)
	@WithResource(name = "META-INF/spring.factories",
			content = "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvider="
					+ "org.springframework.boot.autoconfigure.template.TemplateAvailabilityProvidersTests$TestTemplateAvailabilityProvider")
	@interface WithTestTemplateAvailabilityProvider {

	}

	static class TestTemplateAvailabilityProvider implements TemplateAvailabilityProvider {

		@Override
		public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
				ResourceLoader resourceLoader) {
			return false;
		}

	}

}

Analyze Your Own Codebase

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

Try Supermodel Free