/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2007-2011, 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.test.sql.hand.custom;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import org.hibernate.test.sql.hand.Employment;
import org.hibernate.test.sql.hand.Organization;
import org.hibernate.test.sql.hand.Person;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Abstract test case defining tests of stored procedure support.
*
* @author Gail Badner
*/
@SuppressWarnings( {"UnusedDeclaration"})
public abstract class CustomStoredProcTestSupport extends CustomSQLTestSupport {
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testScalarStoredProcedure() throws HibernateException, SQLException {
Session s = openSession();
Query namedQuery = s.getNamedQuery( "simpleScalar" );
namedQuery.setLong( "number", 43 );
List list = namedQuery.list();
Object o[] = ( Object[] ) list.get( 0 );
assertEquals( o[0], "getAll" );
assertEquals( o[1], Long.valueOf( 43 ) );
s.close();
}
@Test
@SuppressWarnings( {"UnnecessaryBoxing"})
public void testParameterHandling() throws HibernateException, SQLException {
Session s = openSession();
Query namedQuery = s.getNamedQuery( "paramhandling" );
namedQuery.setLong( 0, 10 );
namedQuery.setLong( 1, 20 );
List list = namedQuery.list();
Object[] o = ( Object[] ) list.get( 0 );
assertEquals( o[0], Long.valueOf( 10 ) );
assertEquals( o[1], Long.valueOf( 20 ) );
namedQuery = s.getNamedQuery( "paramhandling_mixed" );
namedQuery.setLong( 0, 10 );
namedQuery.setLong( "second", 20 );
list = namedQuery.list();
o = ( Object[] ) list.get( 0 );
assertEquals( o[0], Long.valueOf( 10 ) );
assertEquals( o[1], Long.valueOf( 20 ) );
s.close();
}
@Test
public void testEntityStoredProcedure() throws HibernateException, SQLException {
Session s = openSession();
Transaction t = s.beginTransaction();
Organization ifa = new Organization( "IFA" );
Organization jboss = new Organization( "JBoss" );
Person gavin = new Person( "Gavin" );
Employment emp = new Employment( gavin, jboss, "AU" );
s.persist( ifa );
s.persist( jboss );
s.persist( gavin );
s.persist( emp );
Query namedQuery = s.getNamedQuery( "selectAllEmployments" );
List list = namedQuery.list();
assertTrue( list.get( 0 ) instanceof Employment );
s.delete( emp );
s.delete( ifa );
s.delete( jboss );
s.delete( gavin );
t.commit();
s.close();
}
}