KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > encoding > EncodingTest


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.sax.SAXDocumentSerializer;
43 import com.sun.xml.fastinfoset.sax.VocabularyGenerator;
44 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
45 import java.io.ByteArrayOutputStream JavaDoc;
46 import java.io.FileOutputStream JavaDoc;
47 import java.io.InputStream JavaDoc;
48 import java.net.URI JavaDoc;
49 import java.net.URL JavaDoc;
50 import javax.xml.parsers.SAXParser JavaDoc;
51 import javax.xml.parsers.SAXParserFactory JavaDoc;
52 import junit.framework.*;
53
54 public class EncodingTest extends TestCase {
55     
56     public static final String JavaDoc FINF_SPEC_UBL_XML_RESOURCE = "X.finf/UBL-example.xml";
57     public static final String JavaDoc FINF_SPEC_UBL_FINF_RESOURCE = "X.finf/UBL-example.finf";
58     public static final String JavaDoc FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE = "X.finf/UBL-example-refvocab.finf";
59     
60     public static final String JavaDoc EXTERNAL_VOCABULARY_URI_STRING = "urn:oasis:names:tc:ubl:Order:1:0:joinery:example";
61     
62     private SAXParserFactory JavaDoc _saxParserFactory;
63     private SAXParser JavaDoc _saxParser;
64     private URL JavaDoc _xmlDocumentURL;
65     private URL JavaDoc _finfDocumentURL;
66     private URL JavaDoc _finfRefVocabDocumentURL;
67
68     private byte[] _finfDocument;
69     
70     private SAXDocumentSerializer _ds;
71     private SerializerVocabulary _initialVocabulary;
72     
73     public EncodingTest(java.lang.String JavaDoc testName) throws Exception JavaDoc {
74         super(testName);
75     
76         _saxParserFactory = SAXParserFactory.newInstance();
77         _saxParserFactory.setNamespaceAware(true);
78         _saxParser = _saxParserFactory.newSAXParser();
79
80         _xmlDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_XML_RESOURCE);
81         _finfDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_RESOURCE);
82         _finfRefVocabDocumentURL = this.getClass().getClassLoader().getResource(FINF_SPEC_UBL_FINF_REFVOCAB_RESOURCE);
83         
84         _ds = new SAXDocumentSerializer();
85         _ds.setCharacterContentChunkSizeLimit(6);
86         _ds.setAttributeValueSizeLimit(6);
87         _initialVocabulary = new SerializerVocabulary();
88     }
89     
90     public static Test suite() {
91         TestSuite suite = new TestSuite(EncodingTest.class);
92         return suite;
93     }
94     
95     public void testEncodeWithVocabulary() throws Exception JavaDoc {
96         SerializerVocabulary externalVocabulary = new SerializerVocabulary();
97         
98         VocabularyGenerator vocabularyGenerator = new VocabularyGenerator(externalVocabulary);
99         vocabularyGenerator.setCharacterContentChunkSizeLimit(0);
100         vocabularyGenerator.setAttributeValueSizeLimit(0);
101         _saxParser.parse(_xmlDocumentURL.openStream(), vocabularyGenerator);
102         
103         _initialVocabulary.setExternalVocabulary(
104                 new URI JavaDoc(EXTERNAL_VOCABULARY_URI_STRING),
105                 externalVocabulary, false);
106
107         _finfDocument = parse();
108         FileOutputStream JavaDoc foas = new FileOutputStream JavaDoc("new-UBL-example-refvocab.finf");
109         foas.write(_finfDocument);
110         
111         compare(obtainBytesFromStream(_finfRefVocabDocumentURL.openStream()));
112     }
113
114     public void testEncodeWithoutVocabulary() throws Exception JavaDoc {
115         _finfDocument = parse();
116         FileOutputStream JavaDoc foas = new FileOutputStream JavaDoc("new-UBL-example.finf");
117         foas.write(_finfDocument);
118         
119         compare(obtainBytesFromStream(_finfDocumentURL.openStream()));
120     }
121     
122     private byte[] parse() throws Exception JavaDoc {
123         _ds.setVocabulary(_initialVocabulary);
124
125         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
126         _ds.setOutputStream(baos);
127
128         _saxParser.parse(_xmlDocumentURL.openStream(), _ds);
129         
130         return baos.toByteArray();
131     }
132         
133     private void compare(byte[] specFiDocument) throws Exception JavaDoc {
134         this.assertTrue("Fast infoset document is not the same length as the X.finf specification",
135             _finfDocument.length == specFiDocument.length);
136         
137         System.out.println(_finfDocument.length);
138         System.out.println(specFiDocument.length);
139         boolean passed = true;
140         for (int i = 0; i < _finfDocument.length; i++) {
141             if (_finfDocument[i] != specFiDocument[i]) {
142                 System.err.println(Integer.toHexString(i) + ": " +
143                         Integer.toHexString(_finfDocument[i] & 0xFF) + " " +
144                         Integer.toHexString(specFiDocument[i] & 0xFF));
145                 passed = false;
146             }
147         }
148         
149         assertTrue("Fast infoset document does not have the same content as the X.finf specification",
150             passed);
151
152     }
153     
154     static byte[] obtainBytesFromStream(InputStream JavaDoc s) throws Exception JavaDoc {
155         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
156         
157         byte[] buffer = new byte[1024];
158         
159         int bytesRead = 0;
160         while ((bytesRead = s.read(buffer)) != -1) {
161             baos.write(buffer, 0, bytesRead);
162         }
163         return baos.toByteArray();
164     }
165 }
166
Popular Tags