<commit_before>package com.vladmihalcea.book.hpjp.hibernate.equality;

import com.vladmihalcea.book.hpjp.hibernate.identifier.Identifiable;
import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.hibernate.Session;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author <NAME>
 */
public abstract class AbstractEqualityCheckTest<T extends Identifiable<? extends Serializable>> extends AbstractTest {

    protected void assertEqualityConsistency(Class<T> clazz, T entity) {

        Set<T> tuples = new HashSet<>();

        assertFalse(tuples.contains(entity));
        tuples.add(entity);
        assertTrue(tuples.contains(entity));

        doInJPA(entityManager -> {
            entityManager.persist(entity);
            entityManager.flush();
            assertTrue("The entity is not found in the Set after it's persisted.", tuples.contains(entity));
        });

        assertTrue(tuples.contains(entity));

        doInJPA(entityManager -> {
            T entityProxy = entityManager.getReference(clazz, entity.getId());
            assertTrue(entityProxy.equals(entity));
        });

        doInJPA(entityManager -> {
            T entityProxy = entityManager.getReference(clazz, entity.getId());
            assertTrue(entity.equals(entityProxy));
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.merge(entity);
            assertTrue("The entity is not found in the Set after it's merged.", tuples.contains(_entity));
        });

        doInJPA(entityManager -> {
            entityManager.unwrap(Session.class).update(entity);
            assertTrue("The entity is not found in the Set after it's reattached.", tuples.contains(entity));
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.find(clazz, entity.getId());
            assertTrue("The entity is not found in the Set after it's loaded in a subsequent Persistence Context.", tuples.contains(_entity));
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.getReference(clazz, entity.getId());
            assertTrue("The entity is not in the Set found after it's loaded as a Proxy in an other Persistence Context.", tuples.contains(_entity));
        });

        T deletedEntity = doInJPA(entityManager -> {
            T _entity = entityManager.getReference(clazz, entity.getId());
            entityManager.remove(_entity);
            return _entity;
        });

        assertTrue("The entity is found in not the Set even after it's deleted.", tuples.contains(deletedEntity));
    }
}
<commit_msg>Code formatting<commit_after>package com.vladmihalcea.book.hpjp.hibernate.equality;

import com.vladmihalcea.book.hpjp.hibernate.identifier.Identifiable;
import com.vladmihalcea.book.hpjp.util.AbstractTest;
import org.hibernate.Session;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * @author <NAME>
 */
public abstract class AbstractEqualityCheckTest<T extends Identifiable<? extends Serializable>> extends AbstractTest {

    protected void assertEqualityConsistency(
            Class<T> clazz,
            T entity) {

        Set<T> tuples = new HashSet<>();

        assertFalse(tuples.contains(entity));
        tuples.add(entity);
        assertTrue(tuples.contains(entity));

        doInJPA(entityManager -> {
            entityManager.persist(entity);
            entityManager.flush();
            assertTrue(
                    "The entity is not found in the Set after it's persisted.",
                    tuples.contains(entity)
            );
        });

        assertTrue(tuples.contains(entity));

        doInJPA(entityManager -> {
            T entityProxy = entityManager.getReference(
                    clazz,
                    entity.getId()
            );
            assertTrue(
                    "The entity proxy is not equal with the entity.",
                    entityProxy.equals(entity)
            );
        });

        doInJPA(entityManager -> {
            T entityProxy = entityManager.getReference(
                    clazz,
                    entity.getId()
            );
            assertTrue(
                    "The entity is not equal with the entity proxy.",
                    entity.equals(entityProxy));
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.merge(entity);
            assertTrue(
                    "The entity is not found in the Set after it's merged.",
                    tuples.contains(_entity)
            );
        });

        doInJPA(entityManager -> {
            entityManager.unwrap(Session.class).update(entity);
            assertTrue(
                    "The entity is not found in the Set after it's reattached.",
                    tuples.contains(entity)
            );
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.find(clazz, entity.getId());
            assertTrue(
                    "The entity is not found in the Set after it's loaded in a subsequent Persistence Context.",
                    tuples.contains(_entity)
            );
        });

        doInJPA(entityManager -> {
            T _entity = entityManager.getReference(clazz, entity.getId());
            assertTrue(
                    "The entity is not in the Set found after it's loaded as a proxy in an other Persistence Context.",
                    tuples.contains(_entity)
            );
        });

        T deletedEntity = doInJPA(entityManager -> {
            T _entity = entityManager.getReference(
                    clazz,
                    entity.getId()
            );
            entityManager.remove(_entity);
            return _entity;
        });

        assertTrue(
                "The entity is found in not the Set even after it's deleted.",
                tuples.contains(deletedEntity)
        );
    }
}

{'fraction_non_alphanumeric': 0.09944571242256277, 'fraction_numerical': 0.0, 'mean_word_length': 2.025147928994083, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149176', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1556, 'n_words': 377}