1   package tests;
2   
3   import junit.framework.TestCase;
4   
5   import javax.xml.namespace.QName;
6   import javax.xml.transform.stream.StreamSource;
7   import javax.xml.parsers.DocumentBuilderFactory;
8   import javax.xml.parsers.DocumentBuilder;
9   import java.io.InputStream;
10  import java.io.FileInputStream;
11  import java.io.ByteArrayOutputStream;
12  import java.io.ByteArrayInputStream;
13  import java.util.Iterator;
14  
15  import org.apache.ws.commons.schema.*;
16  import org.w3c.dom.Document;
17  import org.w3c.dom.NodeList;
18  import org.w3c.dom.Node;
19  import org.w3c.dom.Element;
20  
21  public class ElementRefs2Test extends TestCase {
22      
23      public void testElementRefs() throws Exception {
24          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
25                  "attTests");
26          InputStream is = new FileInputStream(Resources.asURI("elementreferences2.xsd"));
27          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
28          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
29  
30          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
31  
32          assertNotNull(elem);
33  
34          XmlSchemaComplexType cmplxType = (XmlSchemaComplexType)elem.getSchemaType();
35          XmlSchemaObjectCollection items = ((XmlSchemaSequence)cmplxType.getParticle()).getItems();
36  
37          Iterator it = items.getIterator();
38          while (it.hasNext()) {
39              XmlSchemaElement innerElement =  (XmlSchemaElement)it.next();
40              assertNotNull(innerElement.getRefName());
41          }
42  
43          // test writing
44          ByteArrayOutputStream bos = new ByteArrayOutputStream();
45          schema.write(bos);
46  
47          //read this as a plain DOM and inspect our reference in question
48          DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
49          Document document = documentBuilder.parse(new ByteArrayInputStream(bos.toByteArray()));
50  
51          //find the element with name="atttest" and test its type attribute
52          //to see whether it includes a colon.
53          NodeList elementList = document.getElementsByTagName("element");
54          for(int i=0;i < elementList.getLength();i++){
55              Node n = elementList.item(i);
56              if (n.getNodeType() == Node.ELEMENT_NODE &&
57                      ((Element)n).hasAttribute("type")){
58                  assertTrue(((Element)n).getAttribute("type").indexOf(':') < 0);
59              }
60          }
61  
62  
63      }
64  
65  }