From 82c94ec1425311a9d5ba17aa5427840d4edee561 Mon Sep 17 00:00:00 2001 From: Gavin Date: Sun, 8 Jan 2023 14:21:23 +0100 Subject: [PATCH] add BinaryLengthTest to make sure it's possible to have programs with byte[] fields work across Postgres and others --- .../orm/test/length/BinaryLengthTest.java | 32 +++++++++++ .../hibernate/orm/test/length/LengthTest.java | 53 +++++++++++-------- .../orm/test/length/WithLongByteArrays.java | 30 +++++++++++ .../orm/test/length/WithLongStrings.java | 3 ++ 4 files changed, 95 insertions(+), 23 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/length/BinaryLengthTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongByteArrays.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/length/BinaryLengthTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/length/BinaryLengthTest.java new file mode 100644 index 0000000000..72eeaed623 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/length/BinaryLengthTest.java @@ -0,0 +1,32 @@ +package org.hibernate.orm.test.length; + +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +@SessionFactory +@DomainModel(annotatedClasses = WithLongByteArrays.class) +public class BinaryLengthTest { + @Test + public void testLength(SessionFactoryScope scope) { + WithLongByteArrays arrays = new WithLongByteArrays(); + arrays.longish = "hello world ".repeat(2500).getBytes(); + arrays.long16 = "hello world ".repeat(2700).getBytes(); + arrays.long32 = "hello world ".repeat(20000).getBytes(); + // This is an important test that it's possible to handle + // very long binary types in a way that's portable across + // both Postgres (bytea) and other databases (blob) + arrays.lob = "hello world ".repeat(40000).getBytes(); + scope.inTransaction(s -> s.persist(arrays)); + scope.inTransaction(s -> { + WithLongByteArrays arrs = s.find(WithLongByteArrays.class, arrays.id); + assertArrayEquals(arrs.longish, arrays.longish); + assertArrayEquals(arrs.long16, arrays.long16); + assertArrayEquals(arrs.long32, arrays.long32); + assertArrayEquals(arrs.lob, arrays.lob); + }); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/length/LengthTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/length/LengthTest.java index 8b0671869c..d1aba0d93c 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/length/LengthTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/length/LengthTest.java @@ -17,6 +17,36 @@ import static org.junit.Assert.assertEquals; public class LengthTest { @Test public void testLength(SessionFactoryScope scope) { + WithLongStrings strings = new WithLongStrings(); + strings.longish = "hello world ".repeat(2500); + strings.long16 = "hello world ".repeat(2700); + strings.long32 = "hello world ".repeat(20000); + strings.clob = "hello world ".repeat(40000); + scope.inTransaction(s -> s.persist(strings)); + scope.inTransaction(s -> { + WithLongStrings strs = s.find(WithLongStrings.class, strings.id); + assertEquals(strs.longish, strings.longish); + assertEquals(strs.long16, strings.long16); + assertEquals(strs.long32, strings.long32); + assertEquals(strs.clob, strings.clob); + }); + } + + @Test + public void testSqlType(SessionFactoryScope scope) { + WithLongTypeStrings strings = new WithLongTypeStrings(); + strings.longish = "hello world ".repeat(2500); + strings.long32 = "hello world ".repeat(20000); + scope.inTransaction(s -> s.persist(strings)); + scope.inTransaction(s -> { + WithLongTypeStrings strs = s.find(WithLongTypeStrings.class, strings.id); + assertEquals(strs.longish, strings.longish); + assertEquals(strs.long32, strings.long32); + }); + } + + @Test + public void testLong32(SessionFactoryScope scope) { final Dialect dialect = scope.getSessionFactory().getJdbcServices().getDialect(); final BasicValuedMapping mapping = (BasicValuedMapping) scope.getSessionFactory() .getRuntimeMetamodels() @@ -29,28 +59,5 @@ public class LengthTest { else { assertEquals( SqlTypes.VARCHAR, mapping.getJdbcMapping().getJdbcType().getJdbcTypeCode() ); } - WithLongStrings strings = new WithLongStrings(); - strings.longish = "hello world ".repeat(2500); - strings.long16 = "hello world ".repeat(2700); - strings.long32 = "hello world ".repeat(20000); - scope.inTransaction(s->s.persist(strings)); - scope.inTransaction(s-> { - WithLongStrings strs = s.find(WithLongStrings.class, strings.id); - assertEquals(strs.longish, strings.longish); - assertEquals(strs.long16, strings.long16); - assertEquals(strs.long32, strings.long32); - }); - } - @Test - public void testSqlType(SessionFactoryScope scope) { - WithLongTypeStrings strings = new WithLongTypeStrings(); - strings.longish = "hello world ".repeat(2500); - strings.long32 = "hello world ".repeat(20000); - scope.inTransaction(s->s.persist(strings)); - scope.inTransaction(s-> { - WithLongTypeStrings strs = s.find(WithLongTypeStrings.class, strings.id); - assertEquals(strs.longish, strings.longish); - assertEquals(strs.long32, strings.long32); - }); } } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongByteArrays.java b/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongByteArrays.java new file mode 100644 index 0000000000..e31ab5e924 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongByteArrays.java @@ -0,0 +1,30 @@ +package org.hibernate.orm.test.length; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.Id; + +import static org.hibernate.Length.LOB_DEFAULT; +import static org.hibernate.Length.LONG; +import static org.hibernate.Length.LONG16; +import static org.hibernate.Length.LONG32; + +@Entity +public class WithLongByteArrays { + @Id + @GeneratedValue + public int id; + + @Column(length = LONG) + public byte[] longish; + + @Column(length = LONG16) + public byte[] long16; + + @Column(length = LONG32) + public byte[] long32; + + @Column(length = LOB_DEFAULT+1) + public byte[] lob; +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongStrings.java b/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongStrings.java index e9c36c7603..9f48caeec4 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongStrings.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/length/WithLongStrings.java @@ -21,4 +21,7 @@ public class WithLongStrings { @Column(length = LONG32) public String long32; + + @Column(length = LOB_DEFAULT+1) + public String clob; }