Search in sources :

Example 6 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class MultiCircleJpaCascadeTest method testPersistThenUpdate.

@Test
@FailureExpected(jiraKey = "HHH-6999")
public // fails on d.e; should pass
void testPersistThenUpdate() {
    EntityManager em = getOrCreateEntityManager();
    em.getTransaction().begin();
    em.persist(b);
    // remove old e from associations
    e.getDCollection().remove(d);
    d.setE(null);
    f.getECollection().remove(e);
    e.setF(null);
    // add new e to associations
    e = new E();
    e.getDCollection().add(d);
    f.getECollection().add(e);
    d.setE(e);
    e.setF(f);
    em.getTransaction().commit();
    em.close();
    check();
}
Also used : EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 7 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class LockTest method testQueryTimeoutEMProps.

@Test
@RequiresDialect(Oracle10gDialect.class)
@RequiresDialectFeature(DialectChecks.SupportsLockTimeouts.class)
@FailureExpected(jiraKey = "HHH-8001")
public void testQueryTimeoutEMProps() throws Exception {
    EntityManager em = getOrCreateEntityManager();
    Map<String, Object> queryTimeoutProps = new HashMap<String, Object>();
    // 1 sec timeout (should round up)
    queryTimeoutProps.put(QueryHints.SPEC_HINT_TIMEOUT, 500);
    final EntityManager em2 = createIsolatedEntityManager(queryTimeoutProps);
    Lock lock = new Lock();
    Thread t = null;
    FutureTask<Boolean> bgTask;
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        lock.setName("testQueryTimeout");
        em.getTransaction().begin();
        em.persist(lock);
        em.getTransaction().commit();
        em.clear();
        em.getTransaction().begin();
        lock = em.getReference(Lock.class, lock.getId());
        em.lock(lock, LockModeType.PESSIMISTIC_WRITE);
        final Integer id = lock.getId();
        // force entity to be read
        lock.getName();
        log.info("testQueryTimeout: got write lock");
        bgTask = new FutureTask<Boolean>(new Callable<Boolean>() {

            public Boolean call() {
                try {
                    // true (success) if LockTimeoutException occurred
                    boolean timedOut = false;
                    em2.getTransaction().begin();
                    log.info("testQueryTimeout: (BG) about to read write-locked entity");
                    // we should block on the following read
                    Lock lock2 = em2.getReference(Lock.class, id);
                    //  force entity to be read
                    lock2.getName();
                    log.info("testQueryTimeout: (BG) read write-locked entity");
                    try {
                        // we should block on the following read
                        Query query = em2.createQuery("select L from Lock_ L where L.id < 10000 ");
                        query.setLockMode(LockModeType.PESSIMISTIC_READ);
                        List<Lock> resultList = query.getResultList();
                        //  force entity to be read
                        String name = resultList.get(0).getName();
                        log.info("testQueryTimeout: name read =" + name);
                    } catch (QueryTimeoutException e) {
                        // success
                        log.info("testQueryTimeout: (BG) got expected timeout exception");
                        timedOut = true;
                    } catch (Throwable e) {
                        log.info("testQueryTimeout: Expected LockTimeoutException but got unexpected exception", e);
                    }
                    em2.getTransaction().commit();
                    return timedOut;
                } finally {
                    // signal that we finished
                    latch.countDown();
                }
            }
        });
        t = new Thread(bgTask);
        t.setDaemon(true);
        t.setName("testQueryTimeout (bg)");
        t.start();
        // should return quickly on success
        boolean latchSet = latch.await(10, TimeUnit.SECONDS);
        assertTrue("background test thread finished (lock timeout is broken)", latchSet);
        assertTrue("background test thread timed out on lock attempt", bgTask.get());
        em.getTransaction().commit();
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        if (t != null) {
            // wait for background thread to finish beforeQuery deleting entity
            t.join();
        }
        em.getTransaction().begin();
        lock = em.getReference(Lock.class, lock.getId());
        em.remove(lock);
        em.getTransaction().commit();
        em.close();
        em2.close();
    }
}
Also used : Query(javax.persistence.Query) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) QueryTimeoutException(javax.persistence.QueryTimeoutException) EntityManager(javax.persistence.EntityManager) Test(org.junit.Test) RequiresDialectFeature(org.hibernate.testing.RequiresDialectFeature) FailureExpected(org.hibernate.testing.FailureExpected) RequiresDialect(org.hibernate.testing.RequiresDialect)

Example 8 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class DeleteOneToManyOrphansTest method testOrphanedWhileManagedMergeOwner.

@Test
@TestForIssue(jiraKey = "HHH-9568")
@FailureExpected(jiraKey = "HHH-9568")
public void testOrphanedWhileManagedMergeOwner() {
    createData();
    EntityManager entityManager = getOrCreateEntityManager();
    entityManager.getTransaction().begin();
    List results = entityManager.createQuery("from Feature").getResultList();
    assertEquals(1, results.size());
    results = entityManager.createQuery("from Product").getResultList();
    assertEquals(1, results.size());
    Product product = (Product) results.get(0);
    assertEquals(1, product.getFeatures().size());
    product.getFeatures().clear();
    entityManager.merge(product);
    entityManager.getTransaction().commit();
    entityManager.close();
    entityManager = getOrCreateEntityManager();
    entityManager.getTransaction().begin();
    product = entityManager.find(Product.class, product.getId());
    assertEquals(0, product.getFeatures().size());
    results = entityManager.createQuery("from Feature").getResultList();
    assertEquals(0, results.size());
    results = entityManager.createQuery("from Product").getResultList();
    assertEquals(1, results.size());
    entityManager.getTransaction().commit();
    entityManager.close();
    cleanupData();
}
Also used : EntityManager(javax.persistence.EntityManager) List(java.util.List) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Example 9 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class JpaTckUsageTest method testExecuteUpdate.

@Test
@FailureExpected(jiraKey = "HHH-8395", message = "Out of the frying pan into the fire: https://issues.apache.org/jira/browse/DERBY-211")
public void testExecuteUpdate() {
    EntityManager em = entityManagerFactory.createEntityManager();
    em.getTransaction().begin();
    try {
        StoredProcedureQuery query = em.createStoredProcedureQuery("deleteAllUsers");
        int count = query.executeUpdate();
        // this fails because the Derby EmbeddedDriver is returning zero here rather than the actual updateCount :(
        // https://issues.apache.org/jira/browse/DERBY-211
        assertEquals(1, count);
    } finally {
        em.getTransaction().commit();
        em.close();
    }
}
Also used : EntityManager(javax.persistence.EntityManager) StoredProcedureQuery(javax.persistence.StoredProcedureQuery) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected)

Example 10 with FailureExpected

use of org.hibernate.testing.FailureExpected in project hibernate-orm by hibernate.

the class EmbeddedTest method testQueryWithEmbeddedParameterAllNull.

@Test
@TestForIssue(jiraKey = "HHH-8172")
@FailureExpected(jiraKey = "HHH-8172")
public void testQueryWithEmbeddedParameterAllNull() throws Exception {
    Session s;
    Transaction tx;
    Person person = new Person();
    Address a = new Address();
    Country c = new Country();
    Country bornCountry = new Country();
    assertNull(bornCountry.getIso2());
    assertNull(bornCountry.getName());
    a.address1 = "colorado street";
    a.city = "Springfield";
    a.country = c;
    person.address = a;
    person.bornIn = bornCountry;
    person.name = "Homer";
    TransactionUtil.doInHibernate(this::sessionFactory, session -> {
        session.persist(person);
    });
    TransactionUtil.doInHibernate(this::sessionFactory, session -> {
        Person p = (Person) session.createQuery("from Person p where p.bornIn = :b").setParameter("b", person.bornIn).uniqueResult();
        assertNotNull(p);
        assertNotNull(p.address);
        assertEquals("Springfield", p.address.city);
        assertNotNull(p.address.country);
        assertEquals("DM", p.address.country.getIso2());
        assertNull(p.bornIn);
    });
}
Also used : Transaction(org.hibernate.Transaction) Session(org.hibernate.Session) Test(org.junit.Test) FailureExpected(org.hibernate.testing.FailureExpected) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

FailureExpected (org.hibernate.testing.FailureExpected)59 Test (org.junit.Test)58 Session (org.hibernate.Session)39 Transaction (org.hibernate.Transaction)23 TestForIssue (org.hibernate.testing.TestForIssue)14 EntityManager (javax.persistence.EntityManager)12 List (java.util.List)7 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 MetadataSources (org.hibernate.boot.MetadataSources)3 RequiresDialect (org.hibernate.testing.RequiresDialect)3 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Statement (java.sql.Statement)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Callable (java.util.concurrent.Callable)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Query (javax.persistence.Query)2