Search in sources :

Example 11 with DescriptorException

use of org.eclipse.persistence.exceptions.DescriptorException in project eclipselink by eclipse-ee4j.

the class TransformationMappingErrorTestCases method testNoClassForFieldTransformation.

public void testNoClassForFieldTransformation() {
    ClassDescriptor descriptor = context.getSession(0).getDescriptor(Employee.class);
    XMLTransformationMapping mapping = new XMLTransformationMapping();
    mapping.setDescriptor(descriptor);
    // mapping.addFieldTransformerClassName("/normal-hours/start-time", "this.is.a.DummyClass");
    DescriptorException expected = DescriptorException.fieldTransformerClassNotFound("dummy_class_name", mapping, new Exception());
    try {
        mapping.initialize((AbstractSession) context.getSession(0));
    } catch (DescriptorException ex) {
        assertTrue("The incorrect exception was thrown. [Expected] " + expected + " [Found] " + ex, ex.getErrorCode() == expected.getErrorCode());
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 12 with DescriptorException

use of org.eclipse.persistence.exceptions.DescriptorException in project eclipselink by eclipse-ee4j.

the class TransformationMappingErrorTestCases method testInvalidClassForFieldTransformation.

public void testInvalidClassForFieldTransformation() {
    ClassDescriptor descriptor = context.getSession(0).getDescriptor(Employee.class);
    XMLTransformationMapping mapping = new XMLTransformationMapping();
    mapping.setDescriptor(descriptor);
    mapping.addFieldTransformerClassName("/normal-hours/start-time", "org.eclipse.persistence.testing.oxm.mappings.transformation.Employee");
    DescriptorException expected = DescriptorException.fieldTransformerClassInvalid("dummy_class_name", mapping, new Exception());
    try {
        mapping.initialize((AbstractSession) context.getSession(0));
    } catch (DescriptorException ex) {
        assertTrue("The incorrect exception was thrown. [Expected] " + expected + " [Found] " + ex, ex.getErrorCode() == expected.getErrorCode());
    }
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 13 with DescriptorException

use of org.eclipse.persistence.exceptions.DescriptorException in project VaadinUtils by rlsutton1.

the class ViolationConstraintHandler method handleConstraintViolationException.

/**
 * digs down looking for a useful exception, it will throw a runtime
 * exception if it finds an useful exception
 *
 * @param e
 * @param nestLimit
 */
private static void handleConstraintViolationException(Throwable e, int nestLimit) {
    if (nestLimit > 0 && e != null) {
        nestLimit--;
        if (e instanceof DescriptorException) {
            DescriptorException desc = (DescriptorException) e;
            throw new RuntimeException(desc.getMessage());
        }
        if (e instanceof ConstraintViolationException) {
            String groupedViolationMessage = e.getClass().getSimpleName() + " ";
            for (ConstraintViolation<?> violation : ((ConstraintViolationException) e).getConstraintViolations()) {
                logger.error("{}", violation.getLeafBean().getClass().getCanonicalName());
                String violationMessage = violation.getLeafBean().getClass().getSimpleName() + " " + violation.getPropertyPath() + " " + violation.getMessage() + ", the value was " + violation.getInvalidValue();
                logger.error(violationMessage);
                groupedViolationMessage += violationMessage + "\n";
            }
            throw new RuntimeException(groupedViolationMessage);
        }
        handleConstraintViolationException(e.getCause(), nestLimit);
    }
}
Also used : DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ConstraintViolationException(javax.validation.ConstraintViolationException)

Example 14 with DescriptorException

use of org.eclipse.persistence.exceptions.DescriptorException in project eclipselink by eclipse-ee4j.

the class QueryOperation method initialize.

@Override
public void initialize(XRServiceAdapter xrService) {
    super.initialize(xrService);
    if (queryHandler == null) {
        // session query instead of named query
        DatabaseQuery dq = xrService.getORSession().getQuery(name);
        if (dq != null) {
            queryHandler = new QueryHandler() {

                @Override
                public void initializeDatabaseQuery(XRServiceAdapter xrService, QueryOperation queryOperation) {
                // do nothing
                }

                @Override
                public void initializeArguments(XRServiceAdapter xrService, QueryOperation queryOperation, DatabaseQuery databaseQuery) {
                // do nothing
                }

                @Override
                public void initializeCall(XRServiceAdapter xrService, QueryOperation queryOperation, DatabaseQuery databaseQuery) {
                // do nothing
                }
            };
            queryHandler.setDatabaseQuery(dq);
        }
    }
    if (queryHandler == null) {
        throw DBWSException.couldNotLocateQueryForSession(name, xrService.getORSession().getName());
    }
    queryHandler.initialize(xrService, this);
    Session oxSession = xrService.getOXSession();
    QName resultType = result == null ? null : result.getType();
    addSimpleXMLFormatModelDescriptor(xrService);
    addValueObjectDescriptor(xrService);
    if (resultType == null) {
        if (isAttachment()) {
            Attachment attachment = result.getAttachment();
            XMLDescriptor descriptor = (XMLDescriptor) oxSession.getProject().getClassDescriptor(DataHandler.class);
            if (descriptor == null) {
                descriptor = new XMLDescriptor();
                descriptor.setAlias(DATAHANDLER_STR);
                descriptor.setJavaClass(DataHandler.class);
                descriptor.setInstantiationPolicy(new DataHandlerInstantiationPolicy(attachment.getMimeType()));
                XMLBinaryDataMapping mapping = new XMLBinaryDataMapping();
                mapping.setAttributeName(RESULTS_STR);
                mapping.setAttributeAccessor(new AttributeAccessor() {

                    @Override
                    public Object getAttributeValueFromObject(Object object) throws DescriptorException {
                        Object result = null;
                        DataHandler dataHandler = (DataHandler) object;
                        try {
                            result = dataHandler.getContent();
                            if (result instanceof InputStream) {
                                try (InputStream is = (InputStream) result) {
                                    byte[] buf = new byte[2048];
                                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                    int bytesRead = is.read(buf);
                                    while (bytesRead >= 0) {
                                        baos.write(buf, 0, bytesRead);
                                        bytesRead = is.read(buf);
                                    }
                                    result = baos.toByteArray();
                                }
                            }
                        } catch (IOException e) {
                        // ignore
                        }
                        return result;
                    }

                    @Override
                    public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
                    }
                });
                mapping.setXPath(DEFAULT_SIMPLE_XML_FORMAT_TAG + SLASH_CHAR + DEFAULT_SIMPLE_XML_TAG + ATTACHMENT_STR);
                mapping.setSwaRef(true);
                mapping.setShouldInlineBinaryData(false);
                mapping.setMimeType(attachment.getMimeType());
                descriptor.addMapping(mapping);
                NamespaceResolver nr = new NamespaceResolver();
                descriptor.setNamespaceResolver(nr);
                oxSession.getProject().addDescriptor(descriptor);
                ((DatabaseSessionImpl) oxSession).initializeDescriptorIfSessionAlive(descriptor);
                xrService.getXMLContext().storeXMLDescriptorByQName(descriptor);
            }
        }
    }
}
Also used : DatabaseQuery(org.eclipse.persistence.queries.DatabaseQuery) QName(javax.xml.namespace.QName) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) InputStream(java.io.InputStream) DataHandler(jakarta.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) DatabaseSessionImpl(org.eclipse.persistence.internal.sessions.DatabaseSessionImpl) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session)

Example 15 with DescriptorException

use of org.eclipse.persistence.exceptions.DescriptorException in project eclipselink by eclipse-ee4j.

the class EntityTypeFromDescriptor method invalidDescriptorWithoutEntityType.

/**
 * Verify that the descriptor for a dynamic type fails without the
 * additional configuration which is applied to the descriptor during the
 * EntityType creation.
 */
@Test
public void invalidDescriptorWithoutEntityType() throws Exception {
    RelationalDescriptor descriptor = buildMyEntityDescriptor();
    DatabaseSession session = createSession();
    session.addDescriptor(descriptor);
    try {
        session.login();
    } catch (IntegrityException ie) {
        assertEquals(descriptor.getMappings().size(), ie.getIntegrityChecker().getCaughtExceptions().size());
        // Verify NoSuchField errors for each mapping
        for (int index = 0; index < descriptor.getMappings().size(); index++) {
            DescriptorException ex = (DescriptorException) ie.getIntegrityChecker().getCaughtExceptions().get(index);
            assertEquals(DescriptorException.NO_SUCH_FIELD_WHILE_INITIALIZING_ATTRIBUTES_IN_INSTANCE_VARIABLE_ACCESSOR, ex.getErrorCode());
        }
        return;
    }
    fail("Expected IntegrityException not thrown");
}
Also used : RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) DatabaseSession(org.eclipse.persistence.sessions.DatabaseSession) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) IntegrityException(org.eclipse.persistence.exceptions.IntegrityException) Test(org.junit.Test)

Aggregations

DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)21 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)7 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)6 InputStream (java.io.InputStream)4 ValidationException (org.eclipse.persistence.exceptions.ValidationException)4 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)4 JAXBException (jakarta.xml.bind.JAXBException)3 HashMap (java.util.HashMap)3 Vector (java.util.Vector)3 RelationalDescriptor (org.eclipse.persistence.descriptors.RelationalDescriptor)3 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)3 XMLTransformationMapping (org.eclipse.persistence.oxm.mappings.XMLTransformationMapping)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DatabaseException (org.eclipse.persistence.exceptions.DatabaseException)2 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)2 DirectToFieldMapping (org.eclipse.persistence.mappings.DirectToFieldMapping)2 ForeignReferenceMapping (org.eclipse.persistence.mappings.ForeignReferenceMapping)2 XMLContext (org.eclipse.persistence.oxm.XMLContext)2