Search in sources :

Example 1 with CollectionToMany

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany in project cayenne by apache.

the class CayenneDataObjectSetToManyCollectionIT method testReadToMany.

@Test
public void testReadToMany() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertNotNull(targets);
    assertTrue(((ValueHolder) targets).isFault());
    assertEquals(3, targets.size());
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 1)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 3)));
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) Test(org.junit.Test)

Example 2 with CollectionToMany

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany in project cayenne by apache.

the class CDOCollectionRelationshipIT method testReadToManyPrefetching.

@Test
public void testReadToManyPrefetching() throws Exception {
    SelectQuery query = new SelectQuery(CollectionToMany.class, ExpressionFactory.matchDbExp(CollectionToMany.ID_PK_COLUMN, 1));
    query.addPrefetch(CollectionToMany.TARGETS.disjoint());
    CollectionToMany o1 = (CollectionToMany) Cayenne.objectForQuery(context, query);
    Collection<?> targets = o1.getTargets();
    assertFalse(((ValueHolder) targets).isFault());
    assertNotNull(targets);
    assertEquals(3, targets.size());
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 1)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 3)));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) Test(org.junit.Test)

Example 3 with CollectionToMany

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany in project cayenne by apache.

the class CDOCollectionRelationshipIT method testAddToMany.

@Test
public void testAddToMany() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    CollectionToManyTarget newTarget = o1.getObjectContext().newObject(CollectionToManyTarget.class);
    o1.addToTargets(newTarget);
    assertEquals(4, targets.size());
    assertTrue(o1.getTargets().contains(newTarget));
    assertSame(o1, newTarget.getCollectionToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) RefreshQuery(org.apache.cayenne.query.RefreshQuery) CollectionToManyTarget(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget) Test(org.junit.Test)

Example 4 with CollectionToMany

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany in project cayenne by apache.

the class CayenneDataObjectSetToManyCollectionIT method testRelationCollectionTypeCollection.

/**
 * Testing if collection type is Collection, everything should work fine without an
 * runtimexception
 *
 * @throws Exception
 */
@Test
public void testRelationCollectionTypeCollection() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    assertTrue(o1.readProperty(CollectionToMany.TARGETS.getName()) instanceof Collection);
    boolean catchedSomething = false;
    try {
        o1.setToManyTarget(CollectionToMany.TARGETS.getName(), new ArrayList<CollectionToMany>(0), true);
    } catch (RuntimeException e) {
        catchedSomething = true;
    }
    assertEquals(catchedSomething, false);
    assertEquals(o1.getTargets().size(), 0);
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) Collection(java.util.Collection) Test(org.junit.Test)

Example 5 with CollectionToMany

use of org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany in project cayenne by apache.

the class CDOCollectionRelationshipIT method testReadToMany.

@Test
public void testReadToMany() throws Exception {
    CollectionToMany o1 = Cayenne.objectForPK(context, CollectionToMany.class, 1);
    Collection<?> targets = o1.getTargets();
    assertNotNull(targets);
    assertTrue(((ValueHolder) targets).isFault());
    assertEquals(3, targets.size());
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 1)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 2)));
    assertTrue(targets.contains(Cayenne.objectForPK(o1.getObjectContext(), CollectionToManyTarget.class, 3)));
}
Also used : CollectionToMany(org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany) Test(org.junit.Test)

Aggregations

CollectionToMany (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToMany)7 Test (org.junit.Test)7 RefreshQuery (org.apache.cayenne.query.RefreshQuery)3 CollectionToManyTarget (org.apache.cayenne.testdo.relationships_collection_to_many.CollectionToManyTarget)3 Collection (java.util.Collection)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1