Search in sources :

Example 1 with DynamicEntityImpl

use of org.eclipse.persistence.internal.dynamic.DynamicEntityImpl in project eclipselink by eclipse-ee4j.

the class DynamicJAXBFromSessionsXMLTestCases method compareObjects.

protected void compareObjects(Object controlObject, Object testObject) throws Exception {
    if (objectsAlreadyCheckedForEquality.contains(testObject)) {
        // To handle cyclic relationships, only check each pair of DynamicEntities once
        return;
    }
    if (controlObject == null && testObject == null) {
        // Nothing to check
        return;
    }
    if (controlObject == null && testObject != null) {
        fail("testObject was [" + testObject + "] but controlObject was [" + controlObject + "].");
    }
    if (controlObject != null && testObject == null) {
        fail("testObject was [" + testObject + "] but controlObject was [" + controlObject + "].");
    }
    if (testObject instanceof Element && controlObject instanceof Element) {
        Element testElement = (Element) testObject;
        Element controlElement = (Element) controlObject;
        boolean equal = true;
        try {
            if (testElement.getNodeType() == Node.ATTRIBUTE_NODE && controlElement.getNodeType() == Node.ATTRIBUTE_NODE) {
                Attr att1 = (Attr) testElement;
                Attr att2 = (Attr) controlElement;
                equal = equal && att1.getNodeValue().equals(att2.getNodeValue());
            } else if (testElement.getNodeType() == Node.TEXT_NODE && controlElement.getNodeType() == Node.TEXT_NODE) {
                Text text1 = (Text) testElement;
                Text text2 = (Text) controlElement;
                equal = equal && text1.getNodeValue().equals(text2.getNodeValue());
            } else if (testElement.getNodeType() == Node.ELEMENT_NODE && controlElement.getNodeType() == Node.ELEMENT_NODE) {
                Element elem1 = testElement;
                Element elem2 = controlElement;
                equal = equal && elem1.getNodeName().equals(elem2.getNodeName());
                equal = equal && (elem1.getChildNodes().getLength() == elem2.getChildNodes().getLength());
                compareObjects(elem1.getFirstChild().getNodeValue(), elem2.getFirstChild().getNodeValue());
            }
            if (!equal) {
                fail("testElement was [" + testElement + "] but controlElement was [" + controlElement + "].");
            }
        } catch (Exception e) {
            fail("Element comparison failed: " + e.getLocalizedMessage());
        }
        return;
    }
    if (testObject instanceof List && controlObject instanceof List) {
        if ((((List) testObject).size()) != (((List) controlObject).size())) {
            fail("testObject had [" + ((List) testObject).size() + "] elements but controlObject had [" + ((List) controlObject).size() + "].");
        }
        for (int i = 0; i < ((List) controlObject).size(); i++) {
            compareObjects(((List) controlObject).get(i), ((List) testObject).get(i));
        }
        // We've now checked all the elements so return
        return;
    }
    if (testObject.getClass().isArray() && controlObject.getClass().isArray()) {
        if (!Helper.comparePotentialArrays(testObject, controlObject)) {
            fail("controlObject [" + controlObject + "] is not equal to testObject [" + testObject + "].");
        }
        // We've now checked all the elements so return
        return;
    }
    if (controlObject instanceof DynamicEntityImpl && testObject instanceof DynamicEntityImpl) {
        DynamicEntityImpl dynamicControl = (DynamicEntityImpl) controlObject;
        DynamicEntityImpl dynamicTest = (DynamicEntityImpl) testObject;
        String controlClass = dynamicControl.getType().getClassName();
        String testClass = dynamicTest.getType().getClassName();
        if (!controlClass.equals(testClass)) {
            fail("testObject [" + testClass + "] and controlObject [" + controlClass + "] are not the same class.");
        }
        objectsAlreadyCheckedForEquality.add(testObject);
        if (dynamicControl.getType().getNumberOfProperties() != dynamicTest.getType().getNumberOfProperties()) {
            fail("testObject and controlObject did not have the same number of properties.");
        }
        List<String> propNames = dynamicControl.getType().getPropertiesNames();
        Iterator<String> it = propNames.iterator();
        while (it.hasNext()) {
            String propName = it.next();
            Object controlValue = dynamicControl.get(propName);
            Object testValue = dynamicTest.get(propName);
            compareObjects(controlValue, testValue);
        }
    } else {
        if (!testObject.equals(controlObject)) {
            fail("testObject was [" + testObject + "] but controlObject was [" + controlObject + "].");
        }
    }
}
Also used : DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) ArrayList(java.util.ArrayList) List(java.util.List) Attr(org.w3c.dom.Attr) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with DynamicEntityImpl

use of org.eclipse.persistence.internal.dynamic.DynamicEntityImpl in project eclipselink by eclipse-ee4j.

the class StreamingOutputMarshaller method createMarshaller.

public static Marshaller createMarshaller(PersistenceContext context, MediaType mediaType) throws JAXBException {
    Marshaller marshaller = context.getJAXBContext().createMarshaller();
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, mediaType.toString());
    marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
    marshaller.setAdapter(new LinkAdapter(context.getBaseURI().toString(), context));
    marshaller.setListener(new Marshaller.Listener() {

        @Override
        public void beforeMarshal(Object source) {
            if (source instanceof DynamicEntity) {
                DynamicEntityImpl sourceImpl = (DynamicEntityImpl) source;
                PropertyChangeListener listener = sourceImpl._persistence_getPropertyChangeListener();
                sourceImpl._persistence_setPropertyChangeListener(null);
                ((DynamicEntity) source).set("self", source);
                sourceImpl._persistence_setPropertyChangeListener(listener);
            }
        }
    });
    return marshaller;
}
Also used : Marshaller(jakarta.xml.bind.Marshaller) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) PropertyChangeListener(java.beans.PropertyChangeListener) DynamicEntity(org.eclipse.persistence.dynamic.DynamicEntity) LinkAdapter(org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter)

Example 3 with DynamicEntityImpl

use of org.eclipse.persistence.internal.dynamic.DynamicEntityImpl in project eclipselink by eclipse-ee4j.

the class SimpleTypes_MultiTable method verifyChangeTracking.

@Test
public void verifyChangeTracking() {
    persistSimpleA();
    DynamicType simpleTypeA = dynamicHelper.getType("SimpleA");
    Assert.assertNotNull(simpleTypeA);
    UnitOfWork uow = session.acquireUnitOfWork();
    ReadObjectQuery findQuery = dynamicHelper.newReadObjectQuery("SimpleA");
    findQuery.setSelectionCriteria(findQuery.getExpressionBuilder().get("id").equal(1));
    DynamicEntityImpl a = (DynamicEntityImpl) uow.executeQuery(findQuery);
    assertNotNull(a);
    assertNotNull(a._persistence_getPropertyChangeListener());
    uow.release();
    session.release();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Example 4 with DynamicEntityImpl

use of org.eclipse.persistence.internal.dynamic.DynamicEntityImpl in project eclipselink by eclipse-ee4j.

the class SimpleTypes_AggregateObject method verifyChangTracking.

@Test
public void verifyChangTracking() {
    persistSimpleA();
    DynamicType simpleTypeA = dynamicHelper.getType("SimpleA");
    Assert.assertNotNull(simpleTypeA);
    UnitOfWork uow = session.acquireUnitOfWork();
    ReadObjectQuery roq = dynamicHelper.newReadObjectQuery("SimpleA");
    roq.setSelectionCriteria(roq.getExpressionBuilder().get("id").equal(1));
    DynamicEntityImpl sharedA = (DynamicEntityImpl) session.executeQuery(roq);
    assertNotNull(sharedA);
    assertNull(sharedA._persistence_getPropertyChangeListener());
    DynamicEntityImpl a = (DynamicEntityImpl) uow.executeQuery(roq);
    assertNotNull(a);
    assertNotNull(a._persistence_getPropertyChangeListener());
    DynamicEntityImpl c = a.<DynamicEntityImpl>get("c");
    assertNotNull(c);
    assertNotNull(c._persistence_getPropertyChangeListener());
    assertTrue(c._persistence_getPropertyChangeListener() instanceof AggregateAttributeChangeListener);
    uow.release();
}
Also used : UnitOfWork(org.eclipse.persistence.sessions.UnitOfWork) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) DynamicType(org.eclipse.persistence.dynamic.DynamicType) AggregateAttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AggregateAttributeChangeListener) Test(org.junit.Test)

Example 5 with DynamicEntityImpl

use of org.eclipse.persistence.internal.dynamic.DynamicEntityImpl in project eclipselink by eclipse-ee4j.

the class SimpleTypes_MultiTable method verifyChangeTracking.

@Test
public void verifyChangeTracking() {
    persistSimpleA();
    DynamicType simpleTypeA = helper.getType("SimpleA");
    assertNotNull(simpleTypeA);
    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    DynamicEntityImpl a = (DynamicEntityImpl) em.find(simpleTypeA.getJavaClass(), 1);
    assertNotNull(a);
    assertNotNull(a._persistence_getPropertyChangeListener());
    em.getTransaction().rollback();
    em.close();
}
Also used : EntityManager(jakarta.persistence.EntityManager) DynamicEntityImpl(org.eclipse.persistence.internal.dynamic.DynamicEntityImpl) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Test(org.junit.Test)

Aggregations

DynamicEntityImpl (org.eclipse.persistence.internal.dynamic.DynamicEntityImpl)8 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 DynamicType (org.eclipse.persistence.dynamic.DynamicType)3 EntityManager (jakarta.persistence.EntityManager)2 DynamicEntity (org.eclipse.persistence.dynamic.DynamicEntity)2 AggregateAttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AggregateAttributeChangeListener)2 ReadObjectQuery (org.eclipse.persistence.queries.ReadObjectQuery)2 UnitOfWork (org.eclipse.persistence.sessions.UnitOfWork)2 Marshaller (jakarta.xml.bind.Marshaller)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 PersistenceEntity (org.eclipse.persistence.internal.descriptors.PersistenceEntity)1 DynamicTypeImpl (org.eclipse.persistence.internal.dynamic.DynamicTypeImpl)1 CMP3Policy (org.eclipse.persistence.internal.jpa.CMP3Policy)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 LinkAdapter (org.eclipse.persistence.jpa.rs.util.xmladapters.LinkAdapter)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1