Skip to content

Commit

Permalink
HSEARCH-4674 Simplify Spring tests configuration
Browse files Browse the repository at this point in the history
- remove some unused/redundant config properties
- make use of @DynamicPropertySource
  • Loading branch information
marko-bekhta committed Oct 24, 2023
1 parent 0c73184 commit b5e3d6e
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spring.jpa:
directory.root: ${LUCENE_ROOT_PATH:target/test-indexes}/${random.uuid}

spring.datasource:
driver-class: org.h2.Driver
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1
username: sa
password: sa
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,15 @@
import org.hibernate.SessionFactory;
import org.hibernate.search.engine.environment.bean.BeanHolder;
import org.hibernate.search.engine.environment.bean.BeanReference;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
import org.hibernate.search.util.impl.integrationtest.common.stub.backend.BackendMappingHandle;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.DatabaseContainer;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.HibernateOrmMappingHandle;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.hibernate.search.util.impl.test.extension.StaticCounters;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.assertj.core.api.InstanceOfAssertFactories;
Expand All @@ -51,6 +50,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.event.EventListener;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.stereotype.Component;

/**
Expand All @@ -60,7 +60,6 @@
* then checking the Spring-defined hooks (@PostConstruct and @PreDestroy) have been called
* exactly as many times as expected.
*/
@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@TestForIssue(jiraKey = { "HSEARCH-1316", "HSEARCH-3171" })
class SpringBeanResolutionIT {

Expand Down Expand Up @@ -180,8 +179,17 @@ private <T> void doTest(ExpectedScope expectedScope, CounterKeys counterKeys, Be
private ConfigurableApplicationContext startApplication() {
Map<String, Object> properties = new LinkedHashMap<>();
properties.put( "test.backendMock", backendMock );

DatabaseContainer.Configuration configuration = DatabaseContainer.configuration();
MockEnvironment environment = new MockEnvironment();
environment.withProperty( "JDBC_DRIVER", configuration.driver() );
environment.withProperty( "JDBC_URL", configuration.url() );
environment.withProperty( "JDBC_USERNAME", configuration.user() );
environment.withProperty( "JDBC_PASSWORD", configuration.pass() );

return new SpringApplicationBuilder( SpringConfig.class )
.web( WebApplicationType.NONE )
.environment( environment )
.properties( properties )
.run();
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,28 @@

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.integrationtest.spring.jta.dao.SnertDAO;
import org.hibernate.search.integrationtest.spring.jta.entity.Snert;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSearchSessionHolder;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
import org.hibernate.search.util.impl.test.annotation.PortedFromSearch5;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaAndSpringApplicationConfiguration.class)
@ActiveProfiles("jta")
@PortedFromSearch5(original = "org.hibernate.search.test.integration.spring.jta.JtaAndSpringIT")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class JtaAndSpringIT {
class JtaAndSpringIT extends AbstractMapperOrmSpringIT {

@Autowired
@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,30 @@

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.integrationtest.spring.jta.dao.BoxDAO;
import org.hibernate.search.integrationtest.spring.jta.entity.Box;
import org.hibernate.search.integrationtest.spring.jta.entity.Doughnut;
import org.hibernate.search.integrationtest.spring.jta.entity.Muffin;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSearchSessionHolder;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
import org.hibernate.search.util.impl.test.annotation.PortedFromSearch5;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaAndSpringApplicationConfiguration.class)
@ActiveProfiles("jta")
@PortedFromSearch5(original = "org.hibernate.search.test.integration.spring.jta.JtaAndSpringMoreComplexIT")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class JtaAndSpringMoreComplexIT {
class JtaAndSpringMoreComplexIT extends AbstractMapperOrmSpringIT {

@Autowired
@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,26 @@

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.integrationtest.spring.jta.dao.SnertDAO;
import org.hibernate.search.integrationtest.spring.jta.entity.Snert;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.mapper.orm.session.impl.HibernateOrmSearchSessionHolder;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaAndSpringOutboxApplicationConfiguration.class)
@ActiveProfiles({ "jta", "outbox" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class JtaAndSpringOutboxIT {
class JtaAndSpringOutboxIT extends AbstractMapperOrmSpringIT {

@Autowired
@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.integrationtest.spring.jta.JtaAndSpringOutboxApplicationConfiguration;
import org.hibernate.search.integrationtest.spring.jta.dao.SnertDAO;
import org.hibernate.search.integrationtest.spring.jta.entity.Snert;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.atomikos.icatch.jta.TransactionManagerImp;
Expand All @@ -29,14 +28,11 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaAndSpringOutboxApplicationConfiguration.class)
@ActiveProfiles({ "jta", "outbox", "transaction-timeout", "raised-timeout" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class RaisedTimeoutJtaAndSpringOutboxIT {
class RaisedTimeoutJtaAndSpringOutboxIT extends AbstractMapperOrmSpringIT {

@Autowired
@RegisterExtension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,25 @@

import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
import org.hibernate.search.integrationtest.spring.extension.HibernateSpringPropertiesSetterExtension;
import org.hibernate.search.integrationtest.spring.jta.JtaAndSpringOutboxApplicationConfiguration;
import org.hibernate.search.integrationtest.spring.jta.dao.SnertDAO;
import org.hibernate.search.integrationtest.spring.jta.entity.Snert;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.atomikos.icatch.jta.TransactionManagerImp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(HibernateSpringPropertiesSetterExtension.class)
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = JtaAndSpringOutboxApplicationConfiguration.class)
@ActiveProfiles({ "jta", "outbox", "transaction-timeout" })
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class TransactionTimeoutJtaAndSpringOutboxIT {
class TransactionTimeoutJtaAndSpringOutboxIT extends AbstractMapperOrmSpringIT {

@Autowired
private SnertDAO snertDAO;
Expand All @@ -58,7 +54,7 @@ void checkJta() {
}

@Test
void test() throws Exception {
void test() {
Snert snert = new Snert();
snert.setId( 1L );
snert.setName( "dave" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,28 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.Id;

import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractSpringITConfig;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.CoordinationStrategyExpectations;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@ActiveProfiles(profiles = { "outbox" })
class TransactionOutboxIT {
class TransactionOutboxIT extends AbstractMapperOrmSpringIT {

@Configuration
@EntityScan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.Id;

import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractSpringITConfig;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession;
Expand All @@ -29,17 +30,14 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

Expand All @@ -48,10 +46,8 @@
* one can create a single SearchSession for all threads, and it will correctly use the correct EntityManager
* depending on the current thread.
*/
@ExtendWith(SpringExtension.class)
@SpringBootTest
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
class SessionProxyIT {
class SessionProxyIT extends AbstractMapperOrmSpringIT {

@Configuration
@EntityScan
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.integrationtest.spring.testsupport;

import org.hibernate.search.util.impl.integrationtest.mapper.orm.DatabaseContainer;

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest
public abstract class AbstractMapperOrmSpringIT {
@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
DatabaseContainer.configuration().addAsSpring( (key, value) -> registry.add( key, () -> value ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import jakarta.persistence.EntityManager;
import jakarta.persistence.Id;

import org.hibernate.search.integrationtest.spring.testsupport.AbstractMapperOrmSpringIT;
import org.hibernate.search.integrationtest.spring.testsupport.AbstractSpringITConfig;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendMock;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -27,16 +27,14 @@
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@ExtendWith(SpringExtension.class)
// Adding a property here is just a "workaround" to make sure that a different context is used for this test
// otherwise there can be build errors when running all the tests via maven.
@SpringBootTest(properties = "spring.jta.atomikos.datasource.bean-name=hsearch-datasource1")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class TransactionIT {
class TransactionIT extends AbstractMapperOrmSpringIT {

@Configuration
@EntityScan
Expand Down
Loading

0 comments on commit b5e3d6e

Please sign in to comment.