Skip to content

Commit

Permalink
HSEARCH-4674 Change how spring receives properties for test execution
Browse files Browse the repository at this point in the history
  • Loading branch information
marko-bekhta committed Nov 8, 2023
1 parent 8b0787b commit b9c1d35
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 17 deletions.
16 changes: 1 addition & 15 deletions integrationtest/mapper/orm-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Remove Hibernate system properties from parent settings:
They are supposed to be handled by the spring.datasource subsystem
and not by the Hibernate internal pool!
See also the failsafe configuration.
See also the DatabaseContainer#springConfiguration().
-->
<failsafe.jvm.args.hibernate-orm></failsafe.jvm.args.hibernate-orm>
</properties>
Expand Down Expand Up @@ -89,20 +89,6 @@
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>${failsafe.spring.skip}</skip>
<!--
Since we do not pass the Hibernate ORM settings through system properties directly
(see the override of the failsafe.jvm.args.hibernate-orm near the top of this file),
we need to pass the settings some other way.
Here we chose to use environment variables.
-->
<environmentVariables>
<HIBERNATE_DIALECT>${db.dialect}</HIBERNATE_DIALECT>
<JDBC_DRIVER>${jdbc.driver}</JDBC_DRIVER>
<JDBC_URL>${jdbc.url}</JDBC_URL>
<JDBC_USERNAME>${jdbc.user}</JDBC_USERNAME>
<JDBC_PASSWORD>${jdbc.pass}</JDBC_PASSWORD>
<JDBC_ISOLATION>${jdbc.isolation}</JDBC_ISOLATION>
</environmentVariables>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand All @@ -31,6 +32,7 @@
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 Down Expand Up @@ -58,6 +60,7 @@
* 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.extension;

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

import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class HibernateSpringPropertiesSetterExtension implements BeforeAllCallback {
@Override
public void beforeAll(ExtensionContext context) {
DatabaseContainer.springConfiguration();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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.mapper.orm.session.impl.HibernateOrmSearchSessionHolder;
Expand All @@ -30,6 +31,7 @@
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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;
Expand All @@ -32,6 +33,7 @@
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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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.mapper.orm.session.impl.HibernateOrmSearchSessionHolder;
Expand All @@ -29,6 +30,7 @@
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" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

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;
Expand All @@ -30,6 +31,7 @@
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" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand All @@ -29,6 +30,7 @@
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" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Future;
import java.util.Locale;
import java.util.function.Consumer;

import com.github.dockerjava.api.model.HostConfig;
import com.github.dockerjava.api.model.Ulimit;
Expand All @@ -32,8 +32,10 @@ public final class DatabaseContainer {
private DatabaseContainer() {
}

private static final Object LOCK = new Object();
private static final SupportedDatabase DATABASE;
private static final HibernateSearchJdbcDatabaseContainer DATABASE_CONTAINER;
private static Boolean H2_INITIALIZED = Boolean.FALSE;


static {
Expand All @@ -46,11 +48,36 @@ private DatabaseContainer() {


public static Configuration configuration() {
return configure( Configuration::addAsSystemProperties );
}

public static Configuration springConfiguration() {
return configure( Configuration::addAsSpringSystemProperties );
}

private static Configuration configure(Consumer<Configuration> propertySetter) {
if ( !SupportedDatabase.H2.equals( DATABASE ) ) {
DATABASE_CONTAINER.start();
}
Configuration configuration = DATABASE.configuration( DATABASE_CONTAINER );
configuration.addAsSystemProperties();

if ( DATABASE_CONTAINER != null && !DATABASE_CONTAINER.isRunning() ) {
synchronized (DATABASE_CONTAINER) {
if ( !DATABASE_CONTAINER.isRunning() ) {
DATABASE_CONTAINER.start();
propertySetter.accept( configuration );
}
}
}
else if ( !H2_INITIALIZED ) {
synchronized (LOCK) {
if ( !H2_INITIALIZED ) {
propertySetter.accept( configuration );
H2_INITIALIZED = Boolean.TRUE;
}
}
}

return configuration;
}

Expand Down Expand Up @@ -365,6 +392,15 @@ private void addAsSystemProperties() {
System.setProperty( "hibernate.connection.password", this.pass );
System.setProperty( "hibernate.connection.isolation", this.isolation );
}

private void addAsSpringSystemProperties() {
System.setProperty( "HIBERNATE_DIALECT", this.dialect );
System.setProperty( "JDBC_DRIVER", this.driver );
System.setProperty( "JDBC_URL", this.url );
System.setProperty( "JDBC_USERNAME", this.user );
System.setProperty( "JDBC_PASSWORD", this.pass );
System.setProperty( "JDBC_ISOLATION", this.isolation );
}
}

}

0 comments on commit b9c1d35

Please sign in to comment.