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.attrib;
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 do a full parsing run with the extensions
34   */
35  public class CustomExtensionSerializerTest extends TestCase {
36  
37      public void testSerialization() throws Exception {
38          //set the system property for the custom extension registry
39          System.setProperty(Constants.SystemConstants.EXTENSION_REGISTRY_KEY,
40                  CustomExtensionRegistry.class.getName());
41  
42          //create a DOM document
43          DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
44          documentBuilderFactory.setNamespaceAware(true);
45          Document doc1 = documentBuilderFactory.newDocumentBuilder().
46                  parse(Resources.asURI("/external/externalAnnotations.xsd"));
47  
48          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
49          XmlSchema schema = schemaCol.read(doc1,null);
50          assertNotNull(schema);
51  
52          //now serialize it to a byte stream
53          //and build a new document out of it
54          ByteArrayOutputStream baos = new ByteArrayOutputStream();
55          schema.write(baos);
56  
57  
58          Document doc2 = documentBuilderFactory.newDocumentBuilder().
59                  parse(new ByteArrayInputStream(baos.toByteArray()));
60  
61          schema = schemaCol.read(doc2,null);
62          assertNotNull(schema);
63  
64          // get the elements and check whether their annotations are properly
65          // populated
66          Iterator values = schema.getElements().getValues();
67          while (values.hasNext()) {
68              XmlSchemaElement elt =  (XmlSchemaElement) values.next();
69              assertNotNull(elt);
70              Map metaInfoMap = elt.getMetaInfoMap();
71              assertNotNull(metaInfoMap);
72  
73              CustomAttribute customAttrib = (CustomAttribute)metaInfoMap.get(CustomAttribute.CUSTOM_ATTRIBUTE_QNAME);
74              assertNotNull(customAttrib);
75  
76          }
77  
78  
79          //remove our system property
80          System.getProperties().remove(Constants.SystemConstants.EXTENSION_REGISTRY_KEY);
81  
82      }
83  
84  }