Home | History | Annotate | Download | only in stringprep
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*
      5  *******************************************************************************
      6  * Copyright (C) 2005-2015, International Business Machines Corporation and
      7  * others. All Rights Reserved.
      8  *******************************************************************************
      9 
     10  *******************************************************************************
     11  */
     12 
     13 package android.icu.dev.test.stringprep;
     14 
     15 import java.io.BufferedReader;
     16 import java.io.IOException;
     17 import java.io.UnsupportedEncodingException;
     18 import java.util.HashMap;
     19 import java.util.Iterator;
     20 import java.util.Set;
     21 import java.util.TreeMap;
     22 
     23 import org.junit.Test;
     24 import org.junit.runner.RunWith;
     25 import org.junit.runners.JUnit4;
     26 
     27 import android.icu.dev.test.TestFmwk;
     28 import android.icu.dev.test.TestUtil;
     29 import android.icu.text.IDNA;
     30 import android.icu.text.StringPrepParseException;
     31 import android.icu.text.UTF16;
     32 import android.icu.testsharding.MainTestShard;
     33 
     34 /**
     35  * @author limaoyu
     36  *
     37  */
     38 @MainTestShard
     39 @RunWith(JUnit4.class)
     40 public class IDNAConformanceTest extends TestFmwk {
     41     @Test
     42     public void TestConformance() {
     43 
     44         TreeMap inputData = null;
     45 
     46         try {
     47             inputData = ReadInput.getInputData();
     48         } catch (UnsupportedEncodingException e) {
     49             errln(e.getMessage());
     50             return;
     51         } catch (IOException e) {
     52             errln(e.getMessage());
     53             return;
     54         }
     55 
     56         Set keyMap = inputData.keySet();
     57         for (Iterator iter = keyMap.iterator(); iter.hasNext();) {
     58             Long element = (Long) iter.next();
     59             HashMap tempHash = (HashMap) inputData.get(element);
     60 
     61             //get all attributes from input data
     62             String passfail = (String) tempHash.get("passfail");
     63             String desc = (String) tempHash.get("desc");
     64             String type = (String) tempHash.get("type");
     65             String namebase = (String) tempHash.get("namebase");
     66             String nameutf8 = (String) tempHash.get("nameutf8");
     67             String namezone = (String) tempHash.get("namezone");
     68             String failzone1 = (String) tempHash.get("failzone1");
     69             String failzone2 = (String) tempHash.get("failzone2");
     70 
     71             //they maybe includes <*> style unicode
     72             namebase = stringReplace(namebase);
     73             namezone = stringReplace(namezone);
     74 
     75             String result = null;
     76             boolean failed = false;
     77 
     78             if ("toascii".equals(tempHash.get("type"))) {
     79 
     80                 //get the result
     81                 try {
     82                     //by default STD3 rules are not used, but if the description
     83                     //includes UseSTD3ASCIIRules, we will set it.
     84                     if (desc.toLowerCase().indexOf(
     85                             "UseSTD3ASCIIRules".toLowerCase()) == -1) {
     86                         result = IDNA.convertIDNToASCII(namebase,
     87                                 IDNA.ALLOW_UNASSIGNED).toString();
     88                     } else {
     89                         result = IDNA.convertIDNToASCII(namebase,
     90                                 IDNA.USE_STD3_RULES).toString();
     91                     }
     92                 } catch (StringPrepParseException e2) {
     93                     //errln(e2.getMessage());
     94                     failed = true;
     95                 }
     96 
     97 
     98                 if ("pass".equals(passfail)) {
     99                     if (!namezone.equals(result)) {
    100                         printInfo(desc, namebase, nameutf8, namezone,
    101                                 failzone1, failzone2, result, type, passfail);
    102                         errln("\t pass fail standard is pass, but failed");
    103                     } else {
    104                         printInfo(desc, namebase, nameutf8, namezone,
    105                                 failzone1, failzone2, result, type, passfail);
    106                         logln("\tpassed");
    107                     }
    108                 }
    109 
    110                 if ("fail".equals(passfail)) {
    111                     if (failed) {
    112                         printInfo(desc, namebase, nameutf8, namezone,
    113                                 failzone1, failzone2, result, type, passfail);
    114                         logln("passed");
    115                     } else {
    116                         printInfo(desc, namebase, nameutf8, namezone,
    117                                 failzone1, failzone2, result, type, passfail);
    118                         errln("\t pass fail standard is fail, but no exception thrown out");
    119                     }
    120                 }
    121             } else if ("tounicode".equals(tempHash.get("type"))) {
    122                 try {
    123                     //by default STD3 rules are not used, but if the description
    124                     //includes UseSTD3ASCIIRules, we will set it.
    125                     if (desc.toLowerCase().indexOf(
    126                             "UseSTD3ASCIIRules".toLowerCase()) == -1) {
    127                         result = IDNA.convertIDNToUnicode(namebase,
    128                                 IDNA.ALLOW_UNASSIGNED).toString();
    129                     } else {
    130                         result = IDNA.convertIDNToUnicode(namebase,
    131                                 IDNA.USE_STD3_RULES).toString();
    132                     }
    133                 } catch (StringPrepParseException e2) {
    134                     //errln(e2.getMessage());
    135                     failed = true;
    136                 }
    137                 if ("pass".equals(passfail)) {
    138                     if (!namezone.equals(result)) {
    139                         printInfo(desc, namebase, nameutf8, namezone,
    140                                 failzone1, failzone2, result, type, passfail);
    141 
    142                         errln("\t Did not get the expected result. Expected: " + prettify(namezone) + " Got: " + prettify(result));
    143                     } else {
    144                         printInfo(desc, namebase, nameutf8, namezone,
    145                                 failzone1, failzone2, result, type, passfail);
    146                         logln("\tpassed");
    147                     }
    148                 }
    149 
    150                 if ("fail".equals(passfail)) {
    151                     if (failed || namebase.equals(result)) {
    152                         printInfo(desc, namebase, nameutf8, namezone,
    153                                 failzone1, failzone2, result, type, passfail);
    154 
    155                         logln("\tpassed");
    156                     } else {
    157                         printInfo(desc, namebase, nameutf8, namezone,
    158                                 failzone1, failzone2, result, type, passfail);
    159 
    160                         errln("\t pass fail standard is fail, but no exception thrown out");
    161                     }
    162                 }
    163             } else {
    164                 continue;
    165             }
    166         }
    167     }
    168 
    169     /**
    170      * Print log message.
    171      * @param desc
    172      * @param namebase
    173      * @param nameutf8
    174      * @param namezone
    175      * @param failzone1
    176      * @param failzone2
    177      * @param result
    178      */
    179     private void printInfo(String desc, String namebase,
    180             String nameutf8, String namezone, String failzone1,
    181             String failzone2, String result, String type, String passfail) {
    182         logln("desc:\t" + desc);
    183         log("\t");
    184         logln("type:\t" + type);
    185         log("\t");
    186         logln("pass fail standard:\t" + passfail);
    187         log("\t");
    188         logln("namebase:\t" + namebase);
    189         log("\t");
    190         logln("nameutf8:\t" + nameutf8);
    191         log("\t");
    192         logln("namezone:\t" + namezone);
    193         log("\t");
    194         logln("failzone1:\t" + failzone1);
    195         log("\t");
    196         logln("failzone2:\t" + failzone2);
    197         log("\t");
    198         logln("result:\t" + result);
    199     }
    200 
    201     /**
    202      * Change unicode string from <00AD> to \u00AD, for the later is accepted
    203      * by Java
    204      * @param str String including <*> style unicode
    205      * @return \\u String
    206      */
    207     private static String stringReplace(String str) {
    208 
    209         StringBuffer result = new StringBuffer();
    210         char[] chars = str.toCharArray();
    211         StringBuffer sbTemp = new StringBuffer();
    212         for (int i = 0; i < chars.length; i++) {
    213             if ('<' == chars[i]) {
    214                 sbTemp = new StringBuffer();
    215                 while ('>' != chars[i + 1]) {
    216                     sbTemp.append(chars[++i]);
    217                 }
    218                 /*
    219                  * The unicode sometimes is larger then \uFFFF, so have to use
    220                  * UTF16.
    221                  */
    222                 int toBeInserted = Integer.parseInt(sbTemp.toString(), 16);
    223                 if ((toBeInserted >> 16) == 0) {
    224                     result.append((char) toBeInserted);
    225                 } else {
    226                     String utf16String = UTF16.valueOf(toBeInserted);
    227                     char[] charsTemp = utf16String.toCharArray();
    228                     for (int j = 0; j < charsTemp.length; j++) {
    229                         result.append(charsTemp[j]);
    230                     }
    231                 }
    232             } else if ('>' == chars[i]) {//end when met with '>'
    233                 continue;
    234             } else {
    235                 result.append(chars[i]);
    236             }
    237 
    238         }
    239         return result.toString();
    240     }
    241 
    242     /**
    243      * This class is used to read test data from TestInput file.
    244      *
    245      * @author limaoyu
    246      *
    247      */
    248     public static class ReadInput {
    249 
    250         public static TreeMap getInputData() throws IOException,
    251                 UnsupportedEncodingException {
    252 
    253             TreeMap result = new TreeMap();
    254             BufferedReader in = TestUtil.getDataReader("IDNATestInput.txt", "utf-8");
    255             try {
    256                 String tempStr = null;
    257                 int records = 0;
    258                 boolean firstLine = true;
    259                 HashMap hashItem = new HashMap();
    260 
    261                 while ((tempStr = in.readLine()) != null) {
    262                     //ignore the first line if it's "====="
    263                     if (firstLine) {
    264                         if ("=====".equals(tempStr))
    265                             continue;
    266                         firstLine = false;
    267                     }
    268 
    269                     //Ignore empty line
    270                     if ("".equals(tempStr)) {
    271                         continue;
    272                     }
    273 
    274                     String attr = "";//attribute
    275                     String body = "";//value
    276 
    277                     //get attr and body from line input, and then set them into each hash item.
    278                     int postion = tempStr.indexOf(":");
    279                     if (postion > -1) {
    280                         attr = tempStr.substring(0, postion).trim();
    281                         body = tempStr.substring(postion + 1).trim();
    282 
    283                         //deal with combination lines when end with '\'
    284                         while (null != body && body.length() > 0
    285                                 && '\\' == body.charAt(body.length() - 1)) {
    286                             body = body.substring(0, body.length() - 1);
    287                             body += "\n";
    288                             tempStr = in.readLine();
    289                             body += tempStr;
    290                         }
    291                     }
    292                     //push them to hash item
    293                     hashItem.put(attr, body);
    294 
    295                     //if met "=====", it means this item is finished
    296                     if ("=====".equals(tempStr)) {
    297                         //set them into result, using records number as key
    298                         result.put(new Long(records), hashItem);
    299                         //create another hash item and continue
    300                         hashItem = new HashMap();
    301                         records++;
    302                         continue;
    303                     }
    304                 }
    305             } finally {
    306                 in.close();
    307             }
    308             return result;
    309         }
    310     }
    311 }
    312