Search in sources :

Example 11 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class SyncMessageTest method testConstructor.

@Test
public void testConstructor() {
    ObjectContext source = mock(ObjectContext.class);
    GraphDiff diff = new CompoundDiff();
    SyncMessage message = new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, diff);
    assertSame(source, message.getSource());
    assertEquals(DataChannel.FLUSH_NOCASCADE_SYNC, message.getType());
    assertSame(diff, message.getSenderChanges());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 12 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class SyncMessageTest method testConstructorInvalid.

@Test
public void testConstructorInvalid() {
    ObjectContext source = mock(ObjectContext.class);
    new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, new CompoundDiff());
    new SyncMessage(source, DataChannel.FLUSH_CASCADE_SYNC, new CompoundDiff());
    new SyncMessage(null, DataChannel.ROLLBACK_CASCADE_SYNC, new CompoundDiff());
    int bogusType = 45678;
    try {
        new SyncMessage(source, bogusType, new CompoundDiff());
        fail("invalid type was allowed to go unnoticed...");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 13 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class SessionContextRequestHandler method requestStart.

public void requestStart(ServletRequest request, ServletResponse response) {
    CayenneRuntime.bindThreadInjector(injector);
    if (request instanceof HttpServletRequest) {
        // this forces session creation if it does not exist yet
        HttpSession session = ((HttpServletRequest) request).getSession();
        ObjectContext context;
        synchronized (session) {
            context = (ObjectContext) session.getAttribute(SESSION_CONTEXT_KEY);
            if (context == null) {
                context = injector.getInstance(ObjectContextFactory.class).createContext();
                session.setAttribute(SESSION_CONTEXT_KEY, context);
            }
        }
        BaseContext.bindThreadObjectContext(context);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ObjectContext(org.apache.cayenne.ObjectContext)

Example 14 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class PersistentObjectHolderTest method testInvalidate.

@Test
public void testInvalidate() {
    ObjectContext context = mock(ObjectContext.class);
    ClientMtTable2 o = new ClientMtTable2();
    o.setPersistenceState(PersistenceState.COMMITTED);
    o.setObjectContext(context);
    PersistentObjectHolder holder = new PersistentObjectHolder(o, ClientMtTable2.TABLE1_PROPERTY);
    assertTrue(holder.isFault());
    ClientMtTable1 o1 = new ClientMtTable1();
    o1.setObjectContext(context);
    holder.setValueDirectly(o1);
    holder.invalidate();
    assertTrue(holder.isFault());
    assertNull(holder.value);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 15 with ObjectContext

use of org.apache.cayenne.ObjectContext in project cayenne by apache.

the class ShallowMergeOperationIT method testMerge_NoOverride.

@Test
public void testMerge_NoOverride() throws Exception {
    createArtistsDataSet();
    ObjectContext childContext = runtime.newContext(context);
    final ShallowMergeOperation op = new ShallowMergeOperation(childContext);
    int modifiedId = 33003;
    final Artist modified = (Artist) Cayenne.objectForQuery(context, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    final Artist peerModified = (Artist) Cayenne.objectForQuery(childContext, new ObjectIdQuery(new ObjectId("Artist", Artist.ARTIST_ID_PK_COLUMN, modifiedId)));
    modified.setArtistName("M1");
    peerModified.setArtistName("M2");
    assertEquals(PersistenceState.MODIFIED, modified.getPersistenceState());
    assertEquals(PersistenceState.MODIFIED, peerModified.getPersistenceState());
    queryInterceptor.runWithQueriesBlocked(new UnitTestClosure() {

        public void execute() {
            Persistent peerModified2 = op.merge(modified);
            assertSame(peerModified, peerModified2);
            assertEquals(PersistenceState.MODIFIED, peerModified2.getPersistenceState());
            assertEquals("M2", peerModified.getArtistName());
            assertEquals("M1", modified.getArtistName());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) UnitTestClosure(org.apache.cayenne.unit.di.UnitTestClosure) Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)108 Test (org.junit.Test)99 Artist (org.apache.cayenne.testdo.testmap.Artist)32 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)14 SelectQuery (org.apache.cayenne.query.SelectQuery)13 Painting (org.apache.cayenne.testdo.testmap.Painting)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 GraphDiff (org.apache.cayenne.graph.GraphDiff)9 ObjectId (org.apache.cayenne.ObjectId)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 QueryResponse (org.apache.cayenne.QueryResponse)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 Query (org.apache.cayenne.query.Query)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4