Search in sources :

Example 66 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodSetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final Setter setter = new SetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findSetterMethod(AnEntity.class, "pk", PK.class));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(setter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Setter setterClone = (Setter) ois.readObject();
    final PK pkNew = new PK(2L);
    setterClone.set(entity, pkNew, null);
    assertSame(pkNew, getter.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) SetterMethodImpl(org.hibernate.property.access.spi.SetterMethodImpl) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 67 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testProtectedMethodGetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testProtectedMethodGetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterMethodImpl(AnEntity.class, "pk", ReflectHelper.findGetterMethod(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(getter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Getter getterClone = (Getter) ois.readObject();
    assertSame(getter.get(entity), getterClone.get(entity));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) GetterMethodImpl(org.hibernate.property.access.spi.GetterMethodImpl) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 68 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testPrivateFieldSetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldSetter() throws Exception {
    AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final Setter setter = new SetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(setter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Setter setterClone = (Setter) ois.readObject();
    final PK pkNew = new PK(2L);
    setterClone.set(entity, pkNew, null);
    assertSame(pkNew, getter.get(entity));
}
Also used : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) Setter(org.hibernate.property.access.spi.Setter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) SetterFieldImpl(org.hibernate.property.access.spi.SetterFieldImpl) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 69 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class GetterSetterSerializationTest method testPrivateFieldGetter.

@Test
@TestForIssue(jiraKey = "HHH-11202")
public void testPrivateFieldGetter() throws Exception {
    final AnEntity entity = new AnEntity(new PK(1L));
    final Getter getter = new GetterFieldImpl(AnEntity.class, "pk", ReflectHelper.findField(AnEntity.class, "pk"));
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(getter);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final Getter getterClone = (Getter) ois.readObject();
    assertSame(getter.get(entity), getterClone.get(entity));
}
Also used : GetterFieldImpl(org.hibernate.property.access.spi.GetterFieldImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) Getter(org.hibernate.property.access.spi.Getter) PK(org.hibernate.serialization.entity.PK) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AnEntity(org.hibernate.serialization.entity.AnEntity) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 70 with ObjectInputStream

use of java.io.ObjectInputStream in project hibernate-orm by hibernate.

the class FumTest method spoofSerialization.

private Session spoofSerialization(Session session) throws IOException {
    try {
        // Serialize the incoming out to memory
        ByteArrayOutputStream serBaOut = new ByteArrayOutputStream();
        ObjectOutputStream serOut = new ObjectOutputStream(serBaOut);
        serOut.writeObject(session);
        // Now, re-constitute the model from memory
        ByteArrayInputStream serBaIn = new ByteArrayInputStream(serBaOut.toByteArray());
        ObjectInputStream serIn = new ObjectInputStream(serBaIn);
        Session outgoing = (Session) serIn.readObject();
        return outgoing;
    } catch (ClassNotFoundException cnfe) {
        throw new IOException("Unable to locate class on reconstruction");
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Session(org.hibernate.Session)

Aggregations

ObjectInputStream (java.io.ObjectInputStream)1041 ByteArrayInputStream (java.io.ByteArrayInputStream)667 ObjectOutputStream (java.io.ObjectOutputStream)427 ByteArrayOutputStream (java.io.ByteArrayOutputStream)354 IOException (java.io.IOException)341 FileInputStream (java.io.FileInputStream)152 Test (org.junit.Test)128 File (java.io.File)89 InputStream (java.io.InputStream)85 BufferedInputStream (java.io.BufferedInputStream)47 Serializable (java.io.Serializable)40 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)31 FileNotFoundException (java.io.FileNotFoundException)27 FileOutputStream (java.io.FileOutputStream)27 Test (org.testng.annotations.Test)26 Map (java.util.Map)25 EOFException (java.io.EOFException)21 GZIPInputStream (java.util.zip.GZIPInputStream)21 ObjectInput (java.io.ObjectInput)20