Skip to content

Commit

Permalink
HSEARCH-4674 Simplify tests configuration
Browse files Browse the repository at this point in the history
- remove some unused/redundant config properties
- make use of @DynamicPropertySource
- make use of `Persistence.createEntityManagerFactory` with properties
- make use of `SystemHelper.setSystemProperty`
  • Loading branch information
marko-bekhta committed Nov 7, 2023
1 parent 8468eb1 commit 4422c26
Show file tree
Hide file tree
Showing 31 changed files with 184 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.hibernate.search.documentation.mapper.orm.gettingstarted.withouthsearch;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.documentation.testsupport.TestConfiguration.databaseConnectionProperties;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;

import java.util.List;
Expand All @@ -26,7 +27,8 @@ class GettingStartedWithoutHibernateSearchIT {

@BeforeEach
void setup() {
entityManagerFactory = Persistence.createEntityManagerFactory( "GettingStartedWithoutHibernateSearchIT" );
entityManagerFactory = Persistence.createEntityManagerFactory( "GettingStartedWithoutHibernateSearchIT",
databaseConnectionProperties() );
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.hibernate.search.util.impl.integrationtest.common.TestConfigurationProvider;
import org.hibernate.search.util.impl.integrationtest.common.extension.BackendConfiguration;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.DatabaseContainer;

public final class TestConfiguration {

Expand All @@ -22,8 +23,8 @@ private TestConfiguration() {
*
* @return Configuration properties to use when bootstrapping Hibernate ORM with JPA.
*/
public static Map<String, String> ormMapperProperties(TestConfigurationProvider configurationProvider) {
Map<String, String> properties = new HashMap<>();
public static Map<String, Object> ormMapperProperties(TestConfigurationProvider configurationProvider) {
Map<String, Object> properties = databaseConnectionProperties();

// Hack: override example properties set in persistence.xml
properties.put( "hibernate.search.backend.hosts", "" );
Expand All @@ -40,6 +41,12 @@ public static Map<String, String> ormMapperProperties(TestConfigurationProvider
return properties;
}

public static Map<String, Object> databaseConnectionProperties() {
Map<String, Object> properties = new HashMap<>();
DatabaseContainer.configuration().add( properties );
return properties;
}

/**
* @param configurationProvider A test configuration provider.
* @param backendConfiguration The backend configuration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static Map<String, Object> connectionProperties() {
SearchBackendContainer.connectionUrl()
);
}
DatabaseContainer.configuration();
DatabaseContainer.configuration().add( properties );

return properties;
}
Expand Down
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
Loading

0 comments on commit 4422c26

Please sign in to comment.