|
Hibernate example source code file (LobCreatorTest.java)
The Hibernate LobCreatorTest.java source code/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package org.hibernate.jdbc; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.Writer; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.sql.Blob; import java.sql.Clob; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.NClob; import java.sql.SQLException; import java.util.Properties; import org.hibernate.cfg.Environment; import org.hibernate.engine.jdbc.BlobImplementer; import org.hibernate.engine.jdbc.ClobImplementer; import org.hibernate.engine.jdbc.ContextualLobCreator; import org.hibernate.engine.jdbc.LobCreationContext; import org.hibernate.engine.jdbc.LobCreator; import org.hibernate.engine.jdbc.NClobImplementer; import org.hibernate.engine.jdbc.NonContextualLobCreator; import org.hibernate.engine.jdbc.WrappedBlob; import org.hibernate.engine.jdbc.WrappedClob; import org.hibernate.engine.jdbc.internal.LobCreatorBuilder; import org.junit.Test; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; /** * @author Steve Ebersole */ public class LobCreatorTest extends org.hibernate.testing.junit4.BaseUnitTestCase { @Test public void testConnectedLobCreator() throws SQLException { final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( true ) ); LobCreationContext lobCreationContext = new LobCreationContextImpl( connection ); LobCreator lobCreator = new LobCreatorBuilder( new Properties(), connection ) .buildLobCreator( lobCreationContext ); assertTrue( lobCreator instanceof ContextualLobCreator ); testLobCreation( lobCreator ); connection.close(); } public void testJdbc3LobCreator() throws SQLException { final Connection connection = createConnectionProxy( 3, new JdbcLobBuilderImpl( false) ); LobCreationContext lobCreationContext = new LobCreationContextImpl( connection ); LobCreator lobCreator = new LobCreatorBuilder( new Properties(), connection ) .buildLobCreator( lobCreationContext ); assertSame( NonContextualLobCreator.INSTANCE, lobCreator ); testLobCreation( lobCreator ); connection.close(); } public void testJdbc4UnsupportedLobCreator() throws SQLException { final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( false ) ); LobCreationContext lobCreationContext = new LobCreationContextImpl( connection ); LobCreator lobCreator = new LobCreatorBuilder( new Properties(), connection ) .buildLobCreator( lobCreationContext ); assertSame( NonContextualLobCreator.INSTANCE, lobCreator ); testLobCreation( lobCreator ); connection.close(); } public void testConfiguredNonContextualLobCreator() throws SQLException { final Connection connection = createConnectionProxy( 4, new JdbcLobBuilderImpl( true ) ); LobCreationContext lobCreationContext = new LobCreationContextImpl( connection ); Properties props = new Properties(); props.setProperty( Environment.NON_CONTEXTUAL_LOB_CREATION, "true" ); LobCreator lobCreator = new LobCreatorBuilder( props, connection ) .buildLobCreator( lobCreationContext ); assertSame( NonContextualLobCreator.INSTANCE, lobCreator ); testLobCreation( lobCreator ); connection.close(); } private void testLobCreation(LobCreator lobCreator) throws SQLException{ Blob blob = lobCreator.createBlob( new byte[] {} ); if ( lobCreator == NonContextualLobCreator.INSTANCE ) { assertTrue( blob instanceof BlobImplementer ); } else { assertTrue( blob instanceof JdbcBlob ); } blob = lobCreator.wrap( blob ); assertTrue( blob instanceof WrappedBlob ); Clob clob = lobCreator.createClob( "Hi" ); if ( lobCreator == NonContextualLobCreator.INSTANCE ) { assertTrue( clob instanceof ClobImplementer ); } else { assertTrue( clob instanceof JdbcClob ); } clob = lobCreator.wrap( clob ); assertTrue( clob instanceof WrappedClob ); Clob nclob = lobCreator.createNClob( "Hi" ); if ( lobCreator == NonContextualLobCreator.INSTANCE ) { assertTrue( nclob instanceof NClobImplementer ); } else { assertTrue( nclob instanceof JdbcNClob ); } assertTrue( NClob.class.isInstance( nclob ) ); nclob = lobCreator.wrap( nclob ); assertTrue( nclob instanceof WrappedClob ); blob.free(); clob.free(); nclob.free(); } private class LobCreationContextImpl implements LobCreationContext { private Connection connection; private LobCreationContextImpl(Connection connection) { this.connection = connection; } public <T> T execute(LobCreationContext.Callback Other Hibernate examples (source code examples)Here is a short list of links related to this Hibernate LobCreatorTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.