1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 package org.w3c.tidy;
55
56 import java.io.ByteArrayInputStream;
57 import java.io.InputStream;
58
59 import junit.framework.TestCase;
60
61
62 /***
63 * @author Fabrizio Giustina
64 * @version $Revision: 1.6 $ ($Author: fgiust $)
65 */
66 public class StreamInImplTest extends TestCase
67 {
68
69 /***
70 * test instance.
71 */
72 private StreamInImpl in;
73
74 /***
75 * Lexer instance saved in StreamInImpl.
76 */
77 private Lexer lexer;
78
79 /***
80 * @see junit.framework.TestCase#setUp()
81 */
82 protected void setUp() throws Exception
83 {
84 super.setUp();
85
86 InputStream stream = new ByteArrayInputStream(new byte[]{0});
87
88 Report report = new Report();
89 Configuration configuration = new Configuration(report);
90 configuration.setInCharEncodingName("US-ASCII");
91 in = new StreamInImpl(stream, configuration);
92
93 lexer = new Lexer(in, configuration, report);
94 lexer.configuration = configuration;
95 in.setLexer(lexer);
96 }
97
98 /***
99 * test readChar() with ascii encoding.
100 */
101 public final void testReadCharFromStreamAscii()
102 {
103 InputStream stream = new ByteArrayInputStream(new byte[]{97, 97, 97});
104
105 Report report = new Report();
106 Configuration configuration = new Configuration(report);
107 configuration.setInCharEncodingName("US-ASCII");
108 in = new StreamInImpl(stream, configuration);
109 in.setLexer(lexer);
110
111 char thechar = (char) in.readCharFromStream();
112 assertEquals('a', thechar);
113 thechar = (char) in.readCharFromStream();
114 assertEquals('a', thechar);
115 }
116
117 /***
118 * test readChar() with UTF16 encoding and no BOM.
119 */
120 public final void testReadCharFromStreamUTF16()
121 {
122 InputStream stream = new ByteArrayInputStream(new byte[]{00, 97, 00, 97});
123
124 Report report = new Report();
125 Configuration configuration = new Configuration(report);
126 configuration.setInCharEncodingName("utf16be");
127 in = new StreamInImpl(stream, configuration);
128 in.setLexer(lexer);
129
130 char thechar = (char) in.readCharFromStream();
131 assertEquals('a', thechar);
132 thechar = (char) in.readCharFromStream();
133 assertEquals('a', thechar);
134 }
135
136 /***
137 * test readChar() with UTF16 encoding and BE BOM.
138 */
139 public final void testReadCharFromStreamUTF16WithBOMLE()
140 {
141 InputStream stream = new ByteArrayInputStream(new byte[]{-1, -2, 97, 00});
142
143 Report report = new Report();
144 Configuration configuration = new Configuration(report);
145 configuration.setInCharEncodingName("utf16");
146 in = new StreamInImpl(stream, configuration);
147 in.setLexer(lexer);
148
149 char thechar = (char) in.readCharFromStream();
150 assertEquals(EncodingUtils.UNICODE_BOM, thechar);
151 thechar = (char) in.readCharFromStream();
152 assertEquals('a', thechar);
153 }
154
155 /***
156 * test readChar() with UTF16 encoding and LE BOM.
157 */
158 public final void testReadCharFromStreamUTF16WithBOMBE()
159 {
160 InputStream stream = new ByteArrayInputStream(new byte[]{-2, -1, 00, 97});
161
162 Report report = new Report();
163 Configuration configuration = new Configuration(report);
164 configuration.setInCharEncodingName("utf16");
165 in = new StreamInImpl(stream, configuration);
166 in.setLexer(lexer);
167
168 char thechar = (char) in.readCharFromStream();
169 assertEquals(EncodingUtils.UNICODE_BOM, thechar);
170 thechar = (char) in.readCharFromStream();
171 assertEquals('a', thechar);
172 }
173
174 }