OSDN Git Service

modify DecodedContent
[jindolf/JinParser.git] / src / test / java / jp / osdn / jindolf / parser / content / DecodedContentTest.java
1 /*
2  * License : The MIT License
3  * Copyright(c) 2009 olyutorskii
4  */
5
6 package jp.osdn.jindolf.parser.content;
7
8 import java.util.List;
9 import org.junit.After;
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14
15 import static org.junit.Assert.*;
16
17 /**
18  */
19 public class DecodedContentTest {
20
21     public DecodedContentTest() {
22     }
23
24     @BeforeClass
25     public static void setUpClass() throws Exception{
26     }
27
28     @AfterClass
29     public static void tearDownClass() throws Exception{
30     }
31
32     @Before
33     public void setUp() {
34     }
35
36     @After
37     public void tearDown() {
38     }
39
40     /**
41      * Test of Constructor, of class DecodedContent.
42      */
43     @Test
44     public void testConstructor(){
45         System.out.println("Constructor");
46
47         DecodedContent content;
48
49         content = new DecodedContent();
50         assertEquals("", content.toString());
51
52         content = new DecodedContent("abc");
53         assertEquals("abc", content.toString());
54
55         content = new DecodedContent(128);
56         assertEquals("", content.toString());
57
58         content = new DecodedContent(0);
59         assertEquals("", content.toString());
60         content.append("abc");
61         assertEquals("abc", content.toString());
62
63         try{
64             Object o = new DecodedContent(-1);
65             fail();
66         }catch(NegativeArraySizeException e){
67         }catch(Throwable e){
68             fail();
69         }
70
71         return;
72     }
73
74     /**
75      * Test of init method, of class DecodedContent.
76      */
77     @Test
78     public void testInit(){
79         System.out.println("init");
80
81         DecodedContent content;
82
83         content = new DecodedContent();
84         content.append("abc");
85         content.addDecodeError((byte)0xff);
86         content.append("def");
87         assertEquals("abc?def", content.toString());
88         assertEquals(1, content.getDecodeErrorList().size());
89
90         content.init();
91         assertEquals("", content.toString());
92         assertEquals(0, content.getDecodeErrorList().size());
93
94         content.append('X');
95         assertEquals("X", content.toString());
96
97         return;
98     }
99
100     /**
101      * Test of hasDecodeError method, of class DecodedContent.
102      */
103     @Test
104     public void testHasDecodeError(){
105         System.out.println("hasDecodeError");
106
107         DecodedContent content;
108
109         content = new DecodedContent();
110         assertFalse(content.hasDecodeError());
111
112         content.append("a");
113         assertFalse(content.hasDecodeError());
114
115         content.addDecodeError((byte)0xff);
116         assertTrue(content.hasDecodeError());
117
118         content.append("b");
119         assertTrue(content.hasDecodeError());
120
121         content.init();
122         assertFalse(content.hasDecodeError());
123
124         content.append("c");
125         assertFalse(content.hasDecodeError());
126
127         content = new DecodedContent();
128         List<DecodeErrorInfo> list = content.getDecodeErrorList();
129         assertEquals(0, list.size());
130         assertFalse(content.hasDecodeError());
131
132         return;
133     }
134
135     /**
136      * Test of getDecodeErrorList method, of class DecodedContent.
137      */
138     @Test
139     public void testGetDecodeErrorList(){
140         System.out.println("getDecodeErrorList");
141
142         DecodedContent content;
143         List<DecodeErrorInfo> list;
144
145         content = new DecodedContent();
146         list = content.getDecodeErrorList();
147         assertEquals(0, list.size());
148
149         content.append("abc");
150         list = content.getDecodeErrorList();
151         assertEquals(0, list.size());
152
153         content.addDecodeError((byte)0xff);
154         list = content.getDecodeErrorList();
155         assertEquals(1, list.size());
156
157         content.append("def");
158         list = content.getDecodeErrorList();
159         assertEquals(1, list.size());
160
161         content.addDecodeError((byte)0x03, (byte)0x04);
162         list = content.getDecodeErrorList();
163         assertEquals(2, list.size());
164
165         return;
166     }
167
168     /**
169      * Test of getRawContent method, of class DecodedContent.
170      */
171     @Test
172     public void testGetRawContent(){
173         System.out.println("getRawContent");
174
175         DecodedContent content;
176
177         content = new DecodedContent();
178         assertEquals("", content.getRawContent().toString());
179
180         content.append("a");
181         assertEquals("a", content.getRawContent().toString());
182
183         content.addDecodeError((byte)0xff);
184         assertEquals("a?", content.getRawContent().toString());
185
186         content.append("b");
187         assertEquals("a?b", content.getRawContent().toString());
188
189         assertEquals(content.toString(), content.getRawContent().toString());
190
191         return;
192     }
193
194     /**
195      * Test of charAt method, of class DecodedContent.
196      */
197     @Test
198     public void testCharAt(){
199         System.out.println("charAt");
200
201         DecodedContent content;
202
203         content = new DecodedContent();
204         content.append("12345");
205         assertEquals('1', content.charAt(0));
206         assertEquals('3', content.charAt(2));
207         assertEquals('5', content.charAt(4));
208
209         try{
210             content.charAt(-1);
211             fail();
212         }catch(IndexOutOfBoundsException e){
213         }catch(Throwable e){
214             fail();
215         }
216
217         try{
218             content.charAt(5);
219             fail();
220         }catch(IndexOutOfBoundsException e){
221         }catch(Throwable e){
222             fail();
223         }
224
225         return;
226     }
227
228     /**
229      * Test of length method, of class DecodedContent.
230      */
231     @Test
232     public void testLength(){
233         System.out.println("length");
234
235         DecodedContent content;
236
237         content = new DecodedContent();
238         assertEquals(0, content.length());
239
240         content.append("12345");
241         assertEquals(5, content.length());
242
243         content.addDecodeError((byte)0xff);
244         assertEquals(6, content.length());
245
246         content.init();
247         assertEquals(0, content.length());
248
249         return;
250     }
251
252     /**
253      * Test of subSequence method, of class DecodedContent.
254      */
255     @Test
256     public void testSubSequence(){
257         System.out.println("subSequence");
258
259         DecodedContent content;
260
261         content = new DecodedContent();
262
263         content.append("12345");
264         assertEquals("234", content.subSequence(1, 4).toString());
265
266         try{
267             content.subSequence(-1, 4);
268             fail();
269         }catch(IndexOutOfBoundsException e){
270         }catch(Throwable e){
271             fail();
272         }
273
274         try{
275             content.subSequence(1, 6);
276             fail();
277         }catch(IndexOutOfBoundsException e){
278         }catch(Throwable e){
279             fail();
280         }
281
282         try{
283             content.subSequence(4, 1);
284             fail();
285         }catch(IndexOutOfBoundsException e){
286         }catch(Throwable e){
287             fail();
288         }
289
290         return;
291     }
292
293     /**
294      * Test of subContent method, of class DecodedContent.
295      */
296     @Test
297     public void testSubContent(){
298         System.out.println("subContent");
299
300         DecodedContent content;
301
302         content = new DecodedContent();
303
304         content.append("12345");
305         assertEquals("234", content.subContent(1, 4).toString());
306
307         try{
308             content.subContent(-1, 4);
309             fail();
310         }catch(IndexOutOfBoundsException e){
311         }catch(Throwable e){
312             fail();
313         }
314
315         try{
316             content.subContent(1, 6);
317             fail();
318         }catch(IndexOutOfBoundsException e){
319         }catch(Throwable e){
320             fail();
321         }
322
323         try{
324             content.subContent(4, 1);
325             fail();
326         }catch(IndexOutOfBoundsException e){
327         }catch(Throwable e){
328             fail();
329         }
330
331         content = new DecodedContent();
332         content.append("ab");
333         content.addDecodeError((byte)0x01);
334         content.append("de");
335         content = content.subContent(1,4);
336         assertEquals("b?d", content.toString());
337
338         List<DecodeErrorInfo> list = content.getDecodeErrorList();
339         assertEquals(1, list.size());
340         assertEquals((byte)0x01, list.get(0).getRawByte1st());
341
342         return;
343     }
344
345     /**
346      * Test of append method, of class DecodedContent.
347      */
348     @Test
349     public void testAppend_char(){
350         System.out.println("append");
351
352         DecodedContent content;
353
354         content = new DecodedContent();
355         content.append('a');
356         assertEquals("a", content.toString());
357
358         return;
359     }
360
361     /**
362      * Test of append method, of class DecodedContent.
363      */
364     @Test
365     public void testAppend_CharSequence(){
366         System.out.println("append");
367
368         DecodedContent content;
369
370         content = new DecodedContent();
371         CharSequence seq = "abc";
372         content.append(seq);
373         assertEquals("abc", content.toString());
374         content.append(null);
375         assertEquals("abcnull", content.toString());
376
377         return;
378     }
379
380     /**
381      * Test of append method, of class DecodedContent.
382      */
383     @Test
384     public void testAppend_3args_1(){
385         System.out.println("append");
386
387         DecodedContent content;
388
389         content = new DecodedContent();
390         content.append("abc");
391         assertEquals("abc", content.toString());
392
393         CharSequence seq = "12345";
394         content.append(seq, 1, 4);
395         assertEquals("abc234", content.toString());
396         content.append((CharSequence)null, 1, 2);
397         assertEquals("abc234null", content.toString());
398
399         return;
400     }
401
402     /**
403      * Test of append method, of class DecodedContent.
404      */
405     @Test
406     public void testAppend_3args_2(){
407         System.out.println("append");
408
409         DecodedContent content;
410
411         content = new DecodedContent();
412         content.append("abc");
413
414         DecodedContent other;
415         other = new DecodedContent();
416         other.append("12345");
417
418         content.append(other, 1, 4);
419         assertEquals("abc234", content.toString());
420
421         content = new DecodedContent();
422         content.append("abc");
423
424         other = new DecodedContent();
425         other.addDecodeError((byte)0x01);
426         other.addDecodeError((byte)0x02);
427         other.addDecodeError((byte)0x03);
428         other.addDecodeError((byte)0x04);
429         other.addDecodeError((byte)0x05);
430
431         content.append(other, 1, 4);
432         assertEquals("abc???", content.toString());
433
434         List<DecodeErrorInfo> list = content.getDecodeErrorList();
435         assertEquals(3, list.size());
436
437         DecodeErrorInfo info;
438
439         info = list.get(0);
440         assertEquals(3, info.getCharPosition());
441         assertEquals((byte)0x02, info.getRawByte1st());
442         info = list.get(1);
443         assertEquals(4, info.getCharPosition());
444         assertEquals((byte)0x03, info.getRawByte1st());
445         info = list.get(2);
446         assertEquals(5, info.getCharPosition());
447         assertEquals((byte)0x04, info.getRawByte1st());
448
449         return;
450     }
451
452     /**
453      * Test of append method, of class DecodedContent.
454      */
455     @Test
456     public void testAppend_3args_3(){
457         System.out.println("append");
458
459         DecodedContent content;
460
461         content = new DecodedContent();
462         content.append("abc");
463         assertEquals("abc", content.toString());
464
465         char[] seq = {'1','2','3','4','5',};
466         content.append(seq, 1, 3);
467         assertEquals("abc234", content.toString());
468         content.append((char[])null, 1, 2);
469         assertEquals("abc234null", content.toString());
470
471         return;
472     }
473
474     /**
475      * Test of addDecodeError method, of class DecodedContent.
476      */
477     @Test
478     public void testAddDecodeError_byte(){
479         System.out.println("addDecodeError");
480
481         DecodedContent content;
482
483         content = new DecodedContent();
484         content.append("abc");
485         content.addDecodeError((byte)0xfe);
486         content.append("def");
487         content.addDecodeError((byte)0xff);
488
489         assertEquals("abc?def?", content.toString());
490         List<DecodeErrorInfo> list = content.getDecodeErrorList();
491         assertEquals(2, list.size());
492
493         DecodeErrorInfo info;
494
495         info = list.get(0);
496         assertEquals(3, list.get(0).getCharPosition());
497         assertFalse(info.has2nd());
498         assertEquals((byte)0xfe, info.getRawByte1st());
499
500         info = list.get(1);
501         assertEquals(7, info.getCharPosition());
502         assertFalse(info.has2nd());
503         assertEquals((byte)0xff, info.getRawByte1st());
504
505         return;
506     }
507
508     /**
509      * Test of addDecodeError method, of class DecodedContent.
510      */
511     @Test
512     public void testAddDecodeError_byte_byte(){
513         System.out.println("addDecodeError");
514
515         DecodedContent content;
516
517         content = new DecodedContent();
518         content.append("abc");
519         content.addDecodeError((byte)0x01, (byte)0x02);
520         content.append("def");
521         content.addDecodeError((byte)0xfe, (byte)0xff);
522
523         assertEquals("abc?def?", content.toString());
524         List<DecodeErrorInfo> list = content.getDecodeErrorList();
525         assertEquals(2, list.size());
526
527         DecodeErrorInfo info;
528
529         info = list.get(0);
530         assertEquals(3, list.get(0).getCharPosition());
531         assertTrue(info.has2nd());
532         assertEquals((byte)0x01, info.getRawByte1st());
533         assertEquals((byte)0x02, info.getRawByte2nd());
534
535         info = list.get(1);
536         assertEquals(7, info.getCharPosition());
537         assertTrue(info.has2nd());
538         assertEquals((byte)0xfe, info.getRawByte1st());
539         assertEquals((byte)0xff, info.getRawByte2nd());
540
541         return;
542     }
543
544     /**
545      * Test of toString method, of class DecodedContent.
546      */
547     @Test
548     public void testToString(){
549         System.out.println("toString");
550
551         DecodedContent content;
552
553         content = new DecodedContent();
554         content.append("abc");
555         content.addDecodeError((byte)0x01, (byte)0x02);
556         content.append("def");
557         content.addDecodeError((byte)0xfe, (byte)0xff);
558
559         assertEquals("abc?def?", content.toString());
560         assertEquals(content.getRawContent().toString(), content.toString());
561
562         return;
563     }
564
565     /**
566      * Test of appendGappedErrorInfo method, of class DecodedContent.
567      */
568     @Test
569     public void testAppendGappedErrorInfo(){
570         System.out.println("appendGappedErrorInfo");
571
572         DecodedContent sourceContent;
573         sourceContent = new DecodedContent();
574         for(int pos = 0; pos <= 50; pos += 10){
575             sourceContent.append("123456789");
576             sourceContent.addDecodeError((byte)0x00);
577         }
578
579         List<DecodeErrorInfo> result;
580         result = DecodedContent.appendGappedErrorInfo(sourceContent, 15, 35, null, -100);
581         assertNotNull(result);
582         assertEquals(2, result.size());
583         assertEquals(119, result.get(0).getCharPosition());
584         assertEquals(129, result.get(1).getCharPosition());
585
586         return;
587     }
588
589     /**
590      * Test of ensureCapacity method, of class DecodedContent.
591      */
592     @Test
593     public void testEnsureCapacity(){
594         System.out.println("ensureCapacity");
595
596         DecodedContent content;
597
598         content = new DecodedContent("abc");
599         content.ensureCapacity(-1);
600         content.ensureCapacity(0);
601         content.ensureCapacity(1);
602         content.ensureCapacity(5);
603         content.append("def");
604         assertEquals("abcdef", content.toString());
605
606         content = new DecodedContent();
607         content.ensureCapacity(5);
608         content.append("abc");
609         assertEquals("abc", content.toString());
610
611         return;
612     }
613
614     /**
615      * Test of setCharAt method, of class DecodedContent.
616      */
617     @Test
618     public void testSetCharAt(){
619         System.out.println("setCharAt");
620
621         DecodedContent content;
622
623         content = new DecodedContent("abc");
624         content.setCharAt(1, 'B');
625         assertEquals("aBc", content.toString());
626
627         content = new DecodedContent("a");
628         content.addDecodeError((byte)0xff);
629         content.append('c');
630         assertEquals("a?c", content.toString());
631         content.setCharAt(1, 'B');
632         assertEquals("aBc", content.toString());
633         assertEquals(1, content.getDecodeErrorList().size());
634         assertEquals(1, content.getDecodeErrorList().get(0).getCharPosition());
635         assertEquals((byte)0xff, content.getDecodeErrorList().get(0).getRawByte1st());
636
637         content = new DecodedContent("abc");
638         try{
639             content.setCharAt(-1, 'B');
640             fail();
641         }catch(IndexOutOfBoundsException e){
642             // NOTHING
643         }
644         try{
645             content.setCharAt(10, 'B');
646             fail();
647         }catch(IndexOutOfBoundsException e){
648             // NOTHING
649         }
650
651         return;
652     }
653
654 }