001: /*
002: * Java HTML Tidy - JTidy
003: * HTML parser and pretty printer
004: *
005: * Copyright (c) 1998-2000 World Wide Web Consortium (Massachusetts
006: * Institute of Technology, Institut National de Recherche en
007: * Informatique et en Automatique, Keio University). All Rights
008: * Reserved.
009: *
010: * Contributing Author(s):
011: *
012: * Dave Raggett <dsr@w3.org>
013: * Andy Quick <ac.quick@sympatico.ca> (translation to Java)
014: * Gary L Peskin <garyp@firstech.com> (Java development)
015: * Sami Lempinen <sami@lempinen.net> (release management)
016: * Fabrizio Giustina <fgiust at users.sourceforge.net>
017: *
018: * The contributing author(s) would like to thank all those who
019: * helped with testing, bug fixes, and patience. This wouldn't
020: * have been possible without all of you.
021: *
022: * COPYRIGHT NOTICE:
023: *
024: * This software and documentation is provided "as is," and
025: * the copyright holders and contributing author(s) make no
026: * representations or warranties, express or implied, including
027: * but not limited to, warranties of merchantability or fitness
028: * for any particular purpose or that the use of the software or
029: * documentation will not infringe any third party patents,
030: * copyrights, trademarks or other rights.
031: *
032: * The copyright holders and contributing author(s) will not be
033: * liable for any direct, indirect, special or consequential damages
034: * arising out of any use of the software or documentation, even if
035: * advised of the possibility of such damage.
036: *
037: * Permission is hereby granted to use, copy, modify, and distribute
038: * this source code, or portions hereof, documentation and executables,
039: * for any purpose, without fee, subject to the following restrictions:
040: *
041: * 1. The origin of this source code must not be misrepresented.
042: * 2. Altered versions must be plainly marked as such and must
043: * not be misrepresented as being the original source.
044: * 3. This Copyright notice may not be removed or altered from any
045: * source or altered source distribution.
046: *
047: * The copyright holders and contributing author(s) specifically
048: * permit, without fee, and encourage the use of this source code
049: * as a component for supporting the Hypertext Markup Language in
050: * commercial products. If you use this source code in a product,
051: * acknowledgment is not required but would be appreciated.
052: *
053: */
054: package org.w3c.tidy;
055:
056: import java.io.ByteArrayInputStream;
057: import java.io.InputStream;
058:
059: import junit.framework.TestCase;
060:
061: /**
062: * @author Fabrizio Giustina
063: * @version $Revision: 1.6 $ ($Author: fgiust $)
064: */
065: public class StreamInImplTest extends TestCase {
066:
067: /**
068: * test instance.
069: */
070: private StreamInImpl in;
071:
072: /**
073: * Lexer instance saved in StreamInImpl.
074: */
075: private Lexer lexer;
076:
077: /**
078: * @see junit.framework.TestCase#setUp()
079: */
080: protected void setUp() throws Exception {
081: super .setUp();
082:
083: InputStream stream = new ByteArrayInputStream(new byte[] { 0 });
084:
085: Report report = new Report();
086: Configuration configuration = new Configuration(report);
087: configuration.setInCharEncodingName("US-ASCII");
088: in = new StreamInImpl(stream, configuration);
089:
090: lexer = new Lexer(in, configuration, report);
091: lexer.configuration = configuration;
092: in.setLexer(lexer);
093: }
094:
095: /**
096: * test readChar() with ascii encoding.
097: */
098: public final void testReadCharFromStreamAscii() {
099: InputStream stream = new ByteArrayInputStream(new byte[] { 97,
100: 97, 97 });
101:
102: Report report = new Report();
103: Configuration configuration = new Configuration(report);
104: configuration.setInCharEncodingName("US-ASCII");
105: in = new StreamInImpl(stream, configuration);
106: in.setLexer(lexer);
107:
108: char thechar = (char) in.readCharFromStream();
109: assertEquals('a', thechar);
110: thechar = (char) in.readCharFromStream();
111: assertEquals('a', thechar);
112: }
113:
114: /**
115: * test readChar() with UTF16 encoding and no BOM.
116: */
117: public final void testReadCharFromStreamUTF16() {
118: InputStream stream = new ByteArrayInputStream(new byte[] { 00,
119: 97, 00, 97 });
120:
121: Report report = new Report();
122: Configuration configuration = new Configuration(report);
123: configuration.setInCharEncodingName("utf16be");
124: in = new StreamInImpl(stream, configuration);
125: in.setLexer(lexer);
126:
127: char thechar = (char) in.readCharFromStream();
128: assertEquals('a', thechar);
129: thechar = (char) in.readCharFromStream();
130: assertEquals('a', thechar);
131: }
132:
133: /**
134: * test readChar() with UTF16 encoding and BE BOM.
135: */
136: public final void testReadCharFromStreamUTF16WithBOMLE() {
137: InputStream stream = new ByteArrayInputStream(new byte[] { -1,
138: -2, 97, 00 });
139:
140: Report report = new Report();
141: Configuration configuration = new Configuration(report);
142: configuration.setInCharEncodingName("utf16");
143: in = new StreamInImpl(stream, configuration);
144: in.setLexer(lexer);
145:
146: char thechar = (char) in.readCharFromStream();
147: assertEquals(EncodingUtils.UNICODE_BOM, thechar);
148: thechar = (char) in.readCharFromStream();
149: assertEquals('a', thechar);
150: }
151:
152: /**
153: * test readChar() with UTF16 encoding and LE BOM.
154: */
155: public final void testReadCharFromStreamUTF16WithBOMBE() {
156: InputStream stream = new ByteArrayInputStream(new byte[] { -2,
157: -1, 00, 97 });
158:
159: Report report = new Report();
160: Configuration configuration = new Configuration(report);
161: configuration.setInCharEncodingName("utf16");
162: in = new StreamInImpl(stream, configuration);
163: in.setLexer(lexer);
164:
165: char thechar = (char) in.readCharFromStream();
166: assertEquals(EncodingUtils.UNICODE_BOM, thechar);
167: thechar = (char) in.readCharFromStream();
168: assertEquals('a', thechar);
169: }
170:
171: }
|