001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.i3test;
028:
029: import com.sun.cldc.i18n.*;
030: import java.io.*;
031:
032: public class TestUtfReaders extends TestCase {
033:
034: public String teststr1 = "你好世界";
035: public String teststr2 = "привет,мир";
036: public String teststr3 = "hello, world!";
037:
038: public String hex1(int x) {
039: return Integer.toHexString(0xff & x);
040: }
041:
042: public String hex2(int x) {
043: return Integer.toHexString(0xffff & x);
044: }
045:
046: public void dumpString(String s) {
047: String t = "String: ";
048: for (int i = 0; i < s.length(); i++)
049: t += "[" + hex2((int) s.charAt(i)) + "] ";
050: info(t);
051: }
052:
053: public void dumpBytes(byte[] b) {
054: String t = "byte[]: ";
055: for (int i = 0; i < b.length; i++)
056: t += "[" + hex1(b[i]) + "] ";
057: info(t);
058: }
059:
060: public void test2way(int strId, String s, String e) {
061: try {
062: declare("test2way string#" + strId + " " + s + " " + e);
063: //dumpString(s);
064: byte[] b = s.getBytes(e);
065: //dumpBytes(b);
066: String t = new String(b, e);
067: //dumpString(t);
068: int diff = s.compareTo(t);
069: assertTrue("strings different", diff == 0);
070: } catch (Throwable t) {
071: t.printStackTrace();
072: }
073: }
074:
075: public void testMark(int strId, String s, String e) {
076: try {
077: declare("test mark string#" + strId + " " + s + " " + e);
078: byte[] b = s.getBytes(e);
079: final StreamReader r = //new ReaderUTF16(new ByteArrayInputStream(b));
080: (StreamReader) Class.forName(
081: "com.sun.cldc.i18n.j2me." + e + "_Reader")
082: .newInstance();
083: final ByteArrayInputStream bais = new ByteArrayInputStream(
084: b);
085: r.open(bais, "UTF_16");
086: //info("bais.markSupported() = "+bais.markSupported());
087: assertTrue("markSupported() must return the same value", r
088: .markSupported() == bais.markSupported());
089: assertTrue("markSupported() must return true", r
090: .markSupported());
091: r.mark(2);
092: int c1 = r.read();
093: int c2 = r.read();
094: r.reset();
095: int c3 = r.read();
096: int c4 = r.read();
097: assertEquals("the first character must be the same", c1, c3);
098: assertEquals("the second character must be the same", c2,
099: c4);
100: r.reset();
101: int c;
102: String s2 = "";
103: while (-1 != (c = r.read())) {
104: s2 += (char) c;
105: }
106: //dumpString(s2);
107: assertEquals("strings (original and read) must be equal",
108: s, s2);
109: } catch (Throwable t) {
110: t.printStackTrace();
111: }
112: }
113:
114: /**
115: * Runs all the tests.
116: */
117: public void runTests() {
118: String[] enc = { "UTF_16", "UTF_16LE", "UTF_16BE", "UTF_8" };
119: String[] str = { teststr1, teststr2, teststr3 };
120: for (int i = 0; i < enc.length; i++) {
121: for (int j = 0; j < str.length; j++) {
122: test2way(j, str[j], enc[i]);
123: testMark(j, str[j], enc[i]);
124: }
125: }
126: }
127:
128: }
|