public void testSerializeCommitted() throws Exception {

    Artist artist = (Artist) context.newObject("Artist");
    artist.setArtistName("artist1");
    assertNotNull(artist.getObjectId());
    context.commitChanges();

    DataContext deserializedContext = Util.cloneViaSerialization(context);

    assertSame(context.getParentDataDomain(), deserializedContext.getParentDataDomain());

    // there should be only one object registered
    Artist deserializedArtist =
        (Artist) deserializedContext.getObjectStore().getObjectIterator().next();

    assertNotNull(deserializedArtist);

    // deserialized as hollow...
    assertEquals(PersistenceState.HOLLOW, deserializedArtist.getPersistenceState());
    assertFalse(deserializedArtist.getObjectId().isTemporary());
    assertEquals("artist1", deserializedArtist.getArtistName());
    assertSame(deserializedContext, deserializedArtist.getObjectContext());

    // test that to-many relationships are initialized
    List<?> paintings = deserializedArtist.getPaintingArray();
    assertNotNull(paintings);
    assertEquals(0, paintings.size());
  }
Example #2
0
  @Test
  public void testIntPk() throws Exception {
    createTwoArtists();

    Artist a3 = SelectById.query(Artist.class, 3).selectOne(context);
    assertNotNull(a3);
    assertEquals("artist3", a3.getArtistName());

    Artist a2 = SelectById.query(Artist.class, 2).selectOne(context);
    assertNotNull(a2);
    assertEquals("artist2", a2.getArtistName());
  }
Example #3
0
  @Test
  public void testObjectIdPk() throws Exception {
    createTwoArtists();

    ObjectId oid3 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 3);
    Artist a3 = SelectById.query(Artist.class, oid3).selectOne(context);
    assertNotNull(a3);
    assertEquals("artist3", a3.getArtistName());

    ObjectId oid2 = new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, 2);
    Artist a2 = SelectById.query(Artist.class, oid2).selectOne(context);
    assertNotNull(a2);
    assertEquals("artist2", a2.getArtistName());
  }
Example #4
0
  @Test
  public void testMapPk() throws Exception {
    createTwoArtists();

    Artist a3 =
        SelectById.query(Artist.class, singletonMap(Artist.ARTIST_ID_PK_COLUMN, 3))
            .selectOne(context);
    assertNotNull(a3);
    assertEquals("artist3", a3.getArtistName());

    Artist a2 =
        SelectById.query(Artist.class, singletonMap(Artist.ARTIST_ID_PK_COLUMN, 2))
            .selectOne(context);
    assertNotNull(a2);
    assertEquals("artist2", a2.getArtistName());
  }
  @Test
  public void testRefreshingToOne() throws Exception {

    createOneArtistOnePaintingDataSet();

    Painting p = Cayenne.objectForPK(context, Painting.class, 1);

    // resolve artist once before running non-refreshing query, to check that we do
    // not refresh the object

    Artist a = Cayenne.objectForPK(context, Artist.class, 1);
    long v = a.getSnapshotVersion();
    int writeCalls = a.getPropertyWrittenDirectly();
    assertEquals("a1", a.getArtistName());

    assertEquals(1, tArtist.update().set("ARTIST_NAME", "a2").where("ARTIST_ID", 1).execute());

    RelationshipQuery toOne =
        new RelationshipQuery(p.getObjectId(), Painting.TO_ARTIST_PROPERTY, true);

    List<Artist> related = context.performQuery(toOne);
    assertEquals(1, related.size());
    assertTrue(related.contains(a));
    assertEquals("a2", a.getArtistName());
    assertTrue(
        "Looks like relationship query didn't cause a snapshot refresh",
        v < a.getSnapshotVersion());
    assertTrue(
        "Looks like relationship query didn't cause a snapshot refresh",
        writeCalls < a.getPropertyWrittenDirectly());
  }
  public void testSerializeWithLocalCache() throws Exception {

    createSingleArtistDataSet();

    // manually assemble a DataContext with local cache....
    DataDomain domain = context.getParentDataDomain();
    DataRowStore snapshotCache =
        new DataRowStore(domain.getName(), domain.getProperties(), domain.getEventManager());

    Map<Object, Persistent> map = new HashMap<Object, Persistent>();

    DataContext localCacheContext = new DataContext(domain, new ObjectStore(snapshotCache, map));
    localCacheContext.setValidatingObjectsOnCommit(domain.isValidatingObjectsOnCommit());
    localCacheContext.setUsingSharedSnapshotCache(false);

    assertNotSame(
        domain.getSharedSnapshotCache(), localCacheContext.getObjectStore().getDataRowCache());

    DataContext deserializedContext = Util.cloneViaSerialization(localCacheContext);

    assertNotSame(localCacheContext, deserializedContext);
    assertNotSame(localCacheContext.getObjectStore(), deserializedContext.getObjectStore());

    assertSame(localCacheContext.getParentDataDomain(), deserializedContext.getParentDataDomain());
    assertNotSame(
        localCacheContext.getObjectStore().getDataRowCache(),
        deserializedContext.getObjectStore().getDataRowCache());
    assertNotSame(
        deserializedContext.getParentDataDomain().getSharedSnapshotCache(),
        deserializedContext.getObjectStore().getDataRowCache());

    Artist a = Cayenne.objectForPK(deserializedContext, Artist.class, 33001);
    assertNotNull(a);
    a.setArtistName(a.getArtistName() + "___");

    // this blows per CAY-796
    deserializedContext.commitChanges();
  }
  public void testLocalObject_FFE_InvalidID() throws Exception {
    tArtist.insert(777, "AA");

    final Artist a1 = Cayenne.objectForPK(context1, Artist.class, 777);

    Artist a3 = context2.localObject(a1);
    assertEquals(PersistenceState.HOLLOW, a3.getPersistenceState());

    context1.invalidateObjects(a1);
    tArtist.deleteAll();

    assertEquals(PersistenceState.HOLLOW, a3.getPersistenceState());

    try {
      a3.getArtistName();

      fail(
          "FaultFailureException wasn't thrown on attempt to "
              + "resolve HOLLOW object with no backing DB row");
    } catch (FaultFailureException e) {
      // expected
    }
  }
  public void testSerializeWithSharedCache() throws Exception {

    createSingleArtistDataSet();

    DataContext deserializedContext = Util.cloneViaSerialization(context);

    assertNotSame(context, deserializedContext);
    assertNotSame(context.getObjectStore(), deserializedContext.getObjectStore());
    assertSame(context.getParentDataDomain(), deserializedContext.getParentDataDomain());
    assertSame(
        context.getObjectStore().getDataRowCache(),
        deserializedContext.getObjectStore().getDataRowCache());
    assertSame(
        deserializedContext.getParentDataDomain().getSharedSnapshotCache(),
        deserializedContext.getObjectStore().getDataRowCache());

    assertNotNull(deserializedContext.getEntityResolver());
    assertSame(context.getEntityResolver(), deserializedContext.getEntityResolver());

    Artist a = Cayenne.objectForPK(deserializedContext, Artist.class, 33001);
    assertNotNull(a);
    a.setArtistName(a.getArtistName() + "___");
    deserializedContext.commitChanges();
  }
  public void testSerializeNew() throws Exception {

    Artist artist = (Artist) context.newObject("Artist");
    artist.setArtistName("artist1");
    assertNotNull(artist.getObjectId());

    DataContext deserializedContext = Util.cloneViaSerialization(context);
    assertSame(context.getParentDataDomain(), deserializedContext.getParentDataDomain());

    // there should be only one object registered
    Artist deserializedArtist =
        (Artist) deserializedContext.getObjectStore().getObjectIterator().next();

    assertNotNull(deserializedArtist);
    assertEquals(PersistenceState.NEW, deserializedArtist.getPersistenceState());
    assertTrue(deserializedArtist.getObjectId().isTemporary());
    assertEquals("artist1", deserializedArtist.getArtistName());
    assertSame(deserializedContext, deserializedArtist.getObjectContext());
  }
  public void testSerializeModified() throws Exception {

    Artist artist = (Artist) context.newObject("Artist");
    artist.setArtistName("artist1");
    assertNotNull(artist.getObjectId());
    context.commitChanges();
    artist.setArtistName("artist2");

    DataContext deserializedContext = Util.cloneViaSerialization(context);

    assertSame(context.getParentDataDomain(), deserializedContext.getParentDataDomain());

    // there should be only one object registered
    Artist deserializedArtist =
        (Artist) deserializedContext.getObjectStore().getObjectIterator().next();

    assertNotNull(deserializedArtist);

    // deserialized as hollow...
    assertEquals(PersistenceState.MODIFIED, deserializedArtist.getPersistenceState());
    assertFalse(deserializedArtist.getObjectId().isTemporary());
    assertEquals("artist2", deserializedArtist.getArtistName());
    assertSame(deserializedContext, deserializedArtist.getObjectContext());
  }