Search in sources :

Example 1 with NaturalIdCacheKey

use of org.hibernate.cache.internal.NaturalIdCacheKey in project hibernate-orm by hibernate.

the class NaturalIdCacheKeyTest method testSerializationRoundTrip.

@Test
public void testSerializationRoundTrip() throws Exception {
    final EntityPersister entityPersister = mock(EntityPersister.class);
    final SessionImplementor sessionImplementor = mock(SessionImplementor.class);
    final SessionFactoryImplementor sessionFactoryImplementor = mock(SessionFactoryImplementor.class);
    final Type mockType = mock(Type.class);
    when(entityPersister.getRootEntityName()).thenReturn("EntityName");
    when(sessionImplementor.getFactory()).thenReturn(sessionFactoryImplementor);
    when(entityPersister.getNaturalIdentifierProperties()).thenReturn(new int[] { 0, 1, 2 });
    when(entityPersister.getPropertyTypes()).thenReturn(new Type[] { mockType, mockType, mockType });
    when(mockType.getHashCode(anyObject(), eq(sessionFactoryImplementor))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].hashCode();
        }
    });
    when(mockType.disassemble(anyObject(), eq(sessionImplementor), eq(null))).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0];
        }
    });
    final NaturalIdCacheKey key = (NaturalIdCacheKey) DefaultCacheKeysFactory.staticCreateNaturalIdKey(new Object[] { "a", "b", "c" }, entityPersister, sessionImplementor);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(key);
    final ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
    final NaturalIdCacheKey keyClone = (NaturalIdCacheKey) ois.readObject();
    assertEquals(key, keyClone);
    assertEquals(key.hashCode(), keyClone.hashCode());
    assertEquals(key.toString(), keyClone.toString());
    assertEquals(key.getEntityName(), keyClone.getEntityName());
    assertArrayEquals(key.getNaturalIdValues(), keyClone.getNaturalIdValues());
    assertEquals(key.getTenantId(), keyClone.getTenantId());
}
Also used : EntityPersister(org.hibernate.persister.entity.EntityPersister) NaturalIdCacheKey(org.hibernate.cache.internal.NaturalIdCacheKey) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Type(org.hibernate.type.Type) ByteArrayInputStream(java.io.ByteArrayInputStream) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Matchers.anyObject(org.mockito.Matchers.anyObject) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 NaturalIdCacheKey (org.hibernate.cache.internal.NaturalIdCacheKey)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 SessionImplementor (org.hibernate.engine.spi.SessionImplementor)1 EntityPersister (org.hibernate.persister.entity.EntityPersister)1 Type (org.hibernate.type.Type)1 Test (org.junit.Test)1 Matchers.anyObject (org.mockito.Matchers.anyObject)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1