Skip to content

Commit 52d977d

Browse files
committed
Convert Search 5/6 tests to JUnit 5 + containers for Elasticsearch
Because the elasticsearch maven plugin stopped working after upgrading to OpenJDK 17 for some reason, and I don't want to spend time figuring out why.
1 parent 8bef635 commit 52d977d

File tree

28 files changed

+427
-421
lines changed

28 files changed

+427
-421
lines changed

search/hibernate-search-5/elasticsearch-5/elasticsearch-maven-plugin/configuration/elasticsearch.yml

Lines changed: 0 additions & 66 deletions
This file was deleted.

search/hibernate-search-5/elasticsearch-5/elasticsearch-maven-plugin/configuration/log4j2.properties

Lines changed: 0 additions & 34 deletions
This file was deleted.

search/hibernate-search-5/elasticsearch-5/elasticsearch-maven-plugin/init/init.script

Lines changed: 0 additions & 5 deletions
This file was deleted.

search/hibernate-search-5/elasticsearch-5/pom.xml

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,32 @@
1212
<version.org.hibernate>5.4.33</version.org.hibernate>
1313

1414
<version.com.h2database>2.3.232</version.com.h2database>
15-
<version.junit>4.13.2</version.junit>
16-
17-
<version.elasticsearch-maven-plugin>6.26</version.elasticsearch-maven-plugin>
18-
<version.org.elasticsearch>5.6.9</version.org.elasticsearch>
15+
<version.junit-jupiter>5.11.0</version.junit-jupiter>
16+
<version.org.assertj.assertj-core>3.26.3</version.org.assertj.assertj-core>
1917

2018
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2119
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2220
</properties>
2321

22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>org.testcontainers</groupId>
26+
<artifactId>testcontainers-bom</artifactId>
27+
<version>1.20.1</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit</groupId>
33+
<artifactId>junit-bom</artifactId>
34+
<version>${version.junit-jupiter}</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
</dependencies>
39+
</dependencyManagement>
40+
2441
<dependencies>
2542
<dependency>
2643
<groupId>org.hibernate</groupId>
@@ -63,9 +80,34 @@
6380
<scope>test</scope>
6481
</dependency>
6582
<dependency>
66-
<groupId>junit</groupId>
67-
<artifactId>junit</artifactId>
68-
<version>${version.junit}</version>
83+
<groupId>org.junit.jupiter</groupId>
84+
<artifactId>junit-jupiter</artifactId>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.junit.platform</groupId>
89+
<artifactId>junit-platform-launcher</artifactId>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.junit.jupiter</groupId>
94+
<artifactId>junit-jupiter-engine</artifactId>
95+
<scope>test</scope>
96+
</dependency>
97+
<dependency>
98+
<groupId>org.junit.vintage</groupId>
99+
<artifactId>junit-vintage-engine</artifactId>
100+
<scope>test</scope>
101+
</dependency>
102+
<dependency>
103+
<groupId>org.assertj</groupId>
104+
<artifactId>assertj-core</artifactId>
105+
<version>${version.org.assertj.assertj-core}</version>
106+
<scope>test</scope>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.testcontainers</groupId>
110+
<artifactId>elasticsearch</artifactId>
69111
<scope>test</scope>
70112
</dependency>
71113
</dependencies>
@@ -118,35 +160,6 @@
118160
</execution>
119161
</executions>
120162
</plugin>
121-
<plugin>
122-
<groupId>com.github.alexcojocaru</groupId>
123-
<artifactId>elasticsearch-maven-plugin</artifactId>
124-
<version>${version.elasticsearch-maven-plugin}</version>
125-
<configuration>
126-
<clusterName>hsearchEsTestCluster</clusterName>
127-
<version>${version.org.elasticsearch}</version>
128-
<httpPort>9200</httpPort>
129-
<pathConf>${project.basedir}/elasticsearch-maven-plugin/configuration/</pathConf>
130-
<pathInitScript>${project.basedir}/elasticsearch-maven-plugin/init/init.script</pathInitScript>
131-
<autoCreateIndex>false</autoCreateIndex>
132-
</configuration>
133-
<executions>
134-
<execution>
135-
<id>start-elasticsearch</id>
136-
<phase>pre-integration-test</phase>
137-
<goals>
138-
<goal>runforked</goal>
139-
</goals>
140-
</execution>
141-
<execution>
142-
<id>stop-elasticsearch</id>
143-
<phase>post-integration-test</phase>
144-
<goals>
145-
<goal>stop</goal>
146-
</goals>
147-
</execution>
148-
</executions>
149-
</plugin>
150163
</plugins>
151164
</build>
152165
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.hibernate.search.bugs;
2+
3+
import java.io.Closeable;
4+
import java.time.Duration;
5+
6+
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
7+
import org.testcontainers.elasticsearch.ElasticsearchContainer;
8+
import org.testcontainers.utility.DockerImageName;
9+
10+
class SearchBackendContainer implements Closeable {
11+
12+
private static final DockerImageName ELASTICSEARCH_IMAGE_NAME =
13+
DockerImageName.parse( "docker.elastic.co/elasticsearch/elasticsearch" )
14+
.withTag( "5.6.16" );
15+
16+
private ElasticsearchContainer elasticsearchContainer;
17+
18+
public ElasticsearchContainer setUp() {
19+
elasticsearchContainer = new ElasticsearchContainer( ELASTICSEARCH_IMAGE_NAME )
20+
.withExposedPorts( 9200, 9300 )
21+
.waitingFor( new HttpWaitStrategy().forPort( 9200 ).forStatusCode( 200 ) )
22+
.withStartupTimeout( Duration.ofMinutes( 5 ) )
23+
.withReuse( true )
24+
.withEnv( "logger.level", "WARN" )
25+
.withEnv( "discovery.type", "single-node" )
26+
.withEnv( "xpack.security.enabled", "false" )
27+
.withEnv( "ES_JAVA_OPTS", "-Xms1g -Xmx1g" )
28+
.withEnv( "cluster.routing.allocation.disk.threshold_enabled", "false" );
29+
elasticsearchContainer.start();
30+
return elasticsearchContainer;
31+
}
32+
33+
@Override
34+
public void close() {
35+
try ( ElasticsearchContainer esToClose = this.elasticsearchContainer ) {
36+
// Nothing to do: we just want resources to get closed.
37+
}
38+
}
39+
}

search/hibernate-search-5/elasticsearch-5/src/test/java/org/hibernate/search/bugs/SearchTestBase.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,25 @@
55
import org.hibernate.boot.MetadataSources;
66
import org.hibernate.boot.SessionFactoryBuilder;
77
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
8-
import org.hibernate.service.spi.ServiceRegistryImplementor;
9-
import org.junit.After;
10-
import org.junit.Before;
8+
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
1111

1212
public abstract class SearchTestBase {
13-
13+
1414
private SessionFactory sessionFactory;
15-
16-
@Before
15+
private SearchBackendContainer elasticsearchContainer;
16+
17+
@BeforeEach
1718
public void setUp() {
18-
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
19-
ServiceRegistryImplementor serviceRegistry = (ServiceRegistryImplementor) registryBuilder.build();
19+
elasticsearchContainer = new SearchBackendContainer();
2020

21-
MetadataSources ms = new MetadataSources( serviceRegistry );
21+
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder();
22+
registryBuilder.applySetting(
23+
"hibernate.search.default.elasticsearch.host",
24+
elasticsearchContainer.setUp().getHttpHostAddress()
25+
);
26+
MetadataSources ms = new MetadataSources( registryBuilder.build() );
2227
Class<?>[] annotatedClasses = getAnnotatedClasses();
2328
if ( annotatedClasses != null ) {
2429
for ( Class<?> entity : annotatedClasses ) {
@@ -31,14 +36,17 @@ public void setUp() {
3136
final SessionFactoryBuilder sfb = metadata.getSessionFactoryBuilder();
3237
this.sessionFactory = sfb.build();
3338
}
34-
35-
@After
39+
40+
@AfterEach
3641
public void tearDown() {
37-
this.sessionFactory.close();
42+
try ( SearchBackendContainer esToClose = this.elasticsearchContainer;
43+
SessionFactory sessionFactoryToClose = this.sessionFactory ) {
44+
// Nothing to do: we just want resources to get closed.
45+
}
3846
}
3947

4048
protected abstract Class<?>[] getAnnotatedClasses();
41-
49+
4250
protected SessionFactory getSessionFactory() {
4351
return sessionFactory;
4452
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hibernate.search.bugs;
2+
3+
import org.hibernate.cfg.Configuration;
4+
5+
import org.hibernate.testing.AfterClassOnce;
6+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
7+
8+
public abstract class SearchTestJunit4Base extends BaseCoreFunctionalTestCase {
9+
10+
private SearchBackendContainer elasticsearchContainer;
11+
12+
@Override
13+
protected final void configure(Configuration configuration) {
14+
super.configure( configuration );
15+
16+
this.elasticsearchContainer = new SearchBackendContainer();
17+
configuration.setProperty( "hibernate.search.backend.hosts", elasticsearchContainer.setUp().getHttpHostAddress() );
18+
19+
configureMore( configuration );
20+
}
21+
22+
protected void configureMore(Configuration configuration) {
23+
}
24+
25+
@Override
26+
protected void releaseSessionFactory() {
27+
try ( SearchBackendContainer esToClose = this.elasticsearchContainer; ) {
28+
super.releaseSessionFactory();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)