OSDN Git Service

rename package
[jindolf/JinParser.git] / src / test / java / jp / osdn / jindolf / parser / EntityConverterTest.java
1 /*
2  * License : The MIT License
3  * Copyright(c) 2009 olyutorskii
4  */
5
6 package jp.osdn.jindolf.parser;
7
8 import java.util.List;
9 import jp.osdn.jindolf.parser.content.DecodeErrorInfo;
10 import jp.osdn.jindolf.parser.content.DecodedContent;
11 import org.junit.After;
12 import org.junit.AfterClass;
13 import org.junit.Before;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16
17 import static org.junit.Assert.*;
18
19 /**
20  */
21 public class EntityConverterTest {
22
23     public EntityConverterTest() {
24     }
25
26     @BeforeClass
27     public static void setUpClass() throws Exception{
28     }
29
30     @AfterClass
31     public static void tearDownClass() throws Exception{
32     }
33
34     @Before
35     public void setUp() {
36     }
37
38     @After
39     public void tearDown() {
40     }
41
42     /**
43      * Test of convert method, of class EntityConverter.
44      */
45     @Test
46     public void testConvert(){
47         System.out.println("convert");
48
49         EntityConverter converter = new EntityConverter();
50
51         DecodedContent from;
52         DecodedContent result;
53
54         from = new DecodedContent();
55         from.append("a>b<c"d&e");
56         result = converter.convert(from, 0, from.length());
57         assertEquals("a>b<c\"d&e", result.toString());
58
59         from = new DecodedContent();
60         from.append("&gt;&lt;&quot;&amp;");
61         result = converter.convert(from, 0, from.length());
62         assertEquals("><\"&", result.toString());
63
64         from = new DecodedContent();
65         from.append("12345");
66         result = converter.convert(from, 1, 3);
67         assertEquals("23", result.toString());
68
69         from = new DecodedContent();
70         from.append("12&gt;45");
71         result = converter.convert(from, 1, 7);
72         assertEquals("2>4", result.toString());
73
74         from = new DecodedContent();
75         from.append("12&gt;45");
76         result = converter.convert(from, 3, 7);
77         assertEquals("gt;4", result.toString());
78
79         from = new DecodedContent();
80         from.append("&amp;gt;");
81         result = converter.convert(from, 0, from.length());
82         assertEquals("&gt;", result.toString());
83
84         from = new DecodedContent();
85         from.append("a&gt;b");
86         result = converter.convert(from);
87         assertEquals("a>b", result.toString());
88
89         from = new DecodedContent();
90         from.append("a&gt;b");
91         from.addDecodeError((byte)0x03);
92         from.append("c");
93         result = converter.convert(from);
94         assertEquals("a>b?c", result.toString());
95         assertTrue(result.hasDecodeError());
96         List<DecodeErrorInfo> list = result.getDecodeErrorList();
97         assertEquals(1, list.size());
98         assertEquals((byte)0x03, list.get(0).getRawByte1st());
99
100         from = new DecodedContent();
101         from.append("");
102         result = converter.convert(from, 0, 0);
103         assertEquals("", result.toString());
104
105         from = new DecodedContent();
106         from.append("a\\b");
107         result = converter.convert(from, 0, from.length());
108         assertEquals("a¥b", result.toString());
109
110         from = new DecodedContent();
111         from.append("abcde");
112         SeqRange range = new SeqRange(1,4);
113         result = converter.convert(from, range);
114         assertEquals("bcd", result.toString());
115
116         from = new DecodedContent();
117         from.append("abcde");
118         try{
119             converter.convert(from, 4, 1);
120             fail();
121         }catch(IndexOutOfBoundsException e){
122             // OK
123         }
124
125         from = new DecodedContent();
126         from.append("abcde");
127         try{
128             converter.convert(from, -1, 4);
129             fail();
130         }catch(IndexOutOfBoundsException e){
131             // OK
132         }
133
134         from = new DecodedContent();
135         from.append("abcde");
136         try{
137             converter.convert(from, 1, 6);
138             fail();
139         }catch(IndexOutOfBoundsException e){
140             // OK
141         }
142
143         from = new DecodedContent();
144         from.append("a\ud83d\udc11b"); // 🐑
145         result = converter.convert(from);
146         assertEquals("a\ud83d\udc11b", result.toString());
147
148         from = new DecodedContent();
149         from.append("a\ud83d\udc11b"); // 🐑
150         EntityConverter repConverter = new EntityConverter(true);
151         result = repConverter.convert(from);
152         assertEquals("a?b", result.toString());
153
154         return;
155     }
156
157     /**
158      * Test of append method, of class EntityConverter.
159      */
160     @Test
161     public void testAppend(){
162         System.out.println("append");
163
164         EntityConverter converter = new EntityConverter();
165         DecodedContent target;
166         DecodedContent from;
167         DecodedContent result;
168
169         target = new DecodedContent("abc");
170         from = new DecodedContent("d&gt;f");
171         result = converter.append(target,from);
172         assertEquals("abcd>f", result.toString());
173
174         target = new DecodedContent("abc");
175         from = new DecodedContent("d&gt;fg&lt;i");
176         result = converter.append(target, from, 6, 12);
177         assertEquals("abcg<i", result.toString());
178
179         target = new DecodedContent("abc");
180         from = new DecodedContent("d&gt;fg&lt;i");
181         result = converter.append(target, from, new SeqRange(6, 12));
182         assertEquals("abcg<i", result.toString());
183
184         target = new DecodedContent();
185         target.append('a');
186         target.addDecodeError((byte)0xff);
187         target.append('c');
188         from = new DecodedContent();
189         from.append('d');
190         from.addDecodeError((byte)0xfe);
191         from.append('f');
192         result = converter.append(target, from);
193         assertEquals("a?cd?f", result.toString());
194         assertTrue(result.hasDecodeError());
195         List<DecodeErrorInfo> list = result.getDecodeErrorList();
196         assertEquals(2, list.size());
197         assertEquals((byte)0xff, list.get(0).getRawByte1st());
198         assertEquals((byte)0xfe, list.get(1).getRawByte1st());
199         assertEquals(1, list.get(0).getCharPosition());
200         assertEquals(4, list.get(1).getCharPosition());
201
202         return;
203     }
204
205 }