2017-03-07 29 views
0

こんにちは私はJava 5からJava 6へアプリケーションを移行するときに問題に直面しています。 Java5でうまくいく単体テストです。java 5からjava 6への移行後のSAXParseException

ユニットテストのコードは、検証の方法が

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> 

ファイルを検証する

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

ある

<?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を使用していますか?

+0

完全なスタックトレースを追加してください – Jens

+0

_complete_スキーマファイルを投稿してください。 –

+0

私はスタックと完全なスキーマファイル –

答えて

0

答えは、DocumentBuilderFactoryのnamespaceAwareは、デフォルトではJava 6でfalseです。問題を解決するには、trueに設定します。

関連する問題