Skip to content

Commit

Permalink
[#1641] tests for @array annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond authored and DavideD committed Feb 8, 2024
1 parent 9e56fe6 commit 934ceb7
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class DB2Database implements TestableDatabase {
expectedDBTypeForClass.put( Character.class, "CHARACTER" );
expectedDBTypeForClass.put( char.class, "CHARACTER" );
expectedDBTypeForClass.put( String.class, "VARCHAR" );
expectedDBTypeForClass.put( String[].class, "VARBINARY" );
expectedDBTypeForClass.put( Long[].class, "VARBINARY" );
expectedDBTypeForClass.put( BigDecimal[].class, "VARBINARY" );
expectedDBTypeForClass.put( BigInteger[].class, "VARBINARY" );
expectedDBTypeForClass.put( Boolean[].class, "VARBINARY" );
}}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class MSSQLServerDatabase implements TestableDatabase {
expectedDBTypeForClass.put( Character.class, "char" );
expectedDBTypeForClass.put( char.class, "char" );
expectedDBTypeForClass.put( String.class, "varchar" );
expectedDBTypeForClass.put( String[].class, "varbinary" );
expectedDBTypeForClass.put( Long[].class, "varbinary" );
expectedDBTypeForClass.put( BigDecimal[].class, "varbinary" );
expectedDBTypeForClass.put( BigInteger[].class, "varbinary" );
expectedDBTypeForClass.put( Boolean[].class, "varbinary" );
}}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class MySQLDatabase implements TestableDatabase {
expectedDBTypeForClass.put( Character.class, "char" );
expectedDBTypeForClass.put( char.class, "char" );
expectedDBTypeForClass.put( String.class, "varchar" );
expectedDBTypeForClass.put( String[].class, "varchar" );
expectedDBTypeForClass.put( Long[].class, "varbinary" );
expectedDBTypeForClass.put( BigDecimal[].class, "varbinary" );
expectedDBTypeForClass.put( BigInteger[].class, "varbinary" );
expectedDBTypeForClass.put( Boolean[].class, "varbinary" );
}};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ class OracleDatabase implements TestableDatabase {
expectedDBTypeForClass.put( Character.class, "CHAR" );
expectedDBTypeForClass.put( char.class, "CHAR" );
expectedDBTypeForClass.put( String.class, "VARCHAR2" );
expectedDBTypeForClass.put( String[].class, "STRINGARRAY" );
expectedDBTypeForClass.put( Long[].class, "LONGARRAY" );
expectedDBTypeForClass.put( BigDecimal[].class, "BIGDECIMALARRAY" );
expectedDBTypeForClass.put( BigInteger[].class, "BIGINTEGERARRAY" );
expectedDBTypeForClass.put( Boolean[].class, "BOOLEANARRAY" );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ class PostgreSQLDatabase implements TestableDatabase {
expectedDBTypeForClass.put( Character.class, "character" );
expectedDBTypeForClass.put( char.class, "character" );
expectedDBTypeForClass.put( String.class, "character varying" );
expectedDBTypeForClass.put( String[].class, "ARRAY" );
expectedDBTypeForClass.put( Long[].class, "ARRAY" );
expectedDBTypeForClass.put( BigDecimal[].class, "ARRAY" );
expectedDBTypeForClass.put( BigInteger[].class, "ARRAY" );
expectedDBTypeForClass.put( Boolean[].class, "ARRAY" );
}}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

import org.hibernate.annotations.Array;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -104,6 +107,31 @@ public class BasicTypesTestEntity {

Serializable serializable;

String[] stringArray;

@Array(length = 5)
String[] stringArrayAnnotated;

Long[] longArray;

@Array(length = 5)
Long[] longArrayAnnotated;

BigDecimal[] bigDecimalArray;

@Array(length = 5)
BigDecimal[] bigDecimalArrayAnnotated;

BigInteger[] bigIntegerArray;

@Array(length = 5)
BigInteger[] bigIntegerArrayAnnotated;

Boolean[] fieldBooleanArray;

@Array(length = 5)
Boolean[] fieldBooleanArrayAnnotated;

public BasicTypesTestEntity() {
}
public BasicTypesTestEntity(String name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@
import java.util.UUID;
import java.util.function.Consumer;

import org.hibernate.annotations.Array;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.annotations.DisabledFor;
import org.hibernate.reactive.testing.SqlStatementTracker;

import org.junit.jupiter.api.Test;

Expand All @@ -29,19 +33,51 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.assertj.core.api.Condition;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.ORACLE;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
import static org.hibernate.reactive.containers.DatabaseConfiguration.dbType;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@Timeout(value = 10, timeUnit = MINUTES)
@DisabledFor( value = ORACLE, reason = "Vert.x does not support arrays for Oracle" )
@DisabledFor(value = ORACLE, reason = "Vert.x does not support arrays for Oracle")
public class JavaTypesArrayTest extends BaseReactiveTest {

private static SqlStatementTracker sqlTracker;

private final static Condition<String> IS_PG_CREATE_TABLE_QUERY = new Condition<>(
s -> s.toLowerCase().startsWith( "create table" ) && s.contains( "stringArrayWithArrayAnnotation varchar(255) array[5]," ),
"generated query for PostgreSQL `create table...`"
);

private final static Condition<String> IS_PG_CREATE_TABLE_NO_ARRAY_ANNOTATION_QUERY = new Condition<>(
s -> s.toLowerCase().startsWith( "create table" ) && s.contains( "stringArray varchar(255) array," ),
"generated query for PostgreSQL `create table...`"
);

@Override
protected Configuration constructConfiguration() {
Configuration configuration = super.constructConfiguration();
sqlTracker = new SqlStatementTracker( JavaTypesArrayTest::filterCreateTable, configuration.getProperties() );
return configuration;
}

@Override
protected void addServices(StandardServiceRegistryBuilder builder) {
sqlTracker.registerService( builder );
}

private static boolean filterCreateTable(String s) {
return s.toLowerCase().startsWith( "create table" );
}

@Override
protected Set<Class<?>> annotatedEntities() {
return Set.of( Basic.class );
Expand All @@ -64,12 +100,59 @@ private void testField(
@Test
public void testStringArrayType(VertxTestContext context) {
Basic basic = new Basic();
String[] dataArray = {"Hello world!", "Hello earth"};
String[] dataArray = { "Hello world!", "Hello earth" };
basic.stringArray = dataArray;

testField( context, basic, found -> assertArrayEquals( dataArray, found.stringArray ) );
testField( context, basic, found -> {
assertArrayEquals( dataArray, found.stringArray );
// PostgreSQL is the only DB that changes it's `create table...` statement to include array information
// This test checks that the logged query is correct and contains "array[100]"
if ( dbType() == POSTGRESQL ) {
assertThat( sqlTracker.getLoggedQueries() ).have( IS_PG_CREATE_TABLE_NO_ARRAY_ANNOTATION_QUERY );
}
} );
}

@Test
public void testStringArrayTypeWithArrayAnnotation(VertxTestContext context) {
Basic basic = new Basic();
String[] dataArray = {"Hello world!", "Hello earth"};
basic.stringArrayWithArrayAnnotation = dataArray;

testField( context, basic, found -> {
assertArrayEquals( dataArray, found.stringArrayWithArrayAnnotation );
// PostgreSQL is the only DB that changes it's `create table...` statement to include array information
// This test checks that the logged query is correct and contains "array[100]"
if ( dbType() == POSTGRESQL ) {
assertThat( sqlTracker.getLoggedQueries() ).have( IS_PG_CREATE_TABLE_QUERY );
}
} );
}

@Test
public void testStringArrayTypeWithColumnAnnotation(VertxTestContext context) {
Basic basic = new Basic();
String[] dataArray = { "Hello world!", "Hello earth" };
basic.stringArrayWithColumnAnnotation = dataArray;

testField( context, basic, found -> {
assertArrayEquals( dataArray, found.stringArrayWithColumnAnnotation );
} );
}

@Test
public void testStringArrayTypeWithBothAnnotations(VertxTestContext context) {
Basic basic = new Basic();
String[] dataArray = { "Hello world!", "Hello earth" };
basic.stringArrayWithBothAnnotations = dataArray;

testField( context, basic, found -> {
assertArrayEquals( dataArray, found.stringArrayWithBothAnnotations );
} );
}



@Test
public void testBooleanArrayType(VertxTestContext context) {
Basic basic = new Basic();
Expand Down Expand Up @@ -277,13 +360,33 @@ public void testBigDecimalArrayType(VertxTestContext context) {
} );
}

@Test
public void testBigDecimalArrayTypeWithArrayAnnotation(VertxTestContext context) {
Basic basic = new Basic();
BigDecimal[] dataArray = {BigDecimal.valueOf( 123384967L ), BigDecimal.ZERO};
basic.bigDecimalArrayWithArrayAnnotation = dataArray;

testField( context, basic, found -> {
assertEquals( dataArray.length, found.bigDecimalArrayWithArrayAnnotation.length );
assertEquals( dataArray[0].compareTo( found.bigDecimalArrayWithArrayAnnotation[0] ), 0 );
assertEquals( dataArray[1].compareTo( found.bigDecimalArrayWithArrayAnnotation[1] ), 0 );
} );
}

@Entity(name = "Basic")
@Table(name = "Basic")
private static class Basic {
@Id
@GeneratedValue
Integer id;
String[] stringArray;
@Array(length = 5)
String[] stringArrayWithArrayAnnotation;
@Column(length = 255)
String[] stringArrayWithColumnAnnotation;
@Array(length = 5)
@Column(length = 255)
String[] stringArrayWithBothAnnotations;
Boolean[] booleanArray;
boolean[] primitiveBooleanArray;
Integer[] integerArray;
Expand All @@ -309,6 +412,9 @@ private static class Basic {
BigInteger[] bigIntegerArray;
@Column(length = 5000)
BigDecimal[] bigDecimalArray;
@Array(length = 5)
@Column(length = 5000)
BigDecimal[] bigDecimalArrayWithArrayAnnotation;
}

enum AnEnum {FIRST, SECOND, THIRD, FOURTH}
Expand Down

0 comments on commit 934ceb7

Please sign in to comment.