Search in sources :

Example 1 with ClientChannel

use of org.apache.cayenne.remote.ClientChannel in project cayenne by apache.

the class CayenneContextIT method testBeforeHollowDeleteShouldChangeStateToCommited.

@Test
public void testBeforeHollowDeleteShouldChangeStateToCommited() {
    ObjectId gid = new ObjectId("MtTable1", "a", "b");
    final ClientMtTable1 inflated = new ClientMtTable1();
    inflated.setPersistenceState(PersistenceState.COMMITTED);
    inflated.setObjectId(gid);
    inflated.setGlobalAttribute1("abc");
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(inflated));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    CayenneContext context = new CayenneContext(channel);
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    ClientMtTable1 hollow = context.localObject(inflated);
    assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
    // testing this...
    context.deleteObjects(hollow);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.DELETED, hollow.getPersistenceState());
}
Also used : GenericResponse(org.apache.cayenne.util.GenericResponse) BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) ClientMessage(org.apache.cayenne.remote.ClientMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(org.apache.cayenne.remote.ClientConnection) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 2 with ClientChannel

use of org.apache.cayenne.remote.ClientChannel in project cayenne by apache.

the class CayenneContextIT method testBeforePropertyReadShouldInflateHollow.

@Test
public void testBeforePropertyReadShouldInflateHollow() {
    ObjectId gid = new ObjectId("MtTable1", "a", "b");
    final ClientMtTable1 inflated = new ClientMtTable1();
    inflated.setPersistenceState(PersistenceState.COMMITTED);
    inflated.setObjectId(gid);
    inflated.setGlobalAttribute1("abc");
    ClientConnection connection = mock(ClientConnection.class);
    when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {

        public Object answer(InvocationOnMock invocation) {
            ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
            if (arg instanceof BootstrapMessage) {
                return new EntityResolver();
            } else {
                return new GenericResponse(Arrays.asList(inflated));
            }
        }
    });
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    // check that a HOLLOW object is infalted on "beforePropertyRead"
    ClientMtTable1 hollow = new ClientMtTable1();
    hollow.setPersistenceState(PersistenceState.HOLLOW);
    hollow.setObjectId(gid);
    final boolean[] selectExecuted = new boolean[1];
    CayenneContext context = new CayenneContext(channel) {

        @Override
        public List<?> performQuery(Query query) {
            selectExecuted[0] = true;
            return super.performQuery(query);
        }
    };
    context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
    context.graphManager.registerNode(hollow.getObjectId(), hollow);
    // testing this...
    context.prepareForAccess(hollow, ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, false);
    assertTrue(selectExecuted[0]);
    assertSame(hollow, context.getGraphManager().getNode(gid));
    assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
    assertEquals(PersistenceState.COMMITTED, hollow.getPersistenceState());
}
Also used : Query(org.apache.cayenne.query.Query) GenericResponse(org.apache.cayenne.util.GenericResponse) BootstrapMessage(org.apache.cayenne.remote.BootstrapMessage) ClientMessage(org.apache.cayenne.remote.ClientMessage) EntityResolver(org.apache.cayenne.map.EntityResolver) ClientChannel(org.apache.cayenne.remote.ClientChannel) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ClientConnection(org.apache.cayenne.remote.ClientConnection) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 3 with ClientChannel

use of org.apache.cayenne.remote.ClientChannel 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)

Example 4 with ClientChannel

use of org.apache.cayenne.remote.ClientChannel 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 5 with ClientChannel

use of org.apache.cayenne.remote.ClientChannel 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)

Aggregations

ClientChannel (org.apache.cayenne.remote.ClientChannel)5 ClientConnection (org.apache.cayenne.remote.ClientConnection)5 Test (org.junit.Test)5 HashMap (java.util.HashMap)3 DataChannel (org.apache.cayenne.DataChannel)3 Binder (org.apache.cayenne.di.Binder)3 MockClientConnection (org.apache.cayenne.remote.MockClientConnection)3 Injector (org.apache.cayenne.di.Injector)2 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)2 MockEventManager (org.apache.cayenne.event.MockEventManager)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 BootstrapMessage (org.apache.cayenne.remote.BootstrapMessage)2 ClientMessage (org.apache.cayenne.remote.ClientMessage)2 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)2 GenericResponse (org.apache.cayenne.util.GenericResponse)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Map (java.util.Map)1 CayenneContext (org.apache.cayenne.CayenneContext)1 ObjectContext (org.apache.cayenne.ObjectContext)1 Module (org.apache.cayenne.di.Module)1