Search in sources :

Example 11 with JavaModelInputImpl

use of org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl in project eclipselink by eclipse-ee4j.

the class SchemaGenMapTestCases method testMapSchemaGeneration.

/**
 * Exception case - Schema for Map should be generated without any exception.
 */
public void testMapSchemaGeneration() {
    Class<?>[] loadedClasses = { MapTest.class };
    org.eclipse.persistence.jaxb.compiler.Generator generator = new Generator(new JavaModelInputImpl(loadedClasses, new JavaModelImpl(MapTest.class.getClassLoader())));
    MySchemaOutputResolver outputResolver = new MySchemaOutputResolver();
    boolean exception = false;
    try {
        generator.generateSchemaFiles(outputResolver, null);
    } catch (Exception e) {
        exception = true;
    }
    assertFalse("Unexpected exception occured", exception);
}
Also used : JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) Generator(org.eclipse.persistence.jaxb.compiler.Generator) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 12 with JavaModelInputImpl

use of org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl in project eclipselink by eclipse-ee4j.

the class ExternalizedMetadataTestCases method createXmlContext.

/**
 * Convenience method that returns a newly created XMLContext based on an array of classes.
 */
protected XMLContext createXmlContext(Class<?>[] classes) {
    try {
        ClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
        Generator generator = new Generator(new JavaModelInputImpl(classes, new JavaModelImpl(classLoader)));
        CoreProject proj = generator.generateProject();
        ConversionManager manager = new ConversionManager();
        manager.setLoader(classLoader);
        for (Iterator<ClassDescriptor> descriptorIt = proj.getOrderedDescriptors().iterator(); descriptorIt.hasNext(); ) {
            ClassDescriptor descriptor = descriptorIt.next();
            if (descriptor.getJavaClass() == null) {
                descriptor.setJavaClass(manager.convertClassNameToClass(descriptor.getJavaClassName()));
            }
        }
        return new XMLContext((Project) proj, classLoader);
    } catch (Exception e) {
        e.printStackTrace();
        fail("XmlContext creation failed");
    }
    return null;
}
Also used : JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) CoreProject(org.eclipse.persistence.core.sessions.CoreProject) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLContext(org.eclipse.persistence.oxm.XMLContext) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) ConversionManager(org.eclipse.persistence.internal.helper.ConversionManager) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) JAXBException(jakarta.xml.bind.JAXBException) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 13 with JavaModelInputImpl

use of org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl in project eclipselink by eclipse-ee4j.

the class JAXBContextTransformationMappingTestCases method testInvalidMethod.

/**
 * Test exception handling:  in this case the method name that has been
 * set is invalid (wrong number or type of params. An exception should
 * be thrown.
 *
 * Negative test.
 */
public void testInvalidMethod() {
    InputStream inputStream = ClassLoader.getSystemResourceAsStream(METADATA_FILE_BAD_METHOD);
    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);
    int exceptionCount = 0;
    try {
        JAXBContext jaxbContext = JAXBContextFactory.createContext(new Class<?>[] { Employee.class }, properties, Thread.currentThread().getContextClassLoader());
    } catch (JAXBException e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
    exceptionCount--;
    // test exception from SchemaGenerator
    try {
        Map<String, XmlBindings> bindings = JAXBContextFactory.getXmlBindingsFromProperties(properties, Thread.currentThread().getContextClassLoader());
        JavaModelInputImpl jModelInput = new JavaModelInputImpl(new Class<?>[] { Employee.class }, new JavaModelImpl(new JaxbClassLoader(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Employee.class })));
        Generator generator = new Generator(jModelInput, bindings, Thread.currentThread().getContextClassLoader(), "", false);
        generator.generateSchema();
    } catch (Exception e) {
        exceptionCount++;
    }
    assertTrue("The expected exception was never thrown.", exceptionCount > 0);
}
Also used : XmlBindings(org.eclipse.persistence.jaxb.xmlmodel.XmlBindings) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) HashMap(java.util.HashMap) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(jakarta.xml.bind.JAXBException) JAXBContext(jakarta.xml.bind.JAXBContext) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) JAXBException(jakarta.xml.bind.JAXBException) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 14 with JavaModelInputImpl

use of org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl in project eclipselink by eclipse-ee4j.

the class RequiredAnnotationTestCases method testAnnotationsProcessor.

public void testAnnotationsProcessor() throws Exception {
    try {
        JaxbClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
        Generator generator = new Generator(new JavaModelInputImpl(new Class<?>[] { RequiredTestObject.class, RequiredTestSubObject.class }, new JavaModelImpl(classLoader)));
        Project proj = (Project) generator.generateProject();
        ClassDescriptor descriptor = proj.getDescriptorForAlias("RequiredTestObject");
        DatabaseMapping mapping = descriptor.getMappingForAttributeName("direct");
        boolean isRequired = ((XMLField) mapping.getField()).isRequired();
        assertTrue("The ('direct') mapping's XMLField's isRequired flag was not set to 'true' despite XML Annotation", isRequired);
        mapping = descriptor.getMappingForAttributeName("directAttribute");
        isRequired = ((XMLField) mapping.getField()).isRequired();
        assertTrue("The ('directAttribute') mapping's XMLField's isRequired flag was not set to 'true' despite XML Annotation", isRequired);
        mapping = descriptor.getMappingForAttributeName("directCollection");
        isRequired = ((XMLField) mapping.getField()).isRequired();
        assertTrue("The ('directCollection') mapping's XMLField's isRequired flag was not set to 'true' despite XML Annotation", isRequired);
        mapping = descriptor.getMappingForAttributeName("compositeObject");
        isRequired = ((XMLField) mapping.getField()).isRequired();
        assertTrue("The ('compositeObject') mapping's XMLField's isRequired flag was not set to 'true' despite XML Annotation", isRequired);
        mapping = descriptor.getMappingForAttributeName("compositeCollection");
        isRequired = ((XMLField) mapping.getField()).isRequired();
        assertTrue("The ('compositeCollection') mapping's XMLField's isRequired flag was not set to 'true' despite XML Annotation", isRequired);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Project(org.eclipse.persistence.sessions.Project) CoreProject(org.eclipse.persistence.core.sessions.CoreProject) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 15 with JavaModelInputImpl

use of org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl in project eclipselink by eclipse-ee4j.

the class BeanValidationBindingsTestCase method generateSchema.

private void generateSchema(Class<?>[] classes) throws FileNotFoundException {
    JavaModelImpl javaModel = new JavaModelImpl(Thread.currentThread().getContextClassLoader());
    JavaModelInputImpl modelInput = new JavaModelInputImpl(classes, javaModel);
    modelInput.setFacets(true);
    Generator gen = new Generator(modelInput);
    gen.generateSchemaFiles(PATH_TO_SCHEMA_DIRECTORY, null);
}
Also used : JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Aggregations

JavaModelImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl)27 JavaModelInputImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl)27 Generator (org.eclipse.persistence.jaxb.compiler.Generator)26 StreamSource (javax.xml.transform.stream.StreamSource)18 File (java.io.File)15 Schema (javax.xml.validation.Schema)14 SchemaFactory (javax.xml.validation.SchemaFactory)14 Validator (javax.xml.validation.Validator)14 JaxbClassLoader (org.eclipse.persistence.internal.jaxb.JaxbClassLoader)10 InputStream (java.io.InputStream)9 JAXBException (jakarta.xml.bind.JAXBException)4 HashMap (java.util.HashMap)4 Source (javax.xml.transform.Source)4 CoreProject (org.eclipse.persistence.core.sessions.CoreProject)4 Property (org.eclipse.persistence.jaxb.compiler.Property)4 TypeInfo (org.eclipse.persistence.jaxb.compiler.TypeInfo)4 XmlBindings (org.eclipse.persistence.jaxb.xmlmodel.XmlBindings)4 Test (org.junit.Test)4 JAXBContext (jakarta.xml.bind.JAXBContext)3 IOException (java.io.IOException)3