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"));
}
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));
}
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);
}
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);
}
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);
}
Aggregations