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  
20  package tests;
21  
22  import junit.framework.TestCase;
23  import org.apache.ws.commons.schema.XmlSchema;
24  import org.apache.ws.commons.schema.XmlSchemaCollection;
25  import org.w3c.dom.Document;
26  
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  import javax.xml.transform.OutputKeys;
30  
31  import java.io.ByteArrayInputStream;
32  import java.io.ByteArrayOutputStream;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  public class EncodingTest extends TestCase {
37  
38  
39      public void testExternalAtt() throws Exception{
40               //create a DOM document
41             DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
42             documentBuilderFactory.setNamespaceAware(true);
43             DocumentBuilder newDocumentBuilder = documentBuilderFactory.newDocumentBuilder();
44  
45             // Skip test in JDK1.4 as it uses crimson parser and an old DOM implementation
46             try {
47                 Document.class.getMethod("getInputEncoding", new Class[]{});
48             } catch (NoSuchMethodException e) {
49                 System.out.println(">>>> Ignoring test as it needs DOM3");
50                 return;
51             }
52  
53  		   Document doc = newDocumentBuilder.
54                     parse(Resources.asURI("other_encoding/japaneseElementForm.xsd"));
55             
56             XmlSchemaCollection schemaCol = new XmlSchemaCollection();
57             XmlSchema s1 = schemaCol.read(doc.getDocumentElement());
58             assertNotNull(s1);
59             assertEquals("EUC-JP", s1.getInputEncoding().toUpperCase());
60             
61            //write it back to a stream - re read it and check the encoding
62             //we need to explicitly say to have the xml header
63             Map options = new HashMap();
64             options.put(OutputKeys.OMIT_XML_DECLARATION, "no");
65             
66             ByteArrayOutputStream baos = new ByteArrayOutputStream();
67             s1.write(baos,options);
68             
69             schemaCol = new XmlSchemaCollection();
70             Document doc2 = newDocumentBuilder.parse(new ByteArrayInputStream(baos.toByteArray()));
71             XmlSchema s2 = schemaCol.read(doc2.getDocumentElement());
72             assertNotNull(s2);
73             assertEquals("EUC-JP", s2.getInputEncoding().toUpperCase());
74              
75             
76             
77  
78         }
79  
80  }