Search in sources :

Example 66 with ObjectContext

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

the class ServerRuntimeBuilderIT method test_UnnamedDomain_CustomNameProjectFile.

/**
 * Test case for CAY-2265
 */
@Test
public void test_UnnamedDomain_CustomNameProjectFile() {
    localRuntime = new ServerRuntimeBuilder(null).addConfigs(CayenneProjects.CUSTOM_NAME_PROJECT).build();
    assertEquals("cayenne", localRuntime.getDataDomain().getName());
    ObjectContext context = localRuntime.newContext();
    assertNotNull(context);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 67 with ObjectContext

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

the class ServerRuntimeTest method testGetDataChannel_CustomModule.

@Test
public void testGetDataChannel_CustomModule() {
    final DataChannel channel = new DataChannel() {

        public EntityResolver getEntityResolver() {
            return null;
        }

        public EventManager getEventManager() {
            return null;
        }

        public QueryResponse onQuery(ObjectContext originatingContext, Query query) {
            return null;
        }

        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return null;
        }
    };
    Module module = binder -> binder.bind(DataChannel.class).toInstance(channel);
    ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
    assertSame(channel, runtime.getChannel());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Module(org.apache.cayenne.di.Module) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) EntityResolver(org.apache.cayenne.map.EntityResolver) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) Key(org.apache.cayenne.di.Key) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) TransactionDescriptor(org.apache.cayenne.tx.TransactionDescriptor) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EventManager(org.apache.cayenne.event.EventManager) QueryResponse(org.apache.cayenne.QueryResponse) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 68 with ObjectContext

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

the class VerticalInheritanceIT method testUpdateRelation_Sub3.

/**
 * @link https://issues.apache.org/jira/browse/CAY-2282
 */
@Test
public void testUpdateRelation_Sub3() throws Exception {
    TableHelper ivRootTable = new TableHelper(dbHelper, "IV_ROOT");
    ivRootTable.setColumns("ID", "NAME", "DISCRIMINATOR");
    ivRootTable.insert(1, "root1", null);
    ivRootTable.insert(2, "root2", null);
    ivRootTable.insert(3, "name", "IvSub3");
    TableHelper ivSub3Table = new TableHelper(dbHelper, "IV_SUB3");
    ivSub3Table.setColumns("ID", "IV_ROOT_ID");
    ivSub3Table.insert(3, 1);
    IvRoot root = SelectById.query(IvRoot.class, 2).selectOne(context);
    IvSub3 sub3 = SelectById.query(IvSub3.class, 3).selectOne(context);
    sub3.setName("new name");
    sub3.setIvRoot(root);
    // this will create 3 queries...
    // update for name, insert for new relationship, delete for old relationship
    context.commitChanges();
    ObjectContext cleanContext = runtime.newContext();
    IvSub3 sub3Clean = SelectById.query(IvSub3.class, 3).selectOne(cleanContext);
    assertNotNull(sub3Clean);
    assertNotSame(sub3, sub3Clean);
    assertEquals("new name", sub3.getName());
    assertNotNull(sub3Clean.getIvRoot());
    assertEquals("root2", sub3Clean.getIvRoot().getName());
}
Also used : TableHelper(org.apache.cayenne.test.jdbc.TableHelper) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 69 with ObjectContext

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

the class SimpleAttributeProperty method resolveFault.

protected void resolveFault(Object object) throws PropertyException {
    try {
        Persistent persistent = (Persistent) object;
        ObjectContext context = persistent.getObjectContext();
        if (context != null) {
            context.prepareForAccess(persistent, getName(), false);
        }
    } catch (ClassCastException e) {
        throw new PropertyException("Object is not a Persistent: '" + object.getClass().getName() + "'", this, object, e);
    }
}
Also used : Persistent(org.apache.cayenne.Persistent) ObjectContext(org.apache.cayenne.ObjectContext)

Example 70 with ObjectContext

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

the class NestedDataContextReadIT method testPrefetchingToOne.

@Test
public void testPrefetchingToOne() throws Exception {
    createPrefetchingDataSet();
    final ObjectContext child = runtime.newContext(context);
    final ObjectId prefetchedId = ObjectId.of("Artist", Artist.ARTIST_ID_PK_COLUMN, 33001);
    final List<Painting> results = ObjectSelect.query(Painting.class).orderBy(Painting.PAINTING_TITLE.asc()).prefetch(Painting.TO_ARTIST.disjoint()).select(child);
    // blockQueries();
    queryInterceptor.runWithQueriesBlocked(() -> {
        assertEquals(2, results.size());
        for (Painting o : results) {
            assertEquals(PersistenceState.COMMITTED, o.getPersistenceState());
            assertSame(child, o.getObjectContext());
            Artist o1 = o.getToArtist();
            assertNotNull(o1);
            assertEquals(PersistenceState.COMMITTED, o1.getPersistenceState());
            assertSame(child, o1.getObjectContext());
            assertEquals(prefetchedId, o1.getObjectId());
        }
    });
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) ObjectContext(org.apache.cayenne.ObjectContext) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)127 Test (org.junit.Test)116 Artist (org.apache.cayenne.testdo.testmap.Artist)35 Painting (org.apache.cayenne.testdo.testmap.Painting)14 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 Table1 (org.apache.cayenne.crypto.db.Table1)10 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 ObjectId (org.apache.cayenne.ObjectId)8 GraphDiff (org.apache.cayenne.graph.GraphDiff)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 IvImpl (org.apache.cayenne.testdo.inheritance_vertical.IvImpl)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 QueryResponse (org.apache.cayenne.QueryResponse)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4