KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > encoding > DecodingTest


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39
40 package encoding;
41
42 import com.sun.xml.fastinfoset.EncodingConstants;
43 import com.sun.xml.fastinfoset.sax.AttributesHolder;
44 import com.sun.xml.fastinfoset.sax.SAXDocumentParser;
45 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
46 import com.sun.xml.fastinfoset.sax.VocabularyGenerator;
47 import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
48 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
49 import java.io.ByteArrayInputStream JavaDoc;
50 import java.io.ByteArrayOutputStream JavaDoc;
51 import java.io.InputStream JavaDoc;
52 import java.net.URI JavaDoc;
53 import java.net.URL JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Map JavaDoc;
56 import javax.xml.parsers.SAXParser JavaDoc;
57 import javax.xml.parsers.SAXParserFactory JavaDoc;
58 import junit.framework.Test;
59 import junit.framework.TestCase;
60 import junit.framework.TestSuite;
61 import org.jvnet.fastinfoset.FastInfosetParser;
62
63 public class DecodingTest extends TestCase {
64
65     public static final String JavaDoc FINF_SPEC_UBL_XML_RESOURCE = "X.finf/UBL-example.xml";
66     public static final String JavaDoc FINF_SPEC_UBL_FINF_RESOURCE = "X.finf/UBL-example.finf";
67     public static final String JavaDoc FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE = "X.finf/UBL-example-refvocab.finf";
68     
69     public static final String JavaDoc EXTERNAL_VOCABULARY_URI_STRING = "urn:oasis:names:tc:ubl:Order:1:0:joinery:example";
70
71     private SAXParserFactory JavaDoc _saxParserFactory;
72     private SAXParser JavaDoc _saxParser;
73     private URL JavaDoc _xmlDocumentURL;
74     private URL JavaDoc _finfDocumentURL;
75     private URL JavaDoc _finfRefVocabDocumentURL;
76         
77     /** Creates a new instance of DecodeTestCase */
78     public DecodingTest() throws Exception JavaDoc {
79         _saxParserFactory = SAXParserFactory.newInstance();
80         _saxParserFactory.setNamespaceAware(true);
81         _saxParser = _saxParserFactory.newSAXParser();
82         
83         _xmlDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_XML_RESOURCE);
84         _finfDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_RESOURCE);
85         _finfRefVocabDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE);
86     }
87
88     public static Test suite() {
89         TestSuite suite = new TestSuite(DecodingTest.class);
90         return suite;
91     }
92
93     public void testDecodeWithVocabulary() throws Exception JavaDoc {
94         SerializerVocabulary serializerExternalVocabulary = new SerializerVocabulary();
95         ParserVocabulary parserExternalVocabulary = new ParserVocabulary();
96         
97         VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(serializerExternalVocabulary, parserExternalVocabulary);
98         vocabularyGenerator.setCharacterContentChunkSizeLimit(0);
99         vocabularyGenerator.setAttributeValueSizeLimit(0);
100         _saxParser.parse(_xmlDocumentURL.openStream(), vocabularyGenerator);
101
102         SerializerVocabulary initialVocabulary = new SerializerVocabulary();
103         initialVocabulary.setExternalVocabulary(
104                 new URI JavaDoc(EXTERNAL_VOCABULARY_URI_STRING),
105                 serializerExternalVocabulary, false);
106
107         // Map<String, ParserVocabulary> externalVocabularies = new HashMap<String, ParserVocabulary>();
108
Map JavaDoc externalVocabularies = new HashMap JavaDoc();
109         externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, parserExternalVocabulary);
110         
111         
112         SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer();
113         documentSerializer.setCharacterContentChunkSizeLimit(6);
114         documentSerializer.setAttributeValueSizeLimit(6);
115         documentSerializer.setVocabulary(initialVocabulary);
116         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
117         documentSerializer.setOutputStream(baos);
118
119         SAXDocumentParser documentParser = new SAXDocumentParser();
120         documentParser.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies);
121         documentParser.setContentHandler(documentSerializer);
122         documentParser.parse(_finfRefVocabDocumentURL.openStream());
123
124         
125         byte[] finfDocument = baos.toByteArray();
126         compare(finfDocument, obtainBytesFromStream(_finfRefVocabDocumentURL.openStream()));
127     }
128
129     public void testDecodeWithoutVocabulary() throws Exception JavaDoc {
130         SerializerVocabulary initialVocabulary = new SerializerVocabulary();
131
132         SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer();
133         documentSerializer.setCharacterContentChunkSizeLimit(6);
134         documentSerializer.setAttributeValueSizeLimit(6);
135         documentSerializer.setVocabulary(initialVocabulary);
136         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
137         documentSerializer.setOutputStream(baos);
138
139         SAXDocumentParser documentParser = new SAXDocumentParser();
140         documentParser.setContentHandler(documentSerializer);
141         documentParser.parse(_finfDocumentURL.openStream());
142
143         
144         byte[] finfDocument = baos.toByteArray();
145         compare(finfDocument, obtainBytesFromStream(_finfDocumentURL.openStream()));
146     }
147  
148         
149     private void compare(byte[] fiDocument, byte[] specFiDocument) throws Exception JavaDoc {
150         this.assertTrue("Fast infoset document is not the same length as the X.finf specification",
151             fiDocument.length == specFiDocument.length);
152         
153         boolean passed = true;
154         for (int i = 0; i < fiDocument.length; i++) {
155             if (fiDocument[i] != specFiDocument[i]) {
156                 System.err.println(Integer.toHexString(i) + ": " +
157                         Integer.toHexString(fiDocument[i] & 0xFF) + " " +
158                         Integer.toHexString(specFiDocument[i] & 0xFF));
159                 passed = false;
160             }
161         }
162         
163         assertTrue("Fast infoset document does not have the same content as the X.finf specification",
164             passed);
165
166     }
167
168     public void testDecodeWithXMLDeclaration() throws Exception JavaDoc {
169         for (int i = 0; i < EncodingConstants.XML_DECLARATION_VALUES.length; i++) {
170             _testDecodeWithXMLDeclaration(EncodingConstants.XML_DECLARATION_VALUES[i]);
171         }
172     }
173     
174     public void _testDecodeWithXMLDeclaration(byte[] header) throws Exception JavaDoc {
175         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
176         baos.write(header);
177         
178         SAXDocumentSerializer sds = new SAXDocumentSerializer();
179         sds.setOutputStream(baos);
180         
181         sds.startDocument();
182             sds.startElement("", "element", "element", new AttributesHolder());
183                 sds.characters(new String JavaDoc("foo").toCharArray(), 0, 3);
184             sds.endElement("", "element", "element");
185         sds.endDocument();
186         
187         SAXDocumentParser sdp = new SAXDocumentParser();
188         sdp.setInputStream(new ByteArrayInputStream JavaDoc(baos.toByteArray()));
189         sdp.parse();
190     }
191
192     
193     static byte[] obtainBytesFromStream(InputStream JavaDoc s) throws Exception JavaDoc {
194         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
195         
196         byte[] buffer = new byte[1024];
197         
198         int bytesRead = 0;
199         while ((bytesRead = s.read(buffer)) != -1) {
200             baos.write(buffer, 0, bytesRead);
201         }
202         return baos.toByteArray();
203     }
204 }
205
Popular Tags