Search in sources :

Example 31 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class JpaVersionDaoTest method testVersionLifecycle.

@Test
public void testVersionLifecycle() {
    final String productName = "TEST_VERSION";
    // Doesn't exist
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            final Version version = versionDao.getVersion(productName);
            assertNull(version);
        }
    });
    // Create
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            Version version = versionDao.getVersion(productName);
            assertNull(version);
            version = versionDao.setVersion(productName, 1, 2, 3, null);
            assertNotNull(version);
            assertEquals(1, version.getMajor());
            assertEquals(2, version.getMinor());
            assertEquals(3, version.getPatch());
            assertNull(version.getLocal());
        }
    });
    // Update
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            Version version = versionDao.getVersion(productName);
            assertNotNull(version);
            assertEquals(1, version.getMajor());
            assertEquals(2, version.getMinor());
            assertEquals(3, version.getPatch());
            assertNull(version.getLocal());
            version = versionDao.setVersion(productName, 4, 5, 6, null);
            assertNotNull(version);
            assertEquals(4, version.getMajor());
            assertEquals(5, version.getMinor());
            assertEquals(6, version.getPatch());
            assertNull(version.getLocal());
        }
    });
    // verify
    this.execute(new CallableWithoutResult() {

        @Override
        protected void callWithoutResult() {
            Version version = versionDao.getVersion(productName);
            assertNotNull(version);
            assertEquals(4, version.getMajor());
            assertEquals(5, version.getMinor());
            assertEquals(6, version.getPatch());
            assertNull(version.getLocal());
        }
    });
}
Also used : Version(org.apereo.portal.version.om.Version) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) Test(org.junit.Test) BasePortalJpaDaoTest(org.apereo.portal.test.BasePortalJpaDaoTest)

Example 32 with CallableWithoutResult

use of org.apereo.portal.concurrency.CallableWithoutResult in project uPortal by Jasig.

the class BaseJpaDaoTest method deleteAllEntities.

/**
 * Deletes ALL entities from the database
 */
@After
public final void deleteAllEntities() {
    final EntityManager entityManager = getEntityManager();
    final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
    final Metamodel metamodel = entityManagerFactory.getMetamodel();
    Set<EntityType<?>> entityTypes = new LinkedHashSet<>(metamodel.getEntities());
    do {
        final Set<EntityType<?>> failedEntitieTypes = new HashSet<>();
        for (final EntityType<?> entityType : entityTypes) {
            final String entityClassName = entityType.getBindableJavaType().getName();
            try {
                this.executeInTransaction(new CallableWithoutResult() {

                    @Override
                    protected void callWithoutResult() {
                        logger.trace("Purging all: " + entityClassName);
                        final Query query = entityManager.createQuery("SELECT e FROM " + entityClassName + " AS e");
                        final List<?> entities = query.getResultList();
                        logger.trace("Found " + entities.size() + " " + entityClassName + " to delete");
                        for (final Object entity : entities) {
                            entityManager.remove(entity);
                        }
                    }
                });
            } catch (DataIntegrityViolationException e) {
                logger.trace("Failed to delete " + entityClassName + ". Must be a dependency of another entity");
                failedEntitieTypes.add(entityType);
            }
        }
        entityTypes = failedEntitieTypes;
    } while (!entityTypes.isEmpty());
    // Reset all spring managed mocks after every test
    MockitoFactoryBean.resetAllMocks();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Query(javax.persistence.Query) CallableWithoutResult(org.apereo.portal.concurrency.CallableWithoutResult) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) EntityType(javax.persistence.metamodel.EntityType) EntityManager(javax.persistence.EntityManager) EntityManagerFactory(javax.persistence.EntityManagerFactory) List(java.util.List) LinkedList(java.util.LinkedList) AccessibleObject(java.lang.reflect.AccessibleObject) Metamodel(javax.persistence.metamodel.Metamodel) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) After(org.junit.After)

Aggregations

CallableWithoutResult (org.apereo.portal.concurrency.CallableWithoutResult)32 Test (org.junit.Test)28 BasePortalJpaDaoTest (org.apereo.portal.test.BasePortalJpaDaoTest)14 BaseAggrEventsJpaDaoTest (org.apereo.portal.test.BaseAggrEventsJpaDaoTest)12 DateTime (org.joda.time.DateTime)8 List (java.util.List)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 CompositeName (javax.naming.CompositeName)7 IEntityGroup (org.apereo.portal.groups.IEntityGroup)7 LinkedList (java.util.LinkedList)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 AggregatedGroupMapping (org.apereo.portal.events.aggr.groups.AggregatedGroupMapping)5 Ignore (org.junit.Ignore)5 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 MutableInt (org.apache.commons.lang.mutable.MutableInt)3 MutableObject (org.apache.commons.lang.mutable.MutableObject)3 FunctionWithoutResult (org.apereo.portal.concurrency.FunctionWithoutResult)3 File (java.io.File)2 FileWriter (java.io.FileWriter)2