Skip to content

Commit

Permalink
HSEARCH-3429 Allow to use configuration files (instead of just a mock…
Browse files Browse the repository at this point in the history
… backend) in mapping setup helpers in integration tests

Will be useful to add more tests to the documentation.
  • Loading branch information
yrodiere committed Dec 12, 2018
1 parent b617328 commit a3e8e3c
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 75 deletions.
Expand Up @@ -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
Expand All @@ -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<TckBackendFeatures> featuresIterator = ServiceLoader.load( TckBackendFeatures.class ).iterator();
if ( featuresIterator.hasNext() ) {
this.backendFeatures = featuresIterator.next();
Expand All @@ -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<String, Object> 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" );
}
}
Expand Up @@ -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;
Expand All @@ -31,35 +32,7 @@ public class SearchSetupHelper implements TestRule {

private final List<SearchIntegration> 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<RuntimeException> 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 );
Expand All @@ -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
Expand All @@ -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<RuntimeException> closer = new Closer<>() ) {
try {
base.evaluate();
}
finally {
closer.pushAll( SearchIntegration::close, mappingRepositories );
mappingRepositories.clear();
}
}
}
};
}

public class SetupContext {

private final ConfigurationPropertySource propertySource;
Expand Down
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
@@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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<String, Object> 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<String, Object> 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;
}
}
Expand Up @@ -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;
Expand All @@ -21,27 +23,40 @@ public abstract class MappingSetupHelper<C extends MappingSetupHelper<C, B, R>.A

private final List<R> 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<Exception> closer = new Closer<>() ) {
testHelper = TestHelper.create( description );
try {
base.evaluate();
}
Expand All @@ -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<String, Object> 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.
Expand Down
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit a3e8e3c

Please sign in to comment.