HHH-12555 : Update test to check results

This commit is contained in:
Gail Badner 2018-12-16 23:59:07 -08:00 committed by Guillaume Smet
parent 4e05953240
commit ca6dc226eb
1 changed files with 75 additions and 20 deletions

View File

@ -7,10 +7,16 @@
package org.hibernate.test.type; package org.hibernate.test.type;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.sql.Blob; import java.sql.Blob;
import java.sql.Clob; import java.sql.Clob;
import java.sql.NClob; import java.sql.NClob;
import java.sql.SQLException;
import java.util.Arrays;
import javax.persistence.Basic; import javax.persistence.Basic;
import javax.persistence.Column; import javax.persistence.Column;
@ -20,6 +26,7 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Lob; import javax.persistence.Lob;
import org.hibernate.Hibernate;
import org.hibernate.annotations.Cache; import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.testing.DialectChecks; import org.hibernate.testing.DialectChecks;
@ -41,36 +48,84 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase {
} }
@Test @Test
public void testBlob() { public void testBlob() throws SQLException {
doInHibernate( this::sessionFactory, s -> { final int id = doInHibernate( this::sessionFactory, s -> {
FileBlob file = new FileBlob(); FileBlob file = new FileBlob();
file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) ); file.setBlob( s.getLobHelper().createBlob( "TEST CASE".getBytes() ) );
s.save( file ); // merge transient entity
s.clear(); file = (FileBlob) s.merge( file );
s.merge( file ); return file.getId();
} ); } );
doInHibernate( this::sessionFactory, s -> {
FileBlob file = s.get( FileBlob.class, id );
assertFalse( Hibernate.isPropertyInitialized( file, "blob" ) );
Blob blob = file.getBlob();
try {
assertTrue(
Arrays.equals( "TEST CASE".getBytes(), blob.getBytes( 1, (int) file.getBlob().length() ) )
);
}
catch (SQLException ex) {
fail( "could not determine Lob length" );
}
});
} }
@Test @Test
public void testClob() { public void testClob() {
doInHibernate( this::sessionFactory, s -> { final int id = doInHibernate( this::sessionFactory, s -> {
FileClob file = new FileClob(); FileClob file = new FileClob();
file.setClob( s.getLobHelper().createClob( "TEST CASE" ) ); file.setClob( s.getLobHelper().createClob( "TEST CASE" ) );
s.save( file ); // merge transient entity
s.clear(); file = (FileClob) s.merge( file );
s.merge( file ); return file.getId();
} ); } );
doInHibernate( this::sessionFactory, s -> {
FileClob file = s.get( FileClob.class, id );
assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) );
Clob clob = file.getClob();
try {
final char[] chars = new char[(int) file.getClob().length()];
clob.getCharacterStream().read( chars );
assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) );
}
catch (SQLException ex ) {
fail( "could not determine Lob length" );
}
catch (IOException ex) {
fail( "could not read Lob" );
}
});
} }
@Test @Test
public void testNClob() { public void testNClob() {
doInHibernate( this::sessionFactory, s -> { final int id = doInHibernate( this::sessionFactory, s -> {
FileNClob file = new FileNClob(); FileNClob file = new FileNClob();
file.setNClob( s.getLobHelper().createNClob( "TEST CASE" ) ); file.setClob( s.getLobHelper().createNClob( "TEST CASE" ) );
s.save( file ); // merge transient entity
s.clear(); file = (FileNClob) s.merge( file );
s.merge( file ); return file.getId();
} ); });
doInHibernate( this::sessionFactory, s -> {
FileNClob file = s.get( FileNClob.class, id );
assertFalse( Hibernate.isPropertyInitialized( file, "clob" ) );
NClob nClob = file.getClob();
try {
final char[] chars = new char[(int) file.getClob().length()];
nClob.getCharacterStream().read( chars );
assertTrue( Arrays.equals( "TEST CASE".toCharArray(), chars ) );
}
catch (SQLException ex ) {
fail( "could not determine Lob length" );
}
catch (IOException ex) {
fail( "could not read Lob" );
}
});
} }
@Entity(name = "FileBlob") @Entity(name = "FileBlob")
@ -139,7 +194,7 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase {
private int id; private int id;
private NClob nClob; private NClob clob;
@Id @Id
@GeneratedValue @GeneratedValue
@ -154,12 +209,12 @@ public class LobUnfetchedPropertyTest extends BaseCoreFunctionalTestCase {
@Column(name = "filedata", length = 1024 * 1024) @Column(name = "filedata", length = 1024 * 1024)
@Lob @Lob
@Basic(fetch = FetchType.LAZY) @Basic(fetch = FetchType.LAZY)
public NClob getNClob() { public NClob getClob() {
return nClob; return clob;
} }
public void setNClob(NClob nClob) { public void setClob(NClob clob) {
this.nClob = nClob; this.clob = clob;
} }
} }
} }