Search in sources :

Example 1 with MapToMany

use of org.apache.cayenne.testdo.map_to_many.MapToMany in project cayenne by apache.

the class CayenneDataObjectSetToManyMapIT method testRelationCollectionTypeMap.

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

Example 2 with MapToMany

use of org.apache.cayenne.testdo.map_to_many.MapToMany in project cayenne by apache.

the class CayenneDataObjectSetToManySetIT method testRelationCollectionTypeMap.

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

Example 3 with MapToMany

use of org.apache.cayenne.testdo.map_to_many.MapToMany in project cayenne by apache.

the class CDOMapRelationshipIT method testRemoveToMany.

@Test
public void testRemoveToMany() throws Exception {
    createTestDataSet();
    MapToMany o1 = Cayenne.objectForPK(context, MapToMany.class, 1);
    Map targets = o1.getTargets();
    assertEquals(3, targets.size());
    MapToManyTarget target = (MapToManyTarget) targets.get("B");
    o1.removeFromTargets(target);
    assertEquals(2, targets.size());
    assertNull(o1.getTargets().get("B"));
    assertNull(target.getMapToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(2, o1.getTargets().size());
    assertNotNull(o1.getTargets().get("A"));
    assertNotNull(o1.getTargets().get("C"));
}
Also used : RefreshQuery(org.apache.cayenne.query.RefreshQuery) Map(java.util.Map) MapToManyTarget(org.apache.cayenne.testdo.map_to_many.MapToManyTarget) IdMapToMany(org.apache.cayenne.testdo.map_to_many.IdMapToMany) MapToMany(org.apache.cayenne.testdo.map_to_many.MapToMany) Test(org.junit.Test)

Example 4 with MapToMany

use of org.apache.cayenne.testdo.map_to_many.MapToMany in project cayenne by apache.

the class CDOMapRelationshipIT method testAddToMany.

@Test
public void testAddToMany() throws Exception {
    createTestDataSet();
    MapToMany o1 = Cayenne.objectForPK(context, MapToMany.class, 1);
    Map targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    MapToManyTarget newTarget = o1.getObjectContext().newObject(MapToManyTarget.class);
    newTarget.setName("X");
    o1.addToTargets(newTarget);
    assertEquals(4, targets.size());
    assertSame(newTarget, o1.getTargets().get("X"));
    assertSame(o1, newTarget.getMapToMany());
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : RefreshQuery(org.apache.cayenne.query.RefreshQuery) Map(java.util.Map) MapToManyTarget(org.apache.cayenne.testdo.map_to_many.MapToManyTarget) IdMapToMany(org.apache.cayenne.testdo.map_to_many.IdMapToMany) MapToMany(org.apache.cayenne.testdo.map_to_many.MapToMany) Test(org.junit.Test)

Example 5 with MapToMany

use of org.apache.cayenne.testdo.map_to_many.MapToMany in project cayenne by apache.

the class CDOMapRelationshipIT method testAddToManyViaReverse.

@Test
public void testAddToManyViaReverse() throws Exception {
    createTestDataSet();
    MapToMany o1 = Cayenne.objectForPK(context, MapToMany.class, 1);
    Map targets = o1.getTargets();
    assertNotNull(targets);
    assertEquals(3, targets.size());
    MapToManyTarget newTarget = o1.getObjectContext().newObject(MapToManyTarget.class);
    newTarget.setName("X");
    newTarget.setMapToMany(o1);
    assertSame(o1, newTarget.getMapToMany());
    assertEquals(4, targets.size());
    assertSame(newTarget, o1.getTargets().get("X"));
    o1.getObjectContext().commitChanges();
    o1.getObjectContext().performGenericQuery(new RefreshQuery());
    assertEquals(4, o1.getTargets().size());
}
Also used : RefreshQuery(org.apache.cayenne.query.RefreshQuery) Map(java.util.Map) MapToManyTarget(org.apache.cayenne.testdo.map_to_many.MapToManyTarget) IdMapToMany(org.apache.cayenne.testdo.map_to_many.IdMapToMany) MapToMany(org.apache.cayenne.testdo.map_to_many.MapToMany) Test(org.junit.Test)

Aggregations

MapToMany (org.apache.cayenne.testdo.map_to_many.MapToMany)8 Test (org.junit.Test)8 Map (java.util.Map)7 IdMapToMany (org.apache.cayenne.testdo.map_to_many.IdMapToMany)6 MapToManyTarget (org.apache.cayenne.testdo.map_to_many.MapToManyTarget)4 RefreshQuery (org.apache.cayenne.query.RefreshQuery)3 Set (java.util.Set)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1 SetToMany (org.apache.cayenne.testdo.relationships_set_to_many.SetToMany)1