Search in sources :

Example 21 with JAXBException

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

the class ExceptionHandlingTestCases method testInvalidMapParameterTypeBadKey.

/**
 * Tests an invalid parameter type by setting a Key of type Class as opposed
 * to String.
 *
 * Negative test.
 */
public void testInvalidMapParameterTypeBadKey() {
    Map<Class<?>, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put(JAXBContextFactory.class, new StreamSource());
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    try {
        JAXBContextFactory.createContext(CONTEXT_PATH, getClass().getClassLoader(), properties);
    } catch (JAXBException e) {
        return;
    } catch (Exception x) {
    }
    fail("The expected JAXBException was not thrown.");
}
Also used : HashMap(java.util.HashMap) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 22 with JAXBException

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

the class MultipleXmlElementRefTestCases method testCreateJAXBContextWithMultipleXmlElementRefSameType.

public void testCreateJAXBContextWithMultipleXmlElementRefSameType() throws Exception {
    Exception caughtException = null;
    try {
        Class<?>[] classes = new Class<?>[] { Root.class, ChildOne.class, ChildTwo.class };
        JAXBContext ctx = JAXBContextFactory.createContext(classes, null, this.getClass().getClassLoader());
        Root root = new Root();
        ChildOne c1 = new ChildOne();
        c1.name = "C-ONE";
        ChildTwo c2 = new ChildTwo();
        c2.name = "C-two";
        ChildTwo c3 = new ChildTwo();
        c3.name = "C-333";
        root.thing = c1;
        root.things.add(c2);
        root.things.add(c3);
        ctx.createMarshaller().marshal(root, new ByteArrayOutputStream());
    } catch (jakarta.xml.bind.JAXBException e) {
        caughtException = e;
        Throwable nested = e.getLinkedException();
        if (nested instanceof JAXBException) {
            assertEquals(JAXBException.MULTIPLE_XMLELEMREF, ((JAXBException) nested).getErrorCode());
            return;
        }
    } catch (Exception e) {
        caughtException = e;
    }
    fail("A multiple XmlElementRef exception should have been thrown, but was: " + caughtException);
}
Also used : JAXBException(org.eclipse.persistence.exceptions.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Example 23 with JAXBException

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

the class TransformerPropertyBuilderTestCase method testTransformerPropertyBuilder.

@Test
public void testTransformerPropertyBuilder() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(BINDINGS_DOC);
    HashMap<String, Source> metadataSourceMap = new HashMap<>();
    metadataSourceMap.put("org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation", new StreamSource(inputStream));
    Map<String, Object> properties = new HashMap<>();
    properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, metadataSourceMap);
    Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
    JavaModelInputImpl jModelInput = new JavaModelInputImpl(DOMAIN_CLASSES, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), DOMAIN_CLASSES)));
    Helper helper = new Helper(jModelInput.getJavaModel());
    Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
    TypeInfo typeInfo = generator.getAnnotationsProcessor().getTypeInfos().get(EMPLOYEE_CLASS_NAME);
    Property normalHoursProperty = typeInfo.getProperties().get(XML_TRANSFORMATION_PROPERTY_NAME);
    ChildSchemaGenerator childSchemaGenerator = new ChildSchemaGenerator(helper);
    TransformerPropertyBuilder transformerPropertyBuilder = childSchemaGenerator.getTransformerPropertyBuilder(normalHoursProperty, typeInfo);
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getPropertyName(...) and org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.buildPropertyFromTransformer(...)
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getTransformerJavaClass(...) with booth options writeTransformer.isSetTransformerClass() true,false
    List<Property> props = transformerPropertyBuilder.buildProperties();
    assertEquals(2, props.size());
    // Verification of result of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getPropertyName(...)
    // Verify property names
    assertEquals("start-time", props.get(0).getPropertyName());
    assertEquals("end-time", props.get(1).getPropertyName());
    // Verification of result of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getReturnTypeFromTransformer(...)
    // Verify property types
    assertEquals(String.class.getName(), props.get(0).getType().getName());
    assertEquals(String.class.getName(), props.get(1).getType().getName());
    assertEquals(NormalHoursAttributeTransformer.class.getName(), normalHoursProperty.getXmlTransformation().getXmlReadTransformer().getTransformerClass());
    assertEquals(StartTimeTransformer.class.getName(), normalHoursProperty.getXmlTransformation().getXmlWriteTransformer().get(0).getTransformerClass());
    assertEquals(String.class.getName(), normalHoursProperty.getGenericType().getName());
    // Indirect call of org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder.getTransformerJavaClass(...) with invalid TransformerClass name
    try {
        normalHoursProperty.getXmlTransformation().getXmlWriteTransformer().get(0).setTransformerClass("xxx.xxx.WrongClassName");
        transformerPropertyBuilder.buildProperties();
        fail("Expected JAXBException.");
    } catch (JAXBException expected) {
        assertEquals(50054, expected.getErrorCode());
    }
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) NormalHoursAttributeTransformer(org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation.NormalHoursAttributeTransformer) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(org.eclipse.persistence.exceptions.JAXBException) StartTimeTransformer(org.eclipse.persistence.testing.jaxb.externalizedmetadata.mappings.xmltransformation.StartTimeTransformer) TypeInfo(org.eclipse.persistence.jaxb.compiler.TypeInfo) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Helper(org.eclipse.persistence.jaxb.javamodel.Helper) TransformerPropertyBuilder(org.eclipse.persistence.jaxb.compiler.builder.TransformerPropertyBuilder) Property(org.eclipse.persistence.jaxb.compiler.Property) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator) Test(org.junit.Test)

Example 24 with JAXBException

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

the class DynamicJAXBRefreshTestCases method testRefreshSchemaInputStream.

public void testRefreshSchemaInputStream() throws Exception {
    ClassLoader classLoader = this.getClass().getClassLoader();
    InputStream metadataStream = classLoader.getResourceAsStream(XSD_METADATA);
    DynamicJAXBContext jc = DynamicJAXBContextFactory.createContextFromXSD(metadataStream, null, classLoader, null);
    Exception caughtException = null;
    try {
        JAXBHelper.getJAXBContext(jc).refreshMetadata();
    } catch (Exception e) {
        caughtException = e;
    }
    assertNotNull("Refresh Exception was not thrown as expected.", caughtException);
    assertEquals("Incorrect exception thrown.", JAXBException.class, caughtException.getClass());
    assertEquals("Incorrect exception thrown.", JAXBException.CANNOT_REFRESH_METADATA, ((JAXBException) caughtException).getErrorCode());
}
Also used : DynamicJAXBContext(org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext) InputStream(java.io.InputStream) JAXBException(org.eclipse.persistence.exceptions.JAXBException)

Aggregations

JAXBException (org.eclipse.persistence.exceptions.JAXBException)24 HashMap (java.util.HashMap)11 Source (javax.xml.transform.Source)9 StreamSource (javax.xml.transform.stream.StreamSource)9 InputStream (java.io.InputStream)7 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)7 JAXBContext (jakarta.xml.bind.JAXBContext)6 ArrayList (java.util.ArrayList)3 QName (javax.xml.namespace.QName)3 XmlProperty (org.eclipse.persistence.oxm.annotations.XmlProperty)3 XmlTransient (jakarta.xml.bind.annotation.XmlTransient)2 Type (java.lang.reflect.Type)2 TypeMappingInfo (org.eclipse.persistence.jaxb.TypeMappingInfo)2 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)2 XmlAnyElement (jakarta.xml.bind.annotation.XmlAnyElement)1 XmlElement (jakarta.xml.bind.annotation.XmlElement)1 XmlJavaTypeAdapter (jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collection (java.util.Collection)1