001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: package com.ibm.icu.dev.test.util;
009:
010: import com.ibm.icu.dev.test.TestFmwk;
011: import com.ibm.icu.impl.ICUBinary;
012: import java.io.IOException;
013: import java.io.ByteArrayInputStream;
014:
015: /**
016: * Testing class for Trie. Tests here will be simple, since both CharTrie and
017: * IntTrie are very similar and are heavily used in other parts of ICU4J.
018: * Codes using Tries are expected to have detailed tests.
019: * @author Syn Wee Quek
020: * @since release 2.1 Jan 01 2002
021: */
022: public final class ICUBinaryTest extends TestFmwk {
023: // constructor ---------------------------------------------------
024:
025: /**
026: * Constructor
027: */
028: public ICUBinaryTest() {
029: }
030:
031: // public methods -----------------------------------------------
032:
033: public static void main(String arg[]) {
034: ICUBinaryTest test = new ICUBinaryTest();
035: try {
036: test.run(arg);
037: } catch (Exception e) {
038: test.errln("Error testing icubinarytest");
039: }
040: }
041:
042: /**
043: * Testing the constructors of the Tries
044: */
045: public void TestReadHeader() {
046: byte formatid[] = { 1, 2, 3, 4 };
047: byte array[] = {
048: // header size
049: 0, 0x18,
050: // magic numbers
051: (byte) 0xda, 0x27,
052: // size
053: 0, 0,
054: // reserved word
055: 0, 0,
056: // bigendian
057: 1,
058: // charset
059: 0,
060: // charsize
061: 2,
062: // reserved byte
063: 0,
064: // data format id
065: 1, 2, 3, 4,
066: // dataVersion
067: 1, 2, 3, 4,
068: // unicodeVersion
069: 3, 2, 0, 0 };
070: ByteArrayInputStream inputstream = new ByteArrayInputStream(
071: array);
072: ICUBinary.Authenticate authenticate = new ICUBinary.Authenticate() {
073: public boolean isDataVersionAcceptable(byte version[]) {
074: return version[0] == 1;
075: }
076: };
077: // check full data version
078: try {
079: ICUBinary.readHeader(inputstream, formatid, authenticate);
080: } catch (IOException e) {
081: errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
082: }
083: // no restriction to the data version
084: try {
085: inputstream.reset();
086: ICUBinary.readHeader(inputstream, formatid, null);
087: } catch (IOException e) {
088: errln("Failed: Null authenticate object should pass ICUBinary.readHeader");
089: }
090: // lenient data version
091: try {
092: inputstream.reset();
093: ICUBinary.readHeader(inputstream, formatid, authenticate);
094: } catch (IOException e) {
095: errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
096: }
097: // changing the version to an incorrect one, expecting failure
098: array[16] = 2;
099: try {
100: inputstream.reset();
101: ICUBinary.readHeader(inputstream, formatid, authenticate);
102: errln("Failed: Invalid version number should not pass authenticate object");
103: } catch (IOException e) {
104: logln("PASS: ICUBinary.readHeader with invalid version number failed as expected");
105: }
106: }
107: }
|