1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.asn1.ber ;
18
19
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Collections;
23
24 import org.apache.commons.lang.ArrayUtils ;
25 import org.apache.asn1.codec.binary.BinaryCodec;
26 import org.apache.asn1.ber.Length;
27 import org.apache.asn1.ber.Tuple;
28
29 import junit.framework.TestCase ;
30
31
32 /***
33 * Tests Tuple class.
34 *
35 * @author <a href="mailto:dev@directory.apache.org">
36 * Apache Directory Project</a>
37 * @version $Rev: 157644 $
38 */
39 public class TupleTest extends TestCase
40 {
41 /*** precalculated left shift of 1 by 14 places */
42 private static final int BIT_13 = 1 << 14 ;
43 /*** precalculated left shift of 1 by 16 places */
44 private static final int BIT_15 = 1 << 16 ;
45 /*** precalculated left shift of 1 by 21 places */
46 private static final int BIT_20 = 1 << 21 ;
47 /*** precalculated left shift of 1 by 24 places */
48 private static final int BIT_23 = 1 << 24 ;
49 /*** precalculated left shift of 1 by 28 places */
50 private static final int BIT_27 = 1 << 28 ;
51
52 /*** for convenience in handling nulls */
53 private static final ByteBuffer EMPTY_BUFFER =
54 ByteBuffer.wrap( ArrayUtils.EMPTY_BYTE_ARRAY ) ;
55
56 public static void main(String[] args)
57 {
58 junit.textui.TestRunner.run(TupleTest.class);
59 }
60
61
62
63
64 protected void setUp() throws Exception
65 {
66 super.setUp();
67 }
68
69
70
71
72 protected void tearDown() throws Exception
73 {
74 super.tearDown();
75 }
76
77 /***
78 * Constructor for TupleTest.
79 * @param arg0
80 */
81 public TupleTest(String arg0)
82 {
83 super(arg0);
84 }
85
86
87
88
89 public void testTuple()
90 {
91 assertNotNull( new Tuple() ) ;
92 }
93
94
95
96
97 public void testTupleint()
98 {
99 Tuple t0 = new Tuple( 0, 0 ) ;
100 assertEquals( 0, t0.id ) ;
101 Tuple t1 = new Tuple( 1, 0 ) ;
102 assertEquals( 1, t1.id ) ;
103 assertFalse( t0.equals(t1) ) ;
104 }
105
106
107
108
109 public void testTupleintint()
110 {
111 Tuple t0 = new Tuple( 0, 0 ) ;
112 assertEquals( 0, t0.id ) ;
113 assertEquals( 0, t0.length ) ;
114 Tuple t1 = new Tuple( 0, 1 ) ;
115 assertEquals( 0, t1.id ) ;
116 assertEquals( 1, t1.length ) ;
117 assertFalse( t0.equals(t1) ) ;
118 }
119
120
121
122
123 public void testTupleintintTypeClass()
124 {
125 Tuple t0 = new Tuple( 0, 0, TypeClass.PRIVATE ) ;
126 assertEquals( 0, t0.id ) ;
127 assertEquals( 0, t0.length ) ;
128 assertEquals( TypeClass.PRIVATE, t0.getTypeClass() ) ;
129 Tuple t1 = new Tuple( 0, 1, null ) ;
130 assertEquals( 0, t1.id ) ;
131 assertEquals( 1, t1.length ) ;
132 assertFalse( t0.equals(t1) ) ;
133 assertEquals( TypeClass.APPLICATION, t1.getTypeClass() ) ;
134 }
135
136
137
138
139 public void testTupleintTypeClass()
140 {
141 Tuple t = new Tuple( 2, TypeClass.PRIVATE ) ;
142 assertEquals( 2, t.getId() ) ;
143 assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
144 assertEquals( false, t.isPrimitive() ) ;
145 assertEquals( Length.INDEFINITE, t.getLength() ) ;
146 assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
147
148 t = new Tuple( 2, (TypeClass) null ) ;
149 assertEquals( 2, t.getId() ) ;
150 assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
151 assertEquals( false, t.isPrimitive() ) ;
152 assertEquals( Length.INDEFINITE, t.getLength() ) ;
153 assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
154 }
155
156 public void testGetId()
157 {
158 Tuple t = new Tuple() ;
159 assertEquals( 0, t.getId() ) ;
160 t = new Tuple( 2, 0 ) ;
161 assertEquals( 2, t.getId() ) ;
162 t.id = 21 ;
163 assertEquals( 21, t.getId() ) ;
164 }
165
166
167 public void testSize()
168 {
169 Tuple t = new Tuple( 1, TypeClass.APPLICATION ) ;
170 assertEquals( 2, t.size() ) ;
171 t.id = 32 ;
172 assertEquals( 3, t.size() ) ;
173 t.id = 127 ;
174 assertEquals( 4, t.size() ) ;
175 t.id = 128 ;
176 assertEquals( 4, t.size() ) ;
177 t.id = 1 << 14 ;
178 assertEquals( 5, t.size() ) ;
179 t.id = 1 << 21 ;
180 assertEquals( 6, t.size() ) ;
181
182 t.length = 127 ;
183 assertEquals( 6+127, t.size() ) ;
184 t.length = 128 ;
185 assertEquals( 7+128, t.size() ) ;
186 t.length = 255 ;
187 assertEquals( 7+255, t.size() ) ;
188 t.length = 256 ;
189 assertEquals( 8+256, t.size() ) ;
190 }
191
192
193 public void testIsIndefinite()
194 {
195 Tuple t = new Tuple() ;
196 assertFalse( t.isIndefinite() ) ;
197 t.length = Length.INDEFINITE ;
198 assertTrue( t.isIndefinite() ) ;
199 }
200
201
202 public void testIsIndefiniteTerminator()
203 {
204 Tuple t = new Tuple() ;
205 assertFalse( t.isIndefiniteTerminator() ) ;
206 t.id = 0 ;
207 t.length = 0 ;
208 t.isPrimitive = true ;
209 t.typeClass = TypeClass.UNIVERSAL ;
210 assertTrue( t.isIndefiniteTerminator() ) ;
211 }
212
213
214 public void testIsPrimitive()
215 {
216 Tuple t = new Tuple() ;
217 assertTrue( t.isPrimitive() ) ;
218 t.isPrimitive = false ;
219 assertFalse( t.isPrimitive() ) ;
220 }
221
222 public void testGetLength()
223 {
224 Tuple t = new Tuple() ;
225 assertEquals( 0, t.getLength() ) ;
226 t = new Tuple( 1, 2 ) ;
227 assertEquals( 2, t.getLength() ) ;
228 t.length = 21 ;
229 assertEquals( 21, t.getLength() ) ;
230 }
231
232 public void testGetTypeClass()
233 {
234 Tuple t = new Tuple() ;
235 assertEquals( t.typeClass, TypeClass.APPLICATION ) ;
236 t = new Tuple( 0, 0 ) ;
237 assertEquals( TypeClass.APPLICATION, t.getTypeClass() ) ;
238 t.typeClass = TypeClass.PRIVATE ;
239 assertEquals( TypeClass.PRIVATE, t.getTypeClass() ) ;
240 }
241
242 public void testGetValue()
243 {
244 Tuple t = new Tuple() ;
245 assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
246 byte[] bites = {1, 2, 3, 45} ;
247 t.valueChunk = ByteBuffer.wrap( bites ) ;
248 assertEquals( ByteBuffer.wrap( bites ), t.getLastValueChunk() ) ;
249 t.clear() ;
250 assertEquals( EMPTY_BUFFER, t.getLastValueChunk() ) ;
251 }
252
253 public void testClear()
254 {
255 Tuple t = new Tuple() ;
256 t.id = 12 ;
257 assertEquals( 12, t.id ) ;
258 t.clear() ;
259 assertEquals( 0, t.id ) ;
260
261 t.length = 12 ;
262 assertEquals( 12, t.length ) ;
263 t.clear() ;
264 assertEquals( Length.UNDEFINED, t.length ) ;
265
266 t.index = 12 ;
267 assertEquals( 12, t.index ) ;
268 t.clear() ;
269 assertEquals( 0, t.index ) ;
270
271 t.isPrimitive = false ;
272 assertEquals( false, t.isPrimitive ) ;
273 t.clear() ;
274 assertEquals( true, t.isPrimitive ) ;
275
276 t.typeClass = TypeClass.CONTEXT_SPECIFIC ;
277 assertEquals( TypeClass.CONTEXT_SPECIFIC, t.typeClass ) ;
278 t.clear() ;
279 assertEquals( TypeClass.APPLICATION, t.typeClass ) ;
280
281 t.valueChunk = ByteBuffer.allocate( 3 ) ;
282 assertNotNull( t.valueChunk ) ;
283 t.clear() ;
284 assertEquals( EMPTY_BUFFER, t.valueChunk ) ;
285
286 t.valueIndex = 12 ;
287 assertEquals( 12, t.valueIndex ) ;
288 t.clear() ;
289 assertEquals( Length.UNDEFINED, t.valueIndex ) ;
290
291 }
292
293
294
295
296 public void testEqualsObject()
297 {
298 Tuple tnull0 = new Tuple() ;
299 tnull0.valueChunk = null ;
300 Tuple tnull1 = new Tuple() ;
301 tnull1.valueChunk = null ;
302 tnull0.equals( tnull1 ) ;
303
304 tnull1.equals( tnull1 ) ;
305 tnull0.equals( tnull0 ) ;
306
307 Tuple t0 = new Tuple() ;
308 Tuple t1 = ( Tuple ) t0.clone() ;
309
310 assertTrue( t0.equals( t1 ) ) ;
311 t0.id = 23 ;
312 assertFalse( t0.equals( t1 ) ) ;
313 t1 = ( Tuple ) t0.clone() ;
314 assertTrue( t0.equals( t1 ) ) ;
315
316
317 t0.index = 23 ;
318 t1.index = 33 ;
319 assertTrue( t0.equals( t1 ) ) ;
320 t1 = ( Tuple ) t0.clone() ;
321 assertTrue( t0.equals( t1 ) ) ;
322
323 t0.isPrimitive = false ;
324 t1.isPrimitive = true ;
325 assertFalse( t0.equals( t1 ) ) ;
326 t1 = ( Tuple ) t0.clone() ;
327 assertTrue( t0.equals( t1 ) ) ;
328
329 t0.length = 23 ;
330 assertFalse( t0.equals( t1 ) ) ;
331 t1 = ( Tuple ) t0.clone() ;
332 assertTrue( t0.equals( t1 ) ) ;
333
334 t0.typeClass = TypeClass.PRIVATE ;
335 t1.typeClass = TypeClass.UNIVERSAL ;
336 assertFalse( t0.equals( t1 ) ) ;
337 t1 = ( Tuple ) t0.clone() ;
338 assertTrue( t0.equals( t1 ) ) ;
339
340
341 t0.valueIndex = 23 ;
342 t1.valueIndex = 3 ;
343 assertTrue( t0.equals( t1 ) ) ;
344 t1 = ( Tuple ) t0.clone() ;
345 assertTrue( t0.equals( t1 ) ) ;
346
347 t0.valueChunk = ByteBuffer.allocate( 4 ) ;
348 t1.valueChunk = null ;
349
350
351 assertTrue( t0.equals( t1 ) ) ;
352
353 t1 = ( Tuple ) t0.clone() ;
354 assertTrue( t0.equals( t1 ) ) ;
355
356 assertFalse( t0.equals( new Object() )) ;
357 }
358
359
360
361
362 public void testClone()
363 {
364 Tuple t = new Tuple() ;
365 assertTrue( t.equals( t.clone() ) ) ;
366 }
367
368 public void testToEncodedBufferConstructed()
369 {
370 Tuple t = null ;
371 ByteBuffer encoded ;
372
373 t = new Tuple( 0, 0 ) ;
374 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
375 assertEquals(
376 "00000000" +
377 "01100000"
378 , toAsciiString( encoded ) ) ;
379
380 t = new Tuple( 2, 0 ) ;
381 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
382 assertEquals(
383 "00000000" +
384 "01100010"
385 , toAsciiString( encoded ) ) ;
386
387 t = new Tuple( 30, 0 ) ;
388 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
389 assertEquals(
390 "00000000" +
391 "01111110"
392 , toAsciiString( encoded ) ) ;
393
394 t = new Tuple( 31, 0 ) ;
395 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
396 assertEquals(
397 "00000000" +
398 "00011111" +
399 "01111111"
400 , toAsciiString( encoded ) ) ;
401
402 t = new Tuple( 128, 0 ) ;
403 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
404 assertEquals(
405 "00000000" +
406 "00000000" +
407 "10000001" +
408 "01111111"
409 , toAsciiString( encoded ) ) ;
410
411 t = new Tuple( 128, 127 ) ;
412 ArrayList list = new ArrayList() ;
413 list.add( ByteBuffer.allocate( 127 ) ) ;
414 encoded = t.toEncodedBuffer( list ) ;
415 assertEquals(
416 "01111111" +
417 "00000000" +
418 "10000001" +
419 "01111111"
420 , toAsciiString( encoded ) ) ;
421
422 t = new Tuple( 128, 128 ) ;
423 list.clear() ;
424 list.add( ByteBuffer.allocate( 128 ) ) ;
425 encoded = t.toEncodedBuffer( list ) ;
426 assertEquals(
427 "10000000" +
428 "10000001" +
429 "00000000" +
430 "10000001" +
431 "01111111"
432 , toAsciiString( encoded ) ) ;
433
434 t = new Tuple( 128, 255 ) ;
435 list.clear() ;
436 list.add( ByteBuffer.allocate( 255 ) ) ;
437 encoded = t.toEncodedBuffer( list ) ;
438 assertEquals(
439 "11111111" +
440 "10000001" +
441 "00000000" +
442 "10000001" +
443 "01111111"
444 , toAsciiString( encoded ) ) ;
445
446 t = new Tuple( 128, 256 ) ;
447 list.clear() ;
448 list.add( ByteBuffer.allocate( 256 ) ) ;
449 encoded = t.toEncodedBuffer( list ) ;
450 assertEquals(
451 "00000000" +
452 "00000001" +
453 "10000010" +
454 "00000000" +
455 "10000001" +
456 "01111111"
457 , toAsciiString( encoded ) ) ;
458 }
459
460 public void testToEncodedBufferPrimitive()
461 {
462 Tuple t = null ;
463 ByteBuffer encoded ;
464 byte[] data ;
465
466 t = new Tuple( 0, 0, true, TypeClass.APPLICATION ) ;
467 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
468 assertEquals(
469 "00000000" +
470 "01000000"
471 , toAsciiString( encoded ) ) ;
472
473 t = new Tuple( 2, 0, true, TypeClass.APPLICATION ) ;
474 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
475 assertEquals(
476 "00000000" +
477 "01000010"
478 , toAsciiString( encoded ) ) ;
479
480 t = new Tuple( 30, 0, true, TypeClass.APPLICATION ) ;
481 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
482 assertEquals(
483 "00000000" +
484 "01011110"
485 , toAsciiString( encoded ) ) ;
486
487 t = new Tuple( 31, 0, true, TypeClass.APPLICATION ) ;
488 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
489 assertEquals(
490 "00000000" +
491 "00011111" +
492 "01011111"
493 , toAsciiString( encoded ) ) ;
494
495 t = new Tuple( 128, 0, true, TypeClass.APPLICATION ) ;
496 encoded = t.toEncodedBuffer( Collections.EMPTY_LIST ) ;
497 assertEquals(
498 "00000000" +
499 "00000000" +
500 "10000001" +
501 "01011111"
502 , toAsciiString( encoded ) ) ;
503
504 data = new byte[1] ;
505 t = new Tuple( 128, 1, true, TypeClass.APPLICATION ) ;
506 ArrayList list = new ArrayList() ;
507 list.add( ByteBuffer.wrap( data ) ) ;
508 encoded = t.toEncodedBuffer( list ) ;
509 assertEquals(
510 "00000000" +
511 "00000001" +
512 "00000000" +
513 "10000001" +
514 "01011111"
515 , toAsciiString( encoded ) ) ;
516
517 data = new byte[127] ;
518 t = new Tuple( 128, 127, true, TypeClass.APPLICATION ) ;
519 list.clear() ;
520 list.add( ByteBuffer.wrap( data ) ) ;
521 encoded = t.toEncodedBuffer( list ) ;
522 assertEquals( BinaryCodec.toAsciiString( data ) +
523 "01111111" +
524 "00000000" +
525 "10000001" +
526 "01011111"
527 , toAsciiString( encoded ) ) ;
528
529 data = new byte[128] ;
530 t = new Tuple( 128, 128, true, TypeClass.APPLICATION ) ;
531 list.clear() ;
532 list.add( ByteBuffer.wrap( data ) ) ;
533 encoded = t.toEncodedBuffer( list ) ;
534 assertEquals( BinaryCodec.toAsciiString( data ) +
535 "10000000" +
536 "10000001" +
537 "00000000" +
538 "10000001" +
539 "01011111"
540 , toAsciiString( encoded ) ) ;
541
542 data = new byte[255] ;
543 t = new Tuple( 128, 255, true, TypeClass.APPLICATION ) ;
544 list.clear() ;
545 list.add( ByteBuffer.wrap( data ) ) ;
546 encoded = t.toEncodedBuffer( list ) ;
547 assertEquals( BinaryCodec.toAsciiString( data ) +
548 "11111111" +
549 "10000001" +
550 "00000000" +
551 "10000001" +
552 "01011111"
553 , toAsciiString( encoded ) ) ;
554
555 data = new byte[256] ;
556 t = new Tuple( 128, 256, true, TypeClass.APPLICATION ) ;
557 list.clear() ;
558 list.add( ByteBuffer.wrap( data ) ) ;
559 encoded = t.toEncodedBuffer( list ) ;
560 assertEquals( BinaryCodec.toAsciiString( data ) +
561 "00000000" +
562 "00000001" +
563 "10000010" +
564 "00000000" +
565 "10000001" +
566 "01011111"
567 , toAsciiString( encoded ) ) ;
568 }
569
570
571 public String toAsciiString( ByteBuffer buf )
572 {
573 return BinaryCodec.toAsciiString( buf.array() ) ;
574 }
575
576
577 public void testSetTagBufferint()
578 {
579 ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
580 Tuple t = new Tuple( 0, 0 ) ;
581 t.setTag( bites, 1 ) ;
582 String binary = toAsciiString( bites ) ;
583 assertEquals( "01100000", binary ) ;
584
585 bites = ByteBuffer.allocate( 1 ) ;
586 t = new Tuple( 30, 0 ) ;
587 t.setTag( bites, 1 ) ;
588 binary = toAsciiString( bites ) ;
589 assertEquals( "01111110", binary ) ;
590
591 bites = ByteBuffer.allocate( 1 ) ;
592 t = new Tuple( 30, 0 ) ;
593 t.isPrimitive = true ;
594 t.setTag( bites, 1 ) ;
595 binary = toAsciiString( bites ) ;
596 assertEquals( "01011110", binary ) ;
597
598 bites = ByteBuffer.allocate( 2 ) ;
599 t = new Tuple( 31, 0 ) ;
600 t.setTag( bites, 2 ) ;
601 binary = toAsciiString( bites ) ;
602 assertEquals( "00011111" + "01111111", binary ) ;
603
604 bites = ByteBuffer.allocate( 2 ) ;
605 t = new Tuple( 127, 0 ) ;
606 t.setTag( bites, 2 ) ;
607 binary = toAsciiString( bites ) ;
608 assertEquals( "01111111" + "01111111", binary ) ;
609
610 bites = ByteBuffer.allocate( 3 ) ;
611 t = new Tuple( 128, 0 ) ;
612 t.setTag( bites, 3 ) ;
613 binary = toAsciiString( bites ) ;
614 assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
615
616 bites = ByteBuffer.allocate( 3 ) ;
617 t = new Tuple( BIT_13 - 1, 0 ) ;
618 t.setTag( bites, 3 ) ;
619 binary = toAsciiString( bites ) ;
620 assertEquals( "01111111" +
621 "11111111" +
622 "01111111", binary ) ;
623
624 bites = ByteBuffer.allocate( 4 ) ;
625 t = new Tuple( BIT_13, 0 ) ;
626 t.setTag( bites, 4 ) ;
627 binary = toAsciiString( bites ) ;
628 assertEquals( "00000000" +
629 "10000000" +
630 "10000001" +
631 "01111111", binary ) ;
632
633 bites = ByteBuffer.allocate( 4 ) ;
634 t = new Tuple( BIT_13 + 1, 0 ) ;
635 t.setTag( bites, 4 ) ;
636 binary = toAsciiString( bites ) ;
637 assertEquals( "00000001" +
638 "10000000" +
639 "10000001" +
640 "01111111", binary ) ;
641
642 bites = ByteBuffer.allocate( 4 ) ;
643 t = new Tuple( BIT_20 - 1, 0 ) ;
644 t.setTag( bites, 4 ) ;
645 binary = toAsciiString( bites ) ;
646 assertEquals( "01111111" +
647 "11111111" +
648 "11111111" +
649 "01111111", binary ) ;
650
651 bites = ByteBuffer.allocate( 5 ) ;
652 t = new Tuple( BIT_20, 0 ) ;
653 t.setTag( bites, 5 ) ;
654 binary = toAsciiString( bites ) ;
655 assertEquals( "00000000" +
656 "10000000" +
657 "10000000" +
658 "10000001" +
659 "01111111", binary ) ;
660
661 bites = ByteBuffer.allocate( 5 ) ;
662 t = new Tuple( BIT_20 + 1, 0 ) ;
663 t.setTag( bites, 5 ) ;
664 binary = toAsciiString( bites ) ;
665 assertEquals( "00000001" +
666 "10000000" +
667 "10000000" +
668 "10000001" +
669 "01111111", binary ) ;
670
671 bites = ByteBuffer.allocate( 5 ) ;
672 t = new Tuple( BIT_27 - 1, 0 ) ;
673 t.setTag( bites, 5 ) ;
674 binary = toAsciiString( bites ) ;
675 assertEquals( "01111111" +
676 "11111111" +
677 "11111111" +
678 "11111111" +
679 "01111111", binary ) ;
680
681 bites = ByteBuffer.allocate( 6 ) ;
682 t = new Tuple( BIT_27, 0 ) ;
683
684 try
685 {
686 t.setTag( bites, 6 ) ;
687 fail( "should never reach this point due to thrown exception" ) ;
688 }
689 catch( IllegalArgumentException e )
690 {
691 assertNotNull( e ) ;
692 }
693 }
694
695 public void testSetTagbyteArrayint()
696 {
697 ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
698 Tuple t = new Tuple( 0, 0 ) ;
699 t.setTag( bites, 1 ) ;
700 String binary = toAsciiString( bites ) ;
701 assertEquals( "01100000", binary ) ;
702
703 bites = ByteBuffer.allocate( 1 ) ;
704 t = new Tuple( 30, 0 ) ;
705 t.setTag( bites, 1 ) ;
706 binary = toAsciiString( bites ) ;
707 assertEquals( "01111110", binary ) ;
708
709 bites = ByteBuffer.allocate( 1 ) ;
710 t = new Tuple( 30, 0 ) ;
711 t.isPrimitive = true ;
712 t.setTag( bites, 1 ) ;
713 binary = toAsciiString( bites ) ;
714 assertEquals( "01011110", binary ) ;
715
716 bites = ByteBuffer.allocate( 2 ) ;
717 t = new Tuple( 31, 0 ) ;
718 t.setTag( bites, 2 ) ;
719 binary = toAsciiString( bites ) ;
720 assertEquals( "00011111" + "01111111", binary ) ;
721
722 bites = ByteBuffer.allocate( 2 ) ;
723 t = new Tuple( 127, 0 ) ;
724 t.setTag( bites, 2 ) ;
725 binary = toAsciiString( bites ) ;
726 assertEquals( "01111111" + "01111111", binary ) ;
727
728 bites = ByteBuffer.allocate( 3 ) ;
729 t = new Tuple( 128, 0 ) ;
730 t.setTag( bites, 3 ) ;
731 binary = toAsciiString( bites ) ;
732 assertEquals( "00000000" + "10000001" + "01111111", binary ) ;
733
734 bites = ByteBuffer.allocate( 3 ) ;
735 t = new Tuple( BIT_13 - 1, 0 ) ;
736 t.setTag( bites, 3 ) ;
737 binary = toAsciiString( bites ) ;
738 assertEquals( "01111111" +
739 "11111111" +
740 "01111111", binary ) ;
741
742 bites = ByteBuffer.allocate( 4 ) ;
743 t = new Tuple( BIT_13, 0 ) ;
744 t.setTag( bites, 4 ) ;
745 binary = toAsciiString( bites ) ;
746 assertEquals( "00000000" +
747 "10000000" +
748 "10000001" +
749 "01111111", binary ) ;
750
751 bites = ByteBuffer.allocate( 4 ) ;
752 t = new Tuple( BIT_13 + 1, 0 ) ;
753 t.setTag( bites, 4 ) ;
754 binary = toAsciiString( bites ) ;
755 assertEquals( "00000001" +
756 "10000000" +
757 "10000001" +
758 "01111111", binary ) ;
759
760 bites = ByteBuffer.allocate( 4 ) ;
761 t = new Tuple( BIT_20 - 1, 0 ) ;
762 t.setTag( bites, 4 ) ;
763 binary = toAsciiString( bites ) ;
764 assertEquals( "01111111" +
765 "11111111" +
766 "11111111" +
767 "01111111", binary ) ;
768
769 bites = ByteBuffer.allocate( 5 ) ;
770 t = new Tuple( BIT_20, 0 ) ;
771 t.setTag( bites, 5 ) ;
772 binary = toAsciiString( bites ) ;
773 assertEquals( "00000000" +
774 "10000000" +
775 "10000000" +
776 "10000001" +
777 "01111111", binary ) ;
778
779 bites = ByteBuffer.allocate( 5 ) ;
780 t = new Tuple( BIT_20 + 1, 0 ) ;
781 t.setTag( bites, 5 ) ;
782 binary = toAsciiString( bites ) ;
783 assertEquals( "00000001" +
784 "10000000" +
785 "10000000" +
786 "10000001" +
787 "01111111", binary ) ;
788
789 bites = ByteBuffer.allocate( 5 ) ;
790 t = new Tuple( BIT_27 - 1, 0 ) ;
791 t.setTag( bites, 5 ) ;
792 binary = toAsciiString( bites ) ;
793 assertEquals( "01111111" +
794 "11111111" +
795 "11111111" +
796 "11111111" +
797 "01111111", binary ) ;
798
799 bites = ByteBuffer.allocate( 6 ) ;
800 t = new Tuple( BIT_27, 0 ) ;
801
802 try
803 {
804 t.setTag( bites, 6 ) ;
805 fail( "should never reach this point due to thrown exception" ) ;
806 }
807 catch( IllegalArgumentException e )
808 {
809 assertNotNull( e ) ;
810 }
811 }
812
813
814 String toAsciiString( int raw )
815 {
816 byte[] intBytes = new byte[4] ;
817 intBytes[0] = (byte) ( (int) 0x000000ff & raw ) ;
818 intBytes[1] = (byte) ( (int) ( 0x0000ff00 & raw ) >> 8 ) ;
819 intBytes[2] = (byte) ( (int) ( 0x00ff0000 & raw ) >> 16 ) ;
820 intBytes[3] = (byte) ( (int) ( 0xff000000 & raw ) >> 24 ) ;
821
822 return BinaryCodec.toAsciiString( intBytes ) ;
823 }
824
825
826 public void testSetLengthBuffer()
827 {
828 ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
829 Tuple t = new Tuple( 0, 0 ) ;
830 t.setLength( bites, 1 ) ;
831 String binary = toAsciiString( bites ) ;
832 assertEquals( "00000000", binary ) ;
833
834 bites = ByteBuffer.allocate( 1 ) ;
835 t = new Tuple( 30, 15 ) ;
836 t.setLength( bites, 1 ) ;
837 binary = toAsciiString( bites ) ;
838 assertEquals( "00001111", binary ) ;
839
840 bites = ByteBuffer.allocate( 1 ) ;
841 t = new Tuple( 30, 127 ) ;
842 t.setLength( bites, 1 ) ;
843 binary = toAsciiString( bites ) ;
844 assertEquals( "01111111", binary ) ;
845
846 bites = ByteBuffer.allocate( 2 ) ;
847 t = new Tuple( 30, 128 ) ;
848 t.setLength( bites, 2 ) ;
849 binary = toAsciiString( bites ) ;
850 assertEquals( "10000000" + "10000001", binary ) ;
851
852 bites = ByteBuffer.allocate( 2 ) ;
853 t = new Tuple( 30, 255 ) ;
854 t.setLength( bites, 2 ) ;
855 binary = toAsciiString( bites ) ;
856 assertEquals( "11111111" + "10000001", binary ) ;
857
858 bites = ByteBuffer.allocate( 3 ) ;
859 t = new Tuple( 30, 256 ) ;
860 t.setLength( bites, 3 ) ;
861 binary = toAsciiString( bites ) ;
862 assertEquals( "00000000" + "00000001" + "10000010", binary ) ;
863
864 bites = ByteBuffer.allocate( 3 ) ;
865 t = new Tuple( 30, BIT_15 - 1 ) ;
866 t.setLength( bites, 3 ) ;
867 binary = toAsciiString( bites ) ;
868 assertEquals( "11111111" +
869 "11111111" + "10000010", binary ) ;
870
871 bites = ByteBuffer.allocate( 4 ) ;
872 t = new Tuple( 30, BIT_15 ) ;
873 t.setLength( bites, 4 ) ;
874 binary = toAsciiString( bites ) ;
875 assertEquals( "00000000" + "00000000" + "00000001" + "10000011", binary ) ;
876
877 bites = ByteBuffer.allocate( 4 ) ;
878 t = new Tuple( 30, BIT_15 + 1 ) ;
879 t.setLength( bites, 4 ) ;
880 binary = toAsciiString( bites ) ;
881 assertEquals( "00000001" +
882 "00000000" +
883 "00000001" + "10000011", binary ) ;
884
885 bites = ByteBuffer.allocate( 4 ) ;
886 t = new Tuple( 30, BIT_23 - 1 ) ;
887 t.setLength( bites, 4 ) ;
888 binary = toAsciiString( bites ) ;
889 assertEquals( "11111111" +
890 "11111111" +
891 "11111111" + "10000011", binary ) ;
892
893 bites = ByteBuffer.allocate( 5 ) ;
894 t = new Tuple( 30, BIT_23 ) ;
895 t.setLength( bites, 5 ) ;
896 binary = toAsciiString( bites ) ;
897 assertEquals( "00000000" +
898 "00000000" +
899 "00000000" +
900 "00000001" +
901 "10000100", binary ) ;
902
903 bites = ByteBuffer.allocate( 5 ) ;
904 t = new Tuple( 30, BIT_23 + 1 ) ;
905 t.setLength( bites, 5 ) ;
906 binary = toAsciiString( bites ) ;
907 assertEquals( "00000001" +
908 "00000000" +
909 "00000000" +
910 "00000001" + "10000100", binary ) ;
911
912 bites = ByteBuffer.allocate( 5 ) ;
913 t = new Tuple( 30, Integer.MAX_VALUE ) ;
914 t.setLength( bites, 5 ) ;
915 binary = toAsciiString( bites ) ;
916 assertEquals( "11111111" +
917 "11111111" +
918 "11111111" +
919 "01111111" + "10000100", binary ) ;
920
921
922 bites = ByteBuffer.allocate( 6 ) ;
923 t = new Tuple( 30, Integer.MAX_VALUE + 1 ) ;
924
925 try
926 {
927 t.setLength( bites, 6 ) ;
928 fail( "should never get here due to thrown exception" ) ;
929 }
930 catch( IllegalArgumentException e )
931 {
932 assertNotNull( e ) ;
933 }
934 }
935
936 public void testSetLengthbyteArrayintint()
937 {
938 ByteBuffer bites = ByteBuffer.allocate( 1 ) ;
939 Tuple t = new Tuple( 0, 0 ) ;
940 t.setLength( bites, 1 ) ;
941 String binary = toAsciiString( bites ) ;
942 assertEquals( "00000000", binary ) ;
943
944 bites = ByteBuffer.allocate( 1 ) ;
945 t = new Tuple( 30, 15 ) ;
946 t.setLength( bites, 1 ) ;
947 binary = toAsciiString( bites ) ;
948 assertEquals( "00001111", binary ) ;
949
950 bites = ByteBuffer.allocate( 1 ) ;
951 t = new Tuple( 30, 127 ) ;
952 t.setLength( bites, 1 ) ;
953 binary = toAsciiString( bites ) ;
954 assertEquals( "01111111", binary ) ;
955
956 bites = ByteBuffer.allocate( 2 ) ;
957 t = new Tuple( 30, 128 ) ;
958 t.setLength( bites, 2 ) ;
959 binary = toAsciiString( bites ) ;
960 assertEquals( "10000000" + "10000001", binary ) ;
961
962 bites = ByteBuffer.allocate( 2 ) ;
963 t = new Tuple( 30, 255 ) ;
964 t.setLength( bites, 2 ) ;
965 binary = toAsciiString( bites ) ;
966 assertEquals( "11111111" + "10000001", binary ) ;
967
968 bites = ByteBuffer.allocate( 3 ) ;
969 t = new Tuple( 30, 256 ) ;
970 t.setLength( bites, 3 ) ;
971 binary = toAsciiString( bites ) ;
972 assertEquals( "00000000" +
973 "00000001" + "10000010", binary ) ;
974
975 bites = ByteBuffer.allocate( 3 ) ;
976 t = new Tuple( 30, BIT_15 - 1 ) ;
977 t.setLength( bites, 3 ) ;
978 binary = toAsciiString( bites ) ;
979 assertEquals( "11111111" + "11111111" + "10000010", binary ) ;
980
981 bites = ByteBuffer.allocate( 4 ) ;
982 t = new Tuple( 30, BIT_15 ) ;
983 t.setLength( bites, 4 ) ;
984 binary = toAsciiString( bites ) ;
985 assertEquals( "00000000" + "00000000" +
986 "00000001" + "10000011", binary ) ;
987
988 bites = ByteBuffer.allocate( 4 ) ;
989 t = new Tuple( 30, BIT_15 + 1 ) ;
990 t.setLength( bites, 4 ) ;
991 binary = toAsciiString( bites ) ;
992 assertEquals( "00000001" + "00000000" +
993 "00000001" + "10000011", binary ) ;
994
995 bites = ByteBuffer.allocate( 4 ) ;
996 t = new Tuple( 30, BIT_23 - 1 ) ;
997 t.setLength( bites, 4 ) ;
998 binary = toAsciiString( bites ) ;
999 assertEquals( "11111111" + "11111111" +
1000 "11111111" + "10000011", binary ) ;
1001
1002 bites = ByteBuffer.allocate( 5 ) ;
1003 t = new Tuple( 30, BIT_23 ) ;
1004 t.setLength( bites, 5 ) ;
1005 binary = toAsciiString( bites ) ;
1006 assertEquals( "00000000" +
1007 "00000000" +
1008 "00000000" +
1009 "00000001" + "10000100", binary ) ;
1010
1011 bites = ByteBuffer.allocate( 5 ) ;
1012 t = new Tuple( 30, BIT_23 + 1 ) ;
1013 t.setLength( bites, 5 ) ;
1014 binary = toAsciiString( bites ) ;
1015 assertEquals( "00000001" +
1016 "00000000" +
1017 "00000000" +
1018 "00000001" + "10000100", binary ) ;
1019
1020 bites = ByteBuffer.allocate( 5 ) ;
1021 t = new Tuple( 30, Integer.MAX_VALUE ) ;
1022 t.setLength( bites, 5 ) ;
1023 binary = toAsciiString( bites ) ;
1024 assertEquals( "11111111" +
1025 "11111111" +
1026 "11111111" +
1027 "01111111" + "10000100", binary ) ;
1028
1029
1030 bites = ByteBuffer.allocate( 6 ) ;
1031 t = new Tuple( 30, Integer.MAX_VALUE + 1 ) ;
1032
1033 try
1034 {
1035 t.setLength( bites, 6 ) ;
1036 fail( "should never get here due to thrown exception" ) ;
1037 }
1038 catch( IllegalArgumentException e )
1039 {
1040 assertNotNull( e ) ;
1041 }
1042 }
1043
1044 public void testGetTagLength()
1045 {
1046 Tuple t = new Tuple() ;
1047 assertEquals( 1, t.getTagLength() ) ;
1048 t.id = 30 ;
1049 assertEquals( 1, t.getTagLength() ) ;
1050 t.id = 31 ;
1051 assertEquals( 2, t.getTagLength() ) ;
1052 t.id = 100 ;
1053 assertEquals( 3, t.getTagLength() ) ;
1054 t.id = 127 ;
1055 assertEquals( 3, t.getTagLength() ) ;
1056 t.id = 128 ;
1057 assertEquals( 3, t.getTagLength() ) ;
1058 t.id = 129 ;
1059 assertEquals( 3, t.getTagLength() ) ;
1060
1061 t.id = BIT_13 - 1 ;
1062 assertEquals( 3, t.getTagLength() ) ;
1063 t.id = BIT_13 ;
1064 assertEquals( 4, t.getTagLength() ) ;
1065 t.id = BIT_13 + 100 ;
1066 assertEquals( 4, t.getTagLength() ) ;
1067
1068 t.id = BIT_20 - 1 ;
1069 assertEquals( 4, t.getTagLength() ) ;
1070 t.id = BIT_20 ;
1071 assertEquals( 5, t.getTagLength() ) ;
1072 t.id = BIT_20 + 100 ;
1073 assertEquals( 5, t.getTagLength() ) ;
1074
1075 t.id = BIT_27 - 1 ;
1076 assertEquals( 5, t.getTagLength() ) ;
1077
1078 t.id = BIT_27 ;
1079
1080 try
1081 {
1082 assertEquals( 6, t.getTagLength() ) ;
1083 fail( "should throw an exception before getting here" ) ;
1084 }
1085 catch( IllegalArgumentException e )
1086 {
1087 assertNotNull( e ) ;
1088 }
1089 }
1090
1091 public void testGetLengthLength()
1092 {
1093 Tuple t = new Tuple() ;
1094 assertEquals( 1, t.getLengthLength() ) ;
1095 t.length = 127 ;
1096 assertEquals( 1, t.getLengthLength() ) ;
1097 t.length = 128 ;
1098 assertEquals( 2, t.getLengthLength() ) ;
1099 t.length = 255 ;
1100 assertEquals( 2, t.getLengthLength() ) ;
1101 t.length = 256 ;
1102 assertEquals( 3, t.getLengthLength() ) ;
1103
1104 t.length = BIT_15 - 1 ;
1105 assertEquals( 3, t.getLengthLength() ) ;
1106 t.length = BIT_15 ;
1107 assertEquals( 4, t.getLengthLength() ) ;
1108 t.length = BIT_15 + 100 ;
1109 assertEquals( 4, t.getLengthLength() ) ;
1110
1111 t.length = BIT_23 - 1 ;
1112 assertEquals( 4, t.getLengthLength() ) ;
1113 t.length = BIT_23 ;
1114 assertEquals( 5, t.getLengthLength() ) ;
1115 t.length = BIT_23 + 100 ;
1116 assertEquals( 5, t.getLengthLength() ) ;
1117
1118 t.length = Integer.MAX_VALUE ;
1119 assertEquals( 5, t.getLengthLength() ) ;
1120
1121
1122 t.length = Integer.MAX_VALUE + 1 ;
1123 try
1124 {
1125 assertEquals( 6, t.getLengthLength() ) ;
1126 fail( "should throw an exception before getting here" ) ;
1127 }
1128 catch( IllegalArgumentException e )
1129 {
1130 assertNotNull( e ) ;
1131 }
1132
1133 }
1134 }