Skip to content

Commit

Permalink
HSEARCH-4328 Refactor JSR-352 ITs for safer startup/shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Sep 24, 2021
1 parent 0bc873f commit 314cff0
Show file tree
Hide file tree
Showing 17 changed files with 709 additions and 775 deletions.
Expand Up @@ -16,22 +16,24 @@
import java.util.List;
import javax.batch.runtime.context.JobContext;
import javax.batch.runtime.context.StepContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.CacheMode;
import org.hibernate.search.batch.jsr352.core.massindexing.impl.JobContextData;
import org.hibernate.search.batch.jsr352.core.massindexing.step.impl.IndexScope;
import org.hibernate.search.batch.jsr352.core.massindexing.step.spi.EntityReader;
import org.hibernate.search.integrationtest.batch.jsr352.massindexing.entity.Company;
import org.hibernate.search.integrationtest.batch.jsr352.util.BackendConfigurations;
import org.hibernate.search.integrationtest.batch.jsr352.util.JobTestUtil;
import org.hibernate.search.integrationtest.batch.jsr352.util.PersistenceUnitTestUtil;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.ReusableOrmSetupHolder;

import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
Expand All @@ -44,14 +46,18 @@
*/
public class EntityReaderComponentIT {

private static final String PERSISTENCE_UNIT_NAME = PersistenceUnitTestUtil.getPersistenceUnitName();

private static final List<Company> COMPANIES = Arrays.asList(
new Company( "Red Hat" ),
new Company( "Google" ),
new Company( "Microsoft" )
);

@ClassRule
public static ReusableOrmSetupHolder setupHolder =
ReusableOrmSetupHolder.withSingleBackend( BackendConfigurations.simple() );
@Rule
public MethodRule setupHolderMethodRule = setupHolder.methodRule();

@Rule
public final MockitoRule mockito = MockitoJUnit.rule().strictness( Strictness.STRICT_STUBS );

Expand All @@ -63,21 +69,17 @@ public class EntityReaderComponentIT {

private EntityReader entityReader;

@ReusableOrmSetupHolder.Setup
public void setup(OrmSetupHelper.SetupContext setupContext) {
setupContext.withAnnotatedTypes( Company.class )
.withProperty( HibernateOrmMapperSettings.AUTOMATIC_INDEXING_ENABLED, false );
}

@Before
public void setUp() {
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME );
em = emf.createEntityManager();
em.getTransaction().begin();
COMPANIES.forEach( em::persist );
em.getTransaction().commit();
}
finally {
if ( em != null ) {
em.close();
}
}
public void init() {
emf = setupHolder.entityManagerFactory();

setupHolder.runInTransaction( session -> COMPANIES.forEach( session::persist ) );

final String cacheMode = CacheMode.IGNORE.name();
final String entityName = Company.class.getName();
Expand Down Expand Up @@ -106,13 +108,6 @@ public void setUp() {
mockedStepContext );
}

@After
public void shutDown() {
if ( emf.isOpen() ) {
emf.close();
}
}

@Test
public void testReadItem_withoutBoundary() throws Exception {
JobContextData jobData = new JobContextData();
Expand Down
Expand Up @@ -15,21 +15,25 @@
import java.util.Properties;
import javax.batch.api.partition.PartitionPlan;
import javax.batch.runtime.context.JobContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.search.batch.jsr352.core.massindexing.impl.JobContextData;
import org.hibernate.search.batch.jsr352.core.massindexing.step.impl.HibernateSearchPartitionMapper;
import org.hibernate.search.batch.jsr352.core.massindexing.util.impl.MassIndexingPartitionProperties;
import org.hibernate.search.integrationtest.batch.jsr352.massindexing.entity.Company;
import org.hibernate.search.integrationtest.batch.jsr352.massindexing.entity.Person;
import org.hibernate.search.integrationtest.batch.jsr352.util.BackendConfigurations;
import org.hibernate.search.integrationtest.batch.jsr352.util.JobTestUtil;
import org.hibernate.search.integrationtest.batch.jsr352.util.PersistenceUnitTestUtil;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.ReusableOrmSetupHolder;

import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

/**
* Single-component test for partition plan validation.
Expand All @@ -42,32 +46,36 @@ public class HibernateSearchPartitionMapperComponentIT {
private static final int COMP_ROWS = 3;
private static final int PERS_ROWS = 8;

@ClassRule
public static ReusableOrmSetupHolder setupHolder =
ReusableOrmSetupHolder.withSingleBackend( BackendConfigurations.simple() );
@Rule
public MethodRule setupHolderMethodRule = setupHolder.methodRule();

private EntityManagerFactory emf;

private JobContext mockedJobContext;

private HibernateSearchPartitionMapper partitionMapper;

@ReusableOrmSetupHolder.Setup
public void setup(OrmSetupHelper.SetupContext setupContext) {
setupContext.withAnnotatedTypes( Company.class, Person.class )
.withProperty( HibernateOrmMapperSettings.AUTOMATIC_INDEXING_ENABLED, false );
}

@Before
public void setUp() {
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME );
em = emf.createEntityManager();
em.getTransaction().begin();
public void init() {
emf = setupHolder.entityManagerFactory();

setupHolder.runInTransaction( session -> {
for ( int i = 1; i <= COMP_ROWS; i++ ) {
em.persist( new Company( "C" + i ) );
session.persist( new Company( "C" + i ) );
}
for ( int i = 1; i <= PERS_ROWS; i++ ) {
em.persist( new Person( "P" + i, "", "" ) );
}
em.getTransaction().commit();
}
finally {
if ( em != null ) {
em.close();
session.persist( new Person( "P" + i, "", "" ) );
}
}
} );

final String fetchSize = String.valueOf( 200 * 1000 );
final String hql = null;
Expand All @@ -87,13 +95,6 @@ public void setUp() {
);
}

@After
public void shutDown() {
if ( emf.isOpen() ) {
emf.close();
}
}

/**
* Prove that there're N partitions for each root entity,
* where N stands for the ceiling number of the division
Expand Down
Expand Up @@ -10,41 +10,36 @@

import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.hibernate.search.batch.jsr352.core.massindexing.util.impl.ValidationUtil;
import org.hibernate.search.integrationtest.batch.jsr352.massindexing.entity.Company;
import org.hibernate.search.integrationtest.batch.jsr352.massindexing.entity.Person;
import org.hibernate.search.integrationtest.batch.jsr352.util.PersistenceUnitTestUtil;
import org.hibernate.search.integrationtest.batch.jsr352.util.BackendConfigurations;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.util.common.SearchException;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.ReusableOrmSetupHolder;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.MethodRule;

/**
* @author Mincong Huang
*/
public class ValidationUtilComponentIT {

private static final String PERSISTENCE_UNIT_NAME = PersistenceUnitTestUtil.getPersistenceUnitName();
@ClassRule
public static ReusableOrmSetupHolder setupHolder =
ReusableOrmSetupHolder.withSingleBackend( BackendConfigurations.simple() );
@Rule
public MethodRule setupHolderMethodRule = setupHolder.methodRule();

private static final String EMF_SCOPE = "persistence-unit-name";

private static EntityManagerFactory emf;

@BeforeClass
public static void setUp() {
emf = Persistence.createEntityManagerFactory( PERSISTENCE_UNIT_NAME );
}

@AfterClass
public static void tearDown() {
if ( emf != null ) {
emf.close();
emf = null;
}
@ReusableOrmSetupHolder.Setup
public void setup(OrmSetupHelper.SetupContext setupContext) {
setupContext.withAnnotatedTypes( Company.class, Person.class )
.withProperty( HibernateOrmMapperSettings.AUTOMATIC_INDEXING_ENABLED, false );
}

@Test
Expand All @@ -54,7 +49,7 @@ public void validateEntityTypes_whenAllTypesAreAvailableInEMF() throws Exception
.map( Class::getName )
.collect( Collectors.joining( "," ) );

ValidationUtil.validateEntityTypes( null, EMF_SCOPE, PERSISTENCE_UNIT_NAME, serializedEntityTypes );
ValidationUtil.validateEntityTypes( null, null, null, serializedEntityTypes );
}

@Test
Expand All @@ -64,7 +59,7 @@ public void validateEntityTypes_whenContainingNonIndexedTypes() throws Exception
.map( Class::getName )
.collect( Collectors.joining( "," ) );

assertThatThrownBy( () -> ValidationUtil.validateEntityTypes( null, EMF_SCOPE, PERSISTENCE_UNIT_NAME, serializedEntityTypes ) )
assertThatThrownBy( () -> ValidationUtil.validateEntityTypes( null, null, null, serializedEntityTypes ) )
.isInstanceOf( SearchException.class )
.hasMessageContaining( "The following selected entity types aren't indexable: "
+ NotIndexed.class.getName() + ". Check whether they are annotated with '@Indexed'." );
Expand Down

This file was deleted.

0 comments on commit 314cff0

Please sign in to comment.