Search in sources :

Example 1 with Generator

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

the class PropertyTypeTestCases method testPropertyTypes.

public void testPropertyTypes() throws Exception {
    JaxbClassLoader classLoader = new JaxbClassLoader(Thread.currentThread().getContextClassLoader());
    Generator generator = new Generator(new JavaModelInputImpl(new Class<?>[] { TestObject.class }, new JavaModelImpl(this.classLoader)));
    CoreProject proj = generator.generateProject();
    TypeInfo info = generator.getAnnotationsProcessor().getTypeInfos().get("org.eclipse.persistence.testing.jaxb.annotations.TestObject");
    Property booleanProp = info.getProperties().get("booleanTest");
    JavaClass type = booleanProp.getType();
    assertEquals("java.lang.Boolean", type.getRawName());
    Property booleanListProp = info.getProperties().get("booleanListTest");
    type = booleanListProp.getType();
    assertEquals("java.util.List", type.getRawName());
    Property byteArrayProp = info.getProperties().get("byteArrayTest");
    type = byteArrayProp.getType();
    assertEquals("java.lang.String", type.getRawName());
    Property byteArrayListProp = info.getProperties().get("byteArrayListTest");
    type = byteArrayListProp.getType();
    assertEquals("java.util.List", type.getRawName());
    assertEquals("java.lang.String", byteArrayListProp.getGenericType().getRawName());
}
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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) TypeInfo(org.eclipse.persistence.jaxb.compiler.TypeInfo) Property(org.eclipse.persistence.jaxb.compiler.Property) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 2 with Generator

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

the class SchemaGenImportTestCases method testSchemaGenerationWithImport.

public void testSchemaGenerationWithImport() {
    Class<?>[] jClasses = new Class<?>[] { Address.class, Employee.class };
    Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
    File outDir = setOutDir();
    try {
        Files.copy(getFile(PACKAGE_RESOURCE + "someExistingSchema.xsd").toPath(), new File(outDir, "someExistingSchema.xsd").toPath());
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
    MySystemIDSchemaOutputResolver mysor = new MySystemIDSchemaOutputResolver(outDir);
    gen.generateSchemaFiles(mysor, null);
    // validate a valid instance doc against the generated employee schema
    SchemaFactory sFact = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema employeeSchema = null;
    try {
        employeeSchema = sFact.newSchema(mysor.schemaFiles.get(EMPLOYEE_NS));
    } catch (SAXException e) {
        e.printStackTrace();
        fail("SchemaFactory could not create Employee schema");
    }
    StreamSource ss;
    Validator validator = employeeSchema.newValidator();
    try {
        ss = new StreamSource(getFile(XML_RESOURCE));
        validator.validate(ss);
    } catch (Exception ex) {
        ex.printStackTrace();
        fail("An unexpected exception occurred");
    }
    try {
        ss = new StreamSource(getFile(INVALID_XML_RESOURCE));
        validator.validate(ss);
    } catch (Exception ex) {
        return;
    }
    fail("The expected exception never occurred");
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Address(org.eclipse.persistence.testing.jaxb.schemagen.imports.address.Address) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) 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 3 with Generator

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

the class SchemaGenImportTestCases method testRelativeSchemaLocationWithNoSlash.

/**
 * Test's that the import schema location can be relativized given a
 * source schema with no path info in the name, i.e. "employee.xsd".
 * No exception should occur.
 */
public void testRelativeSchemaLocationWithNoSlash() {
    File outDir = setOutDir();
    try {
        Class<?>[] jClasses = new Class<?>[] { Address.class, Employee.class };
        Generator gen = new Generator(new JavaModelInputImpl(jClasses, new JavaModelImpl(Thread.currentThread().getContextClassLoader())));
        gen.generateSchemaFiles(outDir.getAbsolutePath(), null);
    } catch (Exception x) {
        x.printStackTrace();
        fail("An unexpected exception occurred during schema generation: " + x.getMessage());
    }
}
Also used : JavaModelInputImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelInputImpl) Address(org.eclipse.persistence.testing.jaxb.schemagen.imports.address.Address) JavaModelImpl(org.eclipse.persistence.jaxb.javamodel.reflection.JavaModelImpl) File(java.io.File) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) Generator(org.eclipse.persistence.jaxb.compiler.Generator)

Example 4 with Generator

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

the class SchemaGenEmployeeTestCases method xtestAbstractSuperclassInvaildDoc.

/**
 * In this test the xsi:type of the root element set to the abstract superclass,
 * which should cause a validation exception.
 */
public void xtestAbstractSuperclassInvaildDoc() throws Exception {
    boolean exception = false;
    String msg = null;
    String src = "org/eclipse/persistence/testing/jaxb/schemagen/employee/invalid_inheritance.xml";
    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<?>[] { MyAbstractTestType.class, MyTestSubType.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.toString();
    }
    assertTrue("Schema validation did not fail as expected: " + msg, exception);
}
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 5 with Generator

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

the class SchemaGenEmployeeTestCases method xtestEmployeeSchemaGenMissingRequiredAttribute.

/**
 * 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 xtestEmployeeSchemaGenMissingRequiredAttribute() throws Exception {
    boolean exception = false;
    String src = "org/eclipse/persistence/testing/jaxb/schemagen/employee/employee_missing_required_attribute.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("id"));
}
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)

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