Search in sources :

Example 11 with DataChannel

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

the class HessianServiceTest method testGetSession.

@Test
public void testGetSession() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put(Constants.SERVER_ROP_EVENT_BRIDGE_FACTORY_PROPERTY, MockEventBridgeFactory.class.getName());
    ObjectContextFactory factory = new ObjectContextFactory() {

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

        public ObjectContext createContext() {
            return null;
        }
    };
    HessianService service = new HessianService(factory, map);
    MockHttpServletRequest request = new MockHttpServletRequest();
    HttpSession session = new MockHttpSession();
    request.setSession(session);
    // for some reason need to call this to get session activated in the
    // mock request
    request.getSession();
    try {
        ServiceContext.begin(request, null, null, null);
        assertSame(session, service.getSession(false));
    } finally {
        ServiceContext.end();
    }
}
Also used : DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) MockHttpServletRequest(com.mockrunner.mock.web.MockHttpServletRequest) HttpSession(javax.servlet.http.HttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) MockHttpSession(com.mockrunner.mock.web.MockHttpSession) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockEventBridgeFactory(org.apache.cayenne.event.MockEventBridgeFactory) Test(org.junit.Test)

Example 12 with DataChannel

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

the class BaseRemoteServiceTest method testProcessMessageExceptionSerializability.

@Test
public void testProcessMessageExceptionSerializability() throws Throwable {
    Map<String, String> map = new HashMap<>();
    ObjectContextFactory factory = new ObjectContextFactory() {

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

        public ObjectContext createContext() {
            return null;
        }
    };
    BaseRemoteService service = new BaseRemoteService(factory, map) {

        @Override
        protected ServerSession createServerSession() {
            return new ServerSession(new RemoteSession("a"), null);
        }

        @Override
        protected ServerSession createServerSession(String name) {
            return createServerSession();
        }

        @Override
        protected ServerSession getServerSession() {
            return createServerSession();
        }
    };
    try {
        service.processMessage(new QueryMessage(null) {

            @Override
            public Query getQuery() {
                // serializable exception thrown
                throw new CayenneRuntimeException();
            }
        });
        fail("Expected to throw");
    } catch (Exception ex) {
        Util.cloneViaSerialization(ex);
    }
    try {
        service.processMessage(new QueryMessage(null) {

            @Override
            public Query getQuery() {
                // non-serializable exception thrown
                throw new MockUnserializableException();
            }
        });
        fail("Expected to throw");
    } catch (Exception ex) {
        Util.cloneViaSerialization(ex);
    }
}
Also used : QueryMessage(org.apache.cayenne.remote.QueryMessage) Query(org.apache.cayenne.query.Query) HashMap(java.util.HashMap) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataChannel(org.apache.cayenne.DataChannel) RemoteSession(org.apache.cayenne.remote.RemoteSession) Test(org.junit.Test)

Example 13 with DataChannel

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

the class BaseRemoteServiceTest method testConstructor.

@Test
public void testConstructor() throws Exception {
    Map<String, String> map = new HashMap<>();
    map.put(Constants.SERVER_ROP_EVENT_BRIDGE_FACTORY_PROPERTY, MockEventBridgeFactory.class.getName());
    ObjectContextFactory factory = new ObjectContextFactory() {

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

        public ObjectContext createContext() {
            return null;
        }
    };
    BaseRemoteService service = new BaseRemoteService(factory, map) {

        @Override
        protected ServerSession createServerSession() {
            return null;
        }

        @Override
        protected ServerSession createServerSession(String name) {
            return null;
        }

        @Override
        protected ServerSession getServerSession() {
            return null;
        }
    };
    assertEquals(MockEventBridgeFactory.class.getName(), service.getEventBridgeFactoryName());
    assertSame(factory, service.contextFactory);
}
Also used : DataChannel(org.apache.cayenne.DataChannel) HashMap(java.util.HashMap) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) MockEventBridgeFactory(org.apache.cayenne.event.MockEventBridgeFactory) Test(org.junit.Test)

Example 14 with DataChannel

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

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

the class ClientModuleTest method testDataChannel.

@Test
public void testDataChannel() {
    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);
    DataChannel channel = injector.getInstance(DataChannel.class);
    assertNotNull(channel);
    assertTrue(channel instanceof ClientChannel);
    assertSame("DataChannel must be a singleton", channel, injector.getInstance(DataChannel.class));
    ClientChannel clientChannel = (ClientChannel) channel;
    assertTrue(clientChannel.getConnection() instanceof MockClientConnection);
    assertTrue(clientChannel.getEventManager() instanceof DefaultEventManager);
    assertFalse(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) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) MockClientConnection(org.apache.cayenne.remote.MockClientConnection) ClientConnection(org.apache.cayenne.remote.ClientConnection) ClientChannel(org.apache.cayenne.remote.ClientChannel) 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