Search in sources :

Example 6 with ObjectContext

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

the class Runtime_AES128_IT method testInsert_MultipleObjects.

@Test
public void testInsert_MultipleObjects() throws SQLException {
    ObjectContext context = runtime.newContext();
    Table2 t1 = context.newObject(Table2.class);
    t1.setPlainBytes("a".getBytes());
    t1.setCryptoBytes("crypto_1".getBytes());
    Table2 t2 = context.newObject(Table2.class);
    t2.setPlainBytes("b".getBytes());
    t2.setCryptoBytes("crypto_2".getBytes());
    Table2 t3 = context.newObject(Table2.class);
    t3.setPlainBytes("c".getBytes());
    t3.setCryptoBytes(null);
    context.commitChanges();
    List<Object[]> data = table2.selectAll();
    assertEquals(3, data.size());
    Map<String, byte[]> cipherByPlain = new HashMap<>();
    for (Object[] r : data) {
        cipherByPlain.put(new String((byte[]) r[1]), (byte[]) r[2]);
    }
    assertArrayEquals("crypto_1".getBytes(), CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("a"), runtime));
    assertArrayEquals("crypto_2".getBytes(), CryptoUnitUtils.decrypt_AES_CBC(cipherByPlain.get("b"), runtime));
    assertNull(cipherByPlain.get("c"));
}
Also used : HashMap(java.util.HashMap) Table2(org.apache.cayenne.crypto.db.Table2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 7 with ObjectContext

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

the class Runtime_AES128_IT method testInsert_Numeric.

@Test
public void testInsert_Numeric() throws SQLException {
    ObjectContext context = runtime.newContext();
    Table1 t1 = context.newObject(Table1.class);
    t1.setPlainInt(59);
    t1.setCryptoInt(61);
    context.commitChanges();
    Object[] data = table1.select();
    assertEquals(59, data[3]);
    assertEquals(new Integer(61), IntegerConverter.INSTANCE.fromBytes(CryptoUnitUtils.decrypt_AES_CBC((byte[]) data[4], runtime)));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Table1(org.apache.cayenne.crypto.db.Table1) Test(org.junit.Test)

Example 8 with ObjectContext

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

the class ClientServerChannelIT method testOnQuery.

@Test
public void testOnQuery() {
    final boolean[] genericDone = new boolean[1];
    MockDataChannel parent = new MockDataChannel(new EntityResolver()) {

        @Override
        public QueryResponse onQuery(ObjectContext context, Query query) {
            genericDone[0] = true;
            return super.onQuery(context, query);
        }
    };
    DataContext context = (DataContext) runtime.newContext(parent);
    QueryMessage message = new QueryMessage(new MockQuery());
    new ClientServerChannel(context).onQuery(null, message.getQuery());
    assertTrue(genericDone[0]);
}
Also used : QueryMessage(org.apache.cayenne.remote.QueryMessage) SelectQuery(org.apache.cayenne.query.SelectQuery) MockQuery(org.apache.cayenne.query.MockQuery) Query(org.apache.cayenne.query.Query) EntityResolver(org.apache.cayenne.map.EntityResolver) ObjectContext(org.apache.cayenne.ObjectContext) MockQuery(org.apache.cayenne.query.MockQuery) MockDataChannel(org.apache.cayenne.MockDataChannel) Test(org.junit.Test)

Example 9 with ObjectContext

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

the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.

@Test
public void testReturnDiffInPrePersist() {
    final List<GenericDiff> diffs = new ArrayList<GenericDiff>();
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
            super.nodePropertyChanged(nodeId, property, oldValue, newValue);
            diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
        }
    };
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

            @Override
            public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
                GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
                assertNotNull(serverDiff);
                serverDiff.apply(diffReader);
                return serverDiff;
            }
        };
        CayenneContext context = new CayenneContext(channel);
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        ObjectId tempId = o.getObjectId();
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertEquals(2, diffReader.size);
        assertEquals(1, diffs.size());
        assertEquals(tempId, diffs.get(0).sourceId);
        assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, diffs.get(0).property);
        assertNull(diffs.get(0).oldValue);
        assertEquals("XXX", diffs.get(0).newValue);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) CayenneContext(org.apache.cayenne.CayenneContext) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectContext(org.apache.cayenne.ObjectContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 10 with ObjectContext

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

the class NestedObjectContextPeerEventsIT method testPeerObjectUpdatedToManyRelationship.

@Test
public void testPeerObjectUpdatedToManyRelationship() throws Exception {
    ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
    a.setGlobalAttribute1("X");
    ClientMtTable2 px = clientContext.newObject(ClientMtTable2.class);
    px.setTable1(a);
    px.setGlobalAttribute("PX");
    ClientMtTable2 py = clientContext.newObject(ClientMtTable2.class);
    py.setGlobalAttribute("PY");
    clientContext.commitChanges();
    ObjectContext peer1 = runtime.newContext(clientContext);
    ClientMtTable2 py1 = peer1.localObject(py);
    ClientMtTable1 a1 = peer1.localObject(a);
    ObjectContext peer2 = runtime.newContext(clientContext);
    ClientMtTable2 py2 = peer2.localObject(py);
    ClientMtTable1 a2 = peer2.localObject(a);
    a1.addToTable2Array(py1);
    assertEquals(1, a2.getTable2Array().size());
    assertFalse(a2.getTable2Array().contains(py2));
    peer1.commitChangesToParent();
    assertEquals(2, a2.getTable2Array().size());
    assertTrue(a2.getTable2Array().contains(py2));
    assertFalse("Peer data context became dirty on event processing", peer2.hasChanges());
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Aggregations

ObjectContext (org.apache.cayenne.ObjectContext)108 Test (org.junit.Test)99 Artist (org.apache.cayenne.testdo.testmap.Artist)32 UnitTestClosure (org.apache.cayenne.unit.di.UnitTestClosure)14 SelectQuery (org.apache.cayenne.query.SelectQuery)13 Painting (org.apache.cayenne.testdo.testmap.Painting)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 ParallelTestContainer (org.apache.cayenne.test.parallel.ParallelTestContainer)10 GraphDiff (org.apache.cayenne.graph.GraphDiff)9 ObjectId (org.apache.cayenne.ObjectId)8 Table2 (org.apache.cayenne.crypto.db.Table2)7 HashMap (java.util.HashMap)6 Persistent (org.apache.cayenne.Persistent)6 QueryResponse (org.apache.cayenne.QueryResponse)6 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)6 Query (org.apache.cayenne.query.Query)5 CayenneContext (org.apache.cayenne.CayenneContext)4 DataChannel (org.apache.cayenne.DataChannel)4 ObjectContextFactory (org.apache.cayenne.configuration.ObjectContextFactory)4 EntityResolver (org.apache.cayenne.map.EntityResolver)4