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