KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alphabet > AlphabetTest


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 alphabet;
41
42 import com.sun.xml.fastinfoset.sax.AttributesHolder;
43 import com.sun.xml.fastinfoset.sax.SAXDocumentParser;
44 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
45 import com.sun.xml.fastinfoset.util.CharArrayString;
46 import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
47 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
48 import java.io.ByteArrayInputStream JavaDoc;
49 import java.io.ByteArrayOutputStream JavaDoc;
50 import java.io.InputStream JavaDoc;
51 import java.net.URI JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Map JavaDoc;
54 import junit.framework.*;
55 import org.jvnet.fastinfoset.FastInfosetParser;
56 import org.jvnet.fastinfoset.sax.FastInfosetDefaultHandler;
57 import org.xml.sax.Attributes JavaDoc;
58 import org.xml.sax.SAXException JavaDoc;
59
60 public class AlphabetTest extends TestCase {
61     protected static final String JavaDoc EXTERNAL_VOCABULARY_URI_STRING = "urn:external-vocabulary";
62         
63     protected AttributesHolder _attributes = new AttributesHolder();
64         
65     public AlphabetTest(String JavaDoc testName) {
66         super(testName);
67     }
68     
69     protected void setUp() throws java.lang.Exception JavaDoc {
70     }
71     
72     protected void tearDown() throws java.lang.Exception JavaDoc {
73     }
74     
75     public static junit.framework.Test suite() {
76         junit.framework.TestSuite suite = new junit.framework.TestSuite(AlphabetTest.class);
77         
78         return suite;
79     }
80         
81     public void testBuiltInAlphabet1() throws Exception JavaDoc {
82         String JavaDoc n = "E";
83         String JavaDoc d = "Z";
84         _testBuiltInAlphabet(n, d);
85     }
86     
87     public void testBuiltInAlphabet2() throws Exception JavaDoc {
88         String JavaDoc n = "E ";
89         String JavaDoc d = "Z ";
90         _testBuiltInAlphabet(n, d);
91     }
92
93     public void testBuiltInAlphabet3() throws Exception JavaDoc {
94         String JavaDoc n = "E 9";
95         String JavaDoc d = "Z 9";
96         _testBuiltInAlphabet(n, d);
97     }
98     
99     public void testBuiltInAlphabet4() throws Exception JavaDoc {
100         String JavaDoc n = "E 91";
101         String JavaDoc d = "Z 91";
102         _testBuiltInAlphabet(n, d);
103     }
104     
105     public void testBuiltInAlphabet5() throws Exception JavaDoc {
106         String JavaDoc n = "E 912";
107         String JavaDoc d = "Z 912";
108         _testBuiltInAlphabet(n, d);
109     }
110     
111     public void testBuiltInAlphabet30() throws Exception JavaDoc {
112         String JavaDoc n = "0123456789-+.E 0123456789-+.E ";
113         String JavaDoc d = "0123456789-:TZ 0123456789-:TZ ";
114         _testBuiltInAlphabet(n, d);
115     }
116     
117     public void _testBuiltInAlphabet(String JavaDoc n, String JavaDoc d) throws Exception JavaDoc {
118         byte[] b = createBuiltInFastInfosetDocument(n, d);
119         InputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
120         
121         SAXDocumentParser p = new SAXDocumentParser();
122         
123         BuiltInTestHandler h = new BuiltInTestHandler(n, d);
124         
125         p.setContentHandler(h);
126
127         p.parse(bais);
128     }
129     
130     protected byte[] createBuiltInFastInfosetDocument(String JavaDoc numericString, String JavaDoc dateTimeString) throws Exception JavaDoc {
131         SAXDocumentSerializer s = new SAXDocumentSerializer();
132         
133         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
134         s.setOutputStream(baos);
135         
136         _attributes.clear();
137                 
138         s.startDocument();
139         
140         s.startElement("", "e", "e", _attributes);
141
142         s.startElement("", "numeric", "numeric", _attributes);
143         s.numericCharacters(numericString.toCharArray(), 0, numericString.length());
144         s.endElement("", "numeric", "numeric");
145                 
146         s.startElement("", "dateTime", "dateTime", _attributes);
147         s.dateTimeCharacters(dateTimeString.toCharArray(), 0, dateTimeString.length());
148         s.endElement("", "dateTime", "dateTime");
149         
150         s.endElement("", "e", "e");
151         
152         s.endDocument();
153         
154         return baos.toByteArray();
155     }
156     
157     public class BuiltInTestHandler extends FastInfosetDefaultHandler {
158         String JavaDoc _localName;
159         
160         String JavaDoc _numericString;
161         
162         String JavaDoc _dateTimeString;
163        
164         public BuiltInTestHandler(String JavaDoc numericString, String JavaDoc dateTimeString) {
165             _numericString = numericString;
166             _dateTimeString = dateTimeString;
167         }
168         
169         // ContentHandler
170

171         public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
172             _localName = localName;
173         }
174         
175         public final void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
176             if (_localName == "numeric") {
177                 assertEquals(_numericString.length(), length);
178                 assertEquals(_numericString, new String JavaDoc(ch, start, length));
179             } else if (_localName == "dateTime") {
180                 assertEquals(_dateTimeString.length(), length);
181                 assertEquals(_dateTimeString, new String JavaDoc(ch, start, length));
182             }
183         }
184     }
185     
186     public void testApplicationNumericAlphabet() throws Exception JavaDoc {
187         _testApplicationAlphabet("0123456789-+.E ", "0123456789-+.E 0123456789-+.E ");
188     }
189     
190     public void testApplicationTwoCharAlphabet() throws Exception JavaDoc {
191         _testApplicationAlphabet("01", "1");
192         _testApplicationAlphabet("01", "0");
193         _testApplicationAlphabet("01", "1010101010101010101");
194     }
195
196     public void testApplicationThreeCharAlphabet() throws Exception JavaDoc {
197         _testApplicationAlphabet("01X", "1");
198         _testApplicationAlphabet("01X", "10");
199         _testApplicationAlphabet("01X", "01X01X01X01X01X01X01X01X");
200     }
201     
202     public void testApplicationFourCharAlphabet() throws Exception JavaDoc {
203         _testApplicationAlphabet("01XY", "0");
204         _testApplicationAlphabet("01XY", "Y0");
205         _testApplicationAlphabet("01XY", "01XY01XY01XY01XY01XY01XY01XY");
206     }
207     
208     public void testApplicationSixteenCharAlphabet() throws Exception JavaDoc {
209         _testApplicationAlphabet("0123456789ABCDEF", "0");
210         _testApplicationAlphabet("0123456789ABCDEF", "F0");
211         _testApplicationAlphabet("0123456789ABCDEF", "0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF");
212     }
213     
214     public void _testApplicationAlphabet(String JavaDoc a, String JavaDoc s) throws Exception JavaDoc {
215         byte[] b = createApplicationFastInfosetDocument(a, s);
216         InputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
217                 
218         SAXDocumentParser p = new SAXDocumentParser();
219         
220         ParserVocabulary externalVocabulary = new ParserVocabulary();
221         externalVocabulary.restrictedAlphabet.add(new CharArrayString(a));
222         
223         Map JavaDoc externalVocabularies = new HashMap JavaDoc();
224         externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, externalVocabulary);
225         p.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies);
226         
227         ApplicationTestHandler h = new ApplicationTestHandler(a, s);
228         
229         p.setContentHandler(h);
230
231         p.parse(bais);
232     }
233
234     protected byte[] createApplicationFastInfosetDocument(String JavaDoc alphabet, String JavaDoc value) throws Exception JavaDoc {
235         SAXDocumentSerializer s = new SAXDocumentSerializer();
236
237         SerializerVocabulary externalVocabulary = new SerializerVocabulary();
238         externalVocabulary.restrictedAlphabet.add(alphabet);
239         
240         SerializerVocabulary initialVocabulary = new SerializerVocabulary();
241         initialVocabulary.setExternalVocabulary(
242                 new URI JavaDoc(EXTERNAL_VOCABULARY_URI_STRING),
243                 externalVocabulary, false);
244         
245         s.setVocabulary(initialVocabulary);
246         
247         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
248         s.setOutputStream(baos);
249         
250         _attributes.clear();
251                 
252         s.startDocument();
253         
254         s.startElement("", "content", "content", _attributes);
255         s.alphabetCharacters(alphabet, value.toCharArray(), 0, value.length());
256         s.endElement("", "content", "content");
257                         
258         s.endDocument();
259         
260         return baos.toByteArray();
261     }
262
263     public class ApplicationTestHandler extends FastInfosetDefaultHandler {
264         String JavaDoc _alphabet;
265         
266         String JavaDoc _string;
267        
268         public ApplicationTestHandler(String JavaDoc alphabet, String JavaDoc string) {
269             _alphabet = alphabet;
270             _string = string;
271         }
272         
273         // ContentHandler
274

275         public final void characters(char[] ch, int start, int length) throws SAXException JavaDoc {
276             assertEquals(_string.length(), length);
277             assertEquals(_string, new String JavaDoc(ch, start, length));
278         }
279     }
280     
281 }
282
Popular Tags