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