Search in sources :

Example 46 with ObjectOutput

use of java.io.ObjectOutput in project drools by kiegroup.

the class ProcessContextTest method testProcessContextGetAssignment.

@Test
public void testProcessContextGetAssignment() {
    KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = kbase.newKieSession();
    assertNotNull(ksession);
    CaseInformation caseInfo = new CaseInformation();
    caseInfo.assign("owner", new OrganizationalEntity() {

        @Override
        public String getId() {
            return "testUser";
        }

        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
        }

        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        }
    });
    ksession.insert(caseInfo);
    ProcessContext processContext = new ProcessContext(ksession);
    CaseAssignment caseAssignment = processContext.getCaseAssignment();
    assertNotNull(caseAssignment);
    Collection<OrganizationalEntity> forRole = caseAssignment.getAssignments("owner");
    assertNotNull(forRole);
    assertEquals(1, forRole.size());
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) ObjectOutput(java.io.ObjectOutput) IOException(java.io.IOException) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ObjectInput(java.io.ObjectInput) Test(org.junit.Test)

Example 47 with ObjectOutput

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

the class CriteriaCompilingTest method serializeDeserialize.

@SuppressWarnings({ "unchecked" })
private <T> T serializeDeserialize(T object) {
    T serializedObject = null;
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(stream);
        out.writeObject(object);
        out.close();
        byte[] serialized = stream.toByteArray();
        stream.close();
        ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
        ObjectInputStream in = new ObjectInputStream(byteIn);
        serializedObject = (T) in.readObject();
        in.close();
        byteIn.close();
    } catch (Exception e) {
        Assert.fail("Unable to serialize / deserialize the object: " + e.getMessage());
    }
    return serializedObject;
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 48 with ObjectOutput

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

the class EntityManagerTest method testSerializableException.

@Test
public void testSerializableException() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    try {
        Query query = em.createQuery("SELECT p FETCH JOIN p.distributors FROM Item p");
        query.getSingleResult();
    } catch (IllegalArgumentException e) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        ObjectOutput out = new ObjectOutputStream(stream);
        out.writeObject(e);
        out.close();
        byte[] serialized = stream.toByteArray();
        stream.close();
        ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
        ObjectInputStream in = new ObjectInputStream(byteIn);
        IllegalArgumentException deserializedException = (IllegalArgumentException) in.readObject();
        in.close();
        byteIn.close();
        assertNull(deserializedException.getCause().getCause());
        assertNull(e.getCause().getCause());
    }
    em.getTransaction().rollback();
    em.close();
    Exception e = new HibernateException("Exception", new NullPointerException("NPE"));
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    ObjectOutput out = new ObjectOutputStream(stream);
    out.writeObject(e);
    out.close();
    byte[] serialized = stream.toByteArray();
    stream.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(serialized);
    ObjectInputStream in = new ObjectInputStream(byteIn);
    HibernateException deserializedException = (HibernateException) in.readObject();
    in.close();
    byteIn.close();
    assertNotNull("Arbitrary exceptions nullified", deserializedException.getCause());
    assertNotNull(e.getCause());
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) HibernateException(org.hibernate.HibernateException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException) PersistenceException(javax.persistence.PersistenceException) HibernateException(org.hibernate.HibernateException) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 49 with ObjectOutput

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

the class EntityManagerFactorySerializationTest method testEntityManagerFactorySerialization.

@Test
public void testEntityManagerFactorySerialization() throws Exception {
    EntityManagerFactory entityManagerFactory = entityManagerFactory();
    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 entityManagerFactory2 = (EntityManagerFactory) in.readObject();
    in.close();
    byteIn.close();
    assertTrue("deserialized EntityManagerFactory should be the same original EntityManagerFactory instance", entityManagerFactory2 == entityManagerFactory);
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityManagerFactory(javax.persistence.EntityManagerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 50 with ObjectOutput

use of java.io.ObjectOutput in project MusicDNA by harjot-oberai.

the class Lyrics method toBytes.

public byte[] toBytes() throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ObjectOutput out = new ObjectOutputStream(bos);
        out.writeObject(this);
        out.close();
    } finally {
        bos.close();
    }
    return bos.toByteArray();
}
Also used : ObjectOutput(java.io.ObjectOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

ObjectOutput (java.io.ObjectOutput)77 ObjectOutputStream (java.io.ObjectOutputStream)47 ByteArrayOutputStream (java.io.ByteArrayOutputStream)46 IOException (java.io.IOException)33 ObjectInput (java.io.ObjectInput)25 Test (org.junit.Test)20 ObjectInputStream (java.io.ObjectInputStream)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 WorkingMemory (org.drools.core.WorkingMemory)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)12 Pattern (org.drools.core.rule.Pattern)12 Consequence (org.drools.core.spi.Consequence)12 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)12 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)8 Declaration (org.drools.core.rule.Declaration)8 IntrospectionException (java.beans.IntrospectionException)7 InvalidRuleException (org.drools.core.rule.InvalidRuleException)7 ConsequenceException (org.drools.core.spi.ConsequenceException)7 OutputStream (java.io.OutputStream)6 ClassObjectType (org.drools.core.base.ClassObjectType)6