Search in sources :

Example 16 with DataChannel

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

the class ClientRuntimeBuilderTest method testDataChannel_NoChannelEvents.

@Test
public void testDataChannel_NoChannelEvents() {
    Map<String, String> properties1 = new HashMap<>();
    properties1.put(ClientConstants.ROP_CHANNEL_EVENTS_PROPERTY, "true");
    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);
            ServerModule.contributeProperties(binder).putAll(properties1);
        }
    };
    Injector injector = DIBootstrap.createInjector(module);
    DataChannel channel = injector.getInstance(DataChannel.class);
    ClientChannel clientChannel = (ClientChannel) channel;
    assertTrue(clientChannel.isChannelEventsEnabled());
}
Also used : Binder(org.apache.cayenne.di.Binder) DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) Injector(org.apache.cayenne.di.Injector) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) HttpClientConnection(org.apache.cayenne.rop.HttpClientConnection) ClientChannel(org.apache.cayenne.remote.ClientChannel) Test(org.junit.Test)

Example 17 with DataChannel

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

the class ClientRuntimeTest method testGetDataChannel.

@Test
public void testGetDataChannel() {
    Map<String, String> properties = new HashMap<>();
    Module extraModule = binder -> binder.bind(ClientConnection.class).to(MockClientConnection.class);
    ClientRuntime runtime = ClientRuntime.builder().properties(properties).addModule(extraModule).build();
    DataChannel channel = runtime.getChannel();
    assertNotNull(channel);
    assertTrue(channel instanceof ClientChannel);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) Binder(org.apache.cayenne.di.Binder) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertNotSame(org.junit.Assert.assertNotSame) Module(org.apache.cayenne.di.Module) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) HashMap(java.util.HashMap) DataChannel(org.apache.cayenne.DataChannel) Assert.assertSame(org.junit.Assert.assertSame) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientConnection(org.apache.cayenne.remote.ClientConnection) EventManager(org.apache.cayenne.event.EventManager) Assert.assertFalse(org.junit.Assert.assertFalse) Map(java.util.Map) CayenneContext(org.apache.cayenne.CayenneContext) DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) Module(org.apache.cayenne.di.Module) ClientChannel(org.apache.cayenne.remote.ClientChannel) Test(org.junit.Test)

Example 18 with DataChannel

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

the class LocalConnectionTest method testConstructors.

@Test
public void testConstructors() {
    DataChannel handler1 = new MockDataChannel();
    LocalConnection connector1 = new LocalConnection(handler1);
    assertFalse(connector1.isSerializingMessages());
    assertSame(handler1, connector1.getChannel());
    DataChannel handler2 = new MockDataChannel();
    LocalConnection connector2 = new LocalConnection(handler2, LocalConnection.JAVA_SERIALIZATION);
    assertTrue(connector2.isSerializingMessages());
    assertSame(handler2, connector2.getChannel());
}
Also used : MockDataChannel(org.apache.cayenne.MockDataChannel) DataChannel(org.apache.cayenne.DataChannel) LocalConnection(org.apache.cayenne.remote.service.LocalConnection) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Example 19 with DataChannel

use of org.apache.cayenne.DataChannel 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

DataChannel (org.apache.cayenne.DataChannel)19 Test (org.junit.Test)13 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)8 HashMap (java.util.HashMap)6 ObjectContext (org.apache.cayenne.ObjectContext)5 Module (org.apache.cayenne.di.Module)5 EventManager (org.apache.cayenne.event.EventManager)5 Collection (java.util.Collection)4 Injector (org.apache.cayenne.di.Injector)4 ClientConnection (org.apache.cayenne.remote.ClientConnection)4 Assert (org.junit.Assert)4 Mockito.mock (org.mockito.Mockito.mock)4 Collections (java.util.Collections)3 HttpSession (javax.servlet.http.HttpSession)3 DataContext (org.apache.cayenne.access.DataContext)3 Binder (org.apache.cayenne.di.Binder)3 DIBootstrap (org.apache.cayenne.di.DIBootstrap)3 ClientChannel (org.apache.cayenne.remote.ClientChannel)3 MockClientConnection (org.apache.cayenne.remote.MockClientConnection)3 MockHttpServletRequest (com.mockrunner.mock.web.MockHttpServletRequest)2