Home | History | Annotate | Download | only in util
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 1996-2014, International Business Machines Corporation and
      6  * others. All Rights Reserved.
      7  *******************************************************************************
      8  */
      9 
     10 package com.ibm.icu.dev.test.util;
     11 
     12 import java.io.IOException;
     13 import java.nio.ByteBuffer;
     14 
     15 import org.junit.Test;
     16 import org.junit.runner.RunWith;
     17 import org.junit.runners.JUnit4;
     18 
     19 import com.ibm.icu.dev.test.TestFmwk;
     20 import com.ibm.icu.impl.ICUBinary;
     21 
     22 /**
     23 * Testing class for Trie. Tests here will be simple, since both CharTrie and
     24 * IntTrie are very similar and are heavily used in other parts of ICU4J.
     25 * Codes using Tries are expected to have detailed tests.
     26 * @author Syn Wee Quek
     27 * @since release 2.1 Jan 01 2002
     28 */
     29 @RunWith(JUnit4.class)
     30 public final class ICUBinaryTest extends TestFmwk
     31 {
     32     // constructor ---------------------------------------------------
     33 
     34     /**
     35     * Constructor
     36     */
     37     public ICUBinaryTest()
     38     {
     39     }
     40 
     41     // public methods -----------------------------------------------
     42 
     43     /**
     44      * Testing the constructors of the Tries
     45      */
     46     @Test
     47     public void TestReadHeader()
     48     {
     49         int formatid = 0x01020304;
     50         byte array[] = {
     51             // header size
     52             0, 0x18,
     53             // magic numbers
     54             (byte)0xda, 0x27,
     55             // size
     56             0, 0x14,
     57             // reserved word
     58             0, 0,
     59             // bigendian
     60             1,
     61             // charset
     62             0,
     63             // charsize
     64             2,
     65             // reserved byte
     66             0,
     67             // data format id
     68             1, 2, 3, 4,
     69             // dataVersion
     70             1, 2, 3, 4,
     71             // unicodeVersion
     72             3, 2, 0, 0
     73         };
     74         ByteBuffer bytes = ByteBuffer.wrap(array);
     75         ICUBinary.Authenticate authenticate
     76                 = new ICUBinary.Authenticate() {
     77                     @Override
     78                     public boolean isDataVersionAcceptable(byte version[])
     79                     {
     80                         return version[0] == 1;
     81                     }
     82                 };
     83         // check full data version
     84         try {
     85             ICUBinary.readHeader(bytes, formatid, authenticate);
     86         } catch (IOException e) {
     87             errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
     88         }
     89         // no restriction to the data version
     90         try {
     91             bytes.rewind();
     92             ICUBinary.readHeader(bytes, formatid, null);
     93         } catch (IOException e) {
     94             errln("Failed: Null authenticate object should pass ICUBinary.readHeader");
     95         }
     96         // lenient data version
     97         array[17] = 9;
     98         try {
     99             bytes.rewind();
    100             ICUBinary.readHeader(bytes, formatid, authenticate);
    101         } catch (IOException e) {
    102             errln("Failed: Lenient authenticate object should pass ICUBinary.readHeader");
    103         }
    104         // changing the version to an incorrect one, expecting failure
    105         array[16] = 2;
    106         try {
    107             bytes.rewind();
    108             ICUBinary.readHeader(bytes, formatid, authenticate);
    109             errln("Failed: Invalid version number should not pass authenticate object");
    110         } catch (IOException e) {
    111             logln("PASS: ICUBinary.readHeader with invalid version number failed as expected");
    112         }
    113     }
    114 }
    115