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-2011, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8 */
      9 package com.ibm.icu.dev.test.stringprep;
     10 
     11 import java.util.Random;
     12 
     13 import org.junit.Ignore;
     14 import org.junit.Test;
     15 
     16 import com.ibm.icu.dev.test.TestFmwk;
     17 import com.ibm.icu.impl.Utility;
     18 import com.ibm.icu.text.IDNA;
     19 import com.ibm.icu.text.StringPrep;
     20 import com.ibm.icu.text.StringPrepParseException;
     21 import com.ibm.icu.text.UCharacterIterator;
     22 import com.ibm.icu.text.UTF16;
     23 
     24 /**
     25  * @author ram
     26  */
     27 public class TestIDNA extends TestFmwk {
     28     private StringPrepParseException unassignedException = new StringPrepParseException("",StringPrepParseException.UNASSIGNED_ERROR);
     29 
     30     @Test
     31     public void TestToUnicode() throws Exception{
     32         for(int i=0; i<TestData.asciiIn.length; i++){
     33             // test StringBuffer toUnicode
     34             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.DEFAULT, null);
     35             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.ALLOW_UNASSIGNED, null);
     36             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES, null);
     37             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES|IDNA.ALLOW_UNASSIGNED, null);
     38 
     39         }
     40     }
     41 
     42     @Test
     43     public void TestToASCII() throws Exception{
     44         for(int i=0; i<TestData.asciiIn.length; i++){
     45             // test StringBuffer toUnicode
     46             doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNA.DEFAULT, null);
     47             doTestToASCII(new String(TestData.unicodeIn[i]),TestData.asciiIn[i],IDNA.ALLOW_UNASSIGNED, null);
     48             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES, null);
     49             doTestToUnicode(TestData.asciiIn[i],new String(TestData.unicodeIn[i]),IDNA.USE_STD3_RULES|IDNA.ALLOW_UNASSIGNED, null);
     50 
     51         }
     52     }
     53 
     54     @Test
     55     public void TestIDNToASCII() throws Exception{
     56         for(int i=0; i<TestData.domainNames.length; i++){
     57             doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.DEFAULT, null);
     58             doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED, null);
     59             doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.USE_STD3_RULES, null);
     60             doTestIDNToASCII(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED|IDNA.USE_STD3_RULES, null);
     61         }
     62 
     63         for(int i=0; i<TestData.domainNames1Uni.length; i++){
     64             doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNA.DEFAULT, null);
     65             doTestIDNToASCII(TestData.domainNames1Uni[i],TestData.domainNamesToASCIIOut[i],IDNA.ALLOW_UNASSIGNED, null);
     66         }
     67     }
     68     @Test
     69     public void TestIDNToUnicode() throws Exception{
     70         for(int i=0; i<TestData.domainNames.length; i++){
     71             doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.DEFAULT, null);
     72             doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED, null);
     73             doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.USE_STD3_RULES, null);
     74             doTestIDNToUnicode(TestData.domainNames[i],TestData.domainNames[i],IDNA.ALLOW_UNASSIGNED|IDNA.USE_STD3_RULES, null);
     75         }
     76         for(int i=0; i<TestData.domainNamesToASCIIOut.length; i++){
     77             doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNA.DEFAULT, null);
     78             doTestIDNToUnicode(TestData.domainNamesToASCIIOut[i],TestData.domainNamesToUnicodeOut[i],IDNA.ALLOW_UNASSIGNED, null);
     79         }
     80     }
     81 
     82     private void doTestToUnicode(String src, String expected, int options, Object expectedException)
     83                 throws Exception{
     84         StringBuffer inBuf = new StringBuffer(src);
     85         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
     86         try{
     87 
     88             StringBuffer out = IDNA.convertToUnicode(src,options);
     89             if(expected!=null && out != null && !out.toString().equals(expected)){
     90                 errln("convertToUnicode did not return expected result with options : "+ options +
     91                       " Expected: " + prettify(expected)+" Got: "+prettify(out));
     92             }
     93             if(expectedException!=null && !unassignedException.equals(expectedException)){
     94                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
     95             }
     96         }catch(StringPrepParseException ex){
     97             if(expectedException == null || !ex.equals(expectedException)){
     98                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
     99             }
    100         }
    101         try{
    102 
    103             StringBuffer out = IDNA.convertToUnicode(inBuf,options);
    104             if(expected!=null && out != null && !out.toString().equals(expected)){
    105                errln("convertToUnicode did not return expected result with options : "+ options +
    106                      " Expected: " + prettify(expected)+" Got: "+out);
    107             }
    108             if(expectedException!=null && !unassignedException.equals(expectedException)){
    109                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    110             }
    111         }catch(StringPrepParseException ex){
    112             if(expectedException == null || !ex.equals(expectedException)){
    113                 errln("convertToUnicode did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
    114             }
    115         }
    116 
    117         try{
    118             StringBuffer out = IDNA.convertToUnicode(inIter,options);
    119             if(expected!=null && out != null && !out.toString().equals(expected)){
    120                errln("convertToUnicode did not return expected result with options : "+ options +
    121                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
    122             }
    123             if(expectedException!=null && !unassignedException.equals(expectedException)){
    124                 errln("Did not get the expected exception. The operation succeeded!");
    125             }
    126         }catch(StringPrepParseException ex){
    127             if(expectedException == null || !ex.equals(expectedException)){
    128                 errln("Did not get the expected exception for source: " + prettify(src) +" Got:  "+ ex.toString());
    129             }
    130         }
    131     }
    132 
    133     private void doTestIDNToUnicode(String src, String expected, int options, Object expectedException)
    134                 throws Exception{
    135         StringBuffer inBuf = new StringBuffer(src);
    136         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    137         try{
    138 
    139             StringBuffer out = IDNA.convertIDNToUnicode(src,options);
    140             if(expected!=null && out != null && !out.toString().equals(expected)){
    141                 errln("convertToUnicode did not return expected result with options : "+ options +
    142                       " Expected: " + prettify(expected)+" Got: "+prettify(out));
    143             }
    144             if(expectedException!=null && !unassignedException.equals(expectedException)){
    145                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    146             }
    147         }catch(StringPrepParseException ex){
    148             if(expectedException == null || !expectedException.equals(ex)){
    149                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    150             }
    151         }
    152         try{
    153             StringBuffer out = IDNA.convertIDNToUnicode(inBuf,options);
    154             if(expected!=null && out != null && !out.toString().equals(expected)){
    155                errln("convertToUnicode did not return expected result with options : "+ options +
    156                      " Expected: " + prettify(expected)+" Got: "+out);
    157             }
    158             if(expectedException!=null && !unassignedException.equals(expectedException)){
    159                 errln("convertToUnicode did not get the expected exception. The operation succeeded!");
    160             }
    161         }catch(StringPrepParseException ex){
    162             if(expectedException == null || !expectedException.equals(ex)){
    163                 errln("convertToUnicode did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    164             }
    165         }
    166 
    167         try{
    168             StringBuffer out = IDNA.convertIDNToUnicode(inIter,options);
    169             if(expected!=null && out != null && !out.toString().equals(expected)){
    170                errln("convertToUnicode did not return expected result with options : "+ options +
    171                      " Expected: " + prettify(expected)+" Got: "+prettify(out));
    172             }
    173             if(expectedException!=null && !unassignedException.equals(expectedException)){
    174                 errln("Did not get the expected exception. The operation succeeded!");
    175             }
    176         }catch(StringPrepParseException ex){
    177             if(expectedException == null || !expectedException.equals(ex)){
    178                 errln("Did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    179             }
    180         }
    181     }
    182     private void doTestToASCII(String src, String expected, int options, Object expectedException)
    183                 throws Exception{
    184         StringBuffer inBuf = new StringBuffer(src);
    185         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    186         try{
    187 
    188             StringBuffer out = IDNA.convertToASCII(src,options);
    189             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    190                 errln("convertToASCII did not return expected result with options : "+ options +
    191                       " Expected: " + expected+" Got: "+out);
    192             }
    193             if(expectedException!=null && !unassignedException.equals(expectedException)){
    194                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    195             }
    196         }catch(StringPrepParseException ex){
    197             if(expectedException == null || !expectedException.equals(ex)){
    198                 errln("convertToASCII did not get the expected exception for source: " +src +"\n Got:  "+ ex.toString() +"\n Expected: " +ex.toString());
    199             }
    200         }
    201 
    202         try{
    203             StringBuffer out = IDNA.convertToASCII(inBuf,options);
    204             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    205                errln("convertToASCII did not return expected result with options : "+ options +
    206                      " Expected: " + expected+" Got: "+out);
    207             }
    208             if(expectedException!=null && !unassignedException.equals(expectedException)){
    209                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    210             }
    211         }catch(StringPrepParseException ex){
    212             if(expectedException == null || !expectedException.equals(ex)){
    213                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    214             }
    215         }
    216 
    217         try{
    218             StringBuffer out = IDNA.convertToASCII(inIter,options);
    219             if(!unassignedException.equals(expectedException) && expected!=null && out != null && expected!=null && out != null && !out.toString().equals(expected.toLowerCase())){
    220                errln("convertToASCII did not return expected result with options : "+ options +
    221                      " Expected: " + expected+" Got: "+ out);
    222             }
    223             if(expectedException!=null && !unassignedException.equals(expectedException)){
    224                 errln("convertToASCII did not get the expected exception. The operation succeeded!");
    225             }
    226         }catch(StringPrepParseException ex){
    227             if(expectedException == null || !expectedException.equals(ex)){
    228                 errln("convertToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    229             }
    230         }
    231     }
    232     private void doTestIDNToASCII(String src, String expected, int options, Object expectedException)
    233                 throws Exception{
    234         StringBuffer inBuf = new StringBuffer(src);
    235         UCharacterIterator inIter = UCharacterIterator.getInstance(src);
    236         try{
    237 
    238             StringBuffer out = IDNA.convertIDNToASCII(src,options);
    239             if(expected!=null && out != null && !out.toString().equals(expected)){
    240                 errln("convertToIDNASCII did not return expected result with options : "+ options +
    241                       " Expected: " + expected+" Got: "+out);
    242             }
    243             if(expectedException!=null && !unassignedException.equals(expectedException)){
    244                 errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
    245             }
    246         }catch(StringPrepParseException ex){
    247             if(expectedException == null || !ex.equals(expectedException)){
    248                 errln("convertToIDNASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    249             }
    250         }
    251         try{
    252             StringBuffer out = IDNA.convertIDNToASCII(inBuf,options);
    253             if(expected!=null && out != null && !out.toString().equals(expected)){
    254                errln("convertToIDNASCII did not return expected result with options : "+ options +
    255                      " Expected: " + expected+" Got: "+out);
    256             }
    257             if(expectedException!=null && !unassignedException.equals(expectedException)){
    258                 errln("convertToIDNASCII did not get the expected exception. The operation succeeded!");
    259             }
    260         }catch(StringPrepParseException ex){
    261             if(expectedException == null || !ex.equals(expectedException)){
    262                 errln("convertToIDNASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    263             }
    264         }
    265 
    266         try{
    267             StringBuffer out = IDNA.convertIDNToASCII(inIter,options);
    268             if(expected!=null && out != null && !out.toString().equals(expected)){
    269                errln("convertIDNToASCII did not return expected result with options : "+ options +
    270                      " Expected: " + expected+" Got: "+ out);
    271             }
    272 
    273             if(expectedException!=null && !unassignedException.equals(expectedException)){
    274                 errln("convertIDNToASCII did not get the expected exception. The operation succeeded!");
    275             }
    276         }catch(StringPrepParseException ex){
    277             if(expectedException == null || !ex.equals(expectedException)){
    278                 errln("convertIDNToASCII did not get the expected exception for source: " +src +" Got:  "+ ex.toString());
    279             }
    280         }
    281     }
    282     @Test
    283     public void TestConformance()throws Exception{
    284         for(int i=0; i<TestData.conformanceTestCases.length;i++){
    285 
    286             TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
    287             if(testCase.expected != null){
    288                 //Test toASCII
    289                 doTestToASCII(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
    290                 doTestToASCII(testCase.input,testCase.output,IDNA.ALLOW_UNASSIGNED,testCase.expected);
    291             }
    292             //Test toUnicode
    293             //doTestToUnicode(testCase.input,testCase.output,IDNA.DEFAULT,testCase.expected);
    294         }
    295     }
    296     @Test
    297     public void TestNamePrepConformance() throws Exception{
    298         StringPrep namePrep = StringPrep.getInstance(StringPrep.RFC3491_NAMEPREP);
    299         for(int i=0; i<TestData.conformanceTestCases.length;i++){
    300             TestData.ConformanceTestCase testCase = TestData.conformanceTestCases[i];
    301             UCharacterIterator iter = UCharacterIterator.getInstance(testCase.input);
    302             try{
    303                 StringBuffer output = namePrep.prepare(iter,StringPrep.DEFAULT);
    304                 if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
    305                     errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
    306                           " Got: "+ prettify(output) );
    307                 }
    308                 if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
    309                     errln("Did not get the expected exception. The operation succeeded!");
    310                 }
    311             }catch(StringPrepParseException ex){
    312                 if(testCase.expected == null || !ex.equals(testCase.expected)){
    313                     errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
    314                 }
    315             }
    316 
    317             try{
    318                 iter.setToStart();
    319                 StringBuffer output = namePrep.prepare(iter,StringPrep.ALLOW_UNASSIGNED);
    320                 if(testCase.output !=null && output!=null && !testCase.output.equals(output.toString())){
    321                     errln("Did not get the expected output. Expected: " + prettify(testCase.output)+
    322                           " Got: "+ prettify(output) );
    323                 }
    324                 if(testCase.expected!=null && !unassignedException.equals(testCase.expected)){
    325                     errln("Did not get the expected exception. The operation succeeded!");
    326                 }
    327             }catch(StringPrepParseException ex){
    328                 if(testCase.expected == null || !ex.equals(testCase.expected)){
    329                     errln("Did not get the expected exception for source: " +testCase.input +" Got:  "+ ex.toString());
    330                 }
    331             }
    332         }
    333 
    334     }
    335     @Test
    336     public void TestErrorCases() throws Exception{
    337         for(int i=0; i < TestData.errorCases.length; i++){
    338             TestData.ErrorCase errCase = TestData.errorCases[i];
    339             if(errCase.testLabel==true){
    340                 // Test ToASCII
    341                 doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.DEFAULT,errCase.expected);
    342                 doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.ALLOW_UNASSIGNED,errCase.expected);
    343                 if(errCase.useSTD3ASCIIRules){
    344                     doTestToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
    345                 }
    346             }
    347             if(errCase.useSTD3ASCIIRules!=true){
    348 
    349                 // Test IDNToASCII
    350                 doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.DEFAULT,errCase.expected);
    351                 doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.ALLOW_UNASSIGNED,errCase.expected);
    352 
    353             }else{
    354                 doTestIDNToASCII(new String(errCase.unicode),errCase.ascii,IDNA.USE_STD3_RULES,errCase.expected);
    355             }
    356             //TestToUnicode
    357             if(errCase.testToUnicode==true){
    358                 if(errCase.useSTD3ASCIIRules!=true){
    359                     // Test IDNToUnicode
    360                     doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.DEFAULT,errCase.expected);
    361                     doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.ALLOW_UNASSIGNED,errCase.expected);
    362 
    363                 }else{
    364                     doTestIDNToUnicode(errCase.ascii,new String(errCase.unicode),IDNA.USE_STD3_RULES,errCase.expected);
    365                 }
    366             }
    367         }
    368     }
    369     private void doTestCompare(String s1, String s2, boolean isEqual){
    370         try{
    371             int retVal = IDNA.compare(s1,s2,IDNA.DEFAULT);
    372             if(isEqual==true && retVal != 0){
    373                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    374                       " s2: "+prettify(s2));
    375             }
    376             retVal = IDNA.compare(new StringBuffer(s1), new StringBuffer(s2), IDNA.DEFAULT);
    377             if(isEqual==true && retVal != 0){
    378                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    379                      " s2: "+prettify(s2));
    380             }
    381             retVal = IDNA.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNA.DEFAULT);
    382             if(isEqual==true && retVal != 0){
    383                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    384                      " s2: "+prettify(s2));
    385             }
    386         }catch(Exception e){
    387             e.printStackTrace();
    388             errln("Unexpected exception thrown by IDNA.compare");
    389         }
    390 
    391         try{
    392             int retVal = IDNA.compare(s1,s2,IDNA.ALLOW_UNASSIGNED);
    393             if(isEqual==true && retVal != 0){
    394                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    395                       " s2: "+prettify(s2));
    396             }
    397             retVal = IDNA.compare(new StringBuffer(s1), new StringBuffer(s2), IDNA.ALLOW_UNASSIGNED);
    398             if(isEqual==true && retVal != 0){
    399                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    400                      " s2: "+prettify(s2));
    401             }
    402             retVal = IDNA.compare(UCharacterIterator.getInstance(s1), UCharacterIterator.getInstance(s2), IDNA.ALLOW_UNASSIGNED);
    403             if(isEqual==true && retVal != 0){
    404                 errln("Did not get the expected result for s1: "+ prettify(s1)+
    405                      " s2: "+prettify(s2));
    406             }
    407         }catch(Exception e){
    408             errln("Unexpected exception thrown by IDNA.compare");
    409         }
    410     }
    411     @Test
    412     public void TestCompare() throws Exception{
    413         String www = "www.";
    414         String com = ".com";
    415         StringBuffer source = new StringBuffer(www);
    416         StringBuffer uni0   = new StringBuffer(www);
    417         StringBuffer uni1   = new StringBuffer(www);
    418         StringBuffer ascii0 = new StringBuffer(www);
    419         StringBuffer ascii1 = new StringBuffer(www);
    420 
    421         uni0.append(TestData.unicodeIn[0]);
    422         uni0.append(com);
    423 
    424         uni1.append(TestData.unicodeIn[1]);
    425         uni1.append(com);
    426 
    427         ascii0.append(TestData.asciiIn[0]);
    428         ascii0.append(com);
    429 
    430         ascii1.append(TestData.asciiIn[1]);
    431         ascii1.append(com);
    432 
    433         for(int i=0;i< TestData.unicodeIn.length; i++){
    434 
    435             // for every entry in unicodeIn array
    436             // prepend www. and append .com
    437             source.setLength(4);
    438             source.append(TestData.unicodeIn[i]);
    439             source.append(com);
    440 
    441             // a) compare it with itself
    442             doTestCompare(source.toString(),source.toString(),true);
    443 
    444             // b) compare it with asciiIn equivalent
    445             doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
    446 
    447             // c) compare it with unicodeIn not equivalent
    448             if(i==0){
    449                 doTestCompare(source.toString(), uni1.toString(), false);
    450             }else{
    451                 doTestCompare(source.toString(),uni0.toString(), false);
    452             }
    453             // d) compare it with asciiIn not equivalent
    454             if(i==0){
    455                 doTestCompare(source.toString(),ascii1.toString(), false);
    456             }else{
    457                 doTestCompare(source.toString(),ascii0.toString(), false);
    458             }
    459 
    460         }
    461     }
    462 
    463     //  test and ascertain
    464     //  func(func(func(src))) == func(src)
    465     private void doTestChainingToASCII(String source) throws Exception {
    466         StringBuffer expected;
    467         StringBuffer chained;
    468 
    469         // test convertIDNToASCII
    470         expected = IDNA.convertIDNToASCII(source,IDNA.DEFAULT);
    471         chained = expected;
    472         for(int i=0; i< 4; i++){
    473             chained = IDNA.convertIDNToASCII(chained,IDNA.DEFAULT);
    474         }
    475         if(!expected.toString().equals(chained.toString())){
    476             errln("Chaining test failed for convertIDNToASCII");
    477         }
    478         // test convertIDNToA
    479         expected = IDNA.convertToASCII(source,IDNA.DEFAULT);
    480         chained = expected;
    481         for(int i=0; i< 4; i++){
    482             chained = IDNA.convertToASCII(chained,IDNA.DEFAULT);
    483         }
    484         if(!expected.toString().equals(chained.toString())){
    485             errln("Chaining test failed for convertToASCII");
    486         }
    487     }
    488 
    489     //  test and ascertain
    490     //  func(func(func(src))) == func(src)
    491     private void doTestChainingToUnicode(String source) throws Exception {
    492         StringBuffer expected;
    493         StringBuffer chained;
    494 
    495         // test convertIDNToUnicode
    496         expected = IDNA.convertIDNToUnicode(source,IDNA.DEFAULT);
    497         chained = expected;
    498         for(int i=0; i< 4; i++){
    499             chained = IDNA.convertIDNToUnicode(chained,IDNA.DEFAULT);
    500         }
    501         if(!expected.toString().equals(chained.toString())){
    502             errln("Chaining test failed for convertIDNToUnicode");
    503         }
    504         // test convertIDNToA
    505         expected = IDNA.convertToUnicode(source,IDNA.DEFAULT);
    506         chained = expected;
    507         for(int i=0; i< 4; i++){
    508             chained = IDNA.convertToUnicode(chained,IDNA.DEFAULT);
    509         }
    510         if(!expected.toString().equals(chained.toString())){
    511             errln("Chaining test failed for convertToUnicode");
    512         }
    513     }
    514     @Test
    515     public void TestChaining() throws Exception{
    516         for(int i=0; i< TestData.asciiIn.length; i++){
    517             doTestChainingToUnicode(TestData.asciiIn[i]);
    518         }
    519         for(int i=0; i< TestData.unicodeIn.length; i++){
    520             doTestChainingToASCII(new String(TestData.unicodeIn[i]));
    521         }
    522     }
    523 
    524 
    525     /* IDNA RFC Says:
    526     A label is an individual part of a domain name.  Labels are usually
    527     shown separated by dots; for example, the domain name
    528     "www.example.com" is composed of three labels: "www", "example", and
    529     "com".  (The zero-length root label described in [STD13], which can
    530     be explicit as in "www.example.com." or implicit as in
    531     "www.example.com", is not considered a label in this specification.)
    532     */
    533     @Test
    534     public void TestRootLabelSeparator() throws Exception{
    535         String www = "www.";
    536         String com = ".com."; //root label separator
    537         StringBuffer source = new StringBuffer(www);
    538         StringBuffer uni0   = new StringBuffer(www);
    539         StringBuffer uni1   = new StringBuffer(www);
    540         StringBuffer ascii0 = new StringBuffer(www);
    541         StringBuffer ascii1 = new StringBuffer(www);
    542 
    543         uni0.append(TestData.unicodeIn[0]);
    544         uni0.append(com);
    545 
    546         uni1.append(TestData.unicodeIn[1]);
    547         uni1.append(com);
    548 
    549         ascii0.append(TestData.asciiIn[0]);
    550         ascii0.append(com);
    551 
    552         ascii1.append(TestData.asciiIn[1]);
    553         ascii1.append(com);
    554 
    555         for(int i=0;i< TestData.unicodeIn.length; i++){
    556 
    557             // for every entry in unicodeIn array
    558             // prepend www. and append .com
    559             source.setLength(4);
    560             source.append(TestData.unicodeIn[i]);
    561             source.append(com);
    562 
    563             // a) compare it with itself
    564             doTestCompare(source.toString(),source.toString(),true);
    565 
    566             // b) compare it with asciiIn equivalent
    567             doTestCompare(source.toString(),www+TestData.asciiIn[i]+com,true);
    568 
    569             // c) compare it with unicodeIn not equivalent
    570             if(i==0){
    571                 doTestCompare(source.toString(), uni1.toString(), false);
    572             }else{
    573                 doTestCompare(source.toString(),uni0.toString(), false);
    574             }
    575             // d) compare it with asciiIn not equivalent
    576             if(i==0){
    577                 doTestCompare(source.toString(),ascii1.toString(), false);
    578             }else{
    579                 doTestCompare(source.toString(),ascii0.toString(), false);
    580             }
    581 
    582         }
    583 
    584     }
    585 
    586 
    587     private static final int loopCount = 100;
    588     private static final int maxCharCount = 15;
    589    // private static final int maxCodePoint = 0x10ffff;
    590     private Random random = null;
    591 
    592     /**
    593      * Return a random integer i where 0 <= i < n.
    594      * A special function that gets random codepoints from planes 0,1,2 and 14
    595      */
    596     private int rand_uni()
    597     {
    598        int retVal = (int)(random.nextLong()& 0x3FFFF);
    599        if(retVal >= 0x30000){
    600            retVal+=0xB0000;
    601        }
    602        return retVal;
    603     }
    604 
    605     private int randi(int n){
    606         return (random.nextInt(0x7fff) % (n+1));
    607     }
    608 
    609     private StringBuffer getTestSource(StringBuffer fillIn) {
    610         // use uniform seed value from the framework
    611         if(random==null){
    612             random = createRandom();
    613         }
    614         int i = 0;
    615         int charCount = (randi(maxCharCount) + 1);
    616         while (i <charCount ) {
    617             int codepoint = rand_uni();
    618             if(codepoint == 0x0000){
    619                 continue;
    620             }
    621             UTF16.append(fillIn, codepoint);
    622             i++;
    623         }
    624         return fillIn;
    625 
    626     }
    627 
    628     // TODO(junit): turned off because not running before
    629     @Ignore
    630     @Test
    631     public void MonkeyTest() throws Exception{
    632          StringBuffer source = new StringBuffer();
    633          /* do the monkey test   */
    634          for(int i=0; i<loopCount; i++){
    635              source.setLength(0);
    636              getTestSource(source);
    637              doTestCompareReferenceImpl(source);
    638          }
    639 
    640          // test string with embedded null
    641          source.append( "\\u0000\\u2109\\u3E1B\\U000E65CA\\U0001CAC5" );
    642 
    643          source = new StringBuffer(Utility.unescape(source.toString()));
    644          doTestCompareReferenceImpl(source);
    645 
    646          //StringBuffer src = new StringBuffer(Utility.unescape("\\uDEE8\\U000E228C\\U0002EE8E\\U000E6350\\U00024DD9\u4049\\U000E0DE4\\U000E448C\\U0001869B\\U000E3380\\U00016A8E\\U000172D5\\U0001C408\\U000E9FB5"));
    647          //doTestCompareReferenceImpl(src);
    648 
    649          //test deletion of code points
    650          source = new StringBuffer(Utility.unescape("\\u043f\\u00AD\\u034f\\u043e\\u0447\\u0435\\u043c\\u0443\\u0436\\u0435\\u043e\\u043d\\u0438\\u043d\\u0435\\u0433\\u043e\\u0432\\u043e\\u0440\\u044f\\u0442\\u043f\\u043e\\u0440\\u0443\\u0441\\u0441\\u043a\\u0438"));
    651          StringBuffer expected = new StringBuffer("xn--b1abfaaepdrnnbgefbadotcwatmq2g4l");
    652          doTestCompareReferenceImpl(source);
    653          doTestToASCII(source.toString(),expected.toString(), IDNA.DEFAULT, null);
    654     }
    655 
    656     private StringBuffer _doTestCompareReferenceImpl(StringBuffer src, boolean toASCII, int options) {
    657         String refIDNAName = toASCII ? "IDNAReference.convertToASCII" : "IDNAReference.convertToUnicode";
    658         String uIDNAName = toASCII ? "IDNA.convertToASCII" : "IDNA.convertToUnicode";
    659 
    660         logln("Comparing " + refIDNAName + " with " + uIDNAName + " for input: "
    661                 + prettify(src) + " with options: " + options);
    662 
    663         StringBuffer exp = null;
    664         int expStatus = -1;
    665         try {
    666             exp = toASCII ? IDNAReference.convertToASCII(src, options) : IDNAReference.convertToUnicode(src, options);
    667         } catch (StringPrepParseException e) {
    668             expStatus = e.getError();
    669         }
    670 
    671         StringBuffer got = null;
    672         int gotStatus = -1;
    673         try {
    674             got = toASCII ? IDNA.convertToASCII(src, options) : IDNA.convertToUnicode(src, options);
    675         } catch (StringPrepParseException e) {
    676             gotStatus = e.getError();
    677         }
    678 
    679         if (expStatus != gotStatus) {
    680             errln("Did not get the expected status while comparing " + refIDNAName + " with " + uIDNAName
    681                     + " Expected: " + expStatus
    682                     + " Got: " + gotStatus
    683                     + " for Source: "+ prettify(src)
    684                     + " Options: " + options);
    685         } else {
    686             // now we know that both implementation yielded same status
    687             if (gotStatus == -1) {
    688                 // compare the outputs
    689                 if (!got.toString().equals(exp.toString())) {
    690                     errln("Did not get the expected output while comparing " + refIDNAName + " with " + uIDNAName
    691                             + " Expected: " + exp
    692                             + " Got: " + got
    693                             + " for Source: "+ prettify(src)
    694                             + " Options: " + options);
    695                 }
    696             } else {
    697                 logln("Got the same error while comparing " + refIDNAName + " with " + uIDNAName
    698                         +" for input: " + prettify(src) + " with options: " + options);
    699             }
    700         }
    701 
    702         return exp;
    703     }
    704 
    705     private void doTestCompareReferenceImpl(StringBuffer src) throws Exception{
    706         // test toASCII
    707         src.setLength(0);
    708         src.append("[");
    709         StringBuffer asciiLabel = _doTestCompareReferenceImpl(src, true, IDNA.ALLOW_UNASSIGNED);
    710         _doTestCompareReferenceImpl(src, true, IDNA.DEFAULT);
    711         _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES);
    712         _doTestCompareReferenceImpl(src, true, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
    713 
    714         if (asciiLabel != null) {
    715             // test toUnicode
    716             _doTestCompareReferenceImpl(src, false, IDNA.ALLOW_UNASSIGNED);
    717             _doTestCompareReferenceImpl(src, false, IDNA.DEFAULT);
    718             _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES);
    719             _doTestCompareReferenceImpl(src, false, IDNA.USE_STD3_RULES | IDNA.ALLOW_UNASSIGNED);
    720         }
    721     }
    722 
    723     @Test
    724     public void TestCompareRefImpl() throws Exception {
    725         for (int i = 65; i < 0x10FFFF; i++) {
    726             StringBuffer src = new StringBuffer();
    727             if (isQuick() == true && i > 0x0FFF) {
    728                 return;
    729             }
    730             if (i == 0x30000) {
    731                 // jump to E0000, no characters assigned in plain 3 to plain 13 as of Unicode 6.0
    732                 i = 0xE0000;
    733             }
    734             UTF16.append(src, i);
    735             doTestCompareReferenceImpl(src);
    736         }
    737     }
    738 
    739     @Test
    740     public void TestJB4490(){
    741         String[] in = new String[]{
    742                 "\u00F5\u00dE\u00dF\u00dD",
    743                 "\uFB00\uFB01"
    744                };
    745         for ( int i=0; i< in.length; i++){
    746             try{
    747                 String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
    748                 try{
    749                     String unicode = IDNA.convertToUnicode(ascii,IDNA.DEFAULT).toString();
    750                     logln("result " + unicode);
    751                 }catch(StringPrepParseException ex){
    752                     errln("Unexpected exception for convertToUnicode: " + ex.getMessage());
    753                 }
    754             }catch(StringPrepParseException ex){
    755                 errln("Unexpected exception for convertToASCII: " + ex.getMessage());
    756             }
    757         }
    758     }
    759     @Test
    760     public void TestJB4475(){
    761         String[] in = new String[]{
    762                         "TEST",
    763                         "test"
    764                        };
    765         for ( int i=0; i< in.length; i++){
    766 
    767             try{
    768                 String ascii = IDNA.convertToASCII(in[i],IDNA.DEFAULT).toString();
    769                 if(!ascii.equals(in[i])){
    770                     errln("Did not get the expected string for convertToASCII. Expected: "+ in[i] +" Got: " + ascii);
    771                 }
    772             }catch(StringPrepParseException ex){
    773                 errln("Unexpected exception: " + ex.getMessage());
    774             }
    775         }
    776 
    777     }
    778 
    779     @Test
    780     public void TestDebug(){
    781         try{
    782             String src = "\u00ED4dn";
    783             String uni = IDNA.convertToUnicode(src,IDNA.DEFAULT).toString();
    784             if(!uni.equals(src)){
    785                 errln("Did not get the expected result. Expected: "+ prettify(src) +" Got: " +uni);
    786             }
    787         }catch(StringPrepParseException ex){
    788             logln("Unexpected exception: " + ex.getMessage());
    789         }
    790         try{
    791             String ascii = IDNA.convertToASCII("\u00AD",IDNA.DEFAULT).toString();
    792             if(ascii!=null){
    793                 errln("Did not get the expected exception");
    794             }
    795         }catch(StringPrepParseException ex){
    796             logln("Got the expected exception: " + ex.getMessage());
    797         }
    798     }
    799     @Test
    800     public void TestJB5273(){
    801         String INVALID_DOMAIN_NAME = "xn--m\u00FCller.de";
    802         try {
    803             IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.DEFAULT);
    804             IDNA.convertIDNToUnicode(INVALID_DOMAIN_NAME, IDNA.USE_STD3_RULES);
    805 
    806         } catch (StringPrepParseException ex) {
    807             errln("Unexpected exception: " + ex.getMessage());
    808         } catch (ArrayIndexOutOfBoundsException ex) {
    809             errln("Got an ArrayIndexOutOfBoundsException calling convertIDNToUnicode(\"" + INVALID_DOMAIN_NAME + "\")");
    810         }
    811 
    812         String domain = "xn--m\u00FCller.de";
    813         try{
    814             IDNA.convertIDNToUnicode(domain, IDNA.DEFAULT);
    815         }catch(StringPrepParseException ex){
    816             logln("Got the expected exception. "+ex.getMessage());
    817         }catch (Exception ex){
    818             errln("Unexpected exception: " + ex.getMessage());
    819         }
    820         try{
    821             IDNA.convertIDNToUnicode(domain, IDNA.USE_STD3_RULES);
    822         }catch(StringPrepParseException ex){
    823             logln("Got the expected exception. "+ex.getMessage());
    824         }catch (Exception ex){
    825             errln("Unexpected exception: " + ex.getMessage());
    826         }
    827         try{
    828             IDNA.convertToUnicode("xn--m\u00FCller", IDNA.DEFAULT);
    829         }catch(Exception ex){
    830             errln("ToUnicode operation failed! "+ex.getMessage());
    831         }
    832         try{
    833             IDNA.convertToUnicode("xn--m\u00FCller", IDNA.USE_STD3_RULES);
    834         }catch(Exception ex){
    835             errln("ToUnicode operation failed! "+ex.getMessage());
    836         }
    837         try{
    838             IDNA.convertIDNToUnicode("xn--m\u1234ller", IDNA.USE_STD3_RULES);
    839         }catch(StringPrepParseException ex){
    840             errln("ToUnicode operation failed! "+ex.getMessage());
    841         }
    842     }
    843 
    844     @Test
    845     public void TestLength(){
    846         String ul = "my_very_very_very_very_very_very_very_very_very_very_very_very_very_long_and_incredibly_uncreative_domain_label";
    847 
    848         /* this unicode string is longer than MAX_LABEL_BUFFER_SIZE and produces an
    849            IDNA prepared string (including xn--)that is exactly 63 bytes long */
    850         String ul1 ="\uC138\uACC4\uC758\uBAA8\uB4E0\uC0AC\uB78C\uB4E4\uC774"+
    851                     "\uD55C\uAD6D\uC5B4\uB97C\uC774\u00AD\u034F\u1806\u180B"+
    852                     "\u180C\u180D\u200B\u200C\u200D\u2060\uFE00\uFE01\uFE02"+
    853                     "\uFE03\uFE04\uFE05\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B"+
    854                     "\uFE0C\uFE0D\uFE0E\uFE0F\uFEFF\uD574\uD55C\uB2E4\uBA74"+
    855                     "\uC138\u0041\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
    856                     "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
    857                     "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
    858                     "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
    859                     "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
    860                     "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
    861                     "\uFE0F\uFEFF\u00AD\u034F\u1806\u180B\u180C\u180D\u200B"+
    862                     "\u200C\u200D\u2060\uFE00\uFE01\uFE02\uFE03\uFE04\uFE05"+
    863                     "\uFE06\uFE07\uFE08\uFE09\uFE0A\uFE0B\uFE0C\uFE0D\uFE0E"+
    864                     "\uFE0F\uFEFF";
    865         try{
    866             IDNA.convertToASCII(ul, IDNA.DEFAULT);
    867             errln("IDNA.convertToUnicode did not fail!");
    868         }catch (StringPrepParseException ex){
    869             if(ex.getError()!= StringPrepParseException.LABEL_TOO_LONG_ERROR){
    870                 errln("IDNA.convertToASCII failed with error: "+ex.toString());
    871             }else{
    872                 logln("IDNA.convertToASCII(ul, IDNA.DEFAULT) Succeeded");
    873             }
    874         }
    875         try{
    876             IDNA.convertToASCII(ul1, IDNA.DEFAULT);
    877         }catch (StringPrepParseException ex){
    878             errln("IDNA.convertToASCII failed with error: "+ex.toString());
    879         }
    880         try{
    881             IDNA.convertToUnicode(ul1, IDNA.DEFAULT);
    882         }catch (StringPrepParseException ex){
    883             errln("IDNA.convertToASCII failed with error: "+ex.toString());
    884         }
    885         try{
    886             IDNA.convertToUnicode(ul, IDNA.DEFAULT);
    887         }catch (StringPrepParseException ex){
    888             errln("IDNA.convertToASCII failed with error: "+ex.toString());
    889         }
    890 
    891         String idn = "my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.my_very_very_long_and_incredibly_uncreative_domain_label.ibm.com";
    892         try{
    893             IDNA.convertIDNToASCII(idn, IDNA.DEFAULT);
    894             errln("IDNA.convertToUnicode did not fail!");
    895         }catch (StringPrepParseException ex){
    896             if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
    897                 errln("IDNA.convertToASCII failed with error: "+ex.toString());
    898             }else{
    899                 logln("IDNA.convertToASCII(idn, IDNA.DEFAULT) Succeeded");
    900             }
    901         }
    902         try{
    903             IDNA.convertIDNToUnicode(idn, IDNA.DEFAULT);
    904             errln("IDNA.convertToUnicode did not fail!");
    905         }catch (StringPrepParseException ex){
    906             if(ex.getError()!= StringPrepParseException.DOMAIN_NAME_TOO_LONG_ERROR){
    907                 errln("IDNA.convertToUnicode failed with error: "+ex.toString());
    908             }else{
    909                 logln("IDNA.convertToUnicode(idn, IDNA.DEFAULT) Succeeded");
    910             }
    911         }
    912     }
    913 
    914     /* Tests the method public static StringBuffer convertToASCII(String src, int options) */
    915     @Test
    916     public void TestConvertToASCII() {
    917         try {
    918             if (!IDNA.convertToASCII("dummy", 0).toString().equals("dummy")) {
    919                 errln("IDNA.convertToASCII(String,int) was suppose to return the same string passed.");
    920             }
    921         } catch (Exception e) {
    922             errln("IDNA.convertToASCII(String,int) was not suppose to return an exception.");
    923         }
    924     }
    925 
    926     /*
    927      * Tests the method public static StringBuffer convertIDNToASCII(UCharacterIterator src, int options), method public
    928      * static StringBuffer public static StringBuffer convertIDNToASCII(StringBuffer src, int options), public static
    929      * StringBuffer convertIDNToASCII(UCharacterIterator src, int options)
    930      */
    931     @Test
    932     public void TestConvertIDNToASCII() {
    933         try {
    934             UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
    935             if (!IDNA.convertIDNToASCII(uci, 0).toString().equals("dummy")) {
    936                 errln("IDNA.convertIDNToASCII(UCharacterIterator, int) was suppose to "
    937                         + "return the same string passed.");
    938             }
    939             if (!IDNA.convertIDNToASCII(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
    940                 errln("IDNA.convertIDNToASCII(StringBuffer, int) was suppose to " + "return the same string passed.");
    941             }
    942         } catch (Exception e) {
    943             errln("IDNA.convertIDNToASCII was not suppose to return an exception.");
    944         }
    945     }
    946 
    947     /*
    948      * Tests the method public static StringBuffer convertToUnicode(String src, int options), public static StringBuffer
    949      * convertToUnicode(StringBuffer src, int options)
    950      */
    951     @Test
    952     public void TestConvertToUnicode() {
    953         try {
    954             if (!IDNA.convertToUnicode("dummy", 0).toString().equals("dummy")) {
    955                 errln("IDNA.convertToUnicode(String, int) was suppose to " + "return the same string passed.");
    956             }
    957             if (!IDNA.convertToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
    958                 errln("IDNA.convertToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
    959             }
    960         } catch (Exception e) {
    961             errln("IDNA.convertToUnicode was not suppose to return an exception.");
    962         }
    963     }
    964 
    965     /* Tests the method public static StringBuffer convertIDNToUnicode(UCharacterIterator src, int options) */
    966     @Test
    967     public void TestConvertIDNToUnicode() {
    968         try {
    969             UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
    970             if (!IDNA.convertIDNToUnicode(uci, 0).toString().equals("dummy")) {
    971                 errln("IDNA.convertIDNToUnicode(UCharacterIterator, int) was suppose to "
    972                         + "return the same string passed.");
    973             }
    974             if (!IDNA.convertIDNToUnicode(new StringBuffer("dummy"), 0).toString().equals("dummy")) {
    975                 errln("IDNA.convertIDNToUnicode(StringBuffer, int) was suppose to " + "return the same string passed.");
    976             }
    977         } catch (Exception e) {
    978             errln("IDNA.convertIDNToUnicode was not suppose to return an exception.");
    979         }
    980     }
    981 
    982     /* Tests the method public static int compare */
    983     @Test
    984     public void TestIDNACompare() {
    985         // Testing the method public static int compare(String s1, String s2, int options)
    986         try {
    987             IDNA.compare((String) null, (String) null, 0);
    988             errln("IDNA.compare((String)null,(String)null) was suppose to return an exception.");
    989         } catch (Exception e) {
    990         }
    991 
    992         try {
    993             IDNA.compare((String) null, "dummy", 0);
    994             errln("IDNA.compare((String)null,'dummy') was suppose to return an exception.");
    995         } catch (Exception e) {
    996         }
    997 
    998         try {
    999             IDNA.compare("dummy", (String) null, 0);
   1000             errln("IDNA.compare('dummy',(String)null) was suppose to return an exception.");
   1001         } catch (Exception e) {
   1002         }
   1003 
   1004         try {
   1005             if (IDNA.compare("dummy", "dummy", 0) != 0) {
   1006                 errln("IDNA.compare('dummy','dummy') was suppose to return a 0.");
   1007             }
   1008         } catch (Exception e) {
   1009             errln("IDNA.compare('dummy','dummy') was not suppose to return an exception.");
   1010         }
   1011 
   1012         // Testing the method public static int compare(StringBuffer s1, StringBuffer s2, int options)
   1013         try {
   1014             IDNA.compare((StringBuffer) null, (StringBuffer) null, 0);
   1015             errln("IDNA.compare((StringBuffer)null,(StringBuffer)null) was suppose to return an exception.");
   1016         } catch (Exception e) {
   1017         }
   1018 
   1019         try {
   1020             IDNA.compare((StringBuffer) null, new StringBuffer("dummy"), 0);
   1021             errln("IDNA.compare((StringBuffer)null,'dummy') was suppose to return an exception.");
   1022         } catch (Exception e) {
   1023         }
   1024 
   1025         try {
   1026             IDNA.compare(new StringBuffer("dummy"), (StringBuffer) null, 0);
   1027             errln("IDNA.compare('dummy',(StringBuffer)null) was suppose to return an exception.");
   1028         } catch (Exception e) {
   1029         }
   1030 
   1031         try {
   1032             if (IDNA.compare(new StringBuffer("dummy"), new StringBuffer("dummy"), 0) != 0) {
   1033                 errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was suppose to return a 0.");
   1034             }
   1035         } catch (Exception e) {
   1036             errln("IDNA.compare(new StringBuffer('dummy'),new StringBuffer('dummy')) was not suppose to return an exception.");
   1037         }
   1038 
   1039         // Testing the method public static int compare(UCharacterIterator s1, UCharacterIterator s2, int options)
   1040         UCharacterIterator uci = UCharacterIterator.getInstance("dummy");
   1041         try {
   1042             IDNA.compare((UCharacterIterator) null, (UCharacterIterator) null, 0);
   1043             errln("IDNA.compare((UCharacterIterator)null,(UCharacterIterator)null) was suppose to return an exception.");
   1044         } catch (Exception e) {
   1045         }
   1046 
   1047         try {
   1048             IDNA.compare((UCharacterIterator) null, uci, 0);
   1049             errln("IDNA.compare((UCharacterIterator)null,UCharacterIterator) was suppose to return an exception.");
   1050         } catch (Exception e) {
   1051         }
   1052 
   1053         try {
   1054             IDNA.compare(uci, (UCharacterIterator) null, 0);
   1055             errln("IDNA.compare(UCharacterIterator,(UCharacterIterator)null) was suppose to return an exception.");
   1056         } catch (Exception e) {
   1057         }
   1058 
   1059         try {
   1060             if (IDNA.compare(uci, uci, 0) != 0) {
   1061                 errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was suppose to return a 0.");
   1062             }
   1063         } catch (Exception e) {
   1064             errln("IDNA.compare(UCharacterIterator('dummy'),UCharacterIterator('dummy')) was not suppose to return an exception.");
   1065         }
   1066     }
   1067 }
   1068