0
您好,我在將應用程序從Java 5遷移到Java 6時遇到了一個問題。 這是一些單元測試,它在Java5上表現良好。SAXParseException從Java 5遷移到Java 6
單元測試的代碼是
public void testValidateAgainstSchemaWhenDocumentIsOk() throws ParserConfigurationException, SAXException, IOException, JAXBException, URISyntaxException {
// Setup
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream stream = this.getClass().getResourceAsStream("/m6/mygale/common/valid_notification.xml");
Document document = builder.parse(stream);
stream.close();
File schemaFile = new File(getClass().getResource("/m6/mygale/common/notification_schema.xsd").toURI());
// pre assert
assertNotNull(schemaFile);
// Action
JaxbHelper.validateSchema(document, schemaFile);
}
驗證的方法是
public static void validateSchema(Document document, File schemaFile) throws SAXException, IOException {
//LOGGER.debug("validation du document" + document.getTextContent() + " schemaFile=" + schemaFile);
SchemaFactory factory = newInstance(W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(schemaFile);
Validator validator = schema.newValidator();
validator.validate(new DOMSource(document));
}
示瑪文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="mygaleNotification" type="notificationXmlBean" />
<xs:complexType name="notificationXmlBean">
<xs:sequence>
<xs:element name="header" type="headerXmlBean" />
<xs:element name="flowParameters"
type="flowParametersXmlBean" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="headerXmlBean">
<xs:all>
<xs:element name="sourceName" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="fluxId" type="xs:string" minOccurs="1"
maxOccurs="1" />
<xs:element name="notificationDateTime" type="xs:string"
minOccurs="1" maxOccurs="1" />
</xs:all>
</xs:complexType>
<xs:element name="audiencesMinuteParametersXmlBean"
type="audiencesMinuteParametersXmlBean" />
<xs:complexType name="audiencesMinuteParametersXmlBean">
<xs:all>
<xs:element name="audiencesBaseUri" type="xs:string"
minOccurs="0" />
<xs:element name="broadcastDate" type="xs:string"
minOccurs="0" />
<xs:element name="channels" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="channel" type="channelXmlBean"
maxOccurs="unbounded" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
<xs:complexType name="channelXmlBean">
<xs:sequence>
<xs:element name="label" type="xs:string" minOccurs="0" />
</xs:sequence>
<xs:attribute name="sherpaId" type="xs:int" />
</xs:complexType>
<xs:complexType name="flowParametersXmlBean">
<xs:sequence>
<xs:element name="audiencesMinuteParameters"
type="audiencesMinuteParametersXmlBean" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="dateTimeAdapter">
<xs:complexContent>
<xs:extension base="xmlAdapter">
<xs:sequence />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="xmlAdapter" abstract="true" />
<xs:complexType name="localDateAdapter">
<xs:complexContent>
<xs:extension base="xmlAdapter">
<xs:sequence />
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
文件來驗證
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mygaleNotification>
<header>
<fluxId>FROM_SHERPA_TO_PARALLELEANTENNE</fluxId>
<notificationDateTime>2008-10-18T11:22:37.310+02:00</notificationDateTime>
<sourceName>Sherpa</sourceName>
</header>
<flowParameters>
<audiencesMinuteParameters>
<audiencesBaseUri>http://lapplipubd3:8880/mock-sherpa</audiencesBaseUri>
<broadcastDate>20081017</broadcastDate>
<channels>
<channel sherpaId="6">
<label>M6</label>
</channel>
<channel sherpaId="205">
<label>W9</label>
</channel>
</channels>
</audiencesMinuteParameters>
</flowParameters>
</mygaleNotification>
堆棧
org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'mygaleNotification'.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.xs.XMLSchemaValidator.startElement(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.beginNode(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.DOMValidatorHelper.validate(Unknown Source)
at org.apache.xerces.jaxp.validation.ValidatorImpl.validate(Unknown Source)
at javax.xml.validation.Validator.validate(Validator.java:127)
at m6.mygale.common.JaxbHelper.validateSchema(JaxbHelper.java:108)
at m6.mygale.common.JaxbHelperUTest.testValidateAgainstSchemaWhenDocumentIsOk(JaxbHelperUTest.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
我使用Java 6u45等的JUnit 4.5,任何人都可以幫助我嗎?
添加完整的stacktrace請 – Jens
您是否也可以發佈_complete_模式文件? –
我添加了堆棧和完整的模式文件 –