1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package tests.customext.elt;
20  
21  import junit.framework.TestCase;
22  import org.apache.ws.commons.schema.XmlSchema;
23  import org.apache.ws.commons.schema.XmlSchemaCollection;
24  import org.apache.ws.commons.schema.XmlSchemaElement;
25  import org.apache.ws.commons.schema.constants.Constants;
26  import org.w3c.dom.Document;
27  import tests.Resources;
28  
29  import javax.xml.parsers.DocumentBuilderFactory;
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.util.Iterator;
33  import java.util.Map;
34  
35  /**
36   *  Test class to run through the full cycle of build-serialize-build-check
37   */
38  public class CustomExtElementSerializerTest extends TestCase {
39  
40  
41      public void testSerialization() throws Exception {
42          //set the system property for the custom extension registry
43          System.setProperty(Constants.SystemConstants.EXTENSION_REGISTRY_KEY,
44                  CustomExtensionRegistry.class.getName());
45  
46          //create a DOM document
47          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
48          documentBuilderFactory.setNamespaceAware(true);
49  
50          // Skip test in JDK1.4 as it uses crimson parser and an old DOM implementation
51          if (documentBuilderFactory.getClass().toString().indexOf("crimson") != -1) {
52              return;
53          }
54          Document doc = documentBuilderFactory.newDocumentBuilder().
55                  parse(Resources.asURI("/external/externalElementAnnotations.xsd"));
56  
57          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
58          XmlSchema schema = schemaCol.read(doc,null);
59          assertNotNull(schema);
60  
61          //now serialize it to a byte stream
62          //and build a new document out of it
63          ByteArrayOutputStream baos = new ByteArrayOutputStream();
64          schema.write(baos);
65          Document doc2 = documentBuilderFactory.newDocumentBuilder().
66                  parse(new ByteArrayInputStream(baos.toByteArray()));
67  
68          // we can't have two copies in the same collection.
69          schemaCol = new XmlSchemaCollection();
70          schema = schemaCol.read(doc2,null);
71          assertNotNull(schema);
72  
73          // get the elements and check whether their annotations are properly
74          // populated
75          Iterator values = schema.getElements().getValues();
76          while (values.hasNext()) {
77              XmlSchemaElement elt =  (XmlSchemaElement) values.next();
78              assertNotNull(elt);
79              Map metaInfoMap = elt.getMetaInfoMap();
80              assertNotNull(metaInfoMap);
81  
82              CustomElement customElt = (CustomElement)metaInfoMap.get(CustomElement.CUSTOM_ELT_QNAME);
83              assertNotNull(customElt);
84  
85          }
86  
87          //remove our system property
88          System.getProperties().remove(Constants.SystemConstants.EXTENSION_REGISTRY_KEY);
89  
90      }
91  }