|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.jpa.test.serialization; |
| 8 | + |
| 9 | +import java.io.ByteArrayInputStream; |
| 10 | +import java.io.ByteArrayOutputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.ObjectInputStream; |
| 13 | +import java.io.ObjectOutputStream; |
| 14 | +import javax.persistence.Column; |
| 15 | +import javax.persistence.Entity; |
| 16 | +import javax.persistence.EntityManager; |
| 17 | +import javax.persistence.GeneratedValue; |
| 18 | +import javax.persistence.Id; |
| 19 | +import javax.persistence.PersistenceException; |
| 20 | +import javax.persistence.Table; |
| 21 | + |
| 22 | +import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; |
| 23 | + |
| 24 | +import org.hibernate.testing.TestForIssue; |
| 25 | +import org.junit.Test; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Andrea Boriero |
| 29 | + */ |
| 30 | +public class EntityManagerDeserializationTest extends BaseEntityManagerFunctionalTestCase { |
| 31 | + |
| 32 | + @Override |
| 33 | + protected Class<?>[] getAnnotatedClasses() { |
| 34 | + return new Class[] {TestEntity.class}; |
| 35 | + } |
| 36 | + |
| 37 | + @Test(expected = PersistenceException.class) |
| 38 | + @TestForIssue(jiraKey = "HHH-11555") |
| 39 | + public void deserializedEntityManagerPersistenceExceptionManagementTest() throws IOException { |
| 40 | + EntityManager entityManager = getOrCreateEntityManager(); |
| 41 | + final EntityManager deserializedSession = spoofSerialization( entityManager ); |
| 42 | + try { |
| 43 | + deserializedSession.getTransaction().begin(); |
| 44 | + TestEntity entity = new TestEntity(); |
| 45 | + entity.setName( "Andrea" ); |
| 46 | + deserializedSession.persist( entity ); |
| 47 | + entity.setName( null ); |
| 48 | + deserializedSession.flush(); |
| 49 | + } |
| 50 | + finally { |
| 51 | + deserializedSession.getTransaction().rollback(); |
| 52 | + deserializedSession.close(); |
| 53 | + entityManager.close(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private EntityManager spoofSerialization(EntityManager entityManager) throws IOException { |
| 58 | + try { |
| 59 | + // Serialize the incoming out to memory |
| 60 | + ByteArrayOutputStream serBaOut = new ByteArrayOutputStream(); |
| 61 | + ObjectOutputStream serOut = new ObjectOutputStream( serBaOut ); |
| 62 | + |
| 63 | + serOut.writeObject( entityManager ); |
| 64 | + |
| 65 | + // Now, re-constitute the model from memory |
| 66 | + ByteArrayInputStream serBaIn = |
| 67 | + new ByteArrayInputStream( serBaOut.toByteArray() ); |
| 68 | + ObjectInputStream serIn = new ObjectInputStream( serBaIn ); |
| 69 | + |
| 70 | + EntityManager outgoing = (EntityManager) serIn.readObject(); |
| 71 | + |
| 72 | + return outgoing; |
| 73 | + } |
| 74 | + catch (ClassNotFoundException cnfe) { |
| 75 | + throw new IOException( "Unable to locate class on reconstruction" ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Entity |
| 80 | + @Table(name = "TEST_ENTITY") |
| 81 | + public static class TestEntity { |
| 82 | + @Id |
| 83 | + @GeneratedValue |
| 84 | + private Long id; |
| 85 | + |
| 86 | + @Column(nullable = false) |
| 87 | + String name; |
| 88 | + |
| 89 | + public Long getId() { |
| 90 | + return id; |
| 91 | + } |
| 92 | + |
| 93 | + public void setId(Long id) { |
| 94 | + this.id = id; |
| 95 | + } |
| 96 | + |
| 97 | + public String getName() { |
| 98 | + return name; |
| 99 | + } |
| 100 | + |
| 101 | + public void setName(String name) { |
| 102 | + this.name = name; |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments