2016-10-19 2 views
0

junit unit 테스트에서 xml 파일의 유효성을 검사하려고합니다. 이것은 내 단순화 된 코드입니다. 스키마가 인해 내장되지 않는 그러나 스키마는 http://www.unece.org/http://www.gs1.org/단위 테스트에서 xsd 요소 이름이 확인되지 않음

package test; 

import java.net.URL; 
import java.nio.file.Paths; 

import javax.xml.XMLConstants; 
import javax.xml.transform.Source; 
import javax.xml.transform.stream.StreamSource; 
import javax.xml.validation.Schema; 
import javax.xml.validation.SchemaFactory; 

import org.junit.Assert; 
import org.junit.BeforeClass; 
import org.junit.Test; 

public class Epcis11MessageCreatorForSoTest { 

    private static Schema schema; 

    private final static String[] XSD_FILES = { 
      "epcis11/xsd/BasicTypes.xsd", 
      "epcis11/xsd/DocumentIdentification.xsd", 
      "epcis11/xsd/Partner.xsd", 
      "epcis11/xsd/Manifest.xsd", 
      "epcis11/xsd/BusinessScope.xsd", 
      "epcis11/xsd/StandardBusinessDocumentHeader.xsd", 
      "epcis11/xsd/EPCglobal.xsd", 
      "epcis11/xsd/EPCglobal-epcis-1_1.xsd", 
      "epcis11/xsd/EPCglobal-epcis-query-1_1.xsd", 
      "epcis11/xsd/EPCglobal-epcis-masterdata-1_1.xsd", 
    }; 

    @BeforeClass 
    public static void beforeClass() throws Exception { 
     try { 
      System.setProperty("jaxp.debug", "10"); 

      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

      Source[] sources = new Source[XSD_FILES.length]; 
      int i = 0; 
      for (String xsdfile : XSD_FILES) { 
       URL resource = Epcis11MessageCreatorForSoTest.class.getClassLoader().getResource(xsdfile); 
       String systemId = Paths.get(resource.toURI()).toFile().getAbsolutePath(); 
       StreamSource ss = new StreamSource(
         Epcis11MessageCreatorForSoTest.class.getClassLoader().getResourceAsStream(xsdfile),systemId); 

       sources[i] = ss; 
       i++; 
      } 
      schema = schemaFactory.newSchema(sources); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      throw e; 
     } 
    } 
    @Test 
    public void testFoo() { 
     Assert.assertTrue(true); 
    } 

} 

에서 액세스 할 수 있습니다 :

대상/테스트 클래스/epcis11/XSD/EPC 글로벌-EPCIS - 1_1.xsd; lineNumber : 46; columnNumber : 60; src-resolve : 'sbdh : StandardBusinessDocumentHeader'를 (n) '요소 선언'구성 요소로 해석 할 수 없습니다.

관련 라인은

내가 IDE와 프로젝트로 Eclipse를 사용

<xsd:schema xmlns:epcis="urn:epcglobal:epcis:xsd:1" xmlns:sbdh="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" xmlns:epcglobal="urn:epcglobal:xsd:1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:epcglobal:epcis:xsd:1" elementFormDefault="unqualified" attributeFormDefault="unqualified" version="1.1"> 
... 
<xsd:import namespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader" schemaLocation="./StandardBusinessDocumentHeader.xsd"/> 
... 
<xsd:element ref="sbdh:StandardBusinessDocumentHeader"/> 

는 JRE 시스템 라이브러리로 JavaSE-1.7을 사용하여이 (EPC 글로벌-EPCIS-1_1.xsd)처럼 보인다. 이 xsd 파일을 볼 때 구문 적으로 괜찮습니다. 그것은 내가 ide에서 xml 관련 오류를 얻지 못한다. 테스트가 ide 또는 maven에서 실행될 때 정확히 동일한 오류가 나타납니다.

내 schemafactory 또는 xsd 소스가 Java 객체로 잘못되었을 수있는 사항에 대한 제안 사항이 있습니까?

+0

'sbdh'네임 스페이스는 어디에 정의되어 있습니까? –

+0

네임 스페이스 접두사 'sbdh'는 EPCglobal-epcis-1_1.xsd에 있습니다. 위 줄의 미리보기가 있습니다. – user7040845

답변

0

디버거를 통해 내 코드를 실행할 수있는 시간이있어서 운이 좋았습니다. 그것은 꽤 무거운 물건이었다. 예를 들어 com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.class에는 100 개가 넘는 가져 오기와 4000 줄 이상의 코드가 포함되어 있습니다.

아무리해도 xsd 문법 또는 스키마 또는 스키마 소스를로드하는 com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar (XMLInputSource)에 프로그래밍 오류가 있다고 생각합니다. 우리의 맥락.

http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader 네임 스페이스에 대해로드 된 첫 번째 문법은 BasicTypes.xsd입니다.

필자의 발견은 그 문법이 네임 스페이스에 의해 매핑되고 다른 스키마에 포함 된 후에 네임 스페이스를 공유하는 includers '를 어떻게 든 방지하고 매핑되는 문법을 공유한다는 것입니다. 내가 EPCglobal-epcis-1_1.xsd에서 BasicTypes.xsd에 정의 된 모든 유형을 참조 할 수 있지만 http : //www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader 네임 스페이스의 다른 요소 또는 유형을 참조하는 경우는 이 아닙니다. BasicTypes.xsd에을 정의하면 이름 확인 오류가 발생합니다.

내 이론을 뒷받침합니다. 유형 정의 또는 요소 선언을 BasicTypes.xsd로 옮기면 EPCglobal-epcis-1_1.xsd에서 해당 유형 또는 요소를 성공적으로 참조 할 수 있습니다.

실제로이 프로젝트에서 사용되는 http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader 네임 스페이스의 모든 유형 정의와 요소 선언을 하나의 xsd 원본 파일로 통합 한 다음 스키마가 올바르게 빌드 될 수 있습니다.

그러나 검증을 위해 사용 된 xsd 파일 중 어느 것도 내가 유지 관리하지 못하기 때문에 일종의 해킹 인 것처럼 느껴집니다. 차라리 내 프로젝트 파일에서 xml 스키마를 빌드 할 수있는 시스템을 사용하고 싶습니다.

+0

이 문제와 관련하여 Oracle에서 승인 한 버그가 있습니다 : http://bugs.java.com/bugdatabase/view_bug.do? bug_id = JDK-8168883 – user7040845

관련 문제