1   /*
2    *  Java HTML Tidy - JTidy
3    *  HTML parser and pretty printer
4    *
5    *  Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
6    *  Institute of Technology, Institut National de Recherche en
7    *  Informatique et en Automatique, Keio University). All Rights
8    *  Reserved.
9    *
10   *  Contributing Author(s):
11   *
12   *     Dave Raggett <dsr@w3.org>
13   *     Andy Quick <ac.quick@sympatico.ca> (translation to Java)
14   *     Gary L Peskin <garyp@firstech.com> (Java development)
15   *     Sami Lempinen <sami@lempinen.net> (release management)
16   *     Fabrizio Giustina <fgiust at users.sourceforge.net>
17   *
18   *  The contributing author(s) would like to thank all those who
19   *  helped with testing, bug fixes, and patience.  This wouldn't
20   *  have been possible without all of you.
21   *
22   *  COPYRIGHT NOTICE:
23   * 
24   *  This software and documentation is provided "as is," and
25   *  the copyright holders and contributing author(s) make no
26   *  representations or warranties, express or implied, including
27   *  but not limited to, warranties of merchantability or fitness
28   *  for any particular purpose or that the use of the software or
29   *  documentation will not infringe any third party patents,
30   *  copyrights, trademarks or other rights. 
31   *
32   *  The copyright holders and contributing author(s) will not be
33   *  liable for any direct, indirect, special or consequential damages
34   *  arising out of any use of the software or documentation, even if
35   *  advised of the possibility of such damage.
36   *
37   *  Permission is hereby granted to use, copy, modify, and distribute
38   *  this source code, or portions hereof, documentation and executables,
39   *  for any purpose, without fee, subject to the following restrictions:
40   *
41   *  1. The origin of this source code must not be misrepresented.
42   *  2. Altered versions must be plainly marked as such and must
43   *     not be misrepresented as being the original source.
44   *  3. This Copyright notice may not be removed or altered from any
45   *     source or altered source distribution.
46   * 
47   *  The copyright holders and contributing author(s) specifically
48   *  permit, without fee, and encourage the use of this source code
49   *  as a component for supporting the Hypertext Markup Language in
50   *  commercial products. If you use this source code in a product,
51   *  acknowledgment is not required but would be appreciated.
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 }