Skip to content

Commit

Permalink
[#929] adapted tests for H2 dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Apr 4, 2022
1 parent 60d5f86 commit 0c63496
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;

import io.vertx.sqlclient.SqlConnectOptions;
import org.assertj.core.api.Assertions;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

/**
* Test the default port is set correctly when using {@link DefaultSqlClientPoolConfiguration}
*/
public class DefaultPortTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );

@Test
public void testDefaultPortIsSet() throws URISyntaxException {
DefaultSqlClientPoolConfiguration configuration = new DefaultSqlClientPoolConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@
import java.util.Collection;
import java.util.List;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;

public class FormulaTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( MARIA, MYSQL );
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( MARIA, MYSQL, H2 );

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
import org.hibernate.HibernateException;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.exception.ConstraintViolationException;
import org.hibernate.reactive.mutiny.Mutiny;

import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

public class MutinyExceptionsTest extends BaseReactiveTest {

@Override
Expand Down Expand Up @@ -48,7 +52,12 @@ public void testDuplicateKeyException(TestContext context) {
.onItem().call( Mutiny.Session::flush )
.onItem().invoke( ignore -> context.fail( "Expected exception not thrown" ) )
.onFailure().recoverWithItem( err -> {
context.assertEquals( getExpectedException(), err.getClass() );
if( dbType() == DatabaseConfiguration.DBType.H2 ) {
context.assertTrue( ConstraintViolationException.class == err.getClass() ||
getExpectedException() == err.getClass() );
} else {
context.assertEquals( getExpectedException(), err.getClass() );
}
return null;
} )
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.vertx.ext.unit.TestContext;

import static java.util.Arrays.asList;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MYSQL;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
Expand Down Expand Up @@ -105,7 +106,7 @@ public String toString() {
public static class ForOtherDbsTest extends UUIDAsBinaryType {

@Rule // Select a UUID field doesn't work with Oracle
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA, ORACLE );
public DatabaseSelectionRule rule = skipTestsFor( MYSQL, MARIA, ORACLE, H2 );

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
import org.hibernate.dialect.SQLServer2012Dialect;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;

public class UriConfigTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );

@Override
protected Configuration constructConfiguration() {
Class<? extends Dialect> dialect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.containers.DatabaseConfiguration;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.ext.unit.TestContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;

/**
* Check that the right exception is thrown when there is an error with the credentials.
Expand All @@ -28,6 +31,9 @@
*/
public class WrongCredentialsTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( H2 );

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
import org.hibernate.HibernateError;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPool;
import org.hibernate.reactive.pool.impl.DefaultSqlClientPoolConfiguration;
import org.hibernate.reactive.testing.DatabaseSelectionRule;

import org.junit.Rule;
import org.junit.Test;

import io.vertx.sqlclient.SqlConnectOptions;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.createJdbcUrl;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.junit.Assert.assertThrows;
Expand All @@ -33,6 +36,9 @@ public class JdbcUrlParserTest {

private static final String DEFAULT_DB = "hreactDB";

@Rule
public DatabaseSelectionRule rule = DatabaseSelectionRule.skipTestsFor( H2 );

@Test
public void exceptionWhenNull() {
final HibernateError error = assertThrows( HibernateError.class, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public enum DBType {
POSTGRESQL( PostgreSQLDatabase.INSTANCE, 5432, "POSTGRES", "PG" ),
COCKROACHDB( CockroachDBDatabase.INSTANCE, 26257, "COCKROACH" ),
SQLSERVER( MSSQLServerDatabase.INSTANCE, 1433, "MSSQL", "MSSQLSERVER" ),
ORACLE( OracleDatabase.INSTANCE, 1521 );
ORACLE( OracleDatabase.INSTANCE, 1521 ),
H2( H2Database.INSTANCE, -1 );

private final TestableDatabase configuration;
private final int defaultPort;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Hibernate, Relational Persistence for Idiomatic Java
*
* SPDX-License-Identifier: Apache-2.0
* Copyright: Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.reactive.containers;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.UUID;

import org.hibernate.type.NumericBooleanType;
import org.hibernate.type.TextType;
import org.hibernate.type.TrueFalseType;
import org.hibernate.type.YesNoType;
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;

public class H2Database implements TestableDatabase {
public static H2Database INSTANCE = new H2Database();

private static Map<Class<?>, String> expectedDBTypeForClass = new HashMap<>();

static {
{
expectedDBTypeForClass.put( boolean.class, "BOOLEAN" );
expectedDBTypeForClass.put( Boolean.class, "BOOLEAN" );
expectedDBTypeForClass.put( NumericBooleanType.class, "INTEGER" );
expectedDBTypeForClass.put( TrueFalseType.class, "CHARACTER" );
expectedDBTypeForClass.put( YesNoType.class, "CHARACTER" );
expectedDBTypeForClass.put( int.class, "INTEGER" );
expectedDBTypeForClass.put( Integer.class, "INTEGER" );
expectedDBTypeForClass.put( long.class, "BIGINT" );
expectedDBTypeForClass.put( Long.class, "BIGINT" );
expectedDBTypeForClass.put( float.class, "DOUBLE PRECISION" );
expectedDBTypeForClass.put( Float.class, "DOUBLE PRECISION" );
expectedDBTypeForClass.put( double.class, "DOUBLE PRECISION" );
expectedDBTypeForClass.put( Double.class, "DOUBLE PRECISION" );
expectedDBTypeForClass.put( byte.class, "TINYINT" );
expectedDBTypeForClass.put( Byte.class, "TINYINT" );
expectedDBTypeForClass.put( PrimitiveByteArrayTypeDescriptor.class, "BINARY VARYING" );
expectedDBTypeForClass.put( URL.class, "VARCHAR_IGNORECASE" );
expectedDBTypeForClass.put( TimeZone.class, "VARCHAR_IGNORECASE" );
expectedDBTypeForClass.put( Date.class, "DATE" );
expectedDBTypeForClass.put( Timestamp.class, "TIMESTAMP" );
expectedDBTypeForClass.put( Time.class, "TIME" );
expectedDBTypeForClass.put( LocalDate.class, "DATE" );
expectedDBTypeForClass.put( LocalTime.class, "time" );
expectedDBTypeForClass.put( LocalDateTime.class, "TIMESTAMP" );
expectedDBTypeForClass.put( BigInteger.class, "NUMERIC" );
expectedDBTypeForClass.put( BigDecimal.class, "NUMERIC" );
expectedDBTypeForClass.put( Serializable.class, "BINARY VARYING" );
expectedDBTypeForClass.put( UUID.class, "binary" );
expectedDBTypeForClass.put( Instant.class, "datetime" );
expectedDBTypeForClass.put( Duration.class, "bigint" );
expectedDBTypeForClass.put( Character.class, "VARCHAR_IGNORECASE" );
expectedDBTypeForClass.put( char.class, "VARCHAR_IGNORECASE" );
expectedDBTypeForClass.put( TextType.class, "text" );
expectedDBTypeForClass.put( String.class, "VARCHAR_IGNORECASE" );
}
}

private String getRegularJdbcUrl() {
return "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";
}

@Override
public String getJdbcUrl() {
return "jdbc:h2:~/test;DATABASE_TO_UPPER=FALSE";
}

@Override
public String getUri() {
return "h2:~/test;DATABASE_TO_UPPER=FALSE";
}


@Override
public String getScheme() {
return "h2:";
}

@Override
public String getNativeDatatypeQuery(String tableName, String columnName) {
return "SELECT DATA_TYPE FROM INFORMATION_SCHEMA.COLUMNS COLS " +
"WHERE COLS.TABLE_NAME = '" + tableName + "'" +
"AND COLS.COLUMN_NAME= '" + columnName + "'";
}

@Override
public String getExpectedNativeDatatype(Class<?> dataType) {
return expectedDBTypeForClass.get( dataType );
}

@Override
public String createJdbcUrl(String host, int port, String database, Map<String, String> params) {
// Primary mode for H2 is embedded which uses the URL format: "jdbc:h2:~/test"
// H2 can also be configured as a remote server.
// EXAMPLE 1: jdbc:h2:tcp://localhost/D:/myproject/data/project-name
// EXAMPLE 2: jdbc:h2:tcp://localhost/~/test
// EXAMpLE 3: jdbc:h2:tcp://localhost:9081/~/test
final StringBuilder paramsBuilder = new StringBuilder();
if ( params != null && !params.isEmpty() ) {
params.forEach( (key, value) -> {
paramsBuilder.append( jdbcParamDelimiter() );
paramsBuilder.append( key );
paramsBuilder.append( "=" );
paramsBuilder.append( value );
} );
}
String url = "jdbc:" + getScheme() + "//" + host;
if ( port > -1 ) {
url += ":" + port;
}
if ( paramsBuilder.length() > 0 ) {
url += jdbcStartQuery() + paramsBuilder.substring( 1 );
}
if ( database != null ) {
return url + ";database=" + database;
}
return url;
}

@Override
public String jdbcStartQuery() {
return ";";
}

@Override
public String jdbcParamDelimiter() {
return ";";
}

private H2Database() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.GROUPED;
Expand Down Expand Up @@ -53,7 +54,7 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
}

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( DB2 );
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( DB2, H2 );

protected Configuration constructConfiguration(String action) {
Configuration configuration = super.constructConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.persistence.Table;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.GROUPED;
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.INDIVIDUALLY;

Expand Down Expand Up @@ -59,7 +60,7 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
}

@Rule
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( DB2 );
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.skipTestsFor( DB2, H2 );

protected Configuration constructConfiguration(String action) {
Configuration configuration = super.constructConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.vertx.ext.unit.TestContext;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;

Expand All @@ -34,7 +35,7 @@
public class JsonTypeTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule selectionRule = DatabaseSelectionRule.skipTestsFor( DB2, SQLSERVER, ORACLE);
public DatabaseSelectionRule selectionRule = DatabaseSelectionRule.skipTestsFor( DB2, SQLSERVER, ORACLE, H2);

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.function.Consumer;

import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.H2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.MARIA;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.SQLSERVER;
Expand All @@ -35,7 +36,7 @@
public class StringToJsonTypeTest extends BaseReactiveTest {

@Rule
public DatabaseSelectionRule selectionRule = DatabaseSelectionRule.skipTestsFor( DB2, SQLSERVER, MARIA, ORACLE );
public DatabaseSelectionRule selectionRule = DatabaseSelectionRule.skipTestsFor( DB2, SQLSERVER, MARIA, ORACLE, H2 );

@Override
protected Collection<Class<?>> annotatedEntities() {
Expand Down
Loading

0 comments on commit 0c63496

Please sign in to comment.