KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > algorithm > AlgorithmTest


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 algorithm;
41
42 import com.sun.xml.fastinfoset.QualifiedName;
43 import com.sun.xml.fastinfoset.algorithm.BASE64EncodingAlgorithm;
44 import com.sun.xml.fastinfoset.algorithm.DoubleEncodingAlgorithm;
45 import com.sun.xml.fastinfoset.algorithm.FloatEncodingAlgorithm;
46 import com.sun.xml.fastinfoset.algorithm.IntEncodingAlgorithm;
47 import com.sun.xml.fastinfoset.algorithm.LongEncodingAlgorithm;
48 import com.sun.xml.fastinfoset.algorithm.ShortEncodingAlgorithm;
49 import com.sun.xml.fastinfoset.algorithm.BooleanEncodingAlgorithm;
50 import com.sun.xml.fastinfoset.dom.DOMDocumentParser;
51 import com.sun.xml.fastinfoset.sax.AttributesHolder;
52 import com.sun.xml.fastinfoset.sax.SAXDocumentParser;
53 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer;
54 import com.sun.xml.fastinfoset.stax.StAXDocumentParser;
55 import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer;
56 import com.sun.xml.fastinfoset.vocab.ParserVocabulary;
57 import com.sun.xml.fastinfoset.vocab.SerializerVocabulary;
58 import java.io.ByteArrayInputStream JavaDoc;
59 import java.io.ByteArrayOutputStream JavaDoc;
60 import java.io.InputStream JavaDoc;
61 import java.net.URI JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Map JavaDoc;
64 import javax.xml.parsers.DocumentBuilder JavaDoc;
65 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
66 import javax.xml.stream.XMLStreamReader;
67 import junit.framework.*;
68 import org.jvnet.fastinfoset.EncodingAlgorithmIndexes;
69 import org.jvnet.fastinfoset.FastInfosetParser;
70 import org.jvnet.fastinfoset.sax.EncodingAlgorithmAttributes;
71 import org.jvnet.fastinfoset.sax.FastInfosetDefaultHandler;
72 import org.w3c.dom.Document JavaDoc;
73 import org.w3c.dom.Text JavaDoc;
74 import org.xml.sax.Attributes JavaDoc;
75 import org.xml.sax.SAXException JavaDoc;
76
77 public class AlgorithmTest extends TestCase {
78     protected static final int ARRAY_SIZE = 8;
79     protected static final int APPLICATION_DEFINED_ALGORITHM_ID = 32;
80     protected static final String JavaDoc APPLICATION_DEFINED_ALGORITHM_URI = "algorithm-32";
81     protected static final String JavaDoc EXTERNAL_VOCABULARY_URI_STRING = "urn:external-vocabulary";
82
83     protected String JavaDoc _base64String = "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygp" +
84             "KissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJT" +
85             "VFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9" +
86             "fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaan" +
87             "qKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR" +
88             "0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7" +
89             "/P3+/w==";
90
91     protected AttributesHolder _attributes = new AttributesHolder();
92
93     protected byte[] _byteArray;
94     protected short[] _shortArray;
95     protected int[] _intArray;
96     protected long[] _longArray;
97     protected float[] _floatArray;
98     protected double[] _doubleArray;
99     protected long[] _uuidArray;
100     protected boolean[] _booleanArray;
101
102     protected String JavaDoc _cdata = new String JavaDoc("CDATA characters");
103     protected String JavaDoc _characters = new String JavaDoc("characters");
104
105     public AlgorithmTest(String JavaDoc testName) {
106         super(testName);
107     }
108
109     protected void setUp() throws java.lang.Exception JavaDoc {
110     }
111
112     protected void tearDown() throws java.lang.Exception JavaDoc {
113     }
114
115     public static junit.framework.Test suite() {
116         junit.framework.TestSuite suite = new junit.framework.TestSuite(AlgorithmTest.class);
117
118         return suite;
119     }
120
121     public void testBase64AlgorithmNoZerosNoWhiteSpace() throws Exception JavaDoc {
122         byte[] data = new byte[9];
123         _testBase64Algorithm("AAAAAAAAAAAA", "AAAAAAAAAAAA", data);
124     }
125
126     public void testBase64AlgorithmNoZerosWhiteSpace() throws Exception JavaDoc {
127         byte[] data = new byte[9];
128         _testBase64Algorithm(" AAA AA AAA AAAA ", "AAAAAAAAAAAA", data);
129     }
130
131     public void testBase64Algorithm() throws Exception JavaDoc {
132         byte[] data = new byte[256];
133         for (int i = 0; i < 256; i++) {
134             data[i] = (byte)i;
135         }
136
137         _testBase64Algorithm(_base64String, _base64String, data);
138     }
139
140     public void _testBase64Algorithm(String JavaDoc base64Characters, String JavaDoc base64CharactersNoWS, byte[] b) throws Exception JavaDoc {
141         BASE64EncodingAlgorithm b64ea = new BASE64EncodingAlgorithm();
142
143         byte[] data = (byte[])b64ea.convertFromCharacters(
144                 base64Characters.toCharArray(), 0, base64Characters.length());
145
146         assertEquals(b.length, data.length);
147         for (int i = 0; i < data.length; i++) {
148             assertEquals(b[i], data[i]);
149         }
150
151         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
152         b64ea.convertToCharacters(data, s);
153         assertEquals(base64CharactersNoWS.length(), s.length());
154         assertEquals(base64CharactersNoWS, s.toString());
155     }
156
157     public void testShortAlgorithm() throws Exception JavaDoc {
158         createArrayValues(ARRAY_SIZE);
159
160         byte[] b = new byte[ARRAY_SIZE * 2];
161
162         ShortEncodingAlgorithm sea = new ShortEncodingAlgorithm();
163         sea.encodeToBytesFromShortArray(_shortArray, 0, ARRAY_SIZE, b, 0);
164
165         short[] i = new short[ARRAY_SIZE];
166         sea.decodeFromBytesToShortArray(i, 0, b, 0, b.length);
167
168         for (int is = 0; is < ARRAY_SIZE; is++) {
169             assertEquals(_shortArray[is], i[is]);
170         }
171     }
172
173     public void testBooleanAlgorithm1() throws Exception JavaDoc {
174         _testBooleanAlgorithm(1);
175     }
176     
177     public void testBooleanAlgorithm2() throws Exception JavaDoc {
178         _testBooleanAlgorithm(2);
179     }
180     
181     public void testBooleanAlgorithm3() throws Exception JavaDoc {
182         _testBooleanAlgorithm(3);
183     }
184     
185     public void testBooleanAlgorithm4() throws Exception JavaDoc {
186         _testBooleanAlgorithm(4);
187     }
188     
189     public void testBooleanAlgorithm5() throws Exception JavaDoc {
190         _testBooleanAlgorithm(5);
191     }
192     
193     public void testBooleanAlgorithm6() throws Exception JavaDoc {
194         _testBooleanAlgorithm(6);
195     }
196     
197     public void testBooleanAlgorithm7() throws Exception JavaDoc {
198         _testBooleanAlgorithm(7);
199     }
200     
201     public void testBooleanAlgorithm8() throws Exception JavaDoc {
202         _testBooleanAlgorithm(8);
203     }
204
205     public void testBooleanAlgorithm9() throws Exception JavaDoc {
206         _testBooleanAlgorithm(9);
207     }
208     
209     public void testBooleanAlgorithm10() throws Exception JavaDoc {
210         _testBooleanAlgorithm(10);
211     }
212     
213     public void testBooleanAlgorithm11() throws Exception JavaDoc {
214         _testBooleanAlgorithm(11);
215     }
216     
217     public void testBooleanAlgorithm12() throws Exception JavaDoc {
218         _testBooleanAlgorithm(12);
219     }
220     
221     public void testBooleanAlgorithm32() throws Exception JavaDoc {
222         _testBooleanAlgorithm(32);
223     }
224     
225     public void _testBooleanAlgorithm(int size) throws Exception JavaDoc {
226         createArrayValues(size);
227
228         BooleanEncodingAlgorithm bea = new BooleanEncodingAlgorithm();
229         byte[] b = new byte[bea.getOctetLengthFromPrimitiveLength(size)];
230         bea.encodeToBytesFromBooleanArray(_booleanArray, 0, size, b, 0);
231
232         int bsize = bea.getPrimtiveLengthFromOctetLength(b.length, b[0]);
233         assertEquals(size, bsize);
234         boolean[] i = new boolean[bsize];
235         bea.decodeFromBytesToBooleanArray(i, 0, bsize, b, 0, b.length);
236
237         for (int is = 0; is < size; is++) {
238             assertEquals(_booleanArray[is], i[is]);
239         }
240     }
241     
242     public void testIntAlgorithm() throws Exception JavaDoc {
243         createArrayValues(ARRAY_SIZE);
244
245         byte[] b = new byte[ARRAY_SIZE * 4];
246
247         IntEncodingAlgorithm iea = new IntEncodingAlgorithm();
248         iea.encodeToBytesFromIntArray(_intArray, 0, ARRAY_SIZE, b, 0);
249
250         int[] i = new int[ARRAY_SIZE];
251         iea.decodeFromBytesToIntArray(i, 0, b, 0, b.length);
252
253         for (int is = 0; is < ARRAY_SIZE; is++) {
254             assertEquals(_intArray[is], i[is]);
255         }
256     }
257
258     public void testLongAlgorithm() throws Exception JavaDoc {
259         createArrayValues(ARRAY_SIZE);
260
261         byte[] b = new byte[ARRAY_SIZE * 8];
262
263         LongEncodingAlgorithm iea = new LongEncodingAlgorithm();
264         iea.encodeToBytesFromLongArray(_longArray, 0, ARRAY_SIZE, b, 0);
265
266         long[] i = new long[ARRAY_SIZE];
267         iea.decodeFromBytesToLongArray(i, 0, b, 0, b.length);
268
269         for (int is = 0; is < ARRAY_SIZE; is++) {
270             assertEquals(_longArray[is], i[is]);
271         }
272     }
273
274     public void testFloatAlgorithm() throws Exception JavaDoc {
275         createArrayValues(ARRAY_SIZE);
276
277         byte[] b = new byte[ARRAY_SIZE * 4];
278
279         FloatEncodingAlgorithm fea = new FloatEncodingAlgorithm();
280         fea.encodeToBytesFromFloatArray(_floatArray, 0, ARRAY_SIZE, b, 0);
281
282         float[] f = new float[ARRAY_SIZE];
283         fea.decodeFromBytesToFloatArray(f, 0, b, 0, b.length);
284
285         for (int is = 0; is < ARRAY_SIZE; is++) {
286             assertEquals(_floatArray[is], f[is], 0.0);
287         }
288     }
289
290     public void testDoubleAlgorithm() throws Exception JavaDoc {
291         createArrayValues(ARRAY_SIZE);
292
293         byte[] b = new byte[ARRAY_SIZE * 8];
294
295         DoubleEncodingAlgorithm fea = new DoubleEncodingAlgorithm();
296         fea.encodeToBytesFromDoubleArray(_doubleArray, 0, ARRAY_SIZE, b, 0);
297
298         double[] f = new double[ARRAY_SIZE];
299         fea.decodeFromBytesToDoubleArray(f, 0, b, 0, b.length);
300
301         for (int is = 0; is < ARRAY_SIZE; is++) {
302             assertEquals(_doubleArray[is], f[is], 0.0);
303         }
304     }
305
306     public void testUUIDAlgorithm() throws Exception JavaDoc {
307         createArrayValues(ARRAY_SIZE);
308
309         byte[] b = new byte[ARRAY_SIZE * 16];
310
311         LongEncodingAlgorithm iea = new LongEncodingAlgorithm();
312         iea.encodeToBytesFromLongArray(_uuidArray, 0, ARRAY_SIZE * 2, b, 0);
313
314         long[] i = new long[ARRAY_SIZE * 2];
315         iea.decodeFromBytesToLongArray(i, 0, b, 0, b.length);
316
317         for (int is = 0; is < ARRAY_SIZE * 2; is++) {
318             assertEquals(_uuidArray[is], i[is]);
319         }
320     }
321
322     public void testBuiltInAlgorithmsSize1() throws Exception JavaDoc {
323         _testBuiltInAlgorithms(1);
324     }
325
326     public void testBuiltInAlgorithmsSize2() throws Exception JavaDoc {
327         _testBuiltInAlgorithms(1);
328     }
329
330     public void testBuiltInAlgorithmsSize8() throws Exception JavaDoc {
331         _testBuiltInAlgorithms(8);
332     }
333
334     public void testBuiltInAlgorithmsSize32() throws Exception JavaDoc {
335         _testBuiltInAlgorithms(32);
336     }
337
338     public void testBuiltInAlgorithmsSiz64() throws Exception JavaDoc {
339         _testBuiltInAlgorithms(64);
340     }
341
342     public void testBuiltInAlgorithmsSiz256() throws Exception JavaDoc {
343         _testBuiltInAlgorithms(256);
344     }
345
346     public void testBuiltInAlgorithmsSiz512() throws Exception JavaDoc {
347         _testBuiltInAlgorithms(512);
348     }
349
350     public void _testBuiltInAlgorithms(int arraySize) throws Exception JavaDoc {
351         createArrayValues(arraySize);
352
353         byte[] b = createBuiltInTestFastInfosetDocument();
354         InputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
355
356         SAXDocumentParser p = new SAXDocumentParser();
357
358         BuiltInTestHandler h = new BuiltInTestHandler(arraySize);
359
360         p.setContentHandler(h);
361         p.setLexicalHandler(h);
362         p.setPrimitiveTypeContentHandler(h);
363
364         p.parse(bais);
365     }
366
367     protected byte[] createBuiltInTestFastInfosetDocument() throws Exception JavaDoc {
368         SAXDocumentSerializer s = new SAXDocumentSerializer();
369
370         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
371         s.setOutputStream(baos);
372
373         _attributes.clear();
374
375
376         s.startDocument();
377
378         s.startElement("", "e", "e", _attributes);
379
380         // Booleans
381
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "boolean", "boolean"),
382                 null, EncodingAlgorithmIndexes.BOOLEAN, _booleanArray);
383         s.startElement("", "boolean", "boolean", _attributes);
384         _attributes.clear();
385         s.booleans(_booleanArray, 0, _booleanArray.length);
386         s.endElement("", "boolean", "boolean");
387         
388         // Bytes
389
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "byte", "byte"),
390                 null, EncodingAlgorithmIndexes.BASE64, _byteArray);
391         s.startElement("", "byte", "byte", _attributes);
392         _attributes.clear();
393         s.bytes(_byteArray, 0, _byteArray.length);
394         s.endElement("", "byte", "byte");
395
396         // Shorts
397
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "short", "short"),
398                 null, EncodingAlgorithmIndexes.SHORT, _shortArray);
399         s.startElement("", "short", "short", _attributes);
400         _attributes.clear();
401         s.shorts(_shortArray, 0, _shortArray.length);
402         s.endElement("", "short", "short");
403
404         // Ints
405
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "int", "int"),
406                 null, EncodingAlgorithmIndexes.INT, _intArray);
407         s.startElement("", "int", "int", _attributes);
408         _attributes.clear();
409         s.ints(_intArray, 0, _intArray.length);
410         s.endElement("", "int", "int");
411
412         // Longs
413
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "long", "long"),
414                 null, EncodingAlgorithmIndexes.LONG, _longArray);
415         s.startElement("", "long", "long", _attributes);
416         _attributes.clear();
417         s.longs(_longArray, 0, _longArray.length);
418         s.endElement("", "long", "long");
419
420         // Floats
421
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "float", "float"),
422                 null, EncodingAlgorithmIndexes.FLOAT, _floatArray);
423         s.startElement("", "float", "float", _attributes);
424         _attributes.clear();
425         s.floats(_floatArray, 0, _floatArray.length);
426         s.endElement("", "float", "float");
427
428         // Doubles
429
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "double", "double"),
430                 null, EncodingAlgorithmIndexes.DOUBLE, _doubleArray);
431         s.startElement("", "double", "double", _attributes);
432         _attributes.clear();
433         s.doubles(_doubleArray, 0, _doubleArray.length);
434         s.endElement("", "double", "double");
435
436         // UUIDs
437
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "uuid", "uuid"),
438                 null, EncodingAlgorithmIndexes.UUID, _uuidArray);
439         s.startElement("", "uuid", "uuid", _attributes);
440         _attributes.clear();
441         s.uuids(_uuidArray, 0, _uuidArray.length);
442         s.endElement("", "uuid", "uuid");
443
444         // CDATA
445
s.startElement("", "cdata", "cdata", _attributes);
446             s.startCDATA();
447             s.characters(_cdata.toCharArray(), 0, _cdata.length());
448             s.endCDATA();
449         s.endElement("", "cdata", "cdata");
450
451         // Characters
452
s.startElement("", "characters", "characters", _attributes);
453             s.characters(_characters.toCharArray(), 0, _characters.length());
454         s.endElement("", "characters", "characters");
455
456         s.endElement("", "e", "e");
457
458         s.endDocument();
459
460         return baos.toByteArray();
461     }
462
463     public class BuiltInTestHandler extends FastInfosetDefaultHandler {
464
465         protected int _arraySize;
466
467         protected boolean _charactersAsCDATA;
468
469         protected boolean _charactersShouldBeAsCDATA;
470
471         public BuiltInTestHandler(int arraySize) {
472             _arraySize = arraySize;
473         }
474
475         // ContentHandler
476

477         public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException JavaDoc {
478             if (atts.getLength() > 0) {
479                 EncodingAlgorithmAttributes eas = (EncodingAlgorithmAttributes)atts;
480
481                 assertEquals(1, atts.getLength());
482
483                 if (localName.equals("boolean")) {
484                     assertEquals("boolean", eas.getLocalName(0));
485                     assertEquals(EncodingAlgorithmIndexes.BOOLEAN, eas.getAlgorithmIndex(0));
486                     assertEquals(null, eas.getAlgorithmURI(0));
487                     assertEquals(true, eas.getAlgorithmData(0) instanceof boolean[]);
488                     boolean[] b = (boolean[])eas.getAlgorithmData(0);
489                     for (int is = 0; is < _booleanArray.length; is++) {
490                         assertEquals(_booleanArray[is], b[is]);
491                     }
492                 } else if (localName.equals("byte")) {
493                     assertEquals("byte", eas.getLocalName(0));
494                     assertEquals(EncodingAlgorithmIndexes.BASE64, eas.getAlgorithmIndex(0));
495                     assertEquals(null, eas.getAlgorithmURI(0));
496                     assertEquals(true, eas.getAlgorithmData(0) instanceof byte[]);
497                     byte[] b = (byte[])eas.getAlgorithmData(0);
498                     for (int is = 0; is < _byteArray.length; is++) {
499                         assertEquals(_byteArray[is], b[is]);
500                     }
501                 } else if (localName.equals("short")) {
502                     assertEquals("short", eas.getLocalName(0));
503                     assertEquals(EncodingAlgorithmIndexes.SHORT, eas.getAlgorithmIndex(0));
504                     assertEquals(null, eas.getAlgorithmURI(0));
505                     assertEquals(true, eas.getAlgorithmData(0) instanceof short[]);
506                     short[] i = (short[])eas.getAlgorithmData(0);
507                     for (int is = 0; is < _shortArray.length; is++) {
508                         assertEquals(_shortArray[is], i[is]);
509                     }
510                 } else if (localName.equals("int")) {
511                     assertEquals("int", eas.getLocalName(0));
512                     assertEquals(EncodingAlgorithmIndexes.INT, eas.getAlgorithmIndex(0));
513                     assertEquals(null, eas.getAlgorithmURI(0));
514                     assertEquals(true, eas.getAlgorithmData(0) instanceof int[]);
515                     int[] i = (int[])eas.getAlgorithmData(0);
516                     for (int is = 0; is < _intArray.length; is++) {
517                         assertEquals(_intArray[is], i[is]);
518                     }
519                 } else if (localName.equals("long")) {
520                     assertEquals("long", eas.getLocalName(0));
521                     assertEquals(EncodingAlgorithmIndexes.LONG, eas.getAlgorithmIndex(0));
522                     assertEquals(null, eas.getAlgorithmURI(0));
523                     assertEquals(true, eas.getAlgorithmData(0) instanceof long[]);
524                     long[] i = (long[])eas.getAlgorithmData(0);
525                     for (int is = 0; is < _longArray.length; is++) {
526                         assertEquals(_longArray[is], i[is]);
527                     }
528                 } else if (localName.equals("float")) {
529                     assertEquals("float", eas.getLocalName(0));
530                     assertEquals(EncodingAlgorithmIndexes.FLOAT, eas.getAlgorithmIndex(0));
531                     assertEquals(null, eas.getAlgorithmURI(0));
532                     assertEquals(true, eas.getAlgorithmData(0) instanceof float[]);
533                     float[] f = (float[])eas.getAlgorithmData(0);
534                     for (int is = 0; is < _floatArray.length; is++) {
535                         assertEquals(_floatArray[is], f[is], 0.0);
536                     }
537                 } else if (localName.equals("double")) {
538                     assertEquals("double", eas.getLocalName(0));
539                     assertEquals(EncodingAlgorithmIndexes.DOUBLE, eas.getAlgorithmIndex(0));
540                     assertEquals(null, eas.getAlgorithmURI(0));
541                     assertEquals(true, eas.getAlgorithmData(0) instanceof double[]);
542                     double[] f = (double[])eas.getAlgorithmData(0);
543                     for (int is = 0; is < _doubleArray.length; is++) {
544                         assertEquals(_doubleArray[is], f[is], 0.0);
545                     }
546                 } else if (localName.equals("uuid")) {
547                     assertEquals("uuid", eas.getLocalName(0));
548                     assertEquals(EncodingAlgorithmIndexes.UUID, eas.getAlgorithmIndex(0));
549                     assertEquals(null, eas.getAlgorithmURI(0));
550                     assertEquals(true, eas.getAlgorithmData(0) instanceof long[]);
551                     long[] i = (long[])eas.getAlgorithmData(0);
552                     for (int is = 0; is < _uuidArray.length; is++) {
553                         assertEquals(_uuidArray[is], i[is]);
554                     }
555                 }
556             } else {
557                 if (localName.equals("cdata")) {
558                     _charactersShouldBeAsCDATA = true;
559                 } else if (localName.equals("characters")) {
560                     _charactersShouldBeAsCDATA = false;
561                 } else {
562                     assertEquals("e", localName);
563                 }
564             }
565         }
566
567         public final void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
568         }
569
570         public final void characters(char[] c, int start, int length) throws SAXException JavaDoc {
571             String JavaDoc s = new String JavaDoc(c, start, length);
572             if (_charactersShouldBeAsCDATA) {
573                 assertEquals(true, _charactersAsCDATA);
574                 assertEquals(_cdata, s);
575             } else {
576                 assertEquals(false, _charactersAsCDATA);
577                 assertEquals(_characters, s);
578             }
579         }
580
581         public final void startCDATA() throws SAXException JavaDoc {
582             _charactersAsCDATA = true;
583         }
584
585         public final void endCDATA() throws SAXException JavaDoc {
586             _charactersAsCDATA = false;
587         }
588
589         // PrimitiveTypeContentHandler
590

591         public final void booleans(boolean[] b, int start, int length) throws SAXException JavaDoc {
592             assertEquals(_arraySize, length);
593             for (int i = 0; i < _booleanArray.length; i++) {
594                 assertEquals(_booleanArray[i], b[i + start]);
595             }
596         }
597         
598         public final void bytes(byte[] b, int start, int length) throws SAXException JavaDoc {
599             assertEquals(_arraySize, length);
600             for (int i = 0; i < _byteArray.length; i++) {
601                 assertEquals(_byteArray[i], b[i + start]);
602             }
603         }
604
605         public final void shorts(short[] i, int start, int length) throws SAXException JavaDoc {
606             assertEquals(_arraySize, length);
607
608             for (int is = 0; is < _shortArray.length; is++) {
609                 assertEquals(_shortArray[is], i[is + start]);
610             }
611         }
612
613         public final void ints(int[] i, int start, int length) throws SAXException JavaDoc {
614             assertEquals(_arraySize, length);
615
616             for (int is = 0; is < _intArray.length; is++) {
617                 assertEquals(_intArray[is], i[is + start]);
618             }
619         }
620
621         public final void longs(long[] i, int start, int length) throws SAXException JavaDoc {
622             assertEquals(_arraySize, length);
623
624             for (int is = 0; is < _longArray.length; is++) {
625                 assertEquals(_longArray[is], i[is + start]);
626             }
627         }
628
629         public final void floats(float[] f, int start, int length) throws SAXException JavaDoc {
630             assertEquals(_arraySize, length);
631
632             for (int i = 0; i < _floatArray.length; i++) {
633                 assertEquals(_floatArray[i], f[i + start], 0.0);
634             }
635         }
636
637         public final void doubles(double[] f, int start, int length) throws SAXException JavaDoc {
638             assertEquals(_arraySize, length);
639
640             for (int i = 0; i < _doubleArray.length; i++) {
641                 assertEquals(_doubleArray[i], f[i + start], 0.0);
642             }
643         }
644     }
645
646
647     public void testGenericAlgorithms() throws Exception JavaDoc {
648         createArrayValues(ARRAY_SIZE);
649
650         byte[] b = createGenericTestFastInfosetDocument();
651         InputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
652
653         SAXDocumentParser p = new SAXDocumentParser();
654
655         ParserVocabulary externalVocabulary = new ParserVocabulary();
656         externalVocabulary.encodingAlgorithm.add(APPLICATION_DEFINED_ALGORITHM_URI);
657
658         Map JavaDoc externalVocabularies = new HashMap JavaDoc();
659         externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, externalVocabulary);
660         p.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies);
661
662         GenericTestHandler h = new GenericTestHandler();
663
664         p.setContentHandler(h);
665         p.setLexicalHandler(h);
666         p.setEncodingAlgorithmContentHandler(h);
667
668         p.parse(bais);
669     }
670
671     protected byte[] createGenericTestFastInfosetDocument() throws Exception JavaDoc {
672         SAXDocumentSerializer s = new SAXDocumentSerializer();
673
674         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
675         s.setOutputStream(baos);
676
677         SerializerVocabulary externalVocabulary = new SerializerVocabulary();
678         externalVocabulary.encodingAlgorithm.add(APPLICATION_DEFINED_ALGORITHM_URI);
679
680         SerializerVocabulary initialVocabulary = new SerializerVocabulary();
681         initialVocabulary.setExternalVocabulary(
682                 new URI JavaDoc(EXTERNAL_VOCABULARY_URI_STRING),
683                 externalVocabulary, false);
684
685         s.setVocabulary(initialVocabulary);
686
687         _attributes.clear();
688
689
690         s.startDocument();
691
692         s.startElement("", "e", "e", _attributes);
693
694         // Application-defined algorithm 31
695
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "algorithm", "algorithm"),
696                 APPLICATION_DEFINED_ALGORITHM_URI, APPLICATION_DEFINED_ALGORITHM_ID, _byteArray);
697         s.startElement("", "algorithm", "algorithm", _attributes);
698         _attributes.clear();
699         s.octets(APPLICATION_DEFINED_ALGORITHM_URI, APPLICATION_DEFINED_ALGORITHM_ID, _byteArray, 0, _byteArray.length);
700         s.endElement("", "algorithm", "algorithm");
701
702
703         s.endElement("", "e", "e");
704
705         s.endDocument();
706
707         return baos.toByteArray();
708     }
709
710     public class GenericTestHandler extends FastInfosetDefaultHandler {
711
712         // ContentHandler
713

714         public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException JavaDoc {
715             if (atts.getLength() > 0) {
716                 EncodingAlgorithmAttributes eas = (EncodingAlgorithmAttributes)atts;
717
718                 assertEquals(1, atts.getLength());
719
720                 if (localName.equals("algorithm")) {
721                     assertEquals("algorithm", eas.getLocalName(0));
722                     assertEquals(APPLICATION_DEFINED_ALGORITHM_ID, eas.getAlgorithmIndex(0));
723                     assertEquals(APPLICATION_DEFINED_ALGORITHM_URI, eas.getAlgorithmURI(0));
724                     assertEquals(true, eas.getAlgorithmData(0) instanceof byte[]);
725                     byte[] b = (byte[])eas.getAlgorithmData(0);
726                     for (int is = 0; is < _byteArray.length; is++) {
727                         assertEquals(_byteArray[is], b[is]);
728                     }
729                 }
730             } else {
731                 assertEquals("e", localName);
732             }
733         }
734
735         public final void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
736         }
737
738         // EncodingAlgorithmContentHandler
739

740         public final void object(String JavaDoc URI, int algorithm, Object JavaDoc data) throws SAXException JavaDoc {
741             assertTrue(true);
742         }
743
744         public final void octets(String JavaDoc URI, int algorithm, byte[] b, int start, int length) throws SAXException JavaDoc {
745             assertEquals(APPLICATION_DEFINED_ALGORITHM_ID, algorithm);
746             assertEquals(APPLICATION_DEFINED_ALGORITHM_URI, URI);
747             assertEquals(ARRAY_SIZE, length);
748
749             for (int i = 0; i < ARRAY_SIZE; i++) {
750                 assertEquals(_byteArray[i], b[i + start]);
751             }
752         }
753     }
754
755     public void testRegisteredAlgorithms() throws Exception JavaDoc {
756         createArrayValues(ARRAY_SIZE);
757
758         byte[] b = createRegisteredTestFastInfosetDocument();
759         InputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(b);
760
761         SAXDocumentParser p = new SAXDocumentParser();
762
763         ParserVocabulary externalVocabulary = new ParserVocabulary();
764         externalVocabulary.encodingAlgorithm.add(APPLICATION_DEFINED_ALGORITHM_URI);
765
766         Map JavaDoc externalVocabularies = new HashMap JavaDoc();
767         externalVocabularies.put(EXTERNAL_VOCABULARY_URI_STRING, externalVocabulary);
768         p.setProperty(FastInfosetParser.EXTERNAL_VOCABULARIES_PROPERTY, externalVocabularies);
769
770         Map JavaDoc algorithms = new HashMap JavaDoc();
771         algorithms.put(APPLICATION_DEFINED_ALGORITHM_URI, new FloatEncodingAlgorithm());
772         p.setRegisteredEncodingAlgorithms(algorithms);
773
774         RegisteredTestHandler h = new RegisteredTestHandler();
775
776         p.setContentHandler(h);
777         p.setLexicalHandler(h);
778         p.setEncodingAlgorithmContentHandler(h);
779
780         p.parse(bais);
781     }
782
783     protected byte[] createRegisteredTestFastInfosetDocument() throws Exception JavaDoc {
784         SAXDocumentSerializer s = new SAXDocumentSerializer();
785
786         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
787         s.setOutputStream(baos);
788
789         SerializerVocabulary externalVocabulary = new SerializerVocabulary();
790         externalVocabulary.encodingAlgorithm.add(APPLICATION_DEFINED_ALGORITHM_URI);
791
792         SerializerVocabulary initialVocabulary = new SerializerVocabulary();
793         initialVocabulary.setExternalVocabulary(
794                 new URI JavaDoc(EXTERNAL_VOCABULARY_URI_STRING),
795                 externalVocabulary, false);
796
797         s.setVocabulary(initialVocabulary);
798
799         Map JavaDoc algorithms = new HashMap JavaDoc();
800         algorithms.put(APPLICATION_DEFINED_ALGORITHM_URI, new FloatEncodingAlgorithm());
801         s.setRegisteredEncodingAlgorithms(algorithms);
802
803         _attributes.clear();
804
805
806         s.startDocument();
807
808         s.startElement("", "e", "e", _attributes);
809
810         // Application-defined algorithm 31
811
_attributes.addAttributeWithAlgorithmData(new QualifiedName("", "", "algorithm", "algorithm"),
812                 APPLICATION_DEFINED_ALGORITHM_URI, APPLICATION_DEFINED_ALGORITHM_ID, _floatArray);
813         s.startElement("", "algorithm", "algorithm", _attributes);
814         _attributes.clear();
815         s.object(APPLICATION_DEFINED_ALGORITHM_URI, APPLICATION_DEFINED_ALGORITHM_ID, _floatArray);
816         s.endElement("", "algorithm", "algorithm");
817
818
819         s.endElement("", "e", "e");
820
821         s.endDocument();
822
823         return baos.toByteArray();
824     }
825
826     public class RegisteredTestHandler extends FastInfosetDefaultHandler {
827
828         // ContentHandler
829

830         public final void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes atts) throws SAXException JavaDoc {
831             if (atts.getLength() > 0) {
832                 EncodingAlgorithmAttributes eas = (EncodingAlgorithmAttributes)atts;
833
834                 assertEquals(1, atts.getLength());
835
836                 if (localName.equals("algorithm")) {
837                     assertEquals("algorithm", eas.getLocalName(0));
838                     assertEquals(APPLICATION_DEFINED_ALGORITHM_ID, eas.getAlgorithmIndex(0));
839                     assertEquals(APPLICATION_DEFINED_ALGORITHM_URI, eas.getAlgorithmURI(0));
840                     assertEquals(true, eas.getAlgorithmData(0) instanceof float[]);
841                     float[] b = (float[])eas.getAlgorithmData(0);
842                     for (int is = 0; is < ARRAY_SIZE; is++) {
843                         assertEquals(_floatArray[is], b[is], 0.0);
844                     }
845                 }
846             } else {
847                 assertEquals("e", localName);
848             }
849         }
850
851         public final void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
852         }
853
854         // EncodingAlgorithmContentHandler
855

856         public final void object(String JavaDoc URI, int algorithm, Object JavaDoc data) throws SAXException JavaDoc {
857             assertEquals(APPLICATION_DEFINED_ALGORITHM_ID, algorithm);
858             assertEquals(APPLICATION_DEFINED_ALGORITHM_URI, URI);
859             assertEquals(true, data instanceof float[]);
860             float[] b = (float[])data;
861             for (int is = 0; is < _floatArray.length; is++) {
862                 assertEquals(_floatArray[is], b[is], 0.0);
863             }
864         }
865
866         public final void octets(String JavaDoc URI, int algorithm, byte[] b, int start, int length) throws SAXException JavaDoc {
867             assertTrue(true);
868         }
869     }
870
871     protected void createArrayValues(int arraySize) {
872         _byteArray = new byte[arraySize];
873         _shortArray = new short[arraySize];
874         _intArray = new int[arraySize];
875         _longArray = new long[arraySize];
876         _floatArray = new float[arraySize];
877         _doubleArray = new double[arraySize];
878         _uuidArray = new long[arraySize * 2];
879         _booleanArray = new boolean[arraySize];
880
881         for (int i = 0; i < arraySize; i++) {
882             _byteArray[i] = (byte)i;
883             _shortArray[i] = (short)(i * Short.MAX_VALUE / arraySize);
884             _intArray[i] = i * (Integer.MAX_VALUE / arraySize);
885             _longArray[i] = i * (Long.MAX_VALUE / arraySize);
886             _floatArray[i] = (float)(i * Math.E);
887             _doubleArray[i] = i * Math.E;
888             _uuidArray[i] = i * (Long.MAX_VALUE / arraySize);
889             _uuidArray[i * 2] = i * (Long.MAX_VALUE / arraySize);
890             if (i % 2 == 0)
891                _booleanArray[i] = true;
892         }
893     }
894
895     public void testStAXBase64EncodingAlgorithm() throws Exception JavaDoc {
896         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
897         StAXDocumentSerializer ds = new StAXDocumentSerializer(baos);
898
899         byte[] data = new byte[256];
900         for (int i = 0; i < 256; i++) {
901             data[i] = (byte)i;
902         }
903
904         ds.writeStartDocument();
905             ds.writeStartElement("element");
906                 ds.writeOctets(data, 0, data.length);
907             ds.writeEndElement();
908         ds.writeEndDocument();
909         ds.close();
910
911         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
912         StAXDocumentParser dp = new StAXDocumentParser(bais);
913         while(dp.hasNext()) {
914             int e = dp.next();
915             if (e == XMLStreamReader.CHARACTERS) {
916                 byte[] b = dp.getTextAlgorithmBytes();
917                 assertEquals(EncodingAlgorithmIndexes.BASE64, dp.getTextAlgorithmIndex());
918                 assertTrue(b != null);
919                 assertEquals(data.length, dp.getTextAlgorithmLength());
920                 for (int i = 0; i < data.length; i++) {
921                     assertEquals(data[i], b[dp.getTextAlgorithmStart() + i]);
922                 }
923
924                 b = dp.getTextAlgorithmBytesClone();
925                 assertTrue(b != null);
926                 assertEquals(data.length, b.length);
927                 for (int i = 0; i < data.length; i++) {
928                     assertEquals(data[i], b[i]);
929                 }
930
931                 String JavaDoc c = dp.getText();
932                 assertEquals(_base64String.length(), c.length());
933                 assertEquals(_base64String, c);
934             }
935         }
936     }
937
938     public void testDOMBase64EncodingAlgorithm() throws Exception JavaDoc {
939         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
940         StAXDocumentSerializer ds = new StAXDocumentSerializer(baos);
941
942         byte[] data = new byte[256];
943         for (int i = 0; i < 256; i++) {
944             data[i] = (byte)i;
945         }
946
947         ds.writeStartDocument();
948             ds.writeStartElement("element");
949                 ds.writeOctets(data, 0, data.length);
950             ds.writeEndElement();
951         ds.writeEndDocument();
952         ds.close();
953
954
955         ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
956         DOMDocumentParser dp = new DOMDocumentParser();
957         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
958         dbf.setNamespaceAware(true);
959         DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
960         Document JavaDoc d = db.newDocument();
961         dp.parse(d, bais);
962
963         Text JavaDoc t = (Text JavaDoc)d.getFirstChild().getFirstChild();
964         String JavaDoc c = t.getNodeValue();
965         assertEquals(_base64String.length(), c.length());
966         assertEquals(_base64String, c);
967     }
968 }
969
Free Books   Free Magazines  
Popular Tags