Search in sources :

Example 1 with Qualified1

use of org.apache.cayenne.testdo.qualified.Qualified1 in project cayenne by apache.

the class CDOQualifiedEntitiesIT method testReadToMany.

@Test
public void testReadToMany() throws Exception {
    if (accessStackAdapter.supportsNullBoolean()) {
        createReadToManyDataSet();
        List<Qualified1> roots = ObjectSelect.query(Qualified1.class).select(context);
        assertEquals(1, roots.size());
        Qualified1 root = roots.get(0);
        assertEquals("OX1", root.getName());
        List<Qualified2> related = root.getQualified2s();
        assertEquals(1, related.size());
        Qualified2 r = related.get(0);
        assertEquals("OY1", r.getName());
    }
}
Also used : Qualified1(org.apache.cayenne.testdo.qualified.Qualified1) Qualified2(org.apache.cayenne.testdo.qualified.Qualified2) Test(org.junit.Test)

Example 2 with Qualified1

use of org.apache.cayenne.testdo.qualified.Qualified1 in project cayenne by apache.

the class ConcurrentPkGeneratorIT method testConcurrentInserts.

/*
     * Attempts to discover any problems regarding thread locking in the PkGenerator
     */
@Test
public void testConcurrentInserts() {
    if (!unitDbAdapter.supportsPKGeneratorConcurrency()) {
        return;
    }
    final DataMap dataMap = runtime.getDataDomain().getDataMap("qualified");
    // clear out the table
    ObjectContext context = runtime.newContext();
    List<Qualified1> qualified1s = context.select(ObjectSelect.query(Qualified1.class));
    context.deleteObjects(qualified1s);
    context.commitChanges();
    // perform concurrent inserts
    int numThreads = 2;
    int insertsPerThread = 100;
    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
    Runnable task = () -> {
        try {
            ObjectContext context1 = runtime.newContext();
            for (ObjEntity entity : dataMap.getObjEntities()) {
                context1.newObject(entity.getJavaClass());
            }
            context1.commitChanges();
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
    for (int j = 0; j < insertsPerThread; j++) {
        for (int i = 0; i < numThreads; i++) {
            executor.submit(task);
        }
    }
    // check for completion or deadlock
    executor.shutdown();
    try {
        // normally this completes in less than 2 seconds. If it takes 30 then it failed.
        boolean didFinish = executor.awaitTermination(30, TimeUnit.SECONDS);
        if (!didFinish) {
            fail("Concurrent inserts either deadlocked or contended over the lock too long.");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
    // check for gaps in the generated sequence numbers
    qualified1s = context.select(ObjectSelect.query(Qualified1.class));
    assertEquals(insertsPerThread * numThreads, qualified1s.size());
// PKs will be used in order most of the time, but the implementation doesn't guarantee it.
// qualified1s.sort(Comparator.comparing(Cayenne::intPKForObject));
// 
// int lastPk = Cayenne.intPKForObject(qualified1s.get(0)) - 1;
// for (Qualified1 qualified1 : qualified1s) {
// if (lastPk+1 != Cayenne.intPKForObject(qualified1)) {
// fail("Found gap in sequence number: " + lastPk + " - " + Cayenne.intPKForObject(qualified1));
// }
// lastPk++;
// }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) Qualified1(org.apache.cayenne.testdo.qualified.Qualified1) ExecutorService(java.util.concurrent.ExecutorService) ObjectContext(org.apache.cayenne.ObjectContext) SQLException(java.sql.SQLException) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 3 with Qualified1

use of org.apache.cayenne.testdo.qualified.Qualified1 in project cayenne by apache.

the class CDOQualifiedEntitiesIT method testReadToOne.

@Test
public void testReadToOne() throws Exception {
    if (accessStackAdapter.supportsNullBoolean()) {
        createReadToOneDataSet();
        List<Qualified2> roots = ObjectSelect.query(Qualified2.class).select(context);
        assertEquals(1, roots.size());
        Qualified2 root = roots.get(0);
        assertEquals("OY1", root.getName());
        Qualified1 target = root.getQualified1();
        assertNull(target);
    }
}
Also used : Qualified1(org.apache.cayenne.testdo.qualified.Qualified1) Qualified2(org.apache.cayenne.testdo.qualified.Qualified2) Test(org.junit.Test)

Aggregations

Qualified1 (org.apache.cayenne.testdo.qualified.Qualified1)3 Test (org.junit.Test)3 Qualified2 (org.apache.cayenne.testdo.qualified.Qualified2)2 SQLException (java.sql.SQLException)1 ExecutorService (java.util.concurrent.ExecutorService)1 ObjectContext (org.apache.cayenne.ObjectContext)1 DataMap (org.apache.cayenne.map.DataMap)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1