Search in sources :

Example 1 with DataContext

use of org.apache.cayenne.access.DataContext in project cayenne by apache.

the class LocalClientServerChannelProvider method get.

public DataChannel get() throws ConfigurationException {
    ObjectContextFactory factory = serverInjector.getInstance(ObjectContextFactory.class);
    // TODO: ugly cast
    DataContext serverContext = (DataContext) factory.createContext();
    return new ClientServerChannel(serverContext);
}
Also used : DataContext(org.apache.cayenne.access.DataContext) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory)

Example 2 with DataContext

use of org.apache.cayenne.access.DataContext in project cayenne by apache.

the class CayenneContextWithDataContextIT method testLocalCacheStaysLocal.

@SuppressWarnings("deprecation")
@Test
public void testLocalCacheStaysLocal() {
    DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();
    SelectQuery query = new SelectQuery(ClientMtTable1.class);
    query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
    List<?> results = clientContext.performQuery(query);
    assertSame(results, clientContext.getQueryCache().get(query.getMetaData(clientContext.getEntityResolver())));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) DataContext(org.apache.cayenne.access.DataContext) Test(org.junit.Test)

Example 3 with DataContext

use of org.apache.cayenne.access.DataContext in project cayenne by apache.

the class CayenneContextWithDataContextIT method testPostAddOnObjectCallback.

@Test
public void testPostAddOnObjectCallback() throws Exception {
    final DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();
    LifecycleCallbackRegistry callbackRegistry = serverContext.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addCallback(LifecycleEvent.POST_ADD, MtTable1.class, "prePersistMethod");
        final Persistent clientObject = clientContext.newObject(ClientMtTable1.class);
        clientContext.commitChanges();
        new ParallelTestContainer() {

            @Override
            protected void assertResult() throws Exception {
                // find peer
                MtTable1 peer = (MtTable1) serverContext.getGraphManager().getNode(clientObject.getObjectId());
                assertNotNull(peer);
                assertTrue(peer.isPrePersisted());
            }
        }.runTest(1000);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : DataContext(org.apache.cayenne.access.DataContext) MtTable1(org.apache.cayenne.testdo.mt.MtTable1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ParallelTestContainer(org.apache.cayenne.test.parallel.ParallelTestContainer) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) Test(org.junit.Test)

Example 4 with DataContext

use of org.apache.cayenne.access.DataContext in project cayenne by apache.

the class DataContextFactoryTest method testCreateDataContextWithDedicatedCache.

@Test
public void testCreateDataContextWithDedicatedCache() throws Exception {
    final EventManager eventManager = new MockEventManager();
    final DataDomain domain = new DataDomain("d1");
    domain.setSharedCacheEnabled(false);
    Module testModule = binder -> {
        binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
        binder.bind(DataDomain.class).toInstance(domain);
        binder.bind(EventManager.class).toInstance(eventManager);
        binder.bind(QueryCache.class).toInstance(new MapQueryCache(5));
        binder.bind(RuntimeProperties.class).toInstance(new DefaultRuntimeProperties(Collections.<String, String>emptyMap()));
        binder.bind(ObjectMapRetainStrategy.class).to(DefaultObjectMapRetainStrategy.class);
        binder.bind(ObjectStoreFactory.class).to(DefaultObjectStoreFactory.class);
        binder.bind(TransactionFactory.class).to(DefaultTransactionFactory.class);
        binder.bind(TransactionManager.class).to(DefaultTransactionManager.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
        binder.bind(EventBridge.class).toProvider(NoopEventBridgeProvider.class);
        binder.bind(DataRowStoreFactory.class).to(DefaultDataRowStoreFactory.class);
    };
    Injector injector = DIBootstrap.createInjector(testModule);
    DataContextFactory factory = new DataContextFactory();
    injector.injectMembers(factory);
    DataContext c3 = (DataContext) factory.createContext();
    assertNotNull(c3.getObjectStore().getDataRowCache());
    assertNull(domain.getSharedSnapshotCache());
    assertNotSame(c3.getObjectStore().getDataRowCache(), domain.getSharedSnapshotCache());
}
Also used : RuntimeProperties(org.apache.cayenne.configuration.RuntimeProperties) Injector(org.apache.cayenne.di.Injector) DataContext(org.apache.cayenne.access.DataContext) DataRowStoreFactory(org.apache.cayenne.access.DataRowStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) TransactionManager(org.apache.cayenne.tx.TransactionManager) QueryCache(org.apache.cayenne.cache.QueryCache) Module(org.apache.cayenne.di.Module) ObjectMapRetainStrategy(org.apache.cayenne.access.ObjectMapRetainStrategy) ObjectStoreFactory(org.apache.cayenne.configuration.ObjectStoreFactory) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DataDomain(org.apache.cayenne.access.DataDomain) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) EventBridge(org.apache.cayenne.event.EventBridge) MockEventManager(org.apache.cayenne.event.MockEventManager) JdbcEventLogger(org.apache.cayenne.log.JdbcEventLogger) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Test(org.junit.Test) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) EventManager(org.apache.cayenne.event.EventManager) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Assert(org.junit.Assert) Collections(java.util.Collections) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) DataDomain(org.apache.cayenne.access.DataDomain) MockEventManager(org.apache.cayenne.event.MockEventManager) EventManager(org.apache.cayenne.event.EventManager) DefaultObjectStoreFactory(org.apache.cayenne.configuration.DefaultObjectStoreFactory) MapQueryCache(org.apache.cayenne.cache.MapQueryCache) DefaultRuntimeProperties(org.apache.cayenne.configuration.DefaultRuntimeProperties) DefaultDataRowStoreFactory(org.apache.cayenne.access.DefaultDataRowStoreFactory) DefaultTransactionManager(org.apache.cayenne.tx.DefaultTransactionManager) DataContext(org.apache.cayenne.access.DataContext) DefaultObjectMapRetainStrategy(org.apache.cayenne.access.DefaultObjectMapRetainStrategy) DefaultTransactionFactory(org.apache.cayenne.tx.DefaultTransactionFactory) Slf4jJdbcEventLogger(org.apache.cayenne.log.Slf4jJdbcEventLogger) Injector(org.apache.cayenne.di.Injector) NoopEventBridgeProvider(org.apache.cayenne.event.NoopEventBridgeProvider) Module(org.apache.cayenne.di.Module) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 5 with DataContext

use of org.apache.cayenne.access.DataContext in project cayenne by apache.

the class ServerRuntimeTest method testGetObjectContext_CustomModule.

@Test
public void testGetObjectContext_CustomModule() {
    final ObjectContext context = new DataContext();
    final ObjectContextFactory factory = new ObjectContextFactory() {

        public ObjectContext createContext(DataChannel parent) {
            return context;
        }

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

Aggregations

DataContext (org.apache.cayenne.access.DataContext)12 Test (org.junit.Test)8 Module (org.apache.cayenne.di.Module)4 Assert (org.junit.Assert)4 Collections (java.util.Collections)3 NestedQueryCache (org.apache.cayenne.cache.NestedQueryCache)3 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)3 DIBootstrap (org.apache.cayenne.di.DIBootstrap)3 EventManager (org.apache.cayenne.event.EventManager)3 TransactionFactory (org.apache.cayenne.tx.TransactionFactory)3 Collection (java.util.Collection)2 DataChannel (org.apache.cayenne.DataChannel)2 ObjectContext (org.apache.cayenne.ObjectContext)2 ClientServerChannel (org.apache.cayenne.access.ClientServerChannel)2 DataDomain (org.apache.cayenne.access.DataDomain)2 DataRowStore (org.apache.cayenne.access.DataRowStore)2 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)2 DefaultDataRowStoreFactory (org.apache.cayenne.access.DefaultDataRowStoreFactory)2 DefaultObjectMapRetainStrategy (org.apache.cayenne.access.DefaultObjectMapRetainStrategy)2 ObjectMapRetainStrategy (org.apache.cayenne.access.ObjectMapRetainStrategy)2