diff --git a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/TckConfiguration.java b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/TckConfiguration.java index 7acd3163ccf..b995874225f 100644 --- a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/TckConfiguration.java +++ b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/TckConfiguration.java @@ -6,19 +6,12 @@ */ package org.hibernate.search.integrationtest.backend.tck.util; -import java.io.IOException; -import java.io.InputStream; -import java.text.SimpleDateFormat; -import java.util.Date; import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Properties; import java.util.ServiceLoader; import java.util.function.Function; import org.hibernate.search.engine.cfg.ConfigurationPropertySource; +import org.hibernate.search.util.impl.integrationtest.common.TestHelper; /** * Allows to run the tests with different backends depending on the content of the property file at @@ -40,14 +33,9 @@ public static TckConfiguration get() { return instance; } - private final String startupTimestamp; - private final TckBackendFeatures backendFeatures; private TckConfiguration() { - this.startupTimestamp = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss.SSS", Locale.ROOT ) - .format( new Date() ); - Iterator featuresIterator = ServiceLoader.load( TckBackendFeatures.class ).iterator(); if ( featuresIterator.hasNext() ) { this.backendFeatures = featuresIterator.next(); @@ -64,37 +52,12 @@ public TckBackendFeatures getBackendFeatures() { return backendFeatures; } - public ConfigurationPropertySource getBackendProperties(String testId, String configurationId) { + public ConfigurationPropertySource getBackendProperties(TestHelper testHelper, String configurationId) { String propertiesPath = configurationId == null ? DEFAULT_PROPERTIES_PATH : SPECIFIC_PROPERTIES_PATH_FUNCTION.apply( configurationId ); - return getPropertySourceFromFile( propertiesPath, testId ); - } - - private ConfigurationPropertySource getPropertySourceFromFile(String propertyFilePath, String testId) { - Properties properties = new Properties(); - try ( InputStream propertiesInputStream = getClass().getResourceAsStream( propertyFilePath ) ) { - if ( propertiesInputStream == null ) { - throw new IllegalStateException( "Missing TCK properties file in the classpath: " + propertyFilePath ); - } - properties.load( propertiesInputStream ); - } - catch (IOException e) { - throw new IllegalStateException( "Error loading TCK properties file: " + propertyFilePath ); - } - - Map overriddenProperties = new LinkedHashMap<>(); - - properties.forEach( (k, v) -> { - if ( v instanceof String ) { - overriddenProperties.put( (String) k, ( (String) v ).replace( "#{tck.test.id}", testId ).replace( "#{tck.startup.timestamp}", startupTimestamp ) ); - } - else { - overriddenProperties.put( (String) k, v ); - } - } ); - - return ConfigurationPropertySource.fromMap( overriddenProperties ).withMask( "backend" ); + return ConfigurationPropertySource.fromMap( testHelper.getPropertiesFromFile( propertiesPath ) ) + .withMask( "backend" ); } } diff --git a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/rule/SearchSetupHelper.java b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/rule/SearchSetupHelper.java index 31ec1c68306..9568524b8f8 100644 --- a/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/rule/SearchSetupHelper.java +++ b/integrationtest/backend/tck/src/main/java/org/hibernate/search/integrationtest/backend/tck/util/rule/SearchSetupHelper.java @@ -13,6 +13,7 @@ import java.util.function.Consumer; import org.hibernate.search.engine.common.spi.SearchIntegrationBuilder; +import org.hibernate.search.util.impl.integrationtest.common.TestHelper; import org.hibernate.search.util.impl.integrationtest.common.stub.mapper.StubMappingIndexManager; import org.hibernate.search.engine.cfg.ConfigurationPropertySource; import org.hibernate.search.engine.common.spi.SearchIntegration; @@ -31,35 +32,7 @@ public class SearchSetupHelper implements TestRule { private final List mappingRepositories = new ArrayList<>(); - private String testId; - - @Override - public Statement apply(Statement base, Description description) { - return statement( base, description ); - } - - private Statement statement(final Statement base, final Description description) { - return new Statement() { - - @Override - public void evaluate() throws Throwable { - before( description ); - try ( Closer closer = new Closer<>() ) { - try { - base.evaluate(); - } - finally { - closer.pushAll( SearchIntegration::close, mappingRepositories ); - mappingRepositories.clear(); - } - } - } - }; - } - - protected void before(Description description) { - testId = description.getTestClass().getSimpleName() + "-" + description.getMethodName(); - } + private TestHelper testHelper; public SetupContext withDefaultConfiguration() { return withConfiguration( null ); @@ -75,7 +48,7 @@ public SetupContext withConfiguration(String configurationId) { public SetupContext withConfiguration(String configurationId, String backendName) { TckConfiguration tckConfiguration = TckConfiguration.get(); - ConfigurationPropertySource propertySource = tckConfiguration.getBackendProperties( testId, configurationId ) + ConfigurationPropertySource propertySource = tckConfiguration.getBackendProperties( testHelper, configurationId ) .withPrefix( "backends." + backendName ); // Hack to have the resolve() method ignore the various masks and prefixes that we added for TCK purposes only @@ -85,6 +58,30 @@ public SetupContext withConfiguration(String configurationId, String backendName .withProperty( "indexes.default.backend", backendName ); } + @Override + public Statement apply(Statement base, Description description) { + return statement( base, description ); + } + + private Statement statement(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + testHelper = TestHelper.create( description ); + try ( Closer closer = new Closer<>() ) { + try { + base.evaluate(); + } + finally { + closer.pushAll( SearchIntegration::close, mappingRepositories ); + mappingRepositories.clear(); + } + } + } + }; + } + public class SetupContext { private final ConfigurationPropertySource propertySource; diff --git a/integrationtest/mapper/pojo/src/test/java/org/hibernate/search/integrationtest/mapper/pojo/test/util/rule/JavaBeanMappingSetupHelper.java b/integrationtest/mapper/pojo/src/test/java/org/hibernate/search/integrationtest/mapper/pojo/test/util/rule/JavaBeanMappingSetupHelper.java index fd1298ddfbb..eb00238169e 100644 --- a/integrationtest/mapper/pojo/src/test/java/org/hibernate/search/integrationtest/mapper/pojo/test/util/rule/JavaBeanMappingSetupHelper.java +++ b/integrationtest/mapper/pojo/src/test/java/org/hibernate/search/integrationtest/mapper/pojo/test/util/rule/JavaBeanMappingSetupHelper.java @@ -41,6 +41,11 @@ protected SetupContext createSetupContext() { return new SetupContext( ConfigurationPropertySource.empty() ); } + @Override + protected String getPropertiesPath(String configurationId) { + return "/javabean-test-" + configurationId + ".properties"; + } + @Override protected void close(CloseableJavaBeanMapping toClose) { toClose.close(); @@ -60,6 +65,7 @@ public final class SetupContext withConfiguration( builder -> overriddenProperties.forEach( builder::setProperty ) ); } + @Override public SetupContext withProperty(String key, Object value) { overriddenProperties.put( key, value ); return thisAsC(); diff --git a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/TestHelper.java b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/TestHelper.java new file mode 100644 index 00000000000..e9c900f4e72 --- /dev/null +++ b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/TestHelper.java @@ -0,0 +1,63 @@ +/* + * 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 . + */ +package org.hibernate.search.util.impl.integrationtest.common; + +import java.io.IOException; +import java.io.InputStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedHashMap; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; + +import org.junit.runner.Description; + +public final class TestHelper { + + private static final String STARTUP_TIMESTAMP = new SimpleDateFormat( "yyyy-MM-dd-HH-mm-ss.SSS", Locale.ROOT ) + .format( new Date() ); + + public static TestHelper create(Description description) { + return new TestHelper( description ); + } + + private final String testId; + + private TestHelper(Description description) { + this.testId = description.getTestClass().getSimpleName() + "-" + description.getMethodName(); + } + + public Map getPropertiesFromFile(String propertyFilePath) { + Properties properties = new Properties(); + try ( InputStream propertiesInputStream = getClass().getResourceAsStream( propertyFilePath ) ) { + if ( propertiesInputStream == null ) { + throw new IllegalStateException( "Missing test properties file in the classpath: " + propertyFilePath ); + } + properties.load( propertiesInputStream ); + } + catch (IOException e) { + throw new IllegalStateException( "Error loading test properties file: " + propertyFilePath, e ); + } + + Map overriddenProperties = new LinkedHashMap<>(); + + properties.forEach( (k, v) -> { + if ( v instanceof String ) { + overriddenProperties.put( + (String) k, + ( (String) v ).replace( "#{tck.test.id}", testId ) + .replace( "#{tck.startup.timestamp}", STARTUP_TIMESTAMP ) ); + } + else { + overriddenProperties.put( (String) k, v ); + } + } ); + + return overriddenProperties; + } +} diff --git a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/rule/MappingSetupHelper.java b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/rule/MappingSetupHelper.java index 04b5213f000..c5f7bb10c05 100644 --- a/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/rule/MappingSetupHelper.java +++ b/util/internal/integrationtest/common/src/main/java/org/hibernate/search/util/impl/integrationtest/common/rule/MappingSetupHelper.java @@ -8,9 +8,11 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.function.Consumer; import org.hibernate.search.util.impl.common.Closer; +import org.hibernate.search.util.impl.integrationtest.common.TestHelper; import org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.impl.StubBackendFactory; import org.junit.rules.TestRule; @@ -21,27 +23,40 @@ public abstract class MappingSetupHelper.A private final List toClose = new ArrayList<>(); + private TestHelper testHelper; + public C withBackendMock(BackendMock backendMock) { + String backendName = backendMock.getBackendName(); return createSetupContext() - .withPropertyRadical( "backends.stubBackend.type", StubBackendFactory.class.getName() ) - .withPropertyRadical( "indexes.default.backend", backendMock.getBackendName() ); + .withPropertyRadical( "backends." + backendName + ".type", StubBackendFactory.class.getName() ) + .withPropertyRadical( "indexes.default.backend", backendName ); + } + + public C withBackend(String configurationId, String backendName) { + String propertiesPath = getPropertiesPath( configurationId ); + return createSetupContext() + .withPropertyRadical( "indexes.default.backend", backendName ) + .withProperties( testHelper.getPropertiesFromFile( propertiesPath ) ); } @Override public Statement apply(Statement base, Description description) { - return statement( base ); + return statement( base, description ); } protected abstract C createSetupContext(); + protected abstract String getPropertiesPath(String configurationId); + protected abstract void close(R toClose) throws Exception; - private Statement statement(Statement base) { + private Statement statement(Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { try ( Closer closer = new Closer<>() ) { + testHelper = TestHelper.create( description ); try { base.evaluate(); } @@ -63,6 +78,13 @@ protected AbstractSetupContext() { protected abstract C withPropertyRadical(String keyRadical, Object value); + protected abstract C withProperty(String keyRadical, Object value); + + protected C withProperties(Map properties) { + properties.forEach( this::withProperty ); + return thisAsC(); + } + /** * Add configuration to be applied to the builder during setup. * @param beforeBuild A consumer called before Hibernate Search is bootstrapped. diff --git a/util/internal/integrationtest/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/orm/OrmSetupHelper.java b/util/internal/integrationtest/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/orm/OrmSetupHelper.java index 443f56fc6a7..36be07d5220 100644 --- a/util/internal/integrationtest/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/orm/OrmSetupHelper.java +++ b/util/internal/integrationtest/orm/src/main/java/org/hibernate/search/util/impl/integrationtest/orm/OrmSetupHelper.java @@ -26,6 +26,11 @@ protected SetupContext createSetupContext() { return new SetupContext(); } + @Override + protected String getPropertiesPath(String configurationId) { + return "/hibernate-test-" + configurationId + ".properties"; + } + @Override protected void close(SessionFactory toClose) { toClose.close(); @@ -47,6 +52,7 @@ public SetupContext withPropertyRadical(String keyRadical, Object value) { return withProperty( SearchOrmSettings.PREFIX + keyRadical, value ); } + @Override public SetupContext withProperty(String key, Object value) { overriddenProperties.put( key, value ); return thisAsC();