Search in sources :

Example 1 with JPAEntityGraph

use of org.datanucleus.api.jpa.JPAEntityGraph in project tests by datanucleus.

the class EntityGraphTest method testNamedGraphSpecification.

/**
 * Test of specification and registering of a named EntityGraph.
 */
public void testNamedGraphSpecification() {
    try {
        Set<String> graphNames = ((JPAEntityManagerFactory) emf).getEntityGraphNames();
        assertNotNull(graphNames);
        assertEquals("Number of EntityGraphs is wrong", 1, graphNames.size());
        JPAEntityGraph eg = (JPAEntityGraph) ((JPAEntityManagerFactory) emf).getNamedEntityGraph(graphNames.iterator().next());
        assertEquals("baseGraph", eg.getName());
        assertEquals(GraphBase.class, eg.getClassType());
        assertFalse(eg.getIncludeAllAttributes());
        List<AttributeNode<?>> egNodes = eg.getAttributeNodes();
        assertNotNull(egNodes);
        assertEquals(3, egNodes.size());
        boolean idPresent = false;
        boolean namePresent = false;
        boolean relationPresent = false;
        for (AttributeNode node : egNodes) {
            if (node.getAttributeName().equals("id")) {
                idPresent = true;
            } else if (node.getAttributeName().equals("name")) {
                namePresent = true;
            } else if (node.getAttributeName().equals("relation")) {
                relationPresent = true;
                Map<Class, Subgraph> subgraphsByClass = node.getSubgraphs();
                assertNotNull(subgraphsByClass);
                assertEquals(1, subgraphsByClass.size());
                Map.Entry<Class, Subgraph> subgraphEntry = subgraphsByClass.entrySet().iterator().next();
                assertEquals(GraphRelated.class, subgraphEntry.getKey());
                Subgraph subgraph = subgraphEntry.getValue();
                List<AttributeNode<?>> subNodes = subgraph.getAttributeNodes();
                assertNotNull(subNodes);
                assertEquals(1, subNodes.size());
                AttributeNode subNode = subNodes.iterator().next();
                assertEquals("id", subNode.getAttributeName());
            }
        }
        assertTrue("id not present", idPresent);
        assertTrue("name not present", namePresent);
        assertTrue("relation not present", relationPresent);
        // Should have been registered as dynamic FetchGroups
        PersistenceNucleusContext nucCtx = emf.unwrap(PersistenceNucleusContext.class);
        Set<FetchGroup> fgs = nucCtx.getFetchGroupManager().getFetchGroupsWithName("baseGraph");
        assertEquals(2, fgs.size());
    } finally {
    }
}
Also used : AttributeNode(javax.persistence.AttributeNode) JPAEntityGraph(org.datanucleus.api.jpa.JPAEntityGraph) JPAEntityManagerFactory(org.datanucleus.api.jpa.JPAEntityManagerFactory) Subgraph(javax.persistence.Subgraph) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) FetchGroup(org.datanucleus.FetchGroup) Map(java.util.Map)

Aggregations

Map (java.util.Map)1 AttributeNode (javax.persistence.AttributeNode)1 Subgraph (javax.persistence.Subgraph)1 FetchGroup (org.datanucleus.FetchGroup)1 PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)1 JPAEntityGraph (org.datanucleus.api.jpa.JPAEntityGraph)1 JPAEntityManagerFactory (org.datanucleus.api.jpa.JPAEntityManagerFactory)1