Package org.apache.cayenne.access

Examples of org.apache.cayenne.access.DataContext


    }

    public void testAddToFlattenedRelationship() throws Exception {

        createTestData("testAddToFlattenedRelationship");
        DataContext context = createDataContext();

        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);
        assertEquals(0, a1.getGroupArray().size());

        SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                "name",
                "g1"));
        List results = context.performQuery(q);
        assertEquals(1, results.size());

        assertFalse(context.hasChanges());
        ArtGroup group = (ArtGroup) results.get(0);
        a1.addToGroupArray(group);
        assertTrue(context.hasChanges());

        List groupList = a1.getGroupArray();
        assertEquals(1, groupList.size());
        assertEquals("g1", ((ArtGroup) groupList.get(0)).getName());

        // Ensure that the commit doesn't fail
        a1.getObjectContext().commitChanges();

        // and check again
        assertFalse(context.hasChanges());

        // refetch artist with a different context
        context = createDataContext();
        a1 = Cayenne.objectForPK(context, Artist.class, 33001);
        groupList = a1.getGroupArray();
View Full Code Here


    }

    // Test case to show up a bug in committing more than once
    public void testDoubleCommitAddToFlattenedRelationship() throws Exception {
        createTestData("testDoubleCommitAddToFlattenedRelationship");
        DataContext context = createDataContext();

        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);

        SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                "name",
                "g1"));
        List results = context.performQuery(q);
        assertEquals(1, results.size());

        ArtGroup group = (ArtGroup) results.get(0);
        a1.addToGroupArray(group);
View Full Code Here

    }

    public void testRemoveFromFlattenedRelationship() throws Exception {
        createTestData("testRemoveFromFlattenedRelationship");
        DataContext context = createDataContext();

        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);

        ArtGroup group = a1.getGroupArray().get(0);
        a1.removeFromGroupArray(group);
View Full Code Here

    // Shows up a possible bug in ordering of deletes, when a flattened relationships link
    // record is deleted
    // at the same time (same transaction) as one of the record to which it links.
    public void testRemoveFlattenedRelationshipAndRootRecord() throws Exception {
        DataContext context = createDataContext();
        createTestData("testRemoveFlattenedRelationshipAndRootRecord");
        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);

        ArtGroup group = a1.getGroupArray().get(0);
        a1.removeFromGroupArray(group); // Cause the delete of the link record

        context.deleteObject(a1); // Cause the deletion of the artist

        try {
            context.commitChanges();
        }
        catch (Exception e) {
            e.printStackTrace();
            fail("Should not have thrown the exception :" + e.getMessage());
        }
View Full Code Here

            fail("Should not have thrown the exception :" + e.getMessage());
        }
    }

    public void testAddRemoveFlattenedRelationship1() throws Exception {
        DataContext context = createDataContext();
        createTestData("testAddRemoveFlattenedRelationship1");
        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);

        SelectQuery q = new SelectQuery(ArtGroup.class, ExpressionFactory.matchExp(
                "name",
                "g1"));
        List results = context.performQuery(q);
        assertEquals(1, results.size());

        ArtGroup group = (ArtGroup) results.get(0);
        a1.addToGroupArray(group);
        group.removeFromArtistArray(a1);

        blockQueries();
        try {
            context.commitChanges();
        }
        finally {
            unblockQueries();
        }
    }
View Full Code Here

    }

    public void testAddRemoveFlattenedRelationship2() throws Exception {
        createTestData("testAddRemoveFlattenedRelationship2");

        DataContext context = createDataContext();

        Artist a1 = Cayenne.objectForPK(context, Artist.class, 33001);

        SelectQuery q = new SelectQuery(ArtGroup.class);
        List results = context.performQuery(q);
        assertEquals(2, results.size());

        ArtGroup g1 = (ArtGroup) results.get(0);
        ArtGroup g2 = (ArtGroup) results.get(1);
        a1.addToGroupArray(g1);
        a1.addToGroupArray(g2);

        // test that there is no delete query issued when a flattened join is first
        // added
        // and then deleted AND there are some other changes (CAY-548)
        a1.removeFromGroupArray(g1);

        MockDataNode engine = MockDataNode.interceptNode(getDomain(), getNode());
        try {
            context.commitChanges();
            assertEquals(1, engine.getRunCount());
        }
        finally {
            engine.stopInterceptNode();
        }
View Full Code Here

        }
    }

    public void testLocalCacheStaysLocal() {

        DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();

        SelectQuery query = new SelectQuery(ClientMtTable1.class);
        query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);

        assertEquals(0, clientContext.getQueryCache().size());
        assertEquals(0, serverContext.getQueryCache().size());

        List<?> results = clientContext.performQuery(query);

        assertEquals(1, clientContext.getQueryCache().size());
        assertSame(results, clientContext.getQueryCache().get(
                query.getMetaData(clientContext.getEntityResolver())));

        assertEquals(0, serverContext.getQueryCache().size());
    }
View Full Code Here

                .getSharedSnapshotCache() : new DataRowStore(
                dataDomain.getName(),
                dataDomain.getProperties(),
                eventManager);

        DataContext context = new DataContext(parent, new ObjectStore(snapshotCache));
        context.setValidatingObjectsOnCommit(dataDomain.isValidatingObjectsOnCommit());
        context.setQueryCache(injector.getInstance(Key.get(
                QueryCache.class,
                BaseContext.QUERY_CACHE_INJECTION_KEY)));
        return context;
    }
View Full Code Here

        }
    }

    public void testPostAddOnObjectCallback() throws Exception {

        DataContext serverContext = (DataContext) clientServerChannel.getParentChannel();

        LifecycleCallbackRegistry callbackRegistry = serverContext
                .getEntityResolver()
                .getCallbackRegistry();

        try {
            callbackRegistry.addListener(
                    LifecycleEvent.POST_ADD,
                    MtTable1.class,
                    "prePersistMethod");

            Persistent clientObject = clientContext.newObject(ClientMtTable1.class);
            clientContext.commitChanges();

            // find peer
            MtTable1 peer = (MtTable1) serverContext.getGraphManager().getNode(
                    clientObject.getObjectId());

            assertNotNull(peer);
            assertTrue(peer.isPrePersisted());
        }
View Full Code Here

    protected ObjectContext createFromDataContext(DataContext parent) {
        // child ObjectStore should not have direct access to snapshot cache, so do not
        // pass it in constructor.
        ObjectStore objectStore = new ObjectStore();

        DataContext context = new DataContext(parent, objectStore);

        context.setValidatingObjectsOnCommit(parent.isValidatingObjectsOnCommit());
        context.setUsingSharedSnapshotCache(parent.isUsingSharedSnapshotCache());
        context.setQueryCache(injector.getInstance(Key.get(
                QueryCache.class,
                BaseContext.QUERY_CACHE_INJECTION_KEY)));

        return context;
    }
View Full Code Here

TOP

Related Classes of org.apache.cayenne.access.DataContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.