From ebbe29cc8528e47a51a1b67bc3bc0a9cd32d6303 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 28 Jul 2017 11:05:09 +0200 Subject: [PATCH] Migrate HazelcastJpaDependencyAutoConfigurationTests to context runner This commit also makes sure to generate unique embedded data source and disable datasource initialization as this is not required by those tests. See gh-9889 --- ...stJpaDependencyAutoConfigurationTests.java | 92 +++++++++---------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastJpaDependencyAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastJpaDependencyAutoConfigurationTests.java index 609dbe9b68c..f005ed28d90 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastJpaDependencyAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/hazelcast/HazelcastJpaDependencyAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,14 +22,15 @@ import java.util.List; import java.util.Map; import com.hazelcast.core.HazelcastInstance; -import org.junit.After; import org.junit.Test; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; -import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.boot.test.context.assertj.AssertableApplicationContext; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -43,74 +44,67 @@ import static org.mockito.Mockito.mock; */ public class HazelcastJpaDependencyAutoConfigurationTests { - private AnnotationConfigApplicationContext context; - - @After - public void closeContext() { - if (this.context != null) { - this.context.close(); - } - } + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class, + HibernateJpaAutoConfiguration.class, + HazelcastJpaDependencyAutoConfiguration.class)) + .withPropertyValues("spring.datasource.generate-unique-name=true", + "spring.datasource.initialize=false"); @Test public void registrationIfHazelcastInstanceHasRegularBeanName() { - load(HazelcastConfiguration.class); - assertThat(getPostProcessor()) - .containsKey("hazelcastInstanceJpaDependencyPostProcessor"); - assertThat(getEntityManagerFactoryDependencies()).contains("hazelcastInstance"); + this.contextRunner.withUserConfiguration( + HazelcastConfiguration.class).run((context) -> { + assertThat(postProcessors(context)) + .containsKey("hazelcastInstanceJpaDependencyPostProcessor"); + assertThat(entityManagerFactoryDependencies(context)).contains( + "hazelcastInstance"); + }); } @Test public void noRegistrationIfHazelcastInstanceHasCustomBeanName() { - load(HazelcastCustomNameConfiguration.class); - assertThat(getEntityManagerFactoryDependencies()) - .doesNotContain("hazelcastInstance"); - assertThat(getPostProcessor()) - .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); + this.contextRunner.withUserConfiguration( + HazelcastCustomNameConfiguration.class).run((context) -> { + assertThat(entityManagerFactoryDependencies(context)) + .doesNotContain("hazelcastInstance"); + assertThat(postProcessors(context)) + .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); + }); } @Test public void noRegistrationWithNoHazelcastInstance() { - load(null); - assertThat(getEntityManagerFactoryDependencies()) - .doesNotContain("hazelcastInstance"); - assertThat(getPostProcessor()) - .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); + this.contextRunner.run((context) -> { + assertThat(entityManagerFactoryDependencies(context)) + .doesNotContain("hazelcastInstance"); + assertThat(postProcessors(context)) + .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); + }); } @Test public void noRegistrationWithNoEntityManagerFactory() { - this.context = new AnnotationConfigApplicationContext(); - this.context.register(HazelcastConfiguration.class, - HazelcastJpaDependencyAutoConfiguration.class); - this.context.refresh(); - assertThat(getPostProcessor()) - .doesNotContainKey("hazelcastInstanceJpaDependencyPostProcessor"); + new ApplicationContextRunner().withUserConfiguration(HazelcastConfiguration.class) + .withConfiguration(AutoConfigurations.of( + HazelcastJpaDependencyAutoConfiguration.class)) + .run((context) -> assertThat(postProcessors(context)).doesNotContainKey( + "hazelcastInstanceJpaDependencyPostProcessor")); } - private Map getPostProcessor() { - return this.context - .getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class); + private Map postProcessors( + AssertableApplicationContext context) { + return context.getBeansOfType(EntityManagerFactoryDependsOnPostProcessor.class); } - private List getEntityManagerFactoryDependencies() { - String[] dependsOn = this.context.getBeanDefinition("entityManagerFactory") - .getDependsOn(); + private List entityManagerFactoryDependencies( + AssertableApplicationContext context) { + String[] dependsOn = ((BeanDefinitionRegistry) context.getSourceApplicationContext()) + .getBeanDefinition("entityManagerFactory").getDependsOn(); return dependsOn != null ? Arrays.asList(dependsOn) : Collections.emptyList(); } - public void load(Class config) { - AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); - if (config != null) { - ctx.register(config); - } - ctx.register(EmbeddedDataSourceConfiguration.class, - DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class); - ctx.register(HazelcastJpaDependencyAutoConfiguration.class); - ctx.refresh(); - this.context = ctx; - } @Configuration static class HazelcastConfiguration {