HHH-6897 test changes to ensure that HHH-6897 doesn't regress

This commit is contained in:
Scott Marlow 2011-12-28 19:16:29 -05:00
parent 2ddaeedd1e
commit 3d517d78f9
3 changed files with 171 additions and 20 deletions

View File

@ -0,0 +1,32 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.hibernate.ejb.test;
/**
* Attempts to serialize this class should cause a NotSerializableException error to be thrown.
*
* @author Scott Marlow
*/
public class NotSerializableClass {
}

View File

@ -0,0 +1,117 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 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.ejb.test.ejb3configuration;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.hibernate.ejb.HibernateEntityManager;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.ejb.test.Cat;
import org.hibernate.ejb.test.Distributor;
import org.hibernate.ejb.test.Item;
import org.hibernate.ejb.test.Kitten;
import org.hibernate.ejb.test.Wallet;
import org.junit.Test;
/**
* @author Emmanuel Bernard
*/
public class EntityManagerFactorySerializationTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void testSerialization() throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream( stream );
out.writeObject( entityManagerFactory() );
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
ObjectInputStream in = new ObjectInputStream( byteIn );
EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
in.close();
byteIn.close();
EntityManager em = serializedFactory.createEntityManager();
//em.getTransaction().begin();
//em.setFlushMode( FlushModeType.NEVER );
Cat cat = new Cat();
cat.setAge( 3 );
cat.setDateOfBirth( new Date() );
cat.setLength( 22 );
cat.setName( "Kitty" );
em.persist( cat );
Item item = new Item();
item.setName( "Train Ticket" );
item.setDescr( "Paris-London" );
em.persist( item );
//em.getTransaction().commit();
//em.getTransaction().begin();
item.setDescr( "Paris-Bruxelles" );
//em.getTransaction().commit();
//fake the in container work
( (HibernateEntityManager) em ).getSession().disconnect();
stream = new ByteArrayOutputStream();
out = new ObjectOutputStream( stream );
out.writeObject( em );
out.close();
serialized = stream.toByteArray();
stream.close();
byteIn = new ByteArrayInputStream( serialized );
in = new ObjectInputStream( byteIn );
em = (EntityManager) in.readObject();
in.close();
byteIn.close();
//fake the in container work
em.getTransaction().begin();
item = em.find( Item.class, item.getName() );
item.setDescr( item.getDescr() + "-Amsterdam" );
cat = (Cat) em.createQuery( "select c from " + Cat.class.getName() + " c" ).getSingleResult();
cat.setLength( 34 );
em.flush();
em.remove( item );
em.remove( cat );
em.flush();
em.getTransaction().commit();
em.close();
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[]{
Item.class,
Distributor.class,
Wallet.class,
Cat.class,
Kitten.class
};
}
}

View File

@ -23,14 +23,14 @@
*/
package org.hibernate.ejb.test.ejb3configuration;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.Date;
import java.util.Map;
import javax.persistence.EntityManager;
import org.hibernate.ejb.HibernateEntityManager;
import org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase;
@ -38,28 +38,18 @@ import org.hibernate.ejb.test.Cat;
import org.hibernate.ejb.test.Distributor;
import org.hibernate.ejb.test.Item;
import org.hibernate.ejb.test.Kitten;
import org.hibernate.ejb.test.NotSerializableClass;
import org.hibernate.ejb.test.Wallet;
import org.junit.Test;
/**
* @author Emmanuel Bernard
* @author Scott Marlow
*/
public class EntityManagerSerializationTest extends BaseEntityManagerFunctionalTestCase {
@Test
public void testSerialization() throws Exception {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream( stream );
out.writeObject( entityManagerFactory() );
out.close();
byte[] serialized = stream.toByteArray();
stream.close();
ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
ObjectInputStream in = new ObjectInputStream( byteIn );
EntityManagerFactory serializedFactory = (EntityManagerFactory) in.readObject();
in.close();
byteIn.close();
EntityManager em = serializedFactory.createEntityManager();
EntityManager em = entityManagerFactory().createEntityManager();
//em.getTransaction().begin();
//em.setFlushMode( FlushModeType.NEVER );
Cat cat = new Cat();
@ -79,14 +69,14 @@ public class EntityManagerSerializationTest extends BaseEntityManagerFunctionalT
//fake the in container work
( (HibernateEntityManager) em ).getSession().disconnect();
stream = new ByteArrayOutputStream();
out = new ObjectOutputStream( stream );
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream( stream );
out.writeObject( em );
out.close();
serialized = stream.toByteArray();
byte[] serialized = stream.toByteArray();
stream.close();
byteIn = new ByteArrayInputStream( serialized );
in = new ObjectInputStream( byteIn );
ByteArrayInputStream byteIn = new ByteArrayInputStream( serialized );
ObjectInputStream in = new ObjectInputStream( byteIn );
em = (EntityManager) in.readObject();
in.close();
byteIn.close();
@ -105,6 +95,18 @@ public class EntityManagerSerializationTest extends BaseEntityManagerFunctionalT
em.close();
}
/**
* Add a non-serializable object to the EMF to ensure that the EM can be serialized even if its EMF is not serializable.
* This will ensure that the fix for HHH-6897 doesn't regress,
* @return
*/
@Override
protected Map getConfig() {
Map result = super.getConfig();
result.put( "org.hibernate.ejb.test.BaseEntityManagerFunctionalTestCase.getConfig_addedNotSerializableObject", new NotSerializableClass());
return result;
}
@Override
public Class[] getAnnotatedClasses() {
return new Class[]{