Home / Class/ AotTests Class — spring-boot Architecture

AotTests Class — spring-boot Architecture

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

Entity Profile

Relationship Graph

Source Code

core/spring-boot/src/test/java/org/springframework/boot/support/EnvironmentPostProcessorApplicationListenerTests.java lines 188–390

	@Nested
	class AotTests {

		private static final ClassName TEST_APP = ClassName.get("com.example", "TestApp");

		@Test
		void aotContributionIsNotNecessaryWithDefaultConfiguration() {
			assertThat(getContribution(new StandardEnvironment())).isNull();
		}

		@Test
		void aotContributionIsNotNecessaryWithDefaultProfileActive() {
			StandardEnvironment environment = new StandardEnvironment();
			environment.setDefaultProfiles("fallback");
			environment.setActiveProfiles("fallback");
			assertThat(getContribution(environment)).isNull();
		}

		@Test
		void aotContributionRegistersActiveProfiles() {
			ConfigurableEnvironment environment = new StandardEnvironment();
			environment.setActiveProfiles("one", "two");
			compile(createContext(environment), (compiled) -> {
				EnvironmentPostProcessor environmentPostProcessor = compiled.getInstance(EnvironmentPostProcessor.class,
						ClassName.get("com.example", "TestApp__EnvironmentPostProcessor").toString());
				StandardEnvironment freshEnvironment = new StandardEnvironment();
				environmentPostProcessor.postProcessEnvironment(freshEnvironment, new SpringApplication());
				assertThat(freshEnvironment.getActiveProfiles()).containsExactly("one", "two");
			});
		}

		@Test
		void aotContributionRegistersReflectionHints() {
			GenericApplicationContext applicationContext = new GenericApplicationContext();
			ConfigurableEnvironment environment = new StandardEnvironment();
			environment.setActiveProfiles("one", "two");
			applicationContext.getBeanFactory().registerSingleton("environment", environment);
			BeanFactoryInitializationAotContribution aotContribution = new EnvironmentBeanFactoryInitializationAotProcessor()
				.processAheadOfTime(applicationContext.getBeanFactory());
			assertThat(aotContribution).isNotNull();
			GenerationContext generationContext = new TestGenerationContext();
			aotContribution.applyTo(generationContext, mock(BeanFactoryInitializationCode.class));
			assertThat(RuntimeHintsPredicates.reflection()
				.onType(TypeReference.of(TestGenerationContext.TEST_TARGET + "__"
						+ EnvironmentPostProcessorApplicationListener.AOT_FEATURE_NAME))
				.withMemberCategory(MemberCategory.INVOKE_PUBLIC_CONSTRUCTORS))
				.accepts(generationContext.getRuntimeHints());
		}

		@Test
		void shouldUseAotEnvironmentPostProcessor() {
			SpringApplication application = new SpringApplication(ExampleAotProcessedApp.class);
			application.setWebApplicationType(WebApplicationType.NONE);
			application.setMainApplicationClass(ExampleAotProcessedApp.class);
			System.setProperty(AotDetector.AOT_ENABLED, "true");
			try {
				ApplicationContext context = application.run();
				assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("one", "three");
				assertThat(context.getBean("test")).isEqualTo("test");
			}
			finally {
				System.clearProperty(AotDetector.AOT_ENABLED);
			}
		}

		@Test
		void aotEnvironmentPostProcessorShouldBeAppliedFirst(@TempDir Path tempDir) {
			Properties properties = new Properties();
			properties.put(EnvironmentPostProcessor.class.getName(), TestEnvironmentPostProcessor.class.getName());
			ClassLoader classLoader = createClassLoaderWithAdditionalSpringFactories(tempDir, properties);
			DefaultResourceLoader resourceLoader = new DefaultResourceLoader(classLoader);

			SpringApplication application = new SpringApplication(ExampleAotProcessedApp.class);
			application.setResourceLoader(resourceLoader);
			application.setWebApplicationType(WebApplicationType.NONE);
			application.setMainApplicationClass(ExampleAotProcessedApp.class);
			System.setProperty(AotDetector.AOT_ENABLED, "true");
			try {
				ApplicationContext context = application.run();
				// See TestEnvironmentPostProcessor
				assertThat(context.getEnvironment().getProperty("test.activeProfiles")).isEqualTo("one,three");
				assertThat(context.getEnvironment().getActiveProfiles()).containsExactly("one", "three");
				assertThat(context.getBean("test")).isEqualTo("test");
			}
			finally {
				System.clearProperty(AotDetector.AOT_ENABLED);
			}
		}

		@Test
		void shouldBeLenientIfAotEnvironmentPostProcessorDoesNotExist() {
			SpringApplication application = new SpringApplication(ExampleAotProcessedNoProfileApp.class);
			application.setWebApplicationType(WebApplicationType.NONE);
			application.setMainApplicationClass(ExampleAotProcessedNoProfileApp.class);
			System.setProperty(AotDetector.AOT_ENABLED, "true");
			try {
				ApplicationContext context = application.run();
				assertThat(context.getEnvironment().getActiveProfiles()).isEmpty();
				assertThat(context.getBean("test")).isEqualTo("test");
			}
			finally {
				System.clearProperty(AotDetector.AOT_ENABLED);
			}
		}

		private @Nullable BeanFactoryInitializationAotContribution getContribution(
				ConfigurableEnvironment environment) {
			DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
			beanFactory.registerSingleton(ConfigurableApplicationContext.ENVIRONMENT_BEAN_NAME, environment);
			return new EnvironmentBeanFactoryInitializationAotProcessor().processAheadOfTime(beanFactory);
		}

		private GenericApplicationContext createContext(ConfigurableEnvironment environment) {
			GenericApplicationContext context = new GenericApplicationContext();
			context.setEnvironment(environment);
			return context;
		}

		private void compile(GenericApplicationContext context, Consumer<Compiled> compiled) {
			TestGenerationContext generationContext = new TestGenerationContext(TEST_APP);
			new ApplicationContextAotGenerator().processAheadOfTime(context, generationContext);
			generationContext.writeGeneratedContent();
			TestCompiler.forSystem()
				.withCompilerOptions("-Xlint:deprecation,removal", "-Werror")
				.with(generationContext)
				.compile(compiled);
		}

		private ClassLoader createClassLoaderWithAdditionalSpringFactories(Path tempDir, Properties properties) {
			return new ClassLoader() {
				@Override
				public Enumeration<URL> getResources(String name) throws IOException {
					Enumeration<URL> resources = super.getResources(name);
					if (SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION.equals(name)) {
						Path springFactories = tempDir.resolve("spring.factories");
						try (BufferedWriter writer = Files.newBufferedWriter(springFactories)) {
							properties.store(writer, "");
						}
						List<URL> allResources = new ArrayList<>();
						allResources.add(springFactories.toUri().toURL());
						allResources.addAll(Collections.list(resources));
						return Collections.enumeration(allResources);
					}
					return resources;
				}
			};
		}

		static class TestEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {

			@Override
			public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
				MockPropertySource propertySource = new MockPropertySource().withProperty("test.activeProfiles",
						StringUtils.arrayToCommaDelimitedString(environment.getActiveProfiles()));
				environment.getPropertySources().addLast(propertySource);
			}

			@Override
			public int getOrder() {
				return Ordered.HIGHEST_PRECEDENCE;
			}

		}

		static class ExampleAotProcessedApp {

		}

		static class ExampleAotProcessedApp__ApplicationContextInitializer
				implements ApplicationContextInitializer<ConfigurableApplicationContext> {

			@Override
			public void initialize(ConfigurableApplicationContext applicationContext) {
				applicationContext.getBeanFactory().registerSingleton("test", "test");
			}

		}

		static class ExampleAotProcessedApp__EnvironmentPostProcessor implements EnvironmentPostProcessor {

			@Override
			public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
				environment.addActiveProfile("one");
				environment.addActiveProfile("three");
			}

		}

		static class ExampleAotProcessedNoProfileApp {

		}

		static class ExampleAotProcessedNoProfileApp__ApplicationContextInitializer
				implements ApplicationContextInitializer<ConfigurableApplicationContext> {

			@Override
			public void initialize(ConfigurableApplicationContext applicationContext) {
				applicationContext.getBeanFactory().registerSingleton("test", "test");
			}

		}

	}

Domain

Analyze Your Own Codebase

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

Try Supermodel Free