Search in sources :

Example 6 with ObjectContextFactory

use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.

the class ClientLocalRuntimeTest method testGetConnection.

@Test
public void testGetConnection() {
    final DataContext serverContext = mock(DataContext.class);
    Module serverModule = binder -> binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {

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

        public ObjectContext createContext() {
            return serverContext;
        }
    });
    ClientRuntime runtime = ClientRuntime.builder().local(DIBootstrap.createInjector(serverModule)).build();
    ClientConnection connection = runtime.getConnection();
    assertNotNull(connection);
    assertTrue(connection instanceof LocalConnection);
    LocalConnection localConnection = (LocalConnection) connection;
    assertTrue(localConnection.getChannel() instanceof ClientServerChannel);
    ClientServerChannel clientServerChannel = (ClientServerChannel) localConnection.getChannel();
    assertSame(serverContext, clientServerChannel.getParentChannel());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) DataChannel(org.apache.cayenne.DataChannel) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) ClientConnection(org.apache.cayenne.remote.ClientConnection) LocalConnection(org.apache.cayenne.remote.service.LocalConnection) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) DataContext(org.apache.cayenne.access.DataContext) DataChannel(org.apache.cayenne.DataChannel) LocalConnection(org.apache.cayenne.remote.service.LocalConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) ClientServerChannel(org.apache.cayenne.access.ClientServerChannel) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) Test(org.junit.Test)

Example 7 with ObjectContextFactory

use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.

the class ClientModuleTest method testObjectContextFactory.

@Test
public void testObjectContextFactory() {
    Map<String, String> properties = new HashMap<>();
    ClientModule module = new ClientModule() {

        @Override
        public void configure(Binder binder) {
            super.configure(binder);
            // use a noop connection to prevent startup errors...
            binder.bind(ClientConnection.class).to(MockClientConnection.class);
        }
    };
    Injector injector = DIBootstrap.createInjector(module);
    ObjectContextFactory factory = injector.getInstance(ObjectContextFactory.class);
    assertNotNull(factory);
    assertSame("ObjectContextFactory must be a singleton", factory, injector.getInstance(ObjectContextFactory.class));
}
Also used : Binder(org.apache.cayenne.di.Binder) HashMap(java.util.HashMap) Injector(org.apache.cayenne.di.Injector) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) Test(org.junit.Test)

Example 8 with ObjectContextFactory

use of org.apache.cayenne.configuration.ObjectContextFactory in project cayenne by apache.

the class ServletContextHandlerTest method testRequestStart_bindContext.

@Test
public void testRequestStart_bindContext() {
    Module module = binder -> {
        binder.bind(DataChannel.class).to(MockDataChannel.class);
        binder.bind(ObjectContextFactory.class).toInstance(new ObjectContextFactory() {

            public ObjectContext createContext(DataChannel parent) {
                return mock(ObjectContext.class);
            }

            public ObjectContext createContext() {
                return mock(ObjectContext.class);
            }
        });
    };
    Injector injector = DIBootstrap.createInjector(module);
    SessionContextRequestHandler handler = new SessionContextRequestHandler();
    injector.injectMembers(handler);
    MockHttpSession session = new MockHttpSession();
    BaseContext.bindThreadObjectContext(null);
    try {
        MockHttpServletRequest request1 = new MockHttpServletRequest();
        MockHttpServletResponse response1 = new MockHttpServletResponse();
        request1.setSession(session);
        handler.requestStart(request1, response1);
        ObjectContext c1 = BaseContext.getThreadObjectContext();
        assertNotNull(c1);
        handler.requestEnd(request1, response1);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
        MockHttpServletRequest request2 = new MockHttpServletRequest();
        MockHttpServletResponse response2 = new MockHttpServletResponse();
        request2.setSession(session);
        handler.requestStart(request2, response2);
        ObjectContext c2 = BaseContext.getThreadObjectContext();
        assertSame(c1, c2);
        handler.requestEnd(request2, response2);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
        MockHttpServletRequest request3 = new MockHttpServletRequest();
        MockHttpServletResponse response3 = new MockHttpServletResponse();
        request3.setSession(new MockHttpSession());
        handler.requestStart(request3, response3);
        ObjectContext c3 = BaseContext.getThreadObjectContext();
        assertNotNull(c3);
        assertNotSame(c1, c3);
        handler.requestEnd(request3, response3);
        try {
            BaseContext.getThreadObjectContext();
            fail("thread context not null");
        } catch (IllegalStateException e) {
        // expected
        }
    } finally {
        BaseContext.bindThreadObjectContext(null);
    }
}
Also used : Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) ObjectContext(org.apache.cayenne.ObjectContext) MockDataChannel(org.apache.cayenne.MockDataChannel) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) BaseContext(org.apache.cayenne.BaseContext) DataChannel(org.apache.cayenne.DataChannel) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) DIBootstrap(org.apache.cayenne.di.DIBootstrap) Assert(org.junit.Assert) Mockito.mock(org.mockito.Mockito.mock) MockDataChannel(org.apache.cayenne.MockDataChannel) DataChannel(org.apache.cayenne.DataChannel) Injector(org.apache.cayenne.di.Injector) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockHttpServletResponse(com.mockrunner.mock.web.MockHttpServletResponse) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Aggregations

ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)8 Test (org.junit.Test)7 DataChannel (org.apache.cayenne.DataChannel)6 HashMap (java.util.HashMap)4 ObjectContext (org.apache.cayenne.ObjectContext)3 DataContext (org.apache.cayenne.access.DataContext)3 Module (org.apache.cayenne.di.Module)3 Assert (org.junit.Assert)3 Mockito.mock (org.mockito.Mockito.mock)3 MockHttpServletRequest (com.mockrunner.mock.web.MockHttpServletRequest)2 MockHttpSession (com.mockrunner.mock.web.MockHttpSession)2 Collection (java.util.Collection)2 ClientServerChannel (org.apache.cayenne.access.ClientServerChannel)2 DIBootstrap (org.apache.cayenne.di.DIBootstrap)2 Injector (org.apache.cayenne.di.Injector)2 MockEventBridgeFactory (org.apache.cayenne.event.MockEventBridgeFactory)2 Query (org.apache.cayenne.query.Query)2 ClientConnection (org.apache.cayenne.remote.ClientConnection)2 MockHttpServletResponse (com.mockrunner.mock.web.MockHttpServletResponse)1 Arrays (java.util.Arrays)1