Search in sources :

Example 6 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenEmployeeTestCases method xtestEmployeeSchemaGenMissingRequiredElement.

/**
 * The following test expects a schema validation exception to occur.
 * This is due to the fact that the supplied instance document does
 * not contain a 'firstName' element, which is required as the
 * corresponding field in the Employee class contains the following
 * annotation:  @XmlElement(required = true)
 */
public void xtestEmployeeSchemaGenMissingRequiredElement() throws Exception {
    boolean exception = false;
    String src = "org/eclipse/persistence/testing/jaxb/schemagen/employee/employee_missing_required_element.xml";
    String msg = "";
    File newXsd = new File(tmpdir, "/schema1.xsd");
    if (newXsd.exists() && newXsd.isFile() && newXsd.delete()) {
        System.err.println("removed existing: " + newXsd.getAbsolutePath());
    }
    try {
        Class<?>[] jClasses = new Class<?>[] { Address.class, Employee.class, PhoneNumber.class, Department.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(newXsd);
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(new File(src));
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.getLocalizedMessage();
    }
    assertTrue("Schema validation passed unexpectedly", exception);
    assertTrue("An unexpected exception occurred: " + msg, msg.contains("firstName"));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 7 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenEmployeeTestCases method testEmployeeSchemaGeneration.

public void testEmployeeSchemaGeneration() throws Exception {
    boolean exception = false;
    String msg = null;
    InputStream employeeXml = null;
    File newXsd = new File(tmpdir, "schema1.xsd");
    if (newXsd.exists() && newXsd.isFile() && newXsd.delete()) {
        System.err.println("removed existing: " + newXsd.getAbsolutePath());
    }
    try {
        employeeXml = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/schemagen/employee/employee.xml");
        Class<?>[] jClasses = new Class<?>[] { Address.class, Employee.class, PhoneNumber.class, Department.class, MyTestType.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(newXsd);
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(employeeXml);
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.toString();
    } finally {
        if (null != employeeXml) {
            try {
                employeeXml.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    builderFactory.setIgnoringElementContentWhitespace(true);
    builderFactory.setNamespaceAware(true);
    DocumentBuilder parser = builderFactory.newDocumentBuilder();
    InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/schemagen/employee/schema1.xsd");
    Document control = parser.parse(stream);
    stream = new FileInputStream(newXsd);
    Document test = parser.parse(stream);
    JAXBXMLComparer xmlComparer = new JAXBXMLComparer();
    assertTrue("schema1.xsd did not match control document", xmlComparer.isSchemaEqual(control, test));
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) JAXBXMLComparer(org.eclipse.persistence.testing.jaxb.JAXBXMLComparer) DocumentBuilder(javax.xml.parsers.DocumentBuilder) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 8 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenXMLRootElementTestCases method testName002Positive.

public void testName002Positive() throws Exception {
    boolean exception = false;
    String msg = null;
    InputStream src = null;
    try {
        src = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/schemagen/customizedmapping/xmlrootelement/Name002p.xml");
        Class<?>[] jClasses = new Class<?>[] { Name002.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(new File(tmpdir, "schema1.xsd"));
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(src);
        validator.validate(ss);
    } catch (Exception ex) {
        msg = ex.getMessage();
        exception = true;
    } finally {
        if (null != src) {
            src.close();
        }
    }
    assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 9 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenXMLRootElementTestCases method testNamespace001Positive.

public void testNamespace001Positive() throws Exception {
    boolean exception = false;
    String msg = null;
    InputStream src = null;
    try {
        src = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/schemagen/customizedmapping/xmlrootelement/NameSpace001p.xml");
        Class<?>[] jClasses = new Class<?>[] { NameSpace001.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(new File(tmpdir, "schema2.xsd"));
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(src);
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.getMessage();
    } finally {
        if (null != src) {
            src.close();
        }
    }
    assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 10 with Generator

use of org.eclipse.persistence.jaxb.compiler.Generator in project eclipselink by eclipse-ee4j.

the class SchemaGenXMLRootElementTestCases method testName001Positive.

public void testName001Positive() throws Exception {
    boolean exception = false;
    String msg = null;
    InputStream src = null;
    try {
        src = Thread.currentThread().getContextClassLoader().getResourceAsStream("org/eclipse/persistence/testing/jaxb/schemagen/customizedmapping/xmlrootelement/Name001p.xml");
        Class<?>[] jClasses = new Class<?>[] { Name001.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(tmpdir, null);
        SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema theSchema = sFact.newSchema(new File(tmpdir, "schema1.xsd"));
        Validator validator = theSchema.newValidator();
        StreamSource ss = new StreamSource(src);
        validator.validate(ss);
    } catch (Exception ex) {
        exception = true;
        msg = ex.toString();
    } finally {
        if (null != src) {
            try {
                src.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    assertTrue("Schema validation failed unexpectedly: " + msg, exception == false);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) InputStream(java.io.InputStream) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) File(java.io.File) Validator(javax.xml.validation.Validator) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Aggregations

Generator (org.eclipse.persistence.jaxb.compiler.Generator)27 JavaModelImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl)26 JavaModelInputImpl (org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl)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 InputStream (java.io.InputStream)9 JaxbClassLoader (org.eclipse.persistence.internal.jaxb.JaxbClassLoader)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 XMLContext (org.eclipse.persistence.oxm.XMLContext)4 JAXBContext (jakarta.xml.bind.JAXBContext)3 IOException (java.io.IOException)3