Home | History | Annotate | Download | only in stringprep
      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) 2003-2010, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8 */
      9 package com.ibm.icu.dev.test.stringprep;
     10 
     11 import org.junit.Test;
     12 
     13 import com.ibm.icu.dev.test.TestFmwk;
     14 import com.ibm.icu.text.StringPrepParseException;
     15 import com.ibm.icu.text.UCharacterIterator;
     16 
     17 /**
     18  * @author ram
     19  *
     20  * To change the template for this generated type comment go to
     21  * Window>Preferences>Java>Code Generation>Code and Comments
     22  */
     23 public class TestIDNARef extends TestFmwk {
     24     private StringPrepParseException unassignedException = new StringPrepParseException("",StringPrepParseException.UNASSIGNED_ERROR);
     25 
     26     @Test
     27     public void TestToUnicode() throws Exception{
     28         try{
     29             for(int i=0; i<TestData.asciiIn.length; i++){
     30                 // test StringBuffer toUnicode
     31                 doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.DEFAULT, null);
     32                 doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.ALLOW_UNASSIGNED, null);
     33                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES, null);
     34                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES|IDNAReference.ALLOW_UNASSIGNED, null);
     35 
     36             }
     37         }catch(java.lang.ExceptionInInitializerError ex){
     38             warnln("Could not load NamePrepTransform data");
     39         }catch(java.lang.NoClassDefFoundError ex){
     40             warnln("Could not load NamePrepTransform data");
     41         }
     42     }
     43 
     44     @Test
     45     public void TestToASCII() throws Exception{
     46         try{
     47             for(int i=0; i<TestData.asciiIn.length; i++){
     48                 // test StringBuffer toUnicode
     49                 doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNAReference.DEFAULT, null);
     50                 doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNAReference.ALLOW_UNASSIGNED, null);
     51                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES, null);
     52                 //doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNAReference.USE_STD3_RULES|IDNAReference.ALLOW_UNASSIGNED, null);
     53 
     54             }
     55         }catch(java.lang.ExceptionInInitializerError ex){
     56             warnln("Could not load NamePrepTransform data");
     57         }catch(java.lang.NoClassDefFoundError ex){
     58             warnln("Could not load NamePrepTransform data");
     59         }
     60     }
     61 
     62     @Test
     63     public void TestIDNToASCII() throws Exception{
     64         try{
     65             for(int i=0; i<TestData.domainNames.length; i++){
     66                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.DEFAULT, null);
     67                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED, null);
     68                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.USE_STD3_RULES, null);
     69                 doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);
     70             }
     71 
     72             for(int i=0; i<TestData.domainNames1Uni.length; i++){
     73                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.DEFAULT, null);
     74                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.ALLOW_UNASSIGNED, null);
     75                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.USE_STD3_RULES, null);
     76                 doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);
     77 
     78             }
     79         }catch(java.lang.ExceptionInInitializerError ex){
     80             warnln("Could not load NamePrepTransform data");
     81         }catch(java.lang.NoClassDefFoundError ex){
     82             warnln("Could not load NamePrepTransform data");
     83         }
     84     }
     85     @Test
     86     public void TestIDNToUnicode() throws Exception{
     87         try{
     88             for(int i=0; i<TestData.domainNames.length; i++){
     89                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.DEFAULT, null);
     90                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED, null);
     91                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.USE_STD3_RULES, null);
     92                 doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNAReference.ALLOW_UNASSIGNED|IDNAReference.USE_STD3_RULES, null);
     93             }
     94             for(int i=0; i<TestData.domainNamesToASCIIOut.length; i++){
     95                 doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNAReference.DEFAULT, null);
     96                 doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNAReference.ALLOW_UNASSIGNED, null);
     97             }
     98         }catch(java.lang.ExceptionInInitializerError ex){
     99             warnln("Could not load NamePrepTransform data");
    100         }catch(java.lang.NoClassDefFoundError ex){
    101             warnln("Could not load NamePrepTransform data");
    102         }
    103     }
    104 
    105     private void doTestToUnicode(String src, String expected, int options, Object expectedException)
    106                 throws Exception{
    107 
    108         if (!IDNAReference.isReady()) {
    109             logln("Transliterator is not available on this environment.  Skipping doTestToUnicode.");
    110             return;
    111         }
    112 
    113         StringBuffer inBuf = new StringBuffer(src);
    114         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    115         try{
    116 
    117             StringBuffer out = IDNAReference.convertToUnicode(src,options);
    118             if(expected!=null && out != null && !out.toString().equals(expected)){
    119                 errln("convertToUnicode did not return expected result with options : "+ options +
    120                       " Expected: " + prettify(expected)+" Got: "+prettify(out));
    121             }
    122             if(expectedException!=null && !unassignedException.equals(expectedException)){
    123                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    124             }
    125         }catch(StringPrepParseException ex){
    126             if(expectedException == null || !ex.equals(expectedException)){
    127                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
    128             }
    129         }
    130         try{
    131 
    132             StringBuffer out = IDNAReference.convertToUnicode(inBuf,options);
    133             if(expected!=null && out != null && !out.toString().equals(expected)){
    134                errln("convertToUnicode did not return expected result with options : "+ options +
    135                      " Expected: " + prettify(expected)+" Got: "+out);
    136             }
    137             if(expectedException!=null && !unassignedException.equals(expectedException)){
    138                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    139             }
    140         }catch(StringPrepParseException ex){
    141             if(expectedException == null || !ex.equals(expectedException)){
    142                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
    143             }
    144         }
    145 
    146         try{
    147             StringBuffer out = IDNAReference.convertToUnicode(inIter,options);
    148             if(expected!=null && out != null && !out.toString().equals(expected)){
    149                errln("convertToUnicode did not return expected result with options : "+ options +
    150                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
    151             }
    152             if(expectedException!=null && !unassignedException.equals(expectedException)){
    153                 errln("Did not get the expected exception. The operation succeeded!");
    154             }
    155         }catch(StringPrepParseException ex){
    156             if(expectedException == null || !ex.equals(expectedException)){
    157                 errln("Did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
    158             }
    159         }
    160     }
    161 
    162     private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException)
    163                 throws Exception{
    164 
    165         if (!IDNAReference.isReady()) {
    166             logln("Transliterator is not available on this environment.  Skipping doTestIDNToUnicode.");
    167             return;
    168         }
    169 
    170         StringBuffer inBuf = new StringBuffer(src);
    171         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    172         try{
    173 
    174             StringBuffer out = IDNAReference.convertIDNToUnicode(src,options);
    175             if(expected!=null && out != null && !out.toString().equals(expected)){
    176                 errln("convertToUnicode did not return expected result with options : "+ options +
    177                       " Expected: " + prettify(expected)+" Got: "+prettify(out));
    178             }
    179             if(expectedException!=null && !unassignedException.equals(expectedException)){
    180                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    181             }
    182         }catch(StringPrepParseException ex){
    183             if(expectedException == null || !expectedException.equals(ex)){
    184                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    185             }
    186         }
    187         try{
    188             StringBuffer out = IDNAReference.convertIDNToUnicode(inBuf,options);
    189             if(expected!=null && out != null && !out.toString().equals(expected)){
    190                errln("convertToUnicode did not return expected result with options : "+ options +
    191                      " Expected: " + prettify(expected)+" Got: "+out);
    192             }
    193             if(expectedException!=null && !unassignedException.equals(expectedException)){
    194                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    195             }
    196         }catch(StringPrepParseException ex){
    197             if(expectedException == null || !expectedException.equals(ex)){
    198                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    199             }
    200         }
    201 
    202         try{
    203             StringBuffer out = IDNAReference.convertIDNToUnicode(inIter,options);
    204             if(expected!=null && out != null && !out.toString().equals(expected)){
    205                errln("convertToUnicode did not return expected result with options : "+ options +
    206                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
    207             }
    208             if(expectedException!=null && !unassignedException.equals(expectedException)){
    209                 errln("Did not get the expected exception. The operation succeeded!");
    210             }
    211         }catch(StringPrepParseException ex){
    212             if(expectedException == null || !expectedException.equals(ex)){
    213                 errln("Did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    214             }
    215         }
    216     }
    217     private void doTestToASCII(String src, String expected, int options, Object expectedException)
    218                 throws Exception{
    219 
    220         if (!IDNAReference.isReady()) {
    221             logln("Transliterator is not available on this environment.  Skipping doTestToASCII.");
    222             return;
    223         }
    224 
    225         StringBuffer inBuf = new StringBuffer(src);
    226         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    227         try{
    228 
    229             StringBuffer out = IDNAReference.convertToASCII(src,options);
    230             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    231                 errln("convertToASCII did not return expected result with options : "+ options +
    232                       " Expected: " + expected+" Got: "+out);
    233             }
    234             if(expectedException!=null && !unassignedException.equals(expectedException)){
    235                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    236             }
    237         }catch(StringPrepParseException ex){
    238             if(expectedException == null || !expectedException.equals(ex)){
    239                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    240             }
    241         }
    242 
    243         try{
    244             StringBuffer out = IDNAReference.convertToASCII(inBuf,options);
    245             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    246                errln("convertToASCII did not return expected result with options : "+ options +
    247                      " Expected: " + expected+" Got: "+out);
    248             }
    249             if(expectedException!=null && !unassignedException.equals(expectedException)){
    250                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    251             }
    252         }catch(StringPrepParseException ex){
    253             if(expectedException == null || !expectedException.equals(ex)){
    254                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    255             }
    256         }
    257 
    258         try{
    259             StringBuffer out = IDNAReference.convertToASCII(inIter,options);
    260             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    261                errln("convertToASCII did not return expected result with options : "+ options +
    262                      " Expected: " + expected+" Got: "+ out);
    263             }
    264             if(expectedException!=null && !unassignedException.equals(expectedException)){
    265                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    266             }
    267         }catch(StringPrepParseException ex){
    268             if(expectedException == null || !expectedException.equals(ex)){
    269                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    270             }
    271         }
    272     }
    273     private void doTestIDNToASCII(String src, String expected, int options, Object expectedException)
    274                 throws Exception{
    275 
    276         if (!IDNAReference.isReady()) {
    277             logln("Transliterator is not available on this environment.  Skipping doTestIDNToASCII.");
    278             return;
    279         }
    280 
    281         StringBuffer inBuf = new StringBuffer(src);
    282         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    283         try{
    284 
    285             StringBuffer out = IDNAReference.convertIDNToASCII(src,options);
    286             if(expected!=null && out != null && !out.toString().equals(expected)){
    287                 errln("convertToIDNAReferenceASCII did not return expected result with options : "+ options +
    288                       " Expected: " + expected+" Got: "+out);
    289             }
    290             if(expectedException!=null && !unassignedException.equals(expectedException)){
    291                 errln("convertToIDNAReferenceASCII did not get the expected exception. The operation succeeded!");
    292             }
    293         }catch(StringPrepParseException ex){
    294             if(expectedException == null || !ex.equals(expectedException)){
    295                 errln("convertToIDNAReferenceASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    296             }
    297         }
    298         try{
    299             StringBuffer out = IDNAReference.convertIDNtoASCII(inBuf,options);
    300             if(expected!=null && out != null && !out.toString().equals(expected)){
    301                errln("convertToIDNAReferenceASCII did not return expected result with options : "+ options +
    302                      " Expected: " + expected+" Got: "+out);
    303             }
    304             if(expectedException!=null && !unassignedException.equals(expectedException)){
    305                 errln("convertToIDNAReferenceSCII did not get the expected exception. The operation succeeded!");
    306             }
    307         }catch(StringPrepParseException ex){
    308             if(expectedException == null || !ex.equals(expectedException)){
    309                 errln("convertToIDNAReferenceSCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    310             }
    311         }
    312 
    313         try{
    314             StringBuffer out = IDNAReference.convertIDNtoASCII(inIter,options);
    315             if(expected!=null && out != null && !out.toString().equals(expected)){
    316                errln("convertIDNToASCII did not return expected result with options : "+ options +
    317                      " Expected: " + expected+" Got: "+ out);
    318             }
    319 
    320             if(expectedException!=null && !unassignedException.equals(expectedException)){
    321                 errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
    322             }
    323         }catch(StringPrepParseException ex){
    324             if(expectedException == null || !ex.equals(expectedException)){
    325                 errln("convertIDNToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    326             }
    327         }
    328     }
    329     @Test
    330     public void TestConformance()throws Exception{
    331         try{
    332             for(int i=0; i<TestData.conformanceTestCases.length;i++){
    333 
    334                 TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
    335                 if(testCase.expected != null){
    336                     //Test toASCII
    337                     doTestToASCII(testCase.input,testCase.output,IDNAReference.DEFAULT,testCase.expected);
    338                     doTestToASCII(testCase.input,testCase.output,IDNAReference.ALLOW_UNASSIGNED,testCase.expected);
    339                 }
    340                 //Test toUnicode
    341                 //doTestToUnicode(testCase.input,testCase.output,IDNAReference.DEFAULT,testCase.expected);
    342             }
    343         }catch(java.lang.ExceptionInInitializerError ex){
    344             warnln("Could not load NamePrepTransform data");
    345         }catch(java.lang.NoClassDefFoundError ex){
    346             warnln("Could not load NamePrepTransform data");
    347         }
    348     }
    349     @Test
    350     public void TestNamePrepConformance() throws Exception{
    351         try{
    352             NamePrepTransform namePrep = NamePrepTransform.getInstance();
    353             if (!namePrep.isReady()) {
    354                 logln("Transliterator is not available on this environment.");
    355                 return;
    356             }
    357             for(int i=0; i<TestData.conformanceTestCases.length;i++){
    358                 TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
    359                 UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
    360                 try{
    361                     StringBuffer output = namePrep.prepare(iter,NamePrepTransform.NONE);
    362                     if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
    363                         errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
    364                               " Got: "+ prettify(output) );
    365                     }
    366                     if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
    367                         errln("Did not get the expected exception. The operation succeeded!");
    368                     }
    369                 }catch(StringPrepParseException ex){
    370                     if(testCase.expected == null || !ex.equals(testCase.expected)){
    371                         errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
    372                     }
    373                 }
    374 
    375                 try{
    376                     iter.setToStart();
    377                     StringBuffer output = namePrep.prepare(iter,NamePrepTransform.ALLOW_UNASSIGNED);
    378                     if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
    379                         errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
    380                               " Got: "+ prettify(output) );
    381                     }
    382                     if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
    383                         errln("Did not get the expected exception. The operation succeeded!");
    384                     }
    385                 }catch(StringPrepParseException ex){
    386                     if(testCase.expected == null || !ex.equals(testCase.expected)){
    387                         errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
    388                     }
    389                 }
    390             }
    391         }catch(java.lang.ExceptionInInitializerError e){
    392             warnln("Could not load NamePrepTransformData");
    393         }catch(java.lang.NoClassDefFoundError ex){
    394             warnln("Could not load NamePrepTransform data");
    395         }
    396 
    397     }
    398     @Test
    399     public void TestErrorCases() throws Exception{
    400         try{
    401             for(int i=0; i < TestData.errorCases.length; i++){
    402                 TestData.ErrorCase errCase = TestData.errorCases[i];
    403                 if(errCase.testLabel==true){
    404                     // Test ToASCII
    405                     doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.DEFAULT,errCase.expected);
    406                     doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.ALLOW_UNASSIGNED,errCase.expected);
    407                     if(errCase.useSTD3ASCIIRules){
    408                         doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.USE_STD3_RULES,errCase.expected);
    409                     }
    410                 }
    411                 if(errCase.useSTD3ASCIIRules!=true){
    412 
    413                     // Test IDNToASCII
    414                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.DEFAULT,errCase.expected);
    415                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.ALLOW_UNASSIGNED,errCase.expected);
    416 
    417                 }else{
    418                     doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNAReference.USE_STD3_RULES,errCase.expected);
    419                 }
    420 
    421                 //TestToUnicode
    422                 if(errCase.testToUnicode==true){
    423                     if(errCase.useSTD3ASCIIRules!=true){
    424                         // Test IDNToUnicode
    425                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.DEFAULT,errCase.expected);
    426                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.ALLOW_UNASSIGNED,errCase.expected);
    427 
    428                     }else{
    429                         doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNAReference.USE_STD3_RULES,errCase.expected);
    430                     }
    431                 }
    432             }
    433         }catch(java.lang.ExceptionInInitializerError ex){
    434             warnln("Could not load NamePrepTransform data");
    435         }catch(java.lang.NoClassDefFoundError ex){
    436             warnln("Could not load NamePrepTransform data");
    437         }
    438     }
    439     private void doTestCompare(String s1, String s2, boolean isEqual){
    440 
    441         if (!IDNAReference.isReady()) {
    442             logln("Transliterator is not available on this environment.  Skipping doTestCompare.");
    443             return;
    444         }
    445 
    446         try{
    447             int retVal = IDNAReference.compare(s1,s2,IDNAReference.DEFAULT);
    448             if(isEqual==true && retVal != 0){
    449                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    450                       " s2: "+prettify(s2));
    451             }
    452             retVal = IDNAReference.compare(new StringBuffer(s1), new StringBuffer(s2), IDNAReference.DEFAULT);
    453             if(isEqual==true && retVal != 0){
    454                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    455                      " s2: "+prettify(s2));
    456             }
    457             retVal = IDNAReference.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNAReference.DEFAULT);
    458             if(isEqual==true && retVal != 0){
    459                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    460                      " s2: "+prettify(s2));
    461             }
    462         }catch(Exception e){
    463             e.printStackTrace();
    464             errln("Unexpected exception thrown by IDNAReference.compare");
    465         }
    466 
    467         try{
    468             int retVal = IDNAReference.compare(s1,s2,IDNAReference.ALLOW_UNASSIGNED);
    469             if(isEqual==true && retVal != 0){
    470                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    471                       " s2: "+prettify(s2));
    472             }
    473             retVal = IDNAReference.compare(new StringBuffer(s1), new StringBuffer(s2), IDNAReference.ALLOW_UNASSIGNED);
    474             if(isEqual==true && retVal != 0){
    475                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    476                      " s2: "+prettify(s2));
    477             }
    478             retVal = IDNAReference.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNAReference.ALLOW_UNASSIGNED);
    479             if(isEqual==true && retVal != 0){
    480                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    481                      " s2: "+prettify(s2));
    482             }
    483         }catch(Exception e){
    484             errln("Unexpected exception thrown by IDNAReference.compare");
    485         }
    486     }
    487     @Test
    488     public void TestCompare() throws Exception{
    489         String www = "www.";
    490         String com = ".com";
    491         StringBuffer source = new StringBuffer(www);
    492         StringBuffer uni0   = new StringBuffer(www);
    493         StringBuffer uni1   = new StringBuffer(www);
    494         StringBuffer ascii0 = new StringBuffer(www);
    495         StringBuffer ascii1 = new StringBuffer(www);
    496 
    497         uni0.append(TestData.unicodeIn[0]);
    498         uni0.append(com);
    499 
    500         uni1.append(TestData.unicodeIn[1]);
    501         uni1.append(com);
    502 
    503         ascii0.append(TestData.asciiIn[0]);
    504         ascii0.append(com);
    505 
    506         ascii1.append(TestData.asciiIn[1]);
    507         ascii1.append(com);
    508         try{
    509             for(int i=0;i< TestData.unicodeIn.length; i++){
    510 
    511                 // for every entry in unicodeIn array
    512                 // prepend www. and append .com
    513                 source.setLength(4);
    514                 source.append(TestData.unicodeIn[i]);
    515                 source.append(com);
    516 
    517                 // a) compare it with itself
    518                 doTestCompare(source.toString(),source.toString(),true);
    519 
    520                 // b) compare it with asciiIn equivalent
    521                 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
    522 
    523                 // c) compare it with unicodeIn not equivalent
    524                 if(i==0){
    525                     doTestCompare(source.toString(), uni1.toString(), false);
    526                 }else{
    527                     doTestCompare(source.toString(),uni0.toString(), false);
    528                 }
    529                 // d) compare it with asciiIn not equivalent
    530                 if(i==0){
    531                     doTestCompare(source.toString(),ascii1.toString(), false);
    532                 }else{
    533                     doTestCompare(source.toString(),ascii0.toString(), false);
    534                 }
    535             }
    536         }catch(java.lang.ExceptionInInitializerError ex){
    537             warnln("Could not load NamePrepTransform data");
    538         }catch(java.lang.NoClassDefFoundError ex){
    539             warnln("Could not load NamePrepTransform data");
    540         }
    541     }
    542 
    543     //  test and ascertain
    544     //  func(func(func(src))) == func(src)
    545      private void doTestChainingToASCII(String source) throws Exception {
    546 
    547         if (!IDNAReference.isReady()) {
    548             logln("Transliterator is not available on this environment.  Skipping doTestChainingToASCII.");
    549             return;
    550         }
    551 
    552         StringBuffer expected;
    553         StringBuffer chained;
    554 
    555         // test convertIDNToASCII
    556         expected = IDNAReference.convertIDNToASCII(source,IDNAReference.DEFAULT);
    557         chained = expected;
    558         for(int i=0; i< 4; i++){
    559             chained = IDNAReference.convertIDNtoASCII(chained,IDNAReference.DEFAULT);
    560         }
    561         if(!expected.toString().equals(chained.toString())){
    562             errln("Chaining test failed for convertIDNToASCII");
    563         }
    564         // test convertIDNToA
    565         expected = IDNAReference.convertToASCII(source,IDNAReference.DEFAULT);
    566         chained = expected;
    567         for(int i=0; i< 4; i++){
    568             chained = IDNAReference.convertToASCII(chained,IDNAReference.DEFAULT);
    569         }
    570         if(!expected.toString().equals(chained.toString())){
    571             errln("Chaining test failed for convertToASCII");
    572         }
    573 
    574     }
    575     //  test and ascertain
    576     //  func(func(func(src))) == func(src)
    577     public void doTestChainingToUnicode(String source) throws Exception {
    578 
    579         if (!IDNAReference.isReady()) {
    580             logln("Transliterator is not available on this environment.  Skipping doTestChainingToUnicode.");
    581             return;
    582         }
    583 
    584         StringBuffer expected;
    585         StringBuffer chained;
    586 
    587         // test convertIDNToUnicode
    588         expected = IDNAReference.convertIDNToUnicode(source,IDNAReference.DEFAULT);
    589         chained = expected;
    590         for(int i=0; i< 4; i++){
    591             chained = IDNAReference.convertIDNToUnicode(chained,IDNAReference.DEFAULT);
    592         }
    593         if(!expected.toString().equals(chained.toString())){
    594             errln("Chaining test failed for convertIDNToUnicode");
    595         }
    596         // test convertIDNToA
    597         expected = IDNAReference.convertToUnicode(source,IDNAReference.DEFAULT);
    598         chained = expected;
    599         for(int i=0; i< 4; i++){
    600             chained = IDNAReference.convertToUnicode(chained,IDNAReference.DEFAULT);
    601         }
    602         if(!expected.toString().equals(chained.toString())){
    603             errln("Chaining test failed for convertToUnicode");
    604         }
    605 
    606     }
    607     @Test
    608     public void TestChaining() throws Exception{
    609         try{
    610             for(int i=0; i< TestData.unicodeIn.length; i++){
    611                 doTestChainingToASCII(new String(TestData.unicodeIn[i]));
    612             }
    613             for(int i=0; i< TestData.asciiIn.length; i++){
    614                 doTestChainingToUnicode(TestData.asciiIn[i]);
    615             }
    616         }catch(java.lang.ExceptionInInitializerError ex){
    617             warnln("Could not load NamePrepTransform data");
    618         }catch(java.lang.NoClassDefFoundError ex){
    619             warnln("Could not load NamePrepTransform data");
    620         }
    621     }
    622     @Test
    623     public void TestRootLabelSeparator() throws Exception{
    624         String www = "www.";
    625         String com = ".com."; /*root label separator*/
    626         StringBuffer source = new StringBuffer(www);
    627         StringBuffer uni0   = new StringBuffer(www);
    628         StringBuffer uni1   = new StringBuffer(www);
    629         StringBuffer ascii0 = new StringBuffer(www);
    630         StringBuffer ascii1 = new StringBuffer(www);
    631 
    632         uni0.append(TestData.unicodeIn[0]);
    633         uni0.append(com);
    634 
    635         uni1.append(TestData.unicodeIn[1]);
    636         uni1.append(com);
    637 
    638         ascii0.append(TestData.asciiIn[0]);
    639         ascii0.append(com);
    640 
    641         ascii1.append(TestData.asciiIn[1]);
    642         ascii1.append(com);
    643         try{
    644             for(int i=0;i< TestData.unicodeIn.length; i++){
    645 
    646                 // for every entry in unicodeIn array
    647                 // prepend www. and append .com
    648                 source.setLength(4);
    649                 source.append(TestData.unicodeIn[i]);
    650                 source.append(com);
    651 
    652                 // a) compare it with itself
    653                 doTestCompare(source.toString(),source.toString(),true);
    654 
    655                 // b) compare it with asciiIn equivalent
    656                 doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
    657 
    658                 // c) compare it with unicodeIn not equivalent
    659                 if(i==0){
    660                     doTestCompare(source.toString(), uni1.toString(), false);
    661                 }else{
    662                     doTestCompare(source.toString(),uni0.toString(), false);
    663                 }
    664                 // d) compare it with asciiIn not equivalent
    665                 if(i==0){
    666                     doTestCompare(source.toString(),ascii1.toString(), false);
    667                 }else{
    668                     doTestCompare(source.toString(),ascii0.toString(), false);
    669                 }
    670             }
    671         }catch(java.lang.ExceptionInInitializerError ex){
    672             warnln("Could not load NamePrepTransform data");
    673         }catch(java.lang.NoClassDefFoundError ex){
    674             warnln("Could not load NamePrepTransform data");
    675         }
    676 
    677     }
    678 }
    679