Skip to content

Commit

Permalink
[#1641] first draft of @array annotation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blafond committed Jan 29, 2024
1 parent 577adb2 commit ffc5efd
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 2 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, "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 @@ -81,6 +81,12 @@ 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 @@ -102,6 +105,31 @@ public class BasicTypesTestEntity {

Duration duration;

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;

Serializable serializable;

public BasicTypesTestEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,54 @@ public void testLocalDateTimeType(VertxTestContext context) {
public void testSerializableType(VertxTestContext context) {
testDatatype( context, "serializable", Serializable.class );
}

@Test
public void testStringArrayType(VertxTestContext context) {
testDatatype( context, "stringArray", String[].class );
}

@Test
public void testStringArrayAnnotatedType(VertxTestContext context) {
testDatatype( context, "stringArrayAnnotated", String[].class );
}

@Test
public void testLongArrayType(VertxTestContext context) {
testDatatype( context, "longArray", Long[].class );
}

@Test
public void testLongArrayAnnotatedType(VertxTestContext context) {
testDatatype( context, "longArrayAnnotated", Long[].class );
}

@Test
public void testBigDecimalArrayType(VertxTestContext context) {
testDatatype( context, "bigDecimalArray", BigDecimal[].class );
}

@Test
public void testBigDecimalArrayAnnotatedType(VertxTestContext context) {
testDatatype( context, "bigDecimalArrayAnnotated", BigDecimal[].class );
}

@Test
public void testBigIntegerArrayType(VertxTestContext context) {
testDatatype( context, "bigIntegerArray", BigInteger[].class );
}

@Test
public void testBigIntegerArrayAnnotatedType(VertxTestContext context) {
testDatatype( context, "bigIntegerArrayAnnotated", BigInteger[].class );
}

@Test
public void testFieldBooleanArrayType(VertxTestContext context) {
testDatatype( context, "fieldBooleanArray", Boolean[].class );
}

@Test
public void testFieldBooleanArrayAnnotatedType(VertxTestContext context) {
testDatatype( context, "fieldBooleanArrayAnnotated", Boolean[].class );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.annotations.DisabledForDbTypes;
import org.hibernate.reactive.provider.Settings;
import org.hibernate.reactive.annotations.DisabledFor;
import org.hibernate.tool.schema.spi.SchemaManagementException;
Expand All @@ -25,7 +26,9 @@
import jakarta.persistence.Table;

import static java.util.concurrent.TimeUnit.MINUTES;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.COCKROACHDB;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2;
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.GROUPED;
import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.INDIVIDUALLY;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -38,7 +41,17 @@
* - TODO: Missing column
* - TODO: Wrong column type
*/
@DisabledFor(value = DB2, reason = "No InformationExtractor for Dialect [org.hibernate.dialect.DB2Dialect..]")
@DisabledForDbTypes({
@DisabledFor(value = DB2, reason = "No InformationExtractor for Dialect [org.hibernate.dialect.DB2Dialect..]"),
@DisabledFor(value = COCKROACHDB,
reason = "<Schema-validation: wrong column type encountered in column [bigDecimalArray] in table [BASIC_TYPES_TABLE]; " +
"found [_numeric (Types#NULL)], but expecting [numeric(38,2) array (Types#ARRAY)]> " +
"but was: <Schema-validation: missing table [EXTRA_TABLE]>"),
@DisabledFor(value = POSTGRESQL,
reason = "<Schema-validation: wrong column type encountered in column [bigDecimalArray] in table [BASIC_TYPES_TABLE]; " +
"found [_numeric (Types#NULL)], but expecting [numeric(38,2) array (Types#ARRAY)]> " +
"but was: <Schema-validation: missing table [EXTRA_TABLE]>")
})
public abstract class SchemaValidationTestBase extends BaseReactiveTest {

public static class IndividuallyStrategyTest extends SchemaValidationTestBase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.UUID;
import java.util.function.Consumer;

import org.hibernate.annotations.Array;
import org.hibernate.reactive.BaseReactiveTest;
import org.hibernate.reactive.annotations.DisabledFor;

Expand Down Expand Up @@ -277,36 +278,68 @@ public void testBigDecimalArrayType(VertxTestContext context) {
} );
}

@Test
public void testArrayWithLengthAnnotationNotChecked(VertxTestContext context) {
Basic basic = new Basic();
// String[] array has a length set to 10 via @Array(length = 10) annotation
// Construct an array larger than 10.
// When a length check is provided via ORM or Reactive, this test should fail.
String[] dataArray = {"s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s10", "s11" };
basic.stringArray = dataArray;

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

@Entity(name = "Basic")
@Table(name = "Basic")
private static class Basic {
@Id
@GeneratedValue
Integer id;
@Array( length = 10 )
String[] stringArray;
@Array( length = 10 )
Boolean[] booleanArray;
@Array( length = 10 )
boolean[] primitiveBooleanArray;
@Array( length = 10 )
Integer[] integerArray;
@Array( length = 10 )
int[] primitiveIntegerArray;
@Array( length = 10 )
Long[] longArray;
@Array( length = 10 )
long[] primitiveLongArray;
@Array( length = 10 )
Float[] floatArray;
@Array( length = 10 )
float[] primitiveFloatArray;
@Array( length = 10 )
Double[] doubleArray;
@Array( length = 10 )
double[] primitiveDoubleArray;
@Array( length = 10 )
UUID[] uuidArray;
@Array( length = 10 )
AnEnum[] enumArray;
@Array( length = 10 )
Short[] shortArray;
@Array( length = 10 )
short[] primitiveShortArray;
@Array( length = 10 )
Date[] dateArray;
@Array( length = 10 )
LocalDate[] localDateArray;
@Array( length = 10 )
LocalTime[] localTimeArray;
@Array( length = 10 )
LocalDateTime[] localDateTimeArray;

@Array( length = 10 )
// We have to specify the length for BigDecimal and BigInteger because
// the default column type when creating the schema is too small on some databases
@Column(length = 5000)
BigInteger[] bigIntegerArray;
@Array( length = 10 )
@Column(length = 5000)
BigDecimal[] bigDecimalArray;
}
Expand Down

0 comments on commit ffc5efd

Please sign in to comment.